summaryrefslogtreecommitdiff
path: root/main/shared.py
diff options
context:
space:
mode:
Diffstat (limited to 'main/shared.py')
-rw-r--r--main/shared.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/main/shared.py b/main/shared.py
new file mode 100644
index 0000000..7e5ee63
--- /dev/null
+++ b/main/shared.py
@@ -0,0 +1,64 @@
+# coding: utf8
+
+'''
+Copyright (C) 2008 Guillaume Seguin <guillaume@segu.in>
+
+This application is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as
+published by the Free Software Foundation, either version 3 of the
+License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Affero General Public License for more details.
+
+You should have received a copy of the GNU Affero General Public License
+along with this application. If not, see <http://www.gnu.org/licenses/>.
+'''
+
+from django.template import RequestContext, loader
+from django.http import HttpResponse, HttpResponseRedirect
+from django import forms
+from django.contrib.sites.models import Site
+
+from dusu_website.main.models import *
+
+class LoginForm (forms.Form):
+
+ def __init__ (self, *args, **kwargs):
+ super (LoginForm, self).__init__ (*args, **kwargs)
+ username = forms.CharField (label = "Identifiant", required = True)
+ password = forms.CharField (label = "Mot de passe", required = True,
+ widget = forms.PasswordInput)
+ self.fields["username"] = username
+ self.fields["password"] = password
+
+def context_processor (request):
+ '''Append extra data to the context of the given request'''
+ data = {}
+ data["site"] = Site.objects.get_current ()
+ if not request.user.is_authenticated ():
+ data["login_form"] = LoginForm ()
+ else:
+ data["pages"] = Page.objects.order_by ("position").all ()
+ return data
+
+def render_template (request, data, template = "layout.html"):
+ '''Helper rendering function'''
+ template = loader.get_template (template)
+ context = RequestContext (request, data)
+ return template.render (context)
+
+def render_page (request, data, template = "layout.html"):
+ '''Helper rendering function'''
+ return HttpResponse (render_template (request, data, template))
+
+def redirect (request, to, message, delay = 2000):
+ '''Display a redirect page'''
+ data = {
+ "redirect_message" : message,
+ "redirect_to" : to,
+ "redirect_delay": delay,
+ }
+ return render_page (request, data, template = "redirect.html")