Skip to content
Permalink
Browse files
Fix #7 -- add per-language pages
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Oct 10, 2015
1 parent 9c92ae1 commit da53efc66f7c37d8489df45c61e784774146bb04
Showing with 71 additions and 8 deletions.
  1. +3 −0 sites/urls.py
  2. +52 −8 sites/views.py
  3. +2 −0 templates/index.html
  4. +14 −0 templates/languages.html
@@ -8,4 +8,7 @@
url(r'^remove/?$', views.remove, name='remove'),
url(r'^check/?$', views.check, name='check'),
url(r'^tos/?$', views.tos, name='tos'),
url(r'^lang/?$', views.langlist, name='langlist'),
url(r'^lang/(?P<language_code>[a-z]{2,5})/?$', views.lang, name='lang'),
url(r'^lang/(?P<language_code>[a-z]{2,5})_(?P<country_code>[A-Z]{2,5})/?$', views.lang, name='lang'),
]
@@ -1,12 +1,15 @@
import itertools
from django.shortcuts import render
from django.core.mail import send_mail
from django.utils.html import format_html
from django.http import HttpResponseNotFound
from .models import Site, Language
from .checker import gencheck, CHECK_ERROR_NODATA
from .forms import AddForm, CheckForm

MENU = (
('/', 'Users Home', 'home'),
('/lang/', 'Languages', 'lang'),
('/add/', 'Add', 'add'),
('/edit/', 'Edit', 'edit'),
('/remove/', 'Remove', 'remove'),
@@ -15,25 +18,66 @@
)


def index(request):
"""Index view."""
featured = Site.objects.filter(featured=True).order_by('featured_order')
other = Site.objects.filter(featured=False).order_by('?') # random order
count = Site.objects.count()
def generic_index(request, title='{0} Sites', page='home', objects=None):
"""Generic index view."""
featured = objects.filter(featured=True).order_by('featured_order')
other = objects.filter(featured=False).order_by('?') # random order
count = objects.count()

all_sites = itertools.chain(featured, other)

context = {
'sites': all_sites,
'count': count,
'title': '{0} Sites'.format(count),
'title': title.format(count),
'menu': MENU,
'page': 'home'
'page': page
}

return render(request, 'index.html', context)


def index(request):
"""Language index view."""
objects = Site.objects
return generic_index(request, objects=objects)


def lang(request, **filters):
l = Language.objects.filter(**filters)
if not l:
return HttpResponseNotFound()
elif len(l) == 1:
lnames = str(l[0])
objects = l[0].site_set
else:
lnames = ', '.join(str(_) for _ in l)
objects = Site.objects.filter(languages__language_code=filters['language_code'])
return generic_index(request, title="{{0}} Sites in {0}".format(lnames), page='lang', objects=objects)


def langlist(request):
group_members = list(Language.objects.filter(display_country=True))
groups = {}
for l in group_members:
if l.language_code in groups:
groups[l.language_code].append(l)
else:
groups[l.language_code] = [l]

groups_s = {}
for lc, ll in groups.items():
groups_s[lc] = format_html('<strong>{0}</strong>: {1}', lc, ', '.join(str(i) for i in ll))

context = {
'languages': Language.objects.order_by('name'),
'language_groups': groups_s,
'title': 'Languages',
'menu': MENU,
'page': 'lang'
}
return render(request, 'languages.html', context)


def add(request):
"""Add view."""
context = {
@@ -1,5 +1,6 @@
{% extends "base.html" %}
{% block content %}
{% if page == 'index' %}
<p>
Below is a selection of the many websites made by users of Nikola. Visit a
few sites and see some of what is possible to achieve with Nikola.
@@ -10,6 +11,7 @@
<p class="text-muted"><strong>Notice:</strong>
Many sites are not
fully described yet. We apologize for the inconvenience.</p>
{% endif %}

{% if count == 0 %}
<p class="text-error">No pages found…</p>
@@ -0,0 +1,14 @@
{% extends "base.html" %}
{% block content %}
<ul>
{% for l in languages %}
<li><a href="/lang/{{ l.code }}/">{{ l }}</a></li>
{% endfor %}
</ul>
<h2>Language groups</h2>
<ul>
{% for lc, lg in language_groups.items %}
<li><a href="/lang/{{ lc }}/">{{ lg }}</a></li>
{% endfor %}
</ul>
{% endblock %}

0 comments on commit da53efc

Please sign in to comment.