X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/0543785bb9e74989036d99eb25cf23c3ef82cdf7..24f677a3979ba3f1bdfe420c03ad5e3f8b0398c2:/src/api/models.py diff --git a/src/api/models.py b/src/api/models.py index cc71a06f3..d2716b922 100644 --- a/src/api/models.py +++ b/src/api/models.py @@ -45,11 +45,15 @@ class BookUserData(models.Model): book = models.ForeignKey(Book) user = models.ForeignKey(User) complete = models.BooleanField(default=False) + last_changed = models.DateTimeField(auto_now=True) - def get_state(self): + @property + def state(self): return 'complete' if self.complete else 'reading' - def set_state(self, state): - self.complete = state == 'complete' - - state = property(get_state, set_state) + @classmethod + def update(cls, book, user, state): + instance, created = cls.objects.get_or_create(book=book, user=user) + instance.complete = state == 'complete' + instance.save() + return instance