]> git.p6c8.net - pcdenotes.git/blob - notes/models.py
Began to write a manager for the Note class to get some logic out of the views
[pcdenotes.git] / notes / models.py
1 from django.db import models
2 from django.contrib.auth.models import User
3 from django.urls import reverse
4
5 # Create your models here.
6
7 NOTE_STATUS = ((0, "Draft"),
8 (1, "Published"))
9
10 class NoteQuerySet(models.QuerySet):
11 pass
12
13 class NoteManager(models.Manager):
14 def per_year(self, year):
15 return super().get_queryset().filter(status=1, created_at__year=year)
16
17 def per_month(self, year, month):
18 return super().get_queryset().filter(status=1, created_at__year=year, created_at__month=month)
19
20 class Note(models.Model):
21 title = models.CharField(max_length=250)
22 slug = models.SlugField(max_length=250, unique=True)
23 author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notes_posted')
24 content = models.TextField()
25 status = models.IntegerField(choices=NOTE_STATUS, default=0)
26
27 created_at = models.DateTimeField(auto_now_add=True)
28 updated_at = models.DateTimeField(auto_now=True)
29
30 objects = NoteManager()
31
32 class Meta:
33 ordering = ['-created_at']
34
35 def __str__(self):
36 return self.title
37
38 def get_absolute_url(self):
39 return reverse("notes:note_detail", kwargs={"note_slug": self.slug})
40
41 def is_draft(self):
42 return self.status == 0
43
44 def is_published(self):
45 return self.status == 1

patrick-canterino.de