From f6c958eed331c2da9539130e6a5be9b9ec852d02 Mon Sep 17 00:00:00 2001 From: Gesche Gierse Date: Mon, 19 Apr 2021 00:20:04 +0200 Subject: [PATCH] add helper functions to models --- stories/writingtogether/models.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/stories/writingtogether/models.py b/stories/writingtogether/models.py index 0c29b8e..1f1e228 100644 --- a/stories/writingtogether/models.py +++ b/stories/writingtogether/models.py @@ -15,6 +15,10 @@ class StoryRound(models.Model): def get_absolute_url(self): return reverse('writing:detail', kwargs={'pk': self.pk}) + def get_next_story_part(self, user: User) -> 'StoryPart': + # TODO: implement, for each story get unfinished part and check user + pass + class Participant(models.Model): user = models.ForeignKey(User, on_delete=CASCADE) @@ -27,10 +31,27 @@ class Story(models.Model): created = models.DateTimeField(auto_now_add=True) part_of_round = models.ForeignKey(StoryRound, on_delete=CASCADE) started_by = models.ForeignKey(User, on_delete=CASCADE) + finished = models.BooleanField(default=False) + + def get_first_unfinished_part(self): + if self.finished: + return None + + first_part = StoryPart.objects.get(part_of_id=self.pk, previous_part__isnull=True) + + last_unfinished_part = first_part + while last_unfinished_part.finished: + last_unfinished_part = StoryPart.objects.get(previous_part=last_unfinished_part) + + return last_unfinished_part class StoryPart(models.Model): user = models.ForeignKey(User, on_delete=CASCADE) previous_part = models.ForeignKey('StoryPart', on_delete=CASCADE, null=True, blank=True) - text = models.TextField() + text = models.TextField(null=True, blank=True) part_of = models.ForeignKey('Story', on_delete=CASCADE) + + @property + def finished(self): + return self.text