From a82a83576f8ad2c88a6f56f9b593c03c160b8e64 Mon Sep 17 00:00:00 2001
From: fonfon
Date: Thu, 10 Jul 2014 03:44:52 +0000
Subject: [PATCH] use djangos named URLs and url-reverse instead of manually
constructing urls
---
menu.py | 3 +--
module_loader.py | 6 +++---
modules/apps/urls.py | 2 +-
modules/config/urls.py | 2 +-
modules/diagnostics/templates/diagnostics.html | 6 +++---
modules/diagnostics/urls.py | 4 ++--
modules/expert_mode/urls.py | 2 +-
modules/firewall/urls.py | 2 +-
modules/first_boot/first_boot.py | 6 +++---
.../first_boot/templates/firstboot_state1.html | 2 +-
modules/first_boot/urls.py | 6 +++---
modules/help/help.py | 18 +++++++++++-------
modules/help/templates/about.html | 3 ++-
modules/help/templates/help.html | 4 ++--
modules/help/urls.py | 15 +++++++--------
modules/lib/urls.py | 5 ++---
modules/owncloud/templates/owncloud.html | 2 +-
modules/owncloud/urls.py | 2 +-
modules/packages/urls.py | 2 +-
.../templates/pagekite_introduction.html | 2 +-
modules/pagekite/urls.py | 4 ++--
modules/system/urls.py | 2 +-
modules/tor/urls.py | 2 +-
modules/users/urls.py | 6 +++---
modules/xmpp/urls.py | 6 +++---
plinth.py | 1 +
templates/base.html | 10 +++++-----
urls.py | 2 +-
util.py | 18 +-----------------
views.py | 12 +++++++-----
30 files changed, 73 insertions(+), 84 deletions(-)
diff --git a/menu.py b/menu.py
index 4522768a3..bcda4fd73 100644
--- a/menu.py
+++ b/menu.py
@@ -1,4 +1,3 @@
-from urlparse import urlparse
import util
import cfg
@@ -70,7 +69,7 @@ class Menu(object):
return request_path.startswith(self.url)
def active_item(self, request):
- """Return item list (e.g. submenu) of active menu item."""
+ """Return the first active item that is found"""
for item in self.items:
if request.path.startswith(item.url):
return item
diff --git a/module_loader.py b/module_loader.py
index cf990394b..ad5214776 100644
--- a/module_loader.py
+++ b/module_loader.py
@@ -48,7 +48,7 @@ def load_modules():
LOGGER.exception('Could not import modules/%s: %s',
name, exception)
- _include_module_urls(full_name)
+ _include_module_urls(full_name, name)
ordered_modules = []
remaining_modules = dict(modules)
@@ -97,13 +97,13 @@ def _insert_modules(module_name, module, remaining_modules, ordered_modules):
ordered_modules.append(module_name)
-def _include_module_urls(module_name):
+def _include_module_urls(module_name, namespace):
"""Include the module's URLs in global project URLs list"""
url_module = module_name + '.urls'
try:
urls.urlpatterns += django.conf.urls.patterns(
'', django.conf.urls.url(
- r'', django.conf.urls.include(url_module)))
+ r'', django.conf.urls.include(url_module, namespace)))
except ImportError:
LOGGER.debug('No URLs for %s', module_name)
diff --git a/modules/apps/urls.py b/modules/apps/urls.py
index a65383f51..b47f44cde 100644
--- a/modules/apps/urls.py
+++ b/modules/apps/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.apps.apps',
- url(r'^apps/$', 'index')
+ url(r'^apps/$', 'index', name='index')
)
diff --git a/modules/config/urls.py b/modules/config/urls.py
index e40bdf15a..e80f518dc 100644
--- a/modules/config/urls.py
+++ b/modules/config/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.config.config',
- url(r'^sys/config/$', 'index'),
+ url(r'^sys/config/$', 'index', name='index'),
)
diff --git a/modules/diagnostics/templates/diagnostics.html b/modules/diagnostics/templates/diagnostics.html
index f104ef2f8..c85203f08 100644
--- a/modules/diagnostics/templates/diagnostics.html
+++ b/modules/diagnostics/templates/diagnostics.html
@@ -24,8 +24,8 @@
system to confirm that network services are running and configured
properly. It may take a minute to complete.
-Run diagnostic test
-»
+
+ Run diagnostic test »
+
{% endblock %}
diff --git a/modules/diagnostics/urls.py b/modules/diagnostics/urls.py
index 5b3d94653..4ed974f9b 100644
--- a/modules/diagnostics/urls.py
+++ b/modules/diagnostics/urls.py
@@ -24,6 +24,6 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.diagnostics.diagnostics',
- url(r'^sys/diagnostics/$', 'index'),
- url(r'^sys/diagnostics/test/$', 'test'),
+ url(r'^sys/diagnostics/$', 'index', name='index'),
+ url(r'^sys/diagnostics/test/$', 'test', name='test'),
)
diff --git a/modules/expert_mode/urls.py b/modules/expert_mode/urls.py
index 83ed46428..ffb97da53 100644
--- a/modules/expert_mode/urls.py
+++ b/modules/expert_mode/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.expert_mode.expert_mode',
- url(r'^sys/expert/$', 'index'),
+ url(r'^sys/expert/$', 'index', name='index'),
)
diff --git a/modules/firewall/urls.py b/modules/firewall/urls.py
index 62ae413e7..be2f1494a 100644
--- a/modules/firewall/urls.py
+++ b/modules/firewall/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.firewall.firewall',
- url(r'^sys/firewall/$', 'index')
+ url(r'^sys/firewall/$', 'index', name='index')
)
diff --git a/modules/first_boot/first_boot.py b/modules/first_boot/first_boot.py
index c9ecf5ff9..f784c507b 100644
--- a/modules/first_boot/first_boot.py
+++ b/modules/first_boot/first_boot.py
@@ -21,6 +21,7 @@ The Plinth first-connection process has several stages:
from django import forms
from django.contrib import messages
from django.core import validators
+from django.core.urlresolvers import reverse
from django.http.response import HttpResponseRedirect
from django.template.response import TemplateResponse
from gettext import gettext as _
@@ -94,7 +95,7 @@ def state0(request):
"""
try:
if _read_state() >= 5:
- return HttpResponseRedirect(cfg.server_dir)
+ return HttpResponseRedirect(reverse('index'))
except KeyError:
pass
@@ -112,8 +113,7 @@ def state0(request):
if success:
# Everything is good, permanently mark and move to page 2
_write_state(1)
- return HttpResponseRedirect(
- cfg.server_dir + '/firstboot/state1')
+ return HttpResponseRedirect(reverse('first_boot:state1'))
else:
form = State0Form(initial=status, prefix='firstboot')
diff --git a/modules/first_boot/templates/firstboot_state1.html b/modules/first_boot/templates/firstboot_state1.html
index 7a40fcf1e..fdc675f39 100644
--- a/modules/first_boot/templates/firstboot_state1.html
+++ b/modules/first_boot/templates/firstboot_state1.html
@@ -23,7 +23,7 @@
{% block main_block %}
Welcome screen not completely implemented yet. Press continue to see the rest of the
+ href="{% url 'apps:index' %}">continue to see the rest of the
web interface.
diff --git a/modules/first_boot/urls.py b/modules/first_boot/urls.py
index 6fa4aadc4..68669e6e9 100644
--- a/modules/first_boot/urls.py
+++ b/modules/first_boot/urls.py
@@ -24,7 +24,7 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.first_boot.first_boot',
- url(r'^firstboot/$', 'index'),
- url(r'^firstboot/state0/$', 'state0'),
- url(r'^firstboot/state1/$', 'state1')
+ url(r'^firstboot/$', 'index', name='index'),
+ url(r'^firstboot/state0/$', 'state0', name='state0'),
+ url(r'^firstboot/state1/$', 'state1', name='state1')
)
diff --git a/modules/help/help.py b/modules/help/help.py
index ce72c6d84..30ca646d4 100644
--- a/modules/help/help.py
+++ b/modules/help/help.py
@@ -1,5 +1,6 @@
import os
from gettext import gettext as _
+from django.http import Http404
from django.template.response import TemplateResponse
import cfg
@@ -9,10 +10,10 @@ def init():
"""Initialize the Help module"""
menu = cfg.main_menu.add_item(_('Documentation'), 'icon-book', '/help',
101)
- menu.add_item(_("Where to Get Help"), "icon-search", "/help/index", 5)
+ menu.add_item(_("Where to Get Help"), "icon-search", "/help/index/", 5)
menu.add_item(_('Developer\'s Manual'), 'icon-info-sign',
- '/help/view/plinth', 10)
- menu.add_item(_('FAQ'), 'icon-question-sign', '/help/view/faq', 20)
+ '/help/page/plinth', 10)
+ menu.add_item(_('FAQ'), 'icon-question-sign', '/help/page/faq', 20)
menu.add_item(_('%s Wiki' % cfg.box_name), 'icon-pencil',
'http://wiki.debian.org/FreedomBox', 30)
menu.add_item(_('About'), 'icon-star', '/help/about', 100)
@@ -30,10 +31,13 @@ def about(request):
return TemplateResponse(request, 'about.html', {'title': title})
-def default(request, page=''):
- """Serve the documentation pages"""
- with open(os.path.join('doc', '%s.part.html' % page), 'r') as input_file:
- main = input_file.read()
+def helppage(request, page):
+ """Serve a help page from the 'doc' directory"""
+ try:
+ input_file = open(os.path.join('doc', '%s.part.html' % page), 'r')
+ except IOError:
+ raise Http404
+ main = input_file.read()
title = _('%s Documentation') % cfg.product_name
return TemplateResponse(request, 'base.html',
diff --git a/modules/help/templates/about.html b/modules/help/templates/about.html
index 740afa925..973751fcf 100644
--- a/modules/help/templates/about.html
+++ b/modules/help/templates/about.html
@@ -1,4 +1,5 @@
{% extends 'base.html' %}
+{% load static %}
{% comment %}
#
# This file is part of Plinth.
@@ -20,7 +21,7 @@
{% block main_block %}
-
We live in a world where our use of the network is mediated by
diff --git a/modules/help/templates/help.html b/modules/help/templates/help.html
index 6fd191d75..6269ecfa7 100644
--- a/modules/help/templates/help.html
+++ b/modules/help/templates/help.html
@@ -23,7 +23,7 @@
There are a variety of places to go for help with
{{ cfg.product_name }} and the box it runs on.
-This front end has a
+This front end has a
developer's manual. It isn't complete, but it is the first place
to look. Feel free to offer suggestions, edits, and screenshots for
completing it!
@@ -39,7 +39,7 @@ about the {{ cfg.box_name }} and almost surely know nothing of this
front end, but they are an incredible resource for general Debian
issues.
-There is no FAQ because
+
There is no FAQ because
the question frequency is currently zero for all questions.
{% endblock %}
diff --git a/modules/help/urls.py b/modules/help/urls.py
index 3c623ebcc..6d61537f2 100644
--- a/modules/help/urls.py
+++ b/modules/help/urls.py
@@ -20,15 +20,14 @@ URLs for the Help module
"""
from django.conf.urls import patterns, url
+from django.core.urlresolvers import reverse_lazy
+from django.views.generic import RedirectView
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.help.help',
- url(r'^help/$', 'index'),
- url(r'^help/index/$', 'index'),
- url(r'^help/about/$', 'about'),
- url(r'^help/view/(?Pdesign)/$', 'default'),
- url(r'^help/view/(?Pplinth)/$', 'default'),
- url(r'^help/view/(?Phacking)/$', 'default'),
- url(r'^help/view/(?Pfaq)/$', 'default'),
- )
+ url(r'^help/$', RedirectView.as_view(url=reverse_lazy('help:index'))),
+ url(r'^help/index/$', 'index', name='index'),
+ url(r'^help/about/$', 'about', name='about'),
+ url(r'^help/page/([\w]+)/$', 'helppage', name='helppage'),
+)
diff --git a/modules/lib/urls.py b/modules/lib/urls.py
index 20f497f35..2414b705a 100644
--- a/modules/lib/urls.py
+++ b/modules/lib/urls.py
@@ -23,11 +23,10 @@ from django.conf.urls import patterns, url
import cfg
-
urlpatterns = patterns( # pylint: disable-msg=C0103
'',
url(r'^accounts/login/$', 'django.contrib.auth.views.login',
- {'template_name': 'login.html'}),
+ {'template_name': 'login.html'}, name='login'),
url(r'^accounts/logout/$', 'django.contrib.auth.views.logout',
- {'next_page': cfg.server_dir})
+ {'next_page': cfg.server_dir}, name='logout')
)
diff --git a/modules/owncloud/templates/owncloud.html b/modules/owncloud/templates/owncloud.html
index 5dd7c618c..dad44f08e 100644
--- a/modules/owncloud/templates/owncloud.html
+++ b/modules/owncloud/templates/owncloud.html
@@ -28,7 +28,7 @@
{{ form|bootstrap }}
When enabled, the owncloud installation will be available
- from owncloud on the web server. Visit
+ from owncloud on the web server. Visit
this URL to set up the initial administration account for
owncloud.
diff --git a/modules/owncloud/urls.py b/modules/owncloud/urls.py
index 5ad666505..faa23d43b 100644
--- a/modules/owncloud/urls.py
+++ b/modules/owncloud/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.owncloud.owncloud',
- url(r'^apps/owncloud/$', 'index'),
+ url(r'^apps/owncloud/$', 'index', name='index'),
)
diff --git a/modules/packages/urls.py b/modules/packages/urls.py
index ae963fb4e..60faf35bf 100644
--- a/modules/packages/urls.py
+++ b/modules/packages/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.packages.packages',
- url(r'^sys/packages/$', 'index'),
+ url(r'^sys/packages/$', 'index', name='index'),
)
diff --git a/modules/pagekite/templates/pagekite_introduction.html b/modules/pagekite/templates/pagekite_introduction.html
index 21668bdea..d1b675aea 100644
--- a/modules/pagekite/templates/pagekite_introduction.html
+++ b/modules/pagekite/templates/pagekite_introduction.html
@@ -49,7 +49,7 @@ there. In future, it might be possible to use your buddy's
Configure
+ href="{% url 'pagekite:configure' %}">Configure
PageKite
diff --git a/modules/pagekite/urls.py b/modules/pagekite/urls.py
index 8cbad4dd5..3db1d2f98 100644
--- a/modules/pagekite/urls.py
+++ b/modules/pagekite/urls.py
@@ -24,6 +24,6 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.pagekite.pagekite',
- url(r'^apps/pagekite/$', 'index'),
- url(r'^apps/pagekite/configure/$', 'configure'),
+ url(r'^apps/pagekite/$', 'index', name='index'),
+ url(r'^apps/pagekite/configure/$', 'configure', name='configure'),
)
diff --git a/modules/system/urls.py b/modules/system/urls.py
index 054f9afcc..5982ce850 100644
--- a/modules/system/urls.py
+++ b/modules/system/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.system.system',
- url(r'^sys/$', 'index'),
+ url(r'^sys/$', 'index', name='index'),
)
diff --git a/modules/tor/urls.py b/modules/tor/urls.py
index 95e0422fc..4c13cb138 100644
--- a/modules/tor/urls.py
+++ b/modules/tor/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.tor.tor',
- url(r'^apps/tor/$', 'index')
+ url(r'^apps/tor/$', 'index', name='index')
)
diff --git a/modules/users/urls.py b/modules/users/urls.py
index 8fb5d21cd..0c46aa72e 100644
--- a/modules/users/urls.py
+++ b/modules/users/urls.py
@@ -24,7 +24,7 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.users.users',
- url(r'^sys/users/$', 'index'),
- url(r'^sys/users/add/$', 'add'),
- url(r'^sys/users/edit/$', 'edit')
+ url(r'^sys/users/$', 'index', name='index'),
+ url(r'^sys/users/add/$', 'add', name='add'),
+ url(r'^sys/users/edit/$', 'edit', name='edit')
)
diff --git a/modules/xmpp/urls.py b/modules/xmpp/urls.py
index 43a518c87..050026d81 100644
--- a/modules/xmpp/urls.py
+++ b/modules/xmpp/urls.py
@@ -24,7 +24,7 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'modules.xmpp.xmpp',
- url(r'^apps/xmpp/$', 'index'),
- url(r'^apps/xmpp/configure/$', 'configure'),
- url(r'^apps/xmpp/register/$', 'register')
+ url(r'^apps/xmpp/$', 'index', name='index'),
+ url(r'^apps/xmpp/configure/$', 'configure', name='configure'),
+ url(r'^apps/xmpp/register/$', 'register', name='register')
)
diff --git a/plinth.py b/plinth.py
index b689f0e3b..eb4ca1edc 100755
--- a/plinth.py
+++ b/plinth.py
@@ -35,6 +35,7 @@ def parse_arguments():
parser.add_argument(
'--pidfile', default='plinth.pid',
help='specify a file in which the server may write its pid')
+ # TODO: server_dir is actually a url prefix; use a better variable name
parser.add_argument(
'--server_dir', default='/',
help='web server path under which to serve')
diff --git a/templates/base.html b/templates/base.html
index 0d6ea93ee..5848f7bd0 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -59,11 +59,11 @@
-
+
- FreedomBox
+ FreedomBox
{% block add_nav_and_login %}
@@ -85,15 +85,15 @@
{% if user.is_authenticated %}
- Logged in as {{ user.username }}.
-
+ Logged in as {{ user.username }}.
+
Log out.
{% else %}
Not logged in.
-
+
Log in.
{% endif %}
diff --git a/urls.py b/urls.py
index a2611e009..73837133b 100644
--- a/urls.py
+++ b/urls.py
@@ -24,5 +24,5 @@ from django.conf.urls import patterns, url
urlpatterns = patterns( # pylint: disable-msg=C0103
'views',
- url(r'^$', 'index')
+ url(r'^$', 'index', name='index')
)
diff --git a/util.py b/util.py
index 0f382bf94..3bb0499ce 100644
--- a/util.py
+++ b/util.py
@@ -1,20 +1,4 @@
import os
-from django.http.response import HttpResponseRedirect
-import cfg
-
-
-class PlinthRedirect(HttpResponseRedirect):
- """
- We do not fully use django and thus cannot use its named URLs to construct
- links/redirects, so we have to take care of cfg.server_dir manually.
- This temporary helper class makes sure that plinth-internal redirects
- have the correct server_dir prefix.
- """
- def __init__(self, redirect_to, *args, **kwargs):
- if not redirect_to.startswith(cfg.server_dir):
- redirect_to = rel_urljoin([cfg.server_dir, redirect_to])
- return super(PlinthRedirect, self).__init__(redirect_to,
- *args, **kwargs)
def rel_urljoin(parts, prepend_slash=True):
@@ -22,7 +6,7 @@ def rel_urljoin(parts, prepend_slash=True):
urllibs' urljoin joins ("foo", "/bar") to "/bar".
Instead concatenate the parts with "/" to i.e. /foo/bar
"""
- url = '/'.join(s.strip('/') for s in parts)
+ url = '/'.join(s.strip('/') for s in parts)
if prepend_slash and not url.startswith('/'):
url = '/' + url
return url
diff --git a/views.py b/views.py
index 0c4cef271..055fa5838 100644
--- a/views.py
+++ b/views.py
@@ -19,11 +19,12 @@
Main Plinth views
"""
-from util import PlinthRedirect
import logging
import cfg
from withsqlite.withsqlite import sqlite_db
+from django.http.response import HttpResponseRedirect
+from django.core.urlresolvers import reverse
LOGGER = logging.getLogger(__name__)
@@ -37,13 +38,14 @@ def index(request):
# Permanent redirect causes the browser to cache the redirect,
# preventing the user from navigating to /plinth until the
# browser is restarted.
- return PlinthRedirect('/firstboot')
+ return HttpResponseRedirect(reverse('first_boot:index'))
if database['state'] < 5:
LOGGER.info('First boot state - %d', database['state'])
- return PlinthRedirect('/firstboot/state%d' % database['state'])
+ return HttpResponseRedirect(reverse('first_boot:state%d' %
+ database['state']))
if request.user.is_authenticated():
- return PlinthRedirect('/apps')
+ return HttpResponseRedirect(reverse('apps:index'))
- return PlinthRedirect('/help/about')
+ return HttpResponseRedirect(reverse('apps:about'))