From f25482a3e1ee042921ef30f75e47856f14a39511 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 23 Dec 2017 12:06:13 +0100 Subject: [PATCH] cleanup templates, reuse more blocks --- content/templates/content.html | 19 ++++++ content/templates/events.html | 63 +++++-------------- content/templates/latest_content.html | 18 ++++++ content/templates/preview.html | 36 +++++------ content/templates/projects.html | 50 ++++----------- content/templates/related.html | 4 +- content/templates/results.html | 71 ++++----------------- content/templates/section_index.html | 22 +++---- content/templates/texts.html | 64 ++++--------------- content/templates/works.html | 64 +++++-------------- content/views.py | 91 +++++++++++---------------- 11 files changed, 169 insertions(+), 333 deletions(-) create mode 100644 content/templates/content.html create mode 100644 content/templates/latest_content.html diff --git a/content/templates/content.html b/content/templates/content.html new file mode 100644 index 0000000..04e7e2e --- /dev/null +++ b/content/templates/content.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} + +{% block content %} + {% include "edit.html" %} +
+ {% if content.image %} + + {% endif %} +
+

{{content.title}}

+

{{content.formatted_header|safe}}

+

{{content.formatted_body|safe}}

+ {% include "opt.html" %} + {% include "links.html" %} + {% include "gallery.html" with gallery=content.gallery %} +
+
+ {% include "latest_content.html" %} +{% endblock %} diff --git a/content/templates/events.html b/content/templates/events.html index 77e8d5b..7122d5d 100644 --- a/content/templates/events.html +++ b/content/templates/events.html @@ -1,54 +1,19 @@ - - {% extends 'base.html' %} {% block content %} {% include "edit.html" with content=events %} - -
- {% if events.image %} - - {% endif %} -
-

{{events.title}}

-

{{events.formatted_header|safe}}

-

{{events.formatted_body|safe}}

- {% include "opt.html" with content=events %} - {% include "links.html" with content=events %} - {% include "gallery.html" with gallery=gallery %} -
-
- - - -
- {% include "related.html" with related=events.children.all %} - -
- - {% for events in latest_content_list %} - -
-
- -
-
- {{ events.title }} - -

{{ events.formatted_teaser }}

-
-
- - - {% endfor %} - more - -
-
- +
+ {% if events.image %} + + {% endif %} +
+

{{events.title}}

+

{{events.formatted_header|safe}}

+

{{events.formatted_body|safe}}

+ {% include "opt.html" with content=events %} + {% include "links.html" with content=events %} + {% include "gallery.html" with gallery=gallery %} +
+
+ {% include "latest_content.html" with content=events %} {% endblock %} diff --git a/content/templates/latest_content.html b/content/templates/latest_content.html new file mode 100644 index 0000000..3ccd65e --- /dev/null +++ b/content/templates/latest_content.html @@ -0,0 +1,18 @@ +{% if latest_content_list %} +
+ {% include "related.html" with related=content.children.all %} + +
+ {% for content in latest_content_list %} + {% include "preview.html" with content=content %} + {% endfor %} + {% with section|lower|add:'_list' as section_list %} + {% if has_more_content %} + more + {% endif %} + {% endwith %} +
+
+{% else %} +

No content.

+{% endif %} diff --git a/content/templates/preview.html b/content/templates/preview.html index 287410e..ad69f93 100644 --- a/content/templates/preview.html +++ b/content/templates/preview.html @@ -1,21 +1,19 @@ -
-
-
- {% if content.image_url %} - - {% endif %} -
-
- {{content.title}} - {% if content.type.name == 'events' %} - - {% endif %} -

{{ content.formatted_teaser }}

-
+
+
+ {% if content.image_url %} + + {% endif %}
+
+ {{content.title}} + {% if content.type.name == 'events' %} + + {% endif %} +

{{ content.formatted_teaser }}

+
diff --git a/content/templates/projects.html b/content/templates/projects.html index 2be8880..0936014 100644 --- a/content/templates/projects.html +++ b/content/templates/projects.html @@ -1,47 +1,19 @@ - - {% extends 'base.html' %} {% block content %} {% include "edit.html" with content=projects %} - -
- {% if projects.image %} - - {% endif %} -
-

{{projects.title}}

-

{{projects.formatted_header|safe}}

-

{{projects.formatted_body|safe}}

- {% include "opt.html" with content=projects %} - {% include "links.html" with content=projects %} - {% include "gallery.html" with gallery=gallery %} -
-
- -{% if latest_content_list %} - -
- {% include "related.html" with related=projects.children.all %} - -
- - {% for projects in latest_content_list %} -
-
- -
-
- {{ projects.title }} -

{{projects.formatted_teaser}}

-
-
- {% endfor %} - {% else %} -

No projects.

+
+ {% if projects.image %} + {% endif %} - more +
+

{{projects.title}}

+

{{projects.formatted_header|safe}}

+

{{projects.formatted_body|safe}}

+ {% include "opt.html" with content=projects %} + {% include "links.html" with content=projects %} + {% include "gallery.html" with gallery=gallery %}
- + {% include "latest_content.html" with content=texts %} {% endblock %} diff --git a/content/templates/related.html b/content/templates/related.html index e27e75c..6cca891 100644 --- a/content/templates/related.html +++ b/content/templates/related.html @@ -1,7 +1,9 @@ {% if related %} {% for content in related %} -{% include "preview.html" %} +
+ {% include "preview.html" %} +
{% endfor %}

diff --git a/content/templates/results.html b/content/templates/results.html index 7df050b..5c3bc7b 100644 --- a/content/templates/results.html +++ b/content/templates/results.html @@ -22,69 +22,24 @@
- {% elif row.type.name == 'events' %} -
-
- {% if row.image_url %} - - {% endif %} -
-
- {{ row.title }} - -

{{row.formatted_teaser}}

- read more

-
-
- {% elif row.type.name == 'projects' %} -
-
- {% if row.image_url %} - - {% endif %} -
-
- {{ row.title }} -

{{row.formatted_teaser}}

- read more

-
-
- {% elif row.type.name == 'works' %} -
-
- {% if row.image_url %} - - {% endif %} -
-
- {{ row.title }} -

{{row.formatted_teaser}}

- read more

-
-
- {% elif row.type.name == 'texts' %} -
-
- {% if row.image_url %} - - {% endif %} -
-
- {{ row.title }} -

{{row.formatted_teaser}}

- read more

-
-
{% else %} - {% if request.user.is_staff %} -
+
- Add view for content type: {{row.type}} + {% if row.image_url %} + + {% endif %} +
+
+ {{ row.title }} + {% if row.type.name == 'events' %} + + {% endif %} +

{{row.formatted_teaser}}

+ read more

{% endif %} - {% endif %} {% endfor %}

diff --git a/content/templates/section_index.html b/content/templates/section_index.html index ac450f6..6cf6545 100644 --- a/content/templates/section_index.html +++ b/content/templates/section_index.html @@ -5,20 +5,20 @@
-
- {% for content in content %} -
+
+ {% for content in content %} +
{% if content.image_url %} -
- -
+
+ +
{% endif %} -
- {{ content.title }} +
+ {{ content.title }}

{{content.formatted_teaser}}

-
-
- {% endfor %} +
+
+ {% endfor %} more
diff --git a/content/templates/texts.html b/content/templates/texts.html index baf35c6..6480de2 100644 --- a/content/templates/texts.html +++ b/content/templates/texts.html @@ -2,58 +2,16 @@ {% block content %} {% include "edit.html" with content=texts %} - -
- {% if texts.image %} - - {% endif %} -
-

{{texts.title}}

-

{{texts.formatted_header|safe}}

-

{{texts.formatted_body|safe}}

- {% include "opt.html" with content=texts %} -
-
- -{% if latest_content_list %} - -
- {% if texts.children.all %} - - {% for child in texts.children.all %} - - +
+

{{texts.title}}

+

{{texts.formatted_header|safe}}

+

{{texts.formatted_body|safe}}

+ {% include "opt.html" with content=texts %} +
+
+ {% include "latest_content.html" with content=texts %} {% endblock %} diff --git a/content/templates/works.html b/content/templates/works.html index 3848b4f..700463d 100644 --- a/content/templates/works.html +++ b/content/templates/works.html @@ -1,53 +1,19 @@ - - {% extends 'base.html' %} {% block content %} - -
- -
-

{{works.title}}

-

{{works.formatted_header|safe}}

-

{{works.formatted_body|safe}}

-
-
- -{% if latest_content_list %} - -
- {% if works.children.all %} - - {% for child in works.children.all %} - - {% endfor %} - {% endif %} - -
- - {% for works in latest_content_list %} -
-
- -
-
- {{ works.title }} -

{{ works.formatted_teaser }}

-
-
- {% endfor %} - {% else %} -

No works.

- {% endif %} - more -
-
- + {% include "edit.html" with content=works %} +
+ {% if works.image %} + + {% endif %} +
+

{{works.title}}

+

{{works.formatted_header|safe}}

+

{{works.formatted_body|safe}}

+ {% include "opt.html" with content=works %} + {% include "links.html" with content=projects %} + {% include "gallery.html" with gallery=gallery %} +
+
+ {% include "latest_content.html" with content=works %} {% endblock %} diff --git a/content/views.py b/content/views.py index 489fc18..4808492 100644 --- a/content/views.py +++ b/content/views.py @@ -25,7 +25,7 @@ def index(request): base = base.filter(published=True) upcoming_events = base.filter(datestart__gt=now)[:12] ongoing_events = base.filter(datestart__lt=now, dateend__gte=now)[:12] - past_events = base.filter(Q(dateend__lt=now)|Q(dateend=None, datestart__lt=now))[:12] + past_events = base.filter(Q(dateend__lt=now) | Q(dateend=None, datestart__lt=now))[:12] homepage = Content.objects.filter(type__name='homepage').order_by('-datestart')[:1] context = { @@ -84,9 +84,9 @@ def section_list(request, section): return render(request, 'results.html', { 'base_query': base_query, - 'section': section, 'content': content, - 'query': q + 'query': q, + 'section': section, }) def event(request): @@ -107,71 +107,53 @@ def event(request): 'ongoing_events': ongoing_events, 'past_events': past_events, 'featured': featured, + 'section': 'Events', } return render(request, 'event.html', context) -def events(request, shortname=None): +def get_related_content(types, current=None, max_length=10): + latest_content_list = Content.objects.filter(type__name__in=types).order_by('-datestart') + if current: + latest_content_list = latest_content_list.exclude(pk=current.pk) + latest_content_list = latest_content_list.filter(published=True) + more = latest_content_list.count > max_length + latest_content_list = latest_content_list[:max_length] + return latest_content_list, more + + +def render_content(request, shortname, section, template, types): if not shortname: - return event(request) - events = get_object_or_404(Content, shortname=shortname, type__name__in=['news', 'events']) - if not events.published and not request.user.is_staff: + if section == 'Events': + return event(request) + else: + return section_index(request, section) + content = get_object_or_404(Content, shortname=shortname, type__name__in=types) + if not content.published and not request.user.is_staff: raise Http404 - gallery = get_or_none(Gallery, slug=shortname) - latest_content_list = Content.objects.filter(type__name__in=['events', 'news']).order_by('-datestart') - latest_content_list = latest_content_list.exclude(pk=events.pk) - latest_content_list = latest_content_list[:10] - return render(request, 'events.html', { - 'events': events, + latest_content_list, more = get_related_content(types, content) + return render(request, template, { + 'content': content, 'latest_content_list': latest_content_list, - 'gallery': gallery + 'has_more_content': more, + 'section': section, }) + +def events(request, shortname=None): + return render_content(request, shortname, 'Events', 'content.html', ['events', 'news']) + + def projects(request, shortname=None): - if not shortname: - return section_index(request, 'Projects') - projects = get_object_or_404(Content, shortname=shortname, type__name='projects') - if not projects.published and not request.user.is_staff: - raise Http404 - gallery = get_or_none(Gallery, slug=shortname) - latest_content_list = Content.objects.filter(type__name='projects').order_by('-datestart') - latest_content_list = latest_content_list.exclude(pk=projects.pk) - latest_content_list = latest_content_list[:10] - return render(request, 'projects.html', { - 'projects': projects, - 'latest_content_list': latest_content_list, - 'gallery': gallery - }) + return render_content(request, shortname, 'Projects', 'content.html', ['projects']) + def works(request, shortname=None): - if not shortname: - return section_index(request, 'Works') - works = get_object_or_404(Content, shortname=shortname, type__name='works') - if not works.published and not request.user.is_staff: - raise Http404 - gallery = get_or_none(Gallery, slug=shortname) - latest_content_list = Content.objects.filter(type__name='works') - latest_content_list = latest_content_list.exclude(pk=works.pk) - latest_content_list = latest_content_list[:10] - return render(request, 'works.html', { - 'works': works, - 'latest_content_list': latest_content_list, - 'gallery': gallery - }) + return render_content(request, shortname, 'Works', 'content.html', ['works']) + def texts(request, shortname=None): - if not shortname: - return section_index(request, 'Texts') - texts = get_object_or_404(Content, shortname=shortname, type__name='texts') - gallery = get_or_none(Gallery, slug=shortname) - latest_content_list = Content.objects.filter(type__name='texts') - latest_content_list = latest_content_list.exclude(pk=texts.pk) - latest_content_list = latest_content_list[:10] - return render(request, 'texts.html', { - 'texts': texts, - 'latest_content_list': latest_content_list, - 'gallery': gallery - }) + return render_content(requests, shortname, 'Texts', 'content.html', ['texts']) def page(request, shortname): @@ -186,6 +168,7 @@ def limit_content(content, q): content = content.filter(Q(body__icontains=q) | Q(title__icontains=q) | Q(header__icontains=q)).distinct() return content + def search(request): content = Content.objects.filter(published=True).order_by('-datestart') content = content.exclude(type__name__in=['page', 'homepage'])