models.py 6.1 KB

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