]> gitweb.fperrin.net Git - djsite.git/blob - quotes/models.py
34695dfd9fbdb4af7852b5774d4c6ddb4ac6e7f1
[djsite.git] / quotes / models.py
1 from django.db import models
2 from django.urls import reverse
3
4 from localmodels import HTMLField
5
6 # Create your models here.
7 class BaseTag(models.Model):
8     tag = models.CharField(max_length=100, unique=True)
9
10     def __unicode__(self):
11         return self.tag
12
13     class Meta(object):
14         abstract = True
15
16 class AuthorTag(BaseTag): pass
17 class WorkTag(BaseTag): pass
18 class QuoteTag(BaseTag):
19     def get_absolute_url(self):
20         return reverse('quotes:tags', args=[str(self.id)])
21
22 class CommonData(models.Model):
23     creation_date = models.DateTimeField(auto_now_add=True)
24     last_modification = models.DateTimeField(auto_now=True)
25
26     notes = HTMLField(blank=True, help_text= \
27                       "Notes about the entry; may be left blank. Displayed \
28                       on the public pages")
29     pvt_notes = HTMLField(blank=True, help_text= \
30                           "Notes about the entry; not displayed on \
31                           the public interface")
32
33     nb_display = models.BigIntegerField(default=0)
34     def incr_display(self):
35         self.nb_display = models.F('nb_display') + 1
36         self.save()
37
38     class Meta(object):
39         abstract = True
40
41 class Author(CommonData):
42     name = models.CharField(max_length=100,
43                             help_text="Name of the author",
44                             unique=True)
45
46     tags = models.ManyToManyField(AuthorTag, blank=True,
47                                   help_text='Not implemented yet')
48
49     birth_date = models.DateField(blank=True, null=True,
50                                   help_text="Date of birth")
51     death_date = models.DateField(blank=True, null=True,
52                                   help_text="Date of death (leave blank \
53                                   if still alive!)")
54
55     class Meta(CommonData.Meta):
56         ordering = ['name']
57
58     def __unicode__(self):
59         return self.name
60
61     def get_absolute_url(self):
62         return reverse('quotes:author', args=[str(self.id)])
63
64 # class DatePrecision(models.CharField):
65 #     DAY = "D"
66 #     MONTH = "M"
67 #     YEAR = "Y"
68 #     DECADE = "X"
69 #     CENTURY = "C"
70 #     UNKNOWN = "?"
71 #     PRECISION_CHOICES = [
72 #         (DAY, "Day"),
73 #         (MONTH, "Month"),
74 #         (YEAR, "Year"),
75 #         (DECADE, "Decade"),
76 #         (CENTURY, "Century"),
77 #         (UNKNOWN, "Unknown"),
78 #     ]
79 #     def __init__(self, **kwargs):
80 #         models.CharField(max_length=1, choices=self.PRECISION_CHOICES,
81 #                          default=self.DAY, **kwargs)
82
83 class Work(CommonData):
84     name = models.CharField(max_length=100,
85                             help_text="Name of the context for the quote \
86                             (title of the work or speech it appears in)",
87                             unique=True)
88     author = models.ForeignKey(Author)
89     date = models.DateField(blank=True, null=True,
90                             help_text="Date of the quote")
91     tags = models.ManyToManyField(WorkTag, blank=True,
92                                   help_text='Not implemented yet')
93
94     def __unicode__(self):
95         return "%s: %s (%s)" % (self.author.name, self.name, self.date)
96
97     def get_absolute_url(self):
98         return reverse('quotes:work', args=[str(self.id)])
99
100     class Meta(CommonData.Meta):
101         ordering = ['name']
102
103 class Quote(CommonData):
104     text = HTMLField()
105     tags = models.ManyToManyField(QuoteTag, blank=True)
106     work = models.ForeignKey(Work)
107
108     def __unicode__(self):
109         return self.work.author.name + ": " + self.text
110
111     def get_absolute_url(self):
112         return reverse('quotes:onequote', args=[str(self.id)])
113
114     class Meta(CommonData.Meta):
115         ordering = ['-pk']
116