]> gitweb.fperrin.net Git - djsite.git/blobdiff - quotes/models.py
Fix display when quotes contain unicode
[djsite.git] / quotes / models.py
index 53afcbe246a20f61e2c1c03767de029739540fca..0d7fe333001e4ee77f58216f1ed102cc9c903343 100644 (file)
 from django.db import models
+from django.urls import reverse
+
+from localmodels import HTMLField
 
 # Create your models here.
+class BaseTag(models.Model):
+    tag = models.CharField(max_length=100, unique=True)
+
+    def __unicode__(self):
+        return self.tag
+
+    class Meta(object):
+        abstract = True
+
+class AuthorTag(BaseTag): pass
+class WorkTag(BaseTag): pass
+class QuoteTag(BaseTag):
+    def get_absolute_url(self):
+        return reverse('quotes:tags', args=[str(self.id)])
+
+class CommonData(models.Model):
+    creation_date = models.DateTimeField(auto_now_add=True)
+    last_modification = models.DateTimeField(auto_now=True)
+
+    notes = HTMLField(blank=True, help_text= \
+                      "Notes about the entry; may be left blank. Displayed \
+                      on the public pages")
+    pvt_notes = HTMLField(blank=True, help_text= \
+                          "Notes about the entry; not displayed on \
+                          the public interface")
+
+    nb_display = models.BigIntegerField(default=0)
+    def incr_display(self):
+        self.nb_display = models.F('nb_display') + 1
+        self.save()
 
-class Author(models.Model):
-    name = models.CharField(max_length=100)
-    notes = models.TextField(blank=True, help_text= \
-                             "Notes for the author; may be left blank. Will \
-                              not be HTML-escaped.")
+    class Meta(object):
+        abstract = True
+
+class Author(CommonData):
+    name = models.CharField(max_length=100,
+                            help_text="Name of the author",
+                            unique=True)
+
+    tags = models.ManyToManyField(AuthorTag, blank=True,
+                                  help_text='Not implemented yet')
+
+    birth_date = models.DateField(blank=True, null=True,
+                                  help_text="Date of birth")
+    death_date = models.DateField(blank=True, null=True,
+                                  help_text="Date of death (leave blank \
+                                  if still alive!)")
 
     def __unicode__(self):
         return self.name
 
-class Tag(models.Model):
-    tag = models.CharField(max_length=100)
+    def get_absolute_url(self):
+        return reverse('quotes:author', args=[str(self.id)])
+
+# class DatePrecision(models.CharField):
+#     DAY = "D"
+#     MONTH = "M"
+#     YEAR = "Y"
+#     DECADE = "X"
+#     CENTURY = "C"
+#     UNKNOWN = "?"
+#     PRECISION_CHOICES = [
+#         (DAY, "Day"),
+#         (MONTH, "Month"),
+#         (YEAR, "Year"),
+#         (DECADE, "Decade"),
+#         (CENTURY, "Century"),
+#         (UNKNOWN, "Unknown"),
+#     ]
+#     def __init__(self, **kwargs):
+#         models.CharField(max_length=1, choices=self.PRECISION_CHOICES,
+#                          default=self.DAY, **kwargs)
+
+class Work(CommonData):
+    name = models.CharField(max_length=100,
+                            help_text="Name of the context for the quote \
+                            (title of the work or speech it appears in)",
+                            unique=True)
+    author = models.ForeignKey(Author)
+    date = models.DateField(blank=True, null=True,
+                            help_text="Date of the quote")
+    tags = models.ManyToManyField(WorkTag, blank=True,
+                                  help_text='Not implemented yet')
 
     def __unicode__(self):
-        return self.tag
+        return "%s: %s (%s)" % (self.author.name, self.name, self.date)
 
-    class Meta:
-        ordering = ("tag", )
+    def get_absolute_url(self):
+        return reverse('quotes:work', args=[str(self.id)])
 
-class Quote(models.Model):
-    text = models.TextField()
-    author = models.ForeignKey(Author)
-    tags = models.ManyToManyField(Tag, blank=True)
+class Quote(CommonData):
+    text = HTMLField()
+    tags = models.ManyToManyField(QuoteTag, blank=True)
+    work = models.ForeignKey(Work)
 
     def __unicode__(self):
-        return self.author.name + ": " + self.text
+        return self.work.author.name + ": " + self.text
 
+    def get_absolute_url(self):
+        return reverse('quotes:onequote', args=[str(self.id)])