From e1b491186d911fcdc98b18b4c0031ed65d43fdae Mon Sep 17 00:00:00 2001 From: Brett Parker Date: Thu, 4 Jul 2024 15:00:45 +0100 Subject: [PATCH 1/1] Update for newer django --- sommitrealweird/bpcms/models.py | 12 +++---- sommitrealweird/bpcms/urls.py | 5 ++- sommitrealweird/bpcms/views.py | 32 ++++++++++++------- .../generic/templatetags/thumbnail.py | 6 ++-- sommitrealweird/urls.py | 2 +- 5 files changed, 32 insertions(+), 25 deletions(-) diff --git a/sommitrealweird/bpcms/models.py b/sommitrealweird/bpcms/models.py index 5c49f49..a89f3ba 100644 --- a/sommitrealweird/bpcms/models.py +++ b/sommitrealweird/bpcms/models.py @@ -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}) diff --git a/sommitrealweird/bpcms/urls.py b/sommitrealweird/bpcms/urls.py index 264af0a..2fc374c 100644 --- a/sommitrealweird/bpcms/urls.py +++ b/sommitrealweird/bpcms/urls.py @@ -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[^/]+)$', bpcms.views.css_view), - re_path(r'^(?P[^/]+)/$', bpcms.views.document_view, name='bpcms-document-view'), - re_path(r'^(?P.*)/(?P[^/]+)/$', bpcms.views.document_view, name='bpcms-document-view'), + re_path(r'^(?P.*)/$', bpcms.views.document_view, name='bpcms-document-view'), ] diff --git a/sommitrealweird/bpcms/views.py b/sommitrealweird/bpcms/views.py index e0087c8..82dfb56 100644 --- a/sommitrealweird/bpcms/views.py +++ b/sommitrealweird/bpcms/views.py @@ -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, diff --git a/sommitrealweird/generic/templatetags/thumbnail.py b/sommitrealweird/generic/templatetags/thumbnail.py index 4e20cef..c368a6b 100644 --- a/sommitrealweird/generic/templatetags/thumbnail.py +++ b/sommitrealweird/generic/templatetags/thumbnail.py @@ -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"]) diff --git a/sommitrealweird/urls.py b/sommitrealweird/urls.py index 7313793..bcfc7f0 100644 --- a/sommitrealweird/urls.py +++ b/sommitrealweird/urls.py @@ -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.*)$', django.views.static.serve, {'document_root': MEDIA_ROOT, 'show_indexes': True}), re_path(r'^static/(?P.*)$', django.views.static.serve, {'document_root': STATIC_ROOT, 'show_indexes': True}), re_path(r'^admin/', admin.site.urls), -- 2.39.5