models.py 6.0 KB

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