Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Refactor ahead of package.json discovery #4519

Merged
merged 1 commit into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions gratipay/utils/listings.py
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions gratipay/utils/tabs.py
Original file line number Diff line number Diff line change
@@ -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())
4 changes: 1 addition & 3 deletions scss/elements/buttons-knobs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -81,5 +81,3 @@ button.selected:hover:not(:disabled), button.selected.drag,
left: 5%;
}
}


11 changes: 11 additions & 0 deletions templates/nav-tabs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% macro nav_tabs(tabs) %}
<ul class="nav">
{% for tab in tabs %}
<li>
<a href="{{ tab.link }}"{% if tab.is_selected %} class="selected"{% endif %}>
<span class="textwrap">{{ tab.html|safe }}</span>
</a>
</li>
{% endfor %}
</ul>
{% endmacro %}
44 changes: 13 additions & 31 deletions www/index.html.spt
Original file line number Diff line number Diff line change
@@ -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

[---]
Expand All @@ -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 = {
Expand Down Expand Up @@ -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'] = '<span class="status-icon {}">{}</span> {}'.format( status
, icons.STATUS_ICONS[status]
, tab['title']
)
return '<span class="status-icon {}">{}</span> {}'.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' %}
Expand All @@ -104,16 +95,7 @@ page_id = "homepage"
<button type="submit">{{ _("Fund Your Project") }}</button>
</form>

<ul class="nav">
{% for tab in tabs %}
<li>
<a href="{{tabs[tab]['link']}}"
class="{{ 'selected' if tabs[tab]['is_selected'] }}">
{{ tabs[tab]['html']|safe }}
</a>
</li>
{% endfor %}
</ul>
{{ nav_tabs(tabs) }}

<table class="listing">
{% set project_list = projects %}
Expand Down
26 changes: 3 additions & 23 deletions www/search.spt
Original file line number Diff line number Diff line change
@@ -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
[---]
Expand All @@ -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")
Expand Down Expand Up @@ -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 %}
<h2>{{ ngettext("Found a matching project",
Expand Down Expand Up @@ -247,7 +227,7 @@ zip = zip
{% endif %}

{% if query and not (usernames or statements or emails or projects) %}
<p class="note">{{ _("Sorry, we didn't find anything matching your query.") }}</p>
<p class="sorry">{{ _("Sorry, we didn't find anything matching your query.") }}</p>
{% endif %}

{% endblock %}
Expand Down