Correctly render membership credits without logo/url on front page
authorSybren A. Stüvel <sybren@stuvel.eu>
Thu, 7 Mar 2019 11:02:39 +0000 (12:02 +0100)
committerSybren A. Stüvel <sybren@stuvel.eu>
Thu, 7 Mar 2019 11:02:39 +0000 (12:02 +0100)
I've reduced duplicate code in the template by moving the rendering to a
custom template tag. Now all credits are rendered in the same way. Fields
that should be empty (given the membership's level) are set to `None`
before feeding the membership to the front-end.

blender_fund_main/models.py
blender_fund_main/page_models.py
blender_fund_main/templatetags/membership_tags.py [new file with mode: 0644]
websrc/templates/blender_fund/landing.pug
websrc/templates/blender_fund/tags/membership_credit.pug [new file with mode: 0644]

index 7ca117c..8ad96fc 100644 (file)
@@ -299,6 +299,16 @@ class Membership(looper.model_mixins.RecordModifcationMixin,  # type: ignore
         self.subscription.user_id = self.user_id
         self.subscription.save(update_fields={'user_id'})
 
+    def limit_fields(self) -> None:
+        """Set all fields not allowed by the membership level to None.
+
+        Only use this when sending the Membership to the front end, to hide
+        fields that shouldn't even be set for this membership level.
+        """
+
+        for attr in self.level.hidden_attributes_set:
+            setattr(self, attr, None)
+
 
 class BadgerQueuedCall(models.Model):
     """Queued call to the Badger API of Blender ID.
index 3309786..61234e0 100644 (file)
@@ -144,6 +144,7 @@ class HomePage(Page):
             .all()
         memberships = defaultdict(list)
         for result in query:
+            result.limit_fields()
             memberships[result.level.name].append(result)
         for mems in memberships.values():
             random.shuffle(mems)
diff --git a/blender_fund_main/templatetags/membership_tags.py b/blender_fund_main/templatetags/membership_tags.py
new file mode 100644 (file)
index 0000000..ca0fbbb
--- /dev/null
@@ -0,0 +1,11 @@
+from django import template
+
+from blender_fund_main import models
+
+register = template.Library()
+
+
+@register.inclusion_tag('blender_fund/tags/membership_credit.html')
+def membership_credit(membership: models.Membership):
+    """Show a membership credit, used on the front page."""
+    return {'membership': membership}
index 218a91c..74d9e5b 100644 (file)
@@ -1,4 +1,5 @@
 | {% extends "blender_fund/layout.html" %}
+| {% load membership_tags %}
 | {% load looper %}
 | {% load wagtailcore_tags %}
 | {% load wagtailimages_tags %}
                        | {% endfor %}
                | {% endif %}
 
+
                | {% if memberships.Corporate_Gold %}
                #credits-corporate-gold.membership-level
                        a.section-title(href="#credits-corporate-gold")
 
                        ul.gold
                                | {% for m in memberships.Corporate_Gold %}
-                               li
-                                       a(href="{{ m.url }}", target="_blank")
-                                               | {% if m.logo %}
-                                               img(src="{{ m.logo.url }}" alt="{{ m.display_name }}")
-                                               | {% else %}
-                                               span.name {{ m.display_name }}
-                                               | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
 
                        ul.silver
                                | {% for m in memberships.Corporate_Silver %}
-                               li
-                                       a(href="{{ m.url }}", target="_blank")
-                                               | {% if m.logo %}
-                                               img(src="{{ m.logo.url }}" alt="{{ m.display_name }}")
-                                               | {% else %}
-                                               span.name {{ m.display_name }}
-                                               | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
 
                        ul.bronze
                                | {% for m in memberships.Corporate_Bronze %}
-                               li
-                                       a(href="{{ m.url }}", target="_blank")
-                                               | {% if m.logo %}
-                                               img(src="{{ m.logo.url }}" alt="{{ m.display_name }}")
-                                               | {% else %}
-                                               span.name {{ m.display_name }}
-                                               | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
 
                        ul.diamond
                                | {% for m in memberships.Diamond %}
-                               li
-                                       a(href="{{ m.url }}", target="_blank")
-                                               | {% if m.logo %}
-                                               img(src="{{ m.logo.url }}" alt="{{ m.display_name }}")
-                                               | {% else %}
-                                               span.name {{ m.display_name }}
-                                               | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
 
                        ul.titanium
                                | {% for m in memberships.Titanium %}
-                               li
-                                       | {% if m.url %}
-                                       a(href="{{ m.url }}", target="_blank")
-                                               span.name {{ m.display_name }}
-                                       | {% else %}
-                                       span.name {{ m.display_name }}
-                                       | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
 
                        ul.platinum
                                | {% for m in memberships.Platinum %}
-                               li
-                                       | {% if m.url %}
-                                       a(href="{{ m.url }}", target="_blank")
-                                               span.name {{ m.display_name }}
-                                       | {% else %}
-                                       span.name {{ m.display_name }}
-                                       | {% endif %}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
                                h2.text-gradient Gold
                        ul.gold
                                | {% for m in memberships.Gold %}
-                               li
-                                       span.name {{ m.display_name }}
+                               li {% membership_credit m %}
                                | {% endfor %}
                | {% endif %}
 
diff --git a/websrc/templates/blender_fund/tags/membership_credit.pug b/websrc/templates/blender_fund/tags/membership_credit.pug
new file mode 100644 (file)
index 0000000..0cb3f0d
--- /dev/null
@@ -0,0 +1,13 @@
+//- Membership credit, as shown on the front page.
+| {% if membership.url %}
+a(href="{{ membership.url }}", target="_blank")
+  | {% if membership.logo %}
+  img(src="{{ membership.logo.url }}" alt="{{ membership.display_name }}")
+  | {% else %}
+  span.name {{ membership.display_name }}
+  | {% endif %}
+| {% elif membership.logo %}
+img(src="{{ membership.logo.url }}" alt="{{ membership.display_name }}")
+| {% else %}
+span.name {{ membership.display_name }}
+| {% endif %}