]> git.sommitrealweird.co.uk Git - sommitrealweird.git/blobdiff - sommitrealweird/bpcms/models.py
Changes for django 2.2
[sommitrealweird.git] / sommitrealweird / bpcms / models.py
index bf2b93b019086fc551e1df331b0664a7568c86c7..5c49f49b91f4f6b706af653d7def5c480fb2cda6 100644 (file)
@@ -1,25 +1,42 @@
 from django.db import models
 from django.conf import settings
 from django.contrib import admin
+from django.core.cache import cache
+from django.urls import reverse
 
 FORMAT_CHOICES = (
         ('rst', 'reStructuredText'),
         ('html', 'HTML'),
     )
 
+class CSSDocument(models.Model):
+    title = models.CharField(max_length=150)
+    slug = models.SlugField()
+    content = models.TextField()
+
+    def __str__(self):
+        return self.__unicode__()
+
+    def __unicode__(self):
+        return u'%s' %(self.title,)
+
 class Document(models.Model):
     title = models.CharField(max_length=150)
-    islive = models.BooleanField()
-    folder = models.ForeignKey('Folder', null=True, blank=True)
+    islive = models.BooleanField(default=False)
+    extracss = models.ManyToManyField(CSSDocument, blank=True)
+    folder = models.ForeignKey('Folder', on_delete=models.SET_NULL, null=True, blank=True)
     format = models.CharField(max_length=10, choices=FORMAT_CHOICES)
     slug = models.SlugField()
     content = models.TextField()
 
+    class Meta:
+        ordering = ["folder"]
+
     def __str__(self):
         return self.__unicode__()
 
     def __unicode__(self):
-        return u'%s (%s)' %(self.title, self.slug)
+        return u'%s - %s' %(self.get_basic_url(), self.title)
 
     def save(self):
         super(Document, self).save()
@@ -34,23 +51,34 @@ class Document(models.Model):
                 doc.save()
 
     def get_basic_url(self):
-        folders = []
-        curfolder = self.folder
-        while curfolder != None:
-            folders.append(curfolder.slug)
-            curfolder = curfolder.parent
-
-        folders.reverse()
-        folderstring = "/".join(folders)
+        folderstring = self.get_folder_path()
         if folderstring != u'':
             folderstring = "%s/" %(folderstring,)
 
         return "%s%s%s/" %(settings.BPCMS_ROOT, folderstring, self.slug)
 
+    def get_folder_path(self):
+        if not self.folder:
+            return ''
+        else:
+            return self.folder.get_path()
+
+    def get_absolute_url(self):
+        if self.slug == 'index':
+            if self.folder.parent:
+                return reverse('bpcms-document-view', kwargs={'folders': self.folder.parent.get_path(), 'slug': self.folder.slug})
+            else:
+                return reverse('bpcms-document-view', kwargs={'slug': self.folder.slug})
+        else:
+            if self.folder:
+                return reverse('bpcms-document-view', kwargs={'folders': self.get_folder_path(), 'slug': self.slug})
+            else:
+                return reverse('bpcms-document-view', kwargs={'slug': self.slug})
+
 class Folder(models.Model):
     title = models.CharField(max_length=150)
     slug = models.SlugField()
-    parent = models.ForeignKey('self', null=True, blank=True)
+    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)
 
     def __str__(self):
         return self.__unicode__()
@@ -67,10 +95,17 @@ class Folder(models.Model):
 
         folders.reverse()
         folderstring = "/".join(folders)
-        if folderstring != u'':
-            folderstring = "%s/" %(folderstring,)
-
         return folderstring
-        
-    def get_basic_url(self):
-        return "%s%s" %(settings.BPCMS_ROOT, self.get_path())
+
+    def save(self, force_insert=False, force_update=False):
+        super(Folder, self).save(force_insert, force_update)
+        if self.parent == None:
+            cache.delete('bpcms_mainmenu')
+        else:
+            cache.delete('bpcms_submenu_%s' %(self.get_path()))
+
+    def get_absolute_url(self):
+        if self.parent:
+            return reverse('bpcms-document-view', kwargs={'folders': self.parent.get_path(), 'slug': self.slug})
+        else:
+            return reverse('bpcms-document-view', kwargs={'slug': self.slug})