+ """Close snippet file"""
+ self.file.close()
+
+ def remove(self):
+ self.revision = None
+ try:
+ os.unlink(self.path)
+ self.revision = 0
+ while True:
+ self.revision += 1
+ os.unlink(self.path)
+ except OSError:
+ pass
+
+
+class Index(SolrIndex):
+ """
+ Class indexing books.
+ """
+ def __init__(self):
+ super(Index, self).__init__(mode='rw')
+
+ def delete_query(self, *queries):
+ """
+ index.delete(queries=...) doesn't work, so let's reimplement it
+ using deletion of list of uids.
+ """
+ uids = set()
+ for q in queries:
+ if isinstance(q, sunburnt.search.LuceneQuery):
+ q = self.index.query(q)
+ q.field_limiter.update(['uid'])
+ st = 0
+ rows = 100
+ while True:
+ ids = q.paginate(start=st, rows=rows).execute()
+ if not len(ids):
+ break
+ for res in ids:
+ uids.add(res['uid'])
+ st += rows
+ # print "Will delete %s" % ','.join([x for x in uids])
+ if uids:
+ self.index.delete(uids)
+ return True
+ else:
+ return False
+
+ def index_tags(self, *tags, **kw):
+ """
+ Re-index global tag list.
+ Removes all tags from index, then index them again.
+ Indexed fields include: id, name (with and without polish stems), category
+ """
+ log.debug("Indexing tags")
+ remove_only = kw.get('remove_only', False)
+ # first, remove tags from index.
+ if tags:
+ tag_qs = []
+ for tag in tags:
+ q_id = self.index.Q(tag_id=tag.id)
+
+ if isinstance(tag, PDCounterAuthor):
+ q_cat = self.index.Q(tag_category='pd_author')
+ elif isinstance(tag, PDCounterBook):
+ q_cat = self.index.Q(tag_category='pd_book')
+ else:
+ q_cat = self.index.Q(tag_category=tag.category)
+
+ q_id_cat = self.index.Q(q_id & q_cat)
+ tag_qs.append(q_id_cat)
+ self.delete_query(tag_qs)
+ else: # all
+ q = self.index.Q(tag_id__any=True)
+ self.delete_query(q)
+
+ if not remove_only:
+ # then add them [all or just one passed]
+ if not tags:
+ tags = chain(catalogue.models.Tag.objects.exclude(category='set'), \
+ PDCounterAuthor.objects.all(), \
+ PDCounterBook.objects.all())
+
+ for tag in tags:
+ if isinstance(tag, PDCounterAuthor):
+ doc = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.name,
+ "tag_name_pl": tag.name,
+ "tag_category": 'pd_author',
+ "is_pdcounter": True,
+ "uid": "tag%d_pd_a" % tag.id
+ }
+ elif isinstance(tag, PDCounterBook):
+ doc = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.title,
+ "tag_name_pl": tag.title,
+ "tag_category": 'pd_book',
+ "is_pdcounter": True,
+ "uid": "tag%d_pd_b" % tag.id
+ }
+ else:
+ doc = {
+ "tag_id": int(tag.id),
+ "tag_name": tag.name,
+ "tag_name_pl": tag.name,
+ "tag_category": tag.category,
+ "is_pdcounter": False,
+ "uid": "tag%d" % tag.id
+ }
+ self.index.add(doc)
+
+ def create_book_doc(self, book):
+ """
+ Create a lucene document referring book id.
+ """
+ doc = {
+ 'book_id': int(book.id),
+ }
+ if book.parent is not None:
+ doc["parent_id"] = int(book.parent.id)
+ return doc