models.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from django.db import models
from django.urls import reverse
from django.conf import settings


def todo_id_gen():
    import uuid

    return int(uuid.uuid1())


class List(models.Model):
    @property
    def name(self):
        return self.todo_set.first().text

    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE
    )

    @staticmethod
    def create_new(text, owner):
        import uuid

        list_ = List.objects.create(owner=owner)
        Todo.objects.create(text=text, list=list_, todo_id=todo_id_gen())
        return list_

    def __str__(self) -> str:
        return str(self.todo_set.first().text)


class Todo(models.Model):
    text = models.TextField(max_length=140, default="")
    list = models.ForeignKey(List, on_delete=models.CASCADE)
    todo_id = models.CharField(default="")

    class Meta:
        ordering = ("id",)
        unique_together = ("list", "text")

    def __str__(self) -> str:
        return str(self.text)