From 419a19c6f187406932bf6db907114c49971db679 Mon Sep 17 00:00:00 2001 From: Geert Kapteijns <ghkapteijns@gmail.com> Date: Sun, 15 Jan 2017 16:29:35 +0100 Subject: [PATCH] Extract ThesisLink#header_as_table to partial template Markup logic should be in templates, not in model layer. Partial template is named _header_as_table to signal that it is ment to be included into other templates (analogous to the private method naming convention.) The full text of several of the thesis link's fields (domain, subject_area, etc.) are fetched from constant dicts in custom filters defined in theses.templatetags.theses_extras. --- theses/models.py | 39 ---------------- theses/templates/theses/_header_as_table.html | 46 +++++++++++++++++++ theses/templates/theses/thesis_detail.html | 3 +- theses/templatetags/__init__.py | 0 theses/templatetags/theses_extras.py | 26 +++++++++++ 5 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 theses/templates/theses/_header_as_table.html create mode 100644 theses/templatetags/__init__.py create mode 100644 theses/templatetags/theses_extras.py diff --git a/theses/models.py b/theses/models.py index 50ec6b1f4..fbd5f370d 100644 --- a/theses/models.py +++ b/theses/models.py @@ -69,45 +69,6 @@ class ThesisLink(models.Model): def __str__(self): return self.title - def header_as_table(self): - context = Context({ - 'title': self.title, 'author': self.author, - 'pub_link': self.pub_link, 'institution': self.institution, - 'supervisor': self.supervisor, 'defense_date': self.defense_date}) - header = ( - '<table>' - '<tr><td>Title: </td><td> </td><td>{{ title }}</td></tr>' - '<tr><td>Author: </td><td> </td><td>{{ author }}</td></tr>' - '<tr><td>As Contributor: </td><td> </td>') - if self.author_as_cont.all(): - for auth in self.author_as_cont.all(): - header += ( - '<td><a href="/contributor/' + str(auth.id) + '">' + - auth.user.first_name + ' ' + auth.user.last_name + - '</a></td>') - else: - header += '<td>(not claimed)</td>' - header += ( - '</tr>' - '<tr><td>Type: </td><td></td><td>' + self.THESIS_TYPES_DICT[self.type] + - '</td></tr>' - '<tr><td>Discipline: </td><td></td><td>' + - disciplines_dict[self.discipline] + '</td></tr>' - '<tr><td>Domain: </td><td></td><td>' + - journals_domains_dict[self.domain] + '</td></tr>' - '<tr><td>Subject area: </td><td></td><td>' + - subject_areas_dict[self.subject_area] + '</td></tr>' - '<tr><td>URL: </td><td> </td><td><a href="{{ pub_link }}" ' - 'target="_blank">{{ pub_link }}</a></td></tr>' - '<tr><td>Degree granting institution: </td><td> </td>' - '<td>{{ institution }}</td></tr>' - '<tr><td>Supervisor(s): </td><td></td><td>{{ supervisor }}' - '</td></tr>' '<tr><td>Defense date: </td><td> </td>' - '<td>{{ defense_date }}</td></tr>' - '</table>') - template = Template(header) - return template.render(context) - def header_as_li(self): context = Context({ 'id': self.id, 'title': self.title, 'author': self.author, diff --git a/theses/templates/theses/_header_as_table.html b/theses/templates/theses/_header_as_table.html new file mode 100644 index 000000000..c6f6fb9ba --- /dev/null +++ b/theses/templates/theses/_header_as_table.html @@ -0,0 +1,46 @@ +{% load theses_extras %} + +<table> + <tr> + <td>Title: </td><td> </td><td>{{ title }}</td> + </tr> + <tr> + <td>Author: </td><td> </td><td>{{ author }}</td> + </tr> + <tr> + <td>As Contributor: </td><td> </td> + {% if thesislink.author_as_cont.all %} + {% for author in thesislink.author_as_cont.all %} + <td><a href= {% url 'scipost:contributor_info' author.id %}> + author.user.first_name author.user.last_name + </a></td> + {% endfor %} + {% else %} + <td>(not claimed)</td> + {% endif %} + </tr> + <tr> + <td>Type: </td><td></td><td> {{ thesislink|type }}</td> + </tr> + <tr> + <td>Discipline: </td><td></td><td>{{ thesislink|discipline }}</td> + </tr> + <tr> + <td>Domain: </td><td></td><td>{{ thesislink|domain }}</td> + </tr> + <tr> + <td>Subject area: </td><td></td><td> {{ thesislink|subject_area }} </td> + </tr> + <tr> + <td>URL: </td><td> </td><td><a href="{{ pub_link }}" target="_blank">{{ thesislink.pub_link }}</a></td> + </tr> + <tr> + <td>Degree granting institution: </td><td> </td><td>{{ thesislink.institution }}</td> + </tr> + <tr> + <td>Supervisor(s): </td><td></td><td>{{ thesislink.supervisor }}</td> + </tr> + <tr> + <td>Defense date: </td><td> </td><td>{{ thesislink.defense_date }}</td> + </tr> +</table> diff --git a/theses/templates/theses/thesis_detail.html b/theses/templates/theses/thesis_detail.html index 541f934e1..45b16160b 100644 --- a/theses/templates/theses/thesis_detail.html +++ b/theses/templates/theses/thesis_detail.html @@ -44,7 +44,8 @@ <h2>Thesis information: </h2> </div> </div> - {{ thesislink.header_as_table }} + {% include "./_header_as_table.html" with thesislink=thesislink %} + {# {{ thesislink.header_as_table }}#} <h3>Abstract:</h3> <p>{{ thesislink.abstract }}</p> diff --git a/theses/templatetags/__init__.py b/theses/templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/theses/templatetags/theses_extras.py b/theses/templatetags/theses_extras.py new file mode 100644 index 000000000..f3b441f41 --- /dev/null +++ b/theses/templatetags/theses_extras.py @@ -0,0 +1,26 @@ +from django import template + +from scipost.constants import SCIPOST_DISCIPLINES, subject_areas_dict, disciplines_dict +from journals.models import journals_domains_dict + +register = template.Library() + + +@register.filter +def type(thesislink): + return thesislink.THESIS_TYPES_DICT[thesislink.type] + + +@register.filter +def discipline(thesislink): + return disciplines_dict[thesislink.discipline] + + +@register.filter +def domain(thesislink): + return journals_domains_dict[thesislink.domain] + + +@register.filter +def subject_area(thesislink): + return subject_areas_dict[thesislink.subject_area] -- GitLab