2021-03-30 21:25:52 +02:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.db import models
|
|
|
|
from django.db.models import CASCADE
|
2021-03-31 01:10:04 +02:00
|
|
|
from django.urls import reverse
|
2021-03-30 21:25:52 +02:00
|
|
|
|
|
|
|
User = get_user_model()
|
|
|
|
|
2021-03-31 01:10:04 +02:00
|
|
|
|
|
|
|
class StoryRound(models.Model):
|
|
|
|
name = models.CharField(max_length=256)
|
2021-04-19 00:17:35 +02:00
|
|
|
participants = models.ManyToManyField(User) # for ordering add: through='Participant'
|
2021-03-31 01:10:04 +02:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
number_of_rounds = models.IntegerField()
|
2021-04-20 00:25:15 +02:00
|
|
|
current_turn = models.IntegerField(default=0, blank=True)
|
2021-03-31 01:10:04 +02:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('writing:detail', kwargs={'pk': self.pk})
|
|
|
|
|
2021-04-19 00:20:04 +02:00
|
|
|
def get_next_story_part(self, user: User) -> 'StoryPart':
|
|
|
|
# TODO: implement, for each story get unfinished part and check user
|
|
|
|
pass
|
|
|
|
|
2021-04-20 00:25:58 +02:00
|
|
|
def next_round_ready(self) -> bool:
|
|
|
|
for story in self.story_set.all():
|
|
|
|
if not story.storypart_set.get(turn_number=self.current_turn).finished:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2021-03-31 01:10:04 +02:00
|
|
|
|
2021-04-20 00:01:36 +02:00
|
|
|
#class Participant(models.Model):
|
|
|
|
# user = models.ForeignKey(User, on_delete=CASCADE)
|
|
|
|
# story_round = models.ForeignKey(StoryRound, on_delete=CASCADE)
|
|
|
|
# order_by = models.IntegerField()
|
2021-03-31 01:10:04 +02:00
|
|
|
|
|
|
|
|
2021-03-30 21:25:52 +02:00
|
|
|
class Story(models.Model):
|
|
|
|
name = models.CharField(max_length=256, null=True, blank=True)
|
2021-03-31 01:10:04 +02:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
part_of_round = models.ForeignKey(StoryRound, on_delete=CASCADE)
|
|
|
|
started_by = models.ForeignKey(User, on_delete=CASCADE)
|
2021-04-19 00:20:04 +02:00
|
|
|
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
|
2021-03-30 21:25:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StoryPart(models.Model):
|
|
|
|
user = models.ForeignKey(User, on_delete=CASCADE)
|
2021-03-31 01:10:04 +02:00
|
|
|
previous_part = models.ForeignKey('StoryPart', on_delete=CASCADE, null=True, blank=True)
|
2021-04-19 00:20:04 +02:00
|
|
|
text = models.TextField(null=True, blank=True)
|
2021-03-31 01:10:04 +02:00
|
|
|
part_of = models.ForeignKey('Story', on_delete=CASCADE)
|
2021-04-20 00:25:15 +02:00
|
|
|
turn_number = models.IntegerField()
|
2021-04-19 00:20:04 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def finished(self):
|
|
|
|
return self.text
|