models.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. from decimal import Decimal
  3. import json
  4. import os
  5. import itertools
  6. from datetime import datetime
  7. from . import db, app
  8. import flask_sqlalchemy
  9. from sqlalchemy.types import TypeDecorator, VARCHAR
  10. from sqlalchemy.ext.mutable import MutableDict
  11. from sqlalchemy import event
  12. import whoosh
  13. from whoosh import fields, index, qparser
  14. class fakefloat(float):
  15. def __init__(self, value):
  16. self._value = value
  17. def __repr__(self):
  18. return str(self._value)
  19. def defaultencode(o):
  20. if isinstance(o, Decimal):
  21. # Subclass float with custom repr?
  22. return fakefloat(o)
  23. raise TypeError(repr(o) + " is not JSON serializable")
  24. class JSONEncodedDict(TypeDecorator):
  25. "Represents an immutable structure as a json-encoded string."
  26. impl = VARCHAR
  27. def process_bind_param(self, value, dialect):
  28. if value is not None:
  29. value = json.dumps(value, default=defaultencode)
  30. return value
  31. def process_result_value(self, value, dialect):
  32. if value is not None:
  33. value = json.loads(value)
  34. return value
  35. class ISP(db.Model):
  36. id = db.Column(db.Integer, primary_key=True)
  37. name = db.Column(db.String, nullable=False, index=True, unique=True)
  38. shortname = db.Column(db.String(12), index=True, unique=True)
  39. is_ffdn_member = db.Column(db.Boolean, default=False)
  40. is_disabled = db.Column(db.Boolean, default=False) # True = ISP will not appear
  41. json_url = db.Column(db.String)
  42. last_update_success = db.Column(db.DateTime)
  43. last_update_attempt = db.Column(db.DateTime)
  44. update_error_strike = db.Column(db.Integer, default=0) # if >= 3; then updates are disabled
  45. next_update = db.Column(db.DateTime, default=datetime.now())
  46. tech_email = db.Column(db.String)
  47. json = db.Column(MutableDict.as_mutable(JSONEncodedDict))
  48. def __init__(self, *args, **kwargs):
  49. super(ISP, self).__init__(*args, **kwargs)
  50. self.json={}
  51. def pre_save(self, *args):
  52. if 'name' in self.json:
  53. assert self.name == self.json['name']
  54. if 'shortname' in self.json:
  55. assert self.shortname == self.json['shortname']
  56. def covered_areas_names(self):
  57. return [c['name'] for c in self.json.get('coveredAreas', [])]
  58. @staticmethod
  59. def str2date(_str):
  60. d=None
  61. try:
  62. d=datetime.strptime(_str, '%Y-%m-%d')
  63. except ValueError:
  64. pass
  65. if d is None:
  66. try:
  67. d=datetime.strptime(_str, '%Y-%m')
  68. except ValueError:
  69. pass
  70. return d
  71. def __repr__(self):
  72. return u'<ISP %r>' % (self.shortname if self.shortname else self.name,)
  73. def pre_save_hook(sess):
  74. for v in itertools.chain(sess.new, sess.dirty):
  75. if hasattr(v, 'pre_save') and hasattr(v.pre_save, '__call__'):
  76. v.pre_save(sess)
  77. class ISPWhoosh(object):
  78. """
  79. Helper class to index the ISP model with Whoosh to allow full-text search
  80. """
  81. schema = fields.Schema(
  82. id=fields.ID(unique=True, stored=True),
  83. is_ffdn_member=fields.BOOLEAN(),
  84. is_disabled=fields.BOOLEAN(),
  85. name=fields.TEXT(),
  86. shortname=fields.TEXT(),
  87. description=fields.TEXT(),
  88. covered_areas=fields.KEYWORD(scorable=True, commas=True, lowercase=True),
  89. step=fields.NUMERIC(signed=False),
  90. )
  91. primary_key=schema._fields['id']
  92. @staticmethod
  93. def get_index_dir():
  94. return app.config.get('WHOOSH_INDEX_DIR', 'whoosh')
  95. @classmethod
  96. def get_index(cls):
  97. idxdir=cls.get_index_dir()
  98. if index.exists_in(idxdir):
  99. idx = index.open_dir(idxdir)
  100. else:
  101. if not os.path.exists(idxdir):
  102. os.makedirs(idxdir)
  103. idx = index.create_in(idxdir, cls.schema)
  104. return idx
  105. @classmethod
  106. def _search(cls, s, terms):
  107. return s.search(qparser.MultifieldParser([
  108. 'name', 'shortname', 'description', 'covered_areas'
  109. ], schema=cls.schema).parse(terms),
  110. mask=whoosh.query.Term('is_disabled', True))
  111. @classmethod
  112. def search(cls, terms):
  113. with ISPWhoosh.get_index().searcher() as s:
  114. sres=cls._search(s, terms)
  115. ranks={}
  116. for rank, r in enumerate(sres):
  117. ranks[r['id']]=rank
  118. if not len(ranks):
  119. return []
  120. _res=ISP.query.filter(ISP.id.in_(ranks.keys()))
  121. res=[None]*_res.count()
  122. for isp in _res:
  123. res[ranks[isp.id]]=isp
  124. return res
  125. @classmethod
  126. def update_document(cls, writer, model):
  127. kw={
  128. 'id': unicode(model.id),
  129. '_stored_id': model.id,
  130. 'is_ffdn_member': model.is_ffdn_member,
  131. 'is_disabled': model.is_disabled,
  132. 'name': model.name,
  133. 'shortname': model.shortname,
  134. 'description': model.json.get('description'),
  135. 'covered_areas': model.covered_areas_names(),
  136. 'step': model.json.get('progressStatus')
  137. }
  138. writer.update_document(**kw)
  139. @classmethod
  140. def _after_flush(cls, app, changes):
  141. isp_changes = []
  142. for change in changes:
  143. if change[0].__class__ == ISP:
  144. update = change[1] in ('update', 'insert')
  145. isp_changes.append((update, change[0]))
  146. if not len(changes):
  147. return
  148. idx=cls.get_index()
  149. with idx.writer() as writer:
  150. for update, model in isp_changes:
  151. if update:
  152. cls.update_document(writer, model)
  153. else:
  154. writer.delete_by_term(cls.primary_key, model.id)
  155. flask_sqlalchemy.models_committed.connect(ISPWhoosh._after_flush)
  156. event.listen(flask_sqlalchemy.Session, 'before_commit', pre_save_hook)