X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/7e890833260e5aef8ae217d195309b76fa91c02f..6e102eb7360b2c7df5a8565486e3428e6ccf2ab8:/apps/lesmianator/models.py diff --git a/apps/lesmianator/models.py b/apps/lesmianator/models.py index dce4b15b1..0a59ddee4 100644 --- a/apps/lesmianator/models.py +++ b/apps/lesmianator/models.py @@ -26,7 +26,7 @@ from catalogue.models import Book, Tag class Poem(models.Model): slug = models.SlugField(_('slug'), max_length=120, db_index=True) text = models.TextField(_('text')) - created_by = models.ForeignKey(User) + created_by = models.ForeignKey(User, null=True) created_from = JSONField(_('extra information'), null=True, blank=True) created_at = models.DateTimeField(_('creation date'), auto_now_add=True, editable=False) seen_at = models.DateTimeField(_('last view date'), auto_now_add=True, editable=False) @@ -100,6 +100,9 @@ class Continuations(models.Model): object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id') + class Meta: + unique_together = (('content_type', 'object_id'), ) + def __unicode__(self): return "Continuations for: %s" % unicode(self.content_object) @@ -115,7 +118,6 @@ class Continuations(models.Model): @classmethod def for_book(cls, book, length=3): # count from this book only - print 'for_book', book output = StringIO() f = open(book.xml_file.path) text.transform(f, output, False, ('raw-text',)) @@ -147,11 +149,18 @@ class Continuations(models.Model): @classmethod def get(cls, sth): object_type = ContentType.objects.get_for_model(sth) + should_keys = set([sth.id]) + if isinstance(sth, Tag): + should_keys = set(b.pk for b in Book.tagged.with_any((sth,))) try: obj = cls.objects.get(content_type=object_type, object_id=sth.id) + if not obj.pickle: + raise cls.DoesNotExist f = open(obj.pickle.path) - conts = cPickle.load(f) + keys, conts = cPickle.load(f) f.close() + if set(keys) != should_keys: + raise cls.DoesNotExist return conts except cls.DoesNotExist: if isinstance(sth, Book): @@ -161,8 +170,8 @@ class Continuations(models.Model): else: raise NotImplemented('Lesmianator continuations: only Book and Tag supported') - c = cls(content_object=sth) - c.pickle.save(sth.slug+'.p', ContentFile(cPickle.dumps(conts))) + c, created = cls.objects.get_or_create(content_type=object_type, object_id=sth.id) + c.pickle.save(sth.slug+'.p', ContentFile(cPickle.dumps((should_keys, conts)))) c.save() return conts