]> git.sommitrealweird.co.uk Git - sommitrealweird.git/commitdiff
Update for newer django django-4.2
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Thu, 4 Jul 2024 14:00:45 +0000 (15:00 +0100)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Thu, 4 Jul 2024 14:00:45 +0000 (15:00 +0100)
sommitrealweird/bpcms/models.py
sommitrealweird/bpcms/urls.py
sommitrealweird/bpcms/views.py
sommitrealweird/generic/templatetags/thumbnail.py
sommitrealweird/urls.py

index 5c49f49b91f4f6b706af653d7def5c480fb2cda6..a89f3ba8cadad25ea121505e672f6f748c6e7bc1 100644 (file)
@@ -66,14 +66,14 @@ class Document(models.Model):
     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})
+                return reverse('bpcms-document-view', kwargs={'path': self.folder.parent.get_path() + "/" + self.folder.slug})
             else:
-                return reverse('bpcms-document-view', kwargs={'slug': self.folder.slug})
+                return reverse('bpcms-document-view', kwargs={'path': self.folder.slug})
         else:
             if self.folder:
-                return reverse('bpcms-document-view', kwargs={'folders': self.get_folder_path(), 'slug': self.slug})
+                return reverse('bpcms-document-view', kwargs={'path': self.get_folder_path() + "/" + self.slug})
             else:
-                return reverse('bpcms-document-view', kwargs={'slug': self.slug})
+                return reverse('bpcms-document-view', kwargs={'path': self.slug})
 
 class Folder(models.Model):
     title = models.CharField(max_length=150)
@@ -106,6 +106,6 @@ class Folder(models.Model):
 
     def get_absolute_url(self):
         if self.parent:
-            return reverse('bpcms-document-view', kwargs={'folders': self.parent.get_path(), 'slug': self.slug})
+            return reverse('bpcms-document-view', kwargs={'path': self.parent.get_path() + "/" + self.slug})
         else:
-            return reverse('bpcms-document-view', kwargs={'slug': self.slug})
+            return reverse('bpcms-document-view', kwargs={'path': self.slug})
index 264af0ab5c9ba482c67e1facb7a6a1c57f4b65d3..2fc374cbe3aa7a15bbe2cd7cb90bdd11a27bd092 100644 (file)
@@ -2,9 +2,8 @@ from django.urls import re_path
 import bpcms.views
 
 urlpatterns = [
-    re_path(r'^$', bpcms.views.document_view, {'slug': 'index'}, name='bpcms-document-view'),
+    re_path(r'^$', bpcms.views.document_view, {'path': 'index'}, name='bpcms-document-view'),
     re_path(r'^css-docs/(?P<slug>[^/]+)$', bpcms.views.css_view),
-    re_path(r'^(?P<slug>[^/]+)/$', bpcms.views.document_view, name='bpcms-document-view'),
-    re_path(r'^(?P<folders>.*)/(?P<slug>[^/]+)/$', bpcms.views.document_view, name='bpcms-document-view'),
+    re_path(r'^(?P<path>.*)/$', bpcms.views.document_view, name='bpcms-document-view'),
 ]
 
index e0087c86ad5aa26a4eb14f4a959bb0428aaaab63..82dfb563a176f026d5c453b58aa84755be62b3d8 100644 (file)
@@ -4,28 +4,37 @@ from django.shortcuts import render
 from django.template import RequestContext, loader
 import sys
 
-def document_view(request, slug=None, folders=None):
-    if folders == None and slug == None:
+def document_view(request, path = None):
+
+    if path == None:
         raise Http404()
 
+    folders = path.split("/")
+    slug = None
     folder = None
 
     if folders != None:
-        folders = folders.split('/')
         resfolder = None
-        for folder in folders:
+        prevfolder = None
+        for i,folder_name in enumerate(folders):
             try:
                 if resfolder:
-                    resfolder = Folder.objects.get(slug__exact=folder, parent__exact=resfolder)
+                    resfolder = Folder.objects.get(slug__exact=folder_name, parent__exact=resfolder)
                 else:
-                    resfolder = Folder.objects.get(slug__exact=folder, parent__isnull=True)
+                    resfolder = Folder.objects.get(slug__exact=folder_name, parent__isnull=True)
                 resfolder = resfolder.id
             except:
-                raise Http404
+                slug = folders[i]
+                folders = folders[:i]
+                break
         try:
-            folder = Folder.objects.get(pk=resfolder)
+            if resfolder != None:
+                folder = Folder.objects.get(pk=resfolder)
         except:
-            raise Http404
+            raise Http404("Badgers")
+
+    if slug == None:
+        slug = "index"
 
     if folder != None:
         try:
@@ -42,7 +51,7 @@ def document_view(request, slug=None, folders=None):
                 else:
                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
             except:
-                raise Http404
+                raise Http404(folder + "/" + slug)
     else:
         try:
             if not request.user.is_authenticated:
@@ -50,7 +59,6 @@ def document_view(request, slug=None, folders=None):
             else:
                 doc = Document.objects.get(slug__exact=slug, folder__isnull=True)
         except:
-            sys.stderr.write("Unexpected error:", sys.exc_info()[0])
             try:
                 folder = Folder.objects.get(slug__exact=slug, parent__isnull=True)
                 if not request.user.is_authenticated():
@@ -58,7 +66,7 @@ def document_view(request, slug=None, folders=None):
                 else:
                     doc = Document.objects.get(slug__exact='index', folder__exact=folder)
             except:
-                raise Http404
+                raise Http404(slug)
     template_name = "bpcms/%s.html" %(doc.format,)
     c = {
             "content"   : doc.content,
index 4e20cef8f92ca2d86603b9a8ca3990eb772ffbf3..c368a6bcdd7e850fa1f38f4a06beff4061dbd351 100644 (file)
@@ -22,10 +22,10 @@ def thumbnail(image_url, args=''):
         except:
             pass
 
-    if options.has_key("height") or options.has_key("width"):
-        if options.has_key("width") and options.has_key("height"):
+    if "height" in options or "width" in options:
+        if "width" in options and "height" in options:
             return helpers.make_thumbnail(image_url[len(settings.MEDIA_URL):], width=options["width"], height=options["height"])
-        elif options.has_key("width"):
+        elif "width" in options:
             return helpers.make_thumbnail(image_url[len(settings.MEDIA_URL):], width=options["width"])
         else:
             return helpers.make_thumbnail(image_url[len(settings.MEDIA_URL):], height=options["height"])
index 73137930f7948749e5578e9f9a495046bcff7fe3..bcfc7f0fb98d13d03a551342e6cd26160683111a 100644 (file)
@@ -23,7 +23,7 @@ urlpatterns = [
     # (r'^sommitrealweird/', include('sommitrealweird.foo.urls')),
 
     # Uncomment this for admin:
-    re_path(r'^$', bpcms.views.document_view, {'slug': 'index'}),
+    re_path(r'^$', bpcms.views.document_view, {'path': 'index'}),
     re_path(r'^media/(?P<path>.*)$', django.views.static.serve, {'document_root': MEDIA_ROOT, 'show_indexes': True}),
     re_path(r'^static/(?P<path>.*)$', django.views.static.serve, {'document_root': STATIC_ROOT, 'show_indexes': True}),
     re_path(r'^admin/', admin.site.urls),