models.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. cache_info = db.Column(MutableDict.as_mutable(JSONEncodedDict))
  48. json = db.Column(MutableDict.as_mutable(JSONEncodedDict))
  49. def __init__(self, *args, **kwargs):
  50. super(ISP, self).__init__(*args, **kwargs)
  51. self.json={}
  52. def pre_save(self, *args):
  53. if 'name' in self.json:
  54. assert self.name == self.json['name']
  55. if 'shortname' in self.json:
  56. assert self.shortname == self.json['shortname']
  57. def covered_areas_names(self):
  58. return [c['name'] for c in self.json.get('coveredAreas', [])]
  59. @property
  60. def complete_name(self):
  61. if 'shortname' in self.json:
  62. return u'%s (%s)'%(self.json['shortname'], self.json['name'])
  63. else:
  64. return u'%s'%self.json['name']
  65. @staticmethod
  66. def str2date(_str):
  67. d=None
  68. try:
  69. d=datetime.strptime(_str, '%Y-%m-%d')
  70. except ValueError:
  71. pass
  72. if d is None:
  73. try:
  74. d=datetime.strptime(_str, '%Y-%m')
  75. except ValueError:
  76. pass
  77. return d
  78. def __repr__(self):
  79. return u'<ISP %r>' % (self.shortname if self.shortname else self.name,)
  80. def pre_save_hook(sess):
  81. for v in itertools.chain(sess.new, sess.dirty):
  82. if hasattr(v, 'pre_save') and hasattr(v.pre_save, '__call__'):
  83. v.pre_save(sess)
  84. class ISPWhoosh(object):
  85. """
  86. Helper class to index the ISP model with Whoosh to allow full-text search
  87. """
  88. schema = fields.Schema(
  89. id=fields.ID(unique=True, stored=True),
  90. is_ffdn_member=fields.BOOLEAN(),
  91. is_disabled=fields.BOOLEAN(),
  92. name=fields.TEXT(),
  93. shortname=fields.TEXT(),
  94. description=fields.TEXT(),
  95. covered_areas=fields.KEYWORD(scorable=True, commas=True, lowercase=True),
  96. step=fields.NUMERIC(signed=False),
  97. )
  98. primary_key=schema._fields['id']
  99. @staticmethod
  100. def get_index_dir():
  101. return app.config.get('WHOOSH_INDEX_DIR', 'whoosh')
  102. @classmethod
  103. def get_index(cls):
  104. idxdir=cls.get_index_dir()
  105. if index.exists_in(idxdir):
  106. idx = index.open_dir(idxdir)
  107. else:
  108. if not os.path.exists(idxdir):
  109. os.makedirs(idxdir)
  110. idx = index.create_in(idxdir, cls.schema)
  111. return idx
  112. @classmethod
  113. def _search(cls, s, terms):
  114. return s.search(qparser.MultifieldParser([
  115. 'name', 'shortname', 'description', 'covered_areas'
  116. ], schema=cls.schema).parse(terms),
  117. mask=whoosh.query.Term('is_disabled', True))
  118. @classmethod
  119. def search(cls, terms):
  120. with ISPWhoosh.get_index().searcher() as s:
  121. sres=cls._search(s, terms)
  122. ranks={}
  123. for rank, r in enumerate(sres):
  124. ranks[r['id']]=rank
  125. if not len(ranks):
  126. return []
  127. _res=ISP.query.filter(ISP.id.in_(ranks.keys()))
  128. res=[None]*_res.count()
  129. for isp in _res:
  130. res[ranks[isp.id]]=isp
  131. return res
  132. @classmethod
  133. def update_document(cls, writer, model):
  134. kw={
  135. 'id': unicode(model.id),
  136. '_stored_id': model.id,
  137. 'is_ffdn_member': model.is_ffdn_member,
  138. 'is_disabled': model.is_disabled,
  139. 'name': model.name,
  140. 'shortname': model.shortname,
  141. 'description': model.json.get('description'),
  142. 'covered_areas': model.covered_areas_names(),
  143. 'step': model.json.get('progressStatus')
  144. }
  145. writer.update_document(**kw)
  146. @classmethod
  147. def _after_flush(cls, app, changes):
  148. isp_changes = []
  149. for change in changes:
  150. if change[0].__class__ == ISP:
  151. update = change[1] in ('update', 'insert')
  152. isp_changes.append((update, change[0]))
  153. if not len(changes):
  154. return
  155. idx=cls.get_index()
  156. with idx.writer() as writer:
  157. for update, model in isp_changes:
  158. if update:
  159. cls.update_document(writer, model)
  160. else:
  161. writer.delete_by_term(cls.primary_key, model.id)
  162. flask_sqlalchemy.models_committed.connect(ISPWhoosh._after_flush)
  163. event.listen(flask_sqlalchemy.Session, 'before_commit', pre_save_hook)