summaryrefslogtreecommitdiff
path: root/main/shared.py
blob: 7e5ee63b24b5f12f4fd356812b85b6bc986ae85b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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")