268089ab91b824d1715bbf89fbdb0b591116c7e1
[redakcja.git] / src / alerts / models.py
1 from django.db import models
2 from django.dispatch import receiver
3 from dvcs.models import post_commit
4 from .rules import rules, rules_by_tag
5
6
7 class Alert(models.Model):
8     book = models.ForeignKey('documents.Book', models.CASCADE)
9     tag = models.CharField(max_length=32)
10
11     @property
12     def rule(self):
13         return rules_by_tag[self.tag]
14
15     @classmethod
16     def validate_book(cls, book):
17         cls.objects.filter(book=book).delete()
18         try:
19             wlbook = book.wldocument(publishable=False, librarian2=True)
20         except:
21             cls.objects.create(book=book, tag='parse')
22             return
23
24         for rule in rules:
25             if rule.check_meta(wlbook.meta):
26                 print(rule.tag, book)
27                 cls.objects.create(book=book, tag=rule.tag)
28
29
30 @receiver(post_commit)
31 def validate_post_commit(sender, **kwargs):
32     Alert.validate_book(sender.tree.book)