Django Library PyGal
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

search.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from django.conf import settings
  2. import fstools
  3. import logging
  4. from .models import Item
  5. import os
  6. from whoosh.fields import Schema, ID, TEXT, KEYWORD, NUMERIC, DATETIME
  7. from whoosh.qparser.dateparse import DateParserPlugin
  8. from whoosh import index, qparser
  9. from pygal.models import TYPE_FOLDER
  10. logger = logging.getLogger("WHOOSH")
  11. SCHEMA = Schema(
  12. rel_path=ID(unique=True, stored=True),
  13. # Item
  14. name=TEXT,
  15. type=TEXT,
  16. favourite_of=KEYWORD,
  17. datetime=DATETIME,
  18. size=NUMERIC,
  19. # Tag
  20. tag=KEYWORD,
  21. # Image Cache
  22. exposure_program=TEXT,
  23. exposure_time=NUMERIC,
  24. flash=TEXT,
  25. f_number=NUMERIC,
  26. focal_length=NUMERIC,
  27. lon=NUMERIC,
  28. lat=NUMERIC,
  29. height=NUMERIC,
  30. iso=NUMERIC,
  31. camera=TEXT,
  32. orientation=NUMERIC,
  33. width=NUMERIC,
  34. # Audio Cache
  35. album=TEXT,
  36. artist=TEXT,
  37. bitrate=NUMERIC,
  38. duration=NUMERIC,
  39. genre=TEXT,
  40. title=TEXT,
  41. track=NUMERIC,
  42. year=NUMERIC,
  43. #
  44. ratio=NUMERIC,
  45. )
  46. def create_index():
  47. logger.debug('Search Index created.')
  48. return index.create_in(settings.WHOOSH_PATH, schema=SCHEMA)
  49. def load_index():
  50. if not os.path.exists(settings.WHOOSH_PATH):
  51. fstools.mkdir(settings.WHOOSH_PATH)
  52. try:
  53. ix = index.open_dir(settings.WHOOSH_PATH)
  54. except index.EmptyIndexError:
  55. ix = create_index()
  56. else:
  57. logger.debug('Search Index opened.')
  58. return ix
  59. def item_is_supported(item):
  60. return item.type != TYPE_FOLDER
  61. def add_item(ix, item):
  62. # Collect data for the item
  63. #
  64. data = {
  65. 'rel_path': item.rel_path,
  66. 'name': os.path.splitext(item.rel_path.split('/')[-1])[0],
  67. 'type': item.type,
  68. }
  69. favourite_of = item.favourite_of.all()
  70. if len(favourite_of) > 0:
  71. data['favourite_of'] = ' '.join([u.username for u in favourite_of])
  72. tags = item.tag_set.all()
  73. if len(tags) > 0:
  74. data['tag'] = ' '.join([t.text for t in tags])
  75. for key, value in item.cached_item_data.items():
  76. data[key] = value
  77. # Write data to the index
  78. #
  79. with ix.writer() as w:
  80. logger.info('Adding document with rel_path=%s to the search index.', data.get('rel_path'))
  81. w.add_document(**data)
  82. for key in data:
  83. logger.debug(' - Adding %s=%s', key, repr(data[key]))
  84. def delete_item(ix, item):
  85. with ix.writer() as w:
  86. logger.info('Removing document with rel_path=%s from the search index.', item.rel_path)
  87. w.delete_by_term("rel_path", item.rel_path)
  88. def update_item(ix, item):
  89. if item_is_supported(item):
  90. delete_item(ix, item)
  91. add_item(ix, item)
  92. def rebuild_index(ix):
  93. for item in Item.objects.all().exclude(type=TYPE_FOLDER):
  94. add_item(ix, item)
  95. return len(Item.objects.all().exclude(type=TYPE_FOLDER))
  96. def search(ix, search_txt):
  97. qp = qparser.MultifieldParser(['name', 'tag'], ix.schema)
  98. qp.add_plugin(DateParserPlugin(free=True))
  99. q = qp.parse(search_txt)
  100. with ix.searcher() as s:
  101. results = s.search(q, limit=None)
  102. rpl = []
  103. for hit in results:
  104. rpl.append(hit['rel_path'])
  105. return Item.objects.filter(rel_path__in=rpl)