]> git.p6c8.net - pcdenotes.git/blob - notes/models.py
0056f0e706d29990ba4f5721cc0fc9dbdbf51375
[pcdenotes.git] / notes / models.py
1 from django.db import models
2 from django.db.models import Count
3 from django.db.models.functions import ExtractYear, ExtractMonth, Coalesce
4 from django.db.models.signals import pre_save
5
6 from django.dispatch import receiver
7 from django.contrib.auth.models import User
8 from django.urls import reverse
9
10 from datetime import datetime
11
12 # Create your models here.
13
14 NOTE_STATUS = ((0, "Draft"),
15 (1, "Published"))
16
17 class NoteQuerySet(models.QuerySet):
18 pass
19
20 class NoteManager(models.Manager):
21 def all_published(self):
22 return super().get_queryset().filter(status=1).annotate(publication_date=Coalesce('published_at', 'created_at')).order_by('-publication_date')
23
24 def all(self):
25 return super().all().annotate(publication_date=Coalesce('published_at', 'created_at')).order_by('-publication_date')
26
27 def per_year(self, year):
28 return self.all_published().filter(created_at__year=year)
29
30 def per_month(self, year, month):
31 return self.per_year(year).filter(created_at__month=month)
32
33 def years(self):
34 return self.all_published().annotate(created_year=ExtractYear('created_at')).values_list('created_year', flat=True).distinct().order_by('created_year')
35
36 def years_with_total(self):
37 return self.all_published().annotate(created_year=ExtractYear('created_at')).values('created_year').annotate(total=Count('id')).order_by('created_year').values('created_year', 'total').distinct()
38
39 def months(self, year):
40 return self.per_year(year).annotate(created_month=ExtractMonth('created_at')).values_list('created_month', flat=True).distinct().order_by('created_month')
41
42 def months_with_total(self, year):
43 return self.per_year(year).annotate(created_month=ExtractMonth('created_at')).values('created_month').annotate(total=Count('id')).order_by('created_month').values('created_month', 'total').distinct()
44
45 class Note(models.Model):
46 title = models.CharField(max_length=250)
47 slug = models.SlugField(max_length=250, unique=True)
48 author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='notes_posted')
49 content = models.TextField()
50 status = models.IntegerField(choices=NOTE_STATUS, default=0)
51
52 created_at = models.DateTimeField(auto_now_add=True)
53 updated_at = models.DateTimeField(auto_now=True)
54 published_at = models.DateTimeField(null=True, editable=False)
55
56 objects = NoteManager()
57
58 class Meta:
59 ordering = ['-created_at']
60
61 def __str__(self):
62 return self.title
63
64 def get_absolute_url(self):
65 return reverse("notes:note_detail", kwargs={"note_slug": self.slug})
66
67 def is_draft(self):
68 return self.status == 0
69
70 def is_published(self):
71 return self.status == 1
72
73 @receiver(pre_save, sender=Note)
74 def note_pre_save(sender, instance, **kwargs):
75 if instance.pk is None:
76 if instance.status == 1:
77 instance.published_at = datetime.now()
78 else:
79 old_instance = Note.objects.get(pk=instance.pk)
80
81 if old_instance.status == 0 and instance.status == 1:
82 instance.published_at = datetime.now()

patrick-canterino.de