From 169bd3f02cf4e482f7b5e02c3bda086e5c28c169 Mon Sep 17 00:00:00 2001 From: Emilie Date: Wed, 2 Nov 2016 19:09:24 +0100 Subject: [PATCH] Media : get related FK dynamically, need to set dynamically context object name --- .../core/templatetags/organization_tags.py | 16 ++++++++++++ app/organization/media/views.py | 16 +++++++++--- .../{ => event}/includes/event_card.html | 0 app/templates/core/inc/related_content.html | 4 +-- .../core/inc/related_content_media.html | 25 +++++++++++++++++++ app/templates/festival/artist_detail.html | 2 +- app/templates/festival/video_detail.html | 2 +- app/templates/home/inc/body.html | 8 +++--- app/templates/media/playlist_detail.html | 7 +----- app/templates/pages/custompage.html | 2 +- .../pages/{ => page}/includes/page_box.html | 0 .../pages/{ => page}/includes/page_card.html | 1 - .../{ => page}/includes/page_search.html | 0 app/templates/pages/richtextpage.html | 2 +- app/templates/pages/teampage.html | 2 +- 15 files changed, 66 insertions(+), 21 deletions(-) rename app/templates/agenda/{ => event}/includes/event_card.html (100%) create mode 100644 app/templates/core/inc/related_content_media.html rename app/templates/pages/{ => page}/includes/page_box.html (100%) rename app/templates/pages/{ => page}/includes/page_card.html (99%) rename app/templates/pages/{ => page}/includes/page_search.html (100%) diff --git a/app/organization/core/templatetags/organization_tags.py b/app/organization/core/templatetags/organization_tags.py index 683e37e9..0fe1784f 100644 --- a/app/organization/core/templatetags/organization_tags.py +++ b/app/organization/core/templatetags/organization_tags.py @@ -119,3 +119,19 @@ def sub_topics(topic): @register.filter def classname(obj): return obj.__class__.__name__ + + +@register.filter +def classname(obj): + return obj.__class__.__name__ + +@register.filter +def app_label_short(obj): + app_label = obj._meta.app_config.label + if app_label.find("_") > 0: + app_label_short = app_label.split("_")[1] + elif app_label.find("-") > 0: + app_label_short = app_label.split("-")[1] + else : + app_label_short = app_label + return app_label_short diff --git a/app/organization/media/views.py b/app/organization/media/views.py index 95358fba..88626c7f 100644 --- a/app/organization/media/views.py +++ b/app/organization/media/views.py @@ -12,18 +12,28 @@ class PlaylistDetailView(SlugMixin, DetailView): model = Playlist template_name='media/playlist_detail.html' context_object_name = 'playlist' - def get_context_data(self, **kwargs): - self.related_objects = [] context = super(PlaylistDetailView, self).get_context_data(**kwargs) + self.related_objects = [] + self.concrete_objects = [] + related_model = PlaylistRelated._meta.get_fields() related_playlist = self.object.playlist_related.all() + # get dynamically related objects like articleplaylist, projectplaylist, eventplaylist etc.... for rm in related_model: for rp in related_playlist: if hasattr(rp, rm.name): self.related_objects.append(getattr(rp, rm.name)) - context['related_objects'] = self.related_objects + # get dynamically related instance of related objects. Example: articleplaylist => article + for ro in self.related_objects: + if not isinstance(ro, int) and ro != self.object: + for c_field in ro._meta.get_fields(): + attr = getattr(ro, c_field.name) + if not isinstance(attr, int) and attr != self.object and not isinstance(attr, PlaylistRelated): + self.concrete_objects.append(attr) + + context['concrete_objects'] = self.concrete_objects return context diff --git a/app/templates/agenda/includes/event_card.html b/app/templates/agenda/event/includes/event_card.html similarity index 100% rename from app/templates/agenda/includes/event_card.html rename to app/templates/agenda/event/includes/event_card.html diff --git a/app/templates/core/inc/related_content.html b/app/templates/core/inc/related_content.html index 795c6e76..2b87edc7 100644 --- a/app/templates/core/inc/related_content.html +++ b/app/templates/core/inc/related_content.html @@ -17,11 +17,11 @@ {% endwith %} {% elif content.content_type.model == "event" %} {% with content.content_object as event %} - {% include "agenda/includes/event_card.html" %} + {% include "agenda/event/includes/event_card.html" %} {% endwith %} {% elif content.content_type.model == "custompage" %} {% with content.content_object as page %} - {% include "pages/includes/page_card.html" %} + {% include "pages/page/sincludes/page_card.html" %} {% endwith %} {% endif %} diff --git a/app/templates/core/inc/related_content_media.html b/app/templates/core/inc/related_content_media.html new file mode 100644 index 00000000..ffe1c69c --- /dev/null +++ b/app/templates/core/inc/related_content_media.html @@ -0,0 +1,25 @@ +{% load i18n mezzanine_tags keyword_tags organization_tags pages_tags %} +{% if concrete_objects %} +
+
+
+
+
+

{% if object.related_title.title %}{{ object.related_title.title }}{% else %}{% trans "Also discover" %}{% endif %}

+
+
+
+ {% for concrete_object in concrete_objects %} +
+ {{ concrete_object|app_label_short }} + {% with app_label=concrete_object|app_label_short classname=concrete_object|classname|lower %} + {% with app_label|add:"/"|add:classname|add:"/includes/"|add:classname|add:"_card.html" as template %} + {% include template with classname=concrete_object %} + {% endwith %} + {% endwith %} +
+ {% endfor %} +
+
+
+{% endif %} diff --git a/app/templates/festival/artist_detail.html b/app/templates/festival/artist_detail.html index f94297b8..439ce01b 100644 --- a/app/templates/festival/artist_detail.html +++ b/app/templates/festival/artist_detail.html @@ -28,7 +28,7 @@
{% for event in artist.events.all|no_parents %} - {% include 'agenda/includes/event_card.html' %} + {% include 'agenda/event/includes/event_card.html' %} {% endfor %} {% for video in artist.videos.all %} {% include 'festival/inc/video_card.html' %} diff --git a/app/templates/festival/video_detail.html b/app/templates/festival/video_detail.html index b954cc14..341105ec 100644 --- a/app/templates/festival/video_detail.html +++ b/app/templates/festival/video_detail.html @@ -29,7 +29,7 @@
{% with video.event as event %} - {% include 'agenda/includes/event_card.html' %} + {% include 'agenda/event/includes/event_card.html' %} {% for artist in video.artists.all %} {% include "festival/inc/artist_card.html" %} {% endfor %} diff --git a/app/templates/home/inc/body.html b/app/templates/home/inc/body.html index bf5be256..f5b462f7 100644 --- a/app/templates/home/inc/body.html +++ b/app/templates/home/inc/body.html @@ -13,11 +13,11 @@ {% endwith %} {% elif slider.content_type.model == "event" %} {% with slider.content_object as event %} - {% include "agenda/includes/event_card.html" %} + {% include "agenda/event/includes/event_card.html" %} {% endwith %} {% elif slider.content_type.model == "custompage" %} {% with slider.content_object as page %} - {% include "pages/includes/page_card.html" %} + {% include "pages/page/includes/page_card.html" %} {% endwith %} {% endif %}
@@ -34,11 +34,11 @@ {% endwith %} {% elif content.content_type.model == "event" %} {% with content.content_object as event %} - {% include "agenda/includes/event_card.html" %} + {% include "agenda/event/includes/event_card.html" %} {% endwith %} {% elif content.content_type.model == "custompage" %} {% with content.content_object as page %} - {% include "pages/includes/page_card.html" %} + {% include "pages/page/includes/page_card.html" %} {% endwith %} {% endif %}
diff --git a/app/templates/media/playlist_detail.html b/app/templates/media/playlist_detail.html index 7bf636bf..af929c0f 100644 --- a/app/templates/media/playlist_detail.html +++ b/app/templates/media/playlist_detail.html @@ -64,10 +64,5 @@
{{ playlist.content|safe }}
- - {% for related_object in related_objects %} - {{ related_object }} -
- {% endfor %} - + {% include "core/inc/related_content_media.html" %} {% endblock %} diff --git a/app/templates/pages/custompage.html b/app/templates/pages/custompage.html index 2ffb0b39..ae57217b 100644 --- a/app/templates/pages/custompage.html +++ b/app/templates/pages/custompage.html @@ -43,7 +43,7 @@ {% for child in childrens %} {% if child.in_menus.0 %} {% with child as object %} - {% include "pages/includes/page_box.html" %} + {% include "pages/page/includes/page_box.html" %} {% endwith %} {% endif %} {% endfor %} diff --git a/app/templates/pages/includes/page_box.html b/app/templates/pages/page/includes/page_box.html similarity index 100% rename from app/templates/pages/includes/page_box.html rename to app/templates/pages/page/includes/page_box.html diff --git a/app/templates/pages/includes/page_card.html b/app/templates/pages/page/includes/page_card.html similarity index 99% rename from app/templates/pages/includes/page_card.html rename to app/templates/pages/page/includes/page_card.html index 22a5c391..94e426f4 100644 --- a/app/templates/pages/includes/page_card.html +++ b/app/templates/pages/page/includes/page_card.html @@ -12,7 +12,6 @@ {% block url %} {{ page.get_absolute_url }} {% endblock %} - {% block image %} {% with page.images.all|get_type:'card' as images %} {% if images %} diff --git a/app/templates/pages/includes/page_search.html b/app/templates/pages/page/includes/page_search.html similarity index 100% rename from app/templates/pages/includes/page_search.html rename to app/templates/pages/page/includes/page_search.html diff --git a/app/templates/pages/richtextpage.html b/app/templates/pages/richtextpage.html index 51ed3dc5..5f7953b1 100644 --- a/app/templates/pages/richtextpage.html +++ b/app/templates/pages/richtextpage.html @@ -18,7 +18,7 @@
{% for event in page.featured.all.0.events.all %} - {% include 'agenda/includes/event_card.html' %} + {% include 'agenda/event/includes/event_card.html' %} {% endfor %}
{% endif %} diff --git a/app/templates/pages/teampage.html b/app/templates/pages/teampage.html index f262eadf..3dfd6d33 100644 --- a/app/templates/pages/teampage.html +++ b/app/templates/pages/teampage.html @@ -41,7 +41,7 @@
{% for children in childrens %} {% with children as object %} - {% include "pages/includes/page_box.html" %} + {% include "pages/page/includes/page_box.html" %} {% endwith %} {% endfor %}
-- 2.39.5