From 840b6399750c1d93318412607990a659584dd4dc Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Thu, 15 Jun 2017 15:40:49 -0400 Subject: [PATCH] Refactor ahead of package.json discovery - listings helper for combined project/package situations - tabs helper - minor formatting tweaks --- gratipay/utils/listings.py | 25 ++++++++++++++++++ gratipay/utils/tabs.py | 18 +++++++++++++ scss/elements/buttons-knobs.scss | 4 +-- templates/nav-tabs.html | 11 ++++++++ www/index.html.spt | 44 ++++++++++---------------------- www/search.spt | 26 +++---------------- 6 files changed, 71 insertions(+), 57 deletions(-) create mode 100644 gratipay/utils/listings.py create mode 100644 gratipay/utils/tabs.py create mode 100644 templates/nav-tabs.html diff --git a/gratipay/utils/listings.py b/gratipay/utils/listings.py new file mode 100644 index 0000000000..76879cf466 --- /dev/null +++ b/gratipay/utils/listings.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + + +class FakeProject(object): + + def __init__(self, website, package): + self.website = website + self.package = package + self.name = package.name + self.url_path = '/on/{}/{}/'.format(package.package_manager, package.name) + + def get_image_url(self, size): + assert size in ('large', 'small'), size + return self.website.asset('package-default-{}.png'.format(size)) + + +def with_unclaimed_packages_wrapped(website, projects_and_unclaimed_packages): + out = [] + for project, unclaimed_package in projects_and_unclaimed_packages: + if unclaimed_package: + assert project is None + project = FakeProject(website, unclaimed_package) + out.append(project) + return out diff --git a/gratipay/utils/tabs.py b/gratipay/utils/tabs.py new file mode 100644 index 0000000000..c6e135c8c3 --- /dev/null +++ b/gratipay/utils/tabs.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +from collections import OrderedDict + + +def make(htmlfunc, var, current, *names): + """Helper to spit out a struct for rendering tabs (see templates/nav-tabs.html). + """ + tabs = OrderedDict() + tabs[names[0]] = {} + for name in names[1:]: + tabs[name] = {var: name} + for name, tab in tabs.iteritems(): + tab['link'] = '?{}={}'.format(var, tab[var]) if var in tab else '.' + tab['is_selected'] = (tab.get(var) == current) + tab['html'] = htmlfunc(name, tab) + return list(tabs.values()) diff --git a/scss/elements/buttons-knobs.scss b/scss/elements/buttons-knobs.scss index d31c63f8a8..843fd1ea78 100644 --- a/scss/elements/buttons-knobs.scss +++ b/scss/elements/buttons-knobs.scss @@ -58,7 +58,7 @@ button.selected:hover:not(:disabled), button.selected.drag, .important-button { - margin: 38px auto 30px; + margin: 30px auto 30px; text-align: center; button.large { @@ -81,5 +81,3 @@ button.selected:hover:not(:disabled), button.selected.drag, left: 5%; } } - - diff --git a/templates/nav-tabs.html b/templates/nav-tabs.html new file mode 100644 index 0000000000..127e6b2b20 --- /dev/null +++ b/templates/nav-tabs.html @@ -0,0 +1,11 @@ +{% macro nav_tabs(tabs) %} + +{% endmacro %} diff --git a/www/index.html.spt b/www/index.html.spt index c366442b89..fab7b8abfe 100644 --- a/www/index.html.spt +++ b/www/index.html.spt @@ -1,5 +1,5 @@ from collections import OrderedDict -from gratipay.utils import get_featured_projects, icons +from gratipay.utils import get_featured_projects, icons, tabs import random [---] @@ -17,9 +17,9 @@ status_filter_active = (status_filter and status_filter in ['unreviewed', 'rejec page = request.qs.get('page', 1) try: - page = int(page) + page = int(page) except ValueError: - page = 1 + page = 1 if status_filter_active: status_value = { @@ -54,30 +54,21 @@ if status_filter_active: else: projects = get_featured_projects(website.db) - -tabs = OrderedDict() -tabs['featured'] = {} -tabs['approved'] = {'status_filter': 'approved'} -tabs['unreviewed'] = {'status_filter': 'unreviewed'} -tabs['rejected'] = {'status_filter': 'rejected'} - -for key, tab in tabs.iteritems(): - if tab.get('status_filter'): - tab['link'] = '?status=%s' % tab['status_filter'] - else: - tab['link'] = '.' - tab['is_selected'] = (tab.get('status_filter') == status_filter) - tab['title'] = i18ned_statuses[key] +def tab_html(key, tab): status = icons.REVIEW_MAP[key] - tab['html'] = '{} {}'.format( status - , icons.STATUS_ICONS[status] - , tab['title'] - ) + return '{} {}'.format( status + , icons.STATUS_ICONS[status] + , i18ned_statuses[key] + ) + +tabs = tabs.make(tab_html, 'status', status_filter, + 'featured', 'approved', 'unreviewed', 'rejected') title = _("Projects") suppress_sidebar = True page_id = "homepage" [---] +{% from "templates/nav-tabs.html" import nav_tabs with context %} {% extends "templates/base.html" %} {% block head_early %} {% if website.optimizely_id and request.headers.get('DNT') != '1' %} @@ -104,16 +95,7 @@ page_id = "homepage" - +{{ nav_tabs(tabs) }} {% set project_list = projects %} diff --git a/www/search.spt b/www/search.spt index 24d825ea27..15759f59f2 100644 --- a/www/search.spt +++ b/www/search.spt @@ -1,4 +1,4 @@ -from gratipay.utils import markdown, truncate, icons +from gratipay.utils import markdown, truncate, icons, listings from gratipay.utils.i18n import LANGUAGES_2, SEARCH_CONFS, strip_accents from markupsafe import Markup [---] @@ -17,26 +17,6 @@ def get_processed_excerpt(participant): statement = truncate(statement, 128, '') return process_excerpt(statement) -class FakeProject(object): - - def __init__(self, package): - self.package = package - self.name = package.name - self.url_path = '/on/{}/{}/'.format(package.package_manager, package.name) - - def get_image_url(self, size): - assert size in ('large', 'small'), size - return website.asset('package-default-{}.png'.format(size)) - -def with_unclaimed_packages_wrapped(projects_and_unclaimed_packages): - out = [] - for project, unclaimed_package in projects_and_unclaimed_packages: - if unclaimed_package: - assert project is None - project = FakeProject(unclaimed_package) - out.append(project) - return out - # Can't factor this out because of translations? i18ned_statuses = { "approved": _("Approved") , "unreviewed" : _("Unreviewed") @@ -213,7 +193,7 @@ zip = zip {% set usernames = zip(results.get('usernames', []), empty_excerpts()) %} {% set statements = results.get('statements') %} {% set emails = zip(results.get('emails', []), empty_excerpts()) %} - {% set projects = with_unclaimed_packages_wrapped(results.get('projects', [])) %} + {% set projects = listings.with_unclaimed_packages_wrapped(website, results.get('projects', [])) %} {% if projects %}

{{ ngettext("Found a matching project", @@ -247,7 +227,7 @@ zip = zip {% endif %} {% if query and not (usernames or statements or emails or projects) %} -

{{ _("Sorry, we didn't find anything matching your query.") }}

+

{{ _("Sorry, we didn't find anything matching your query.") }}

{% endif %} {% endblock %}