views.py 3.86 KB
Newer Older
lhark's avatar
lhark committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
chirac's avatar
chirac committed
22 23 24 25
"""
Fonctions de la page d'accueil et diverses fonctions utiles pour tous
les views
"""
lhark's avatar
lhark committed
26

27 28
from __future__ import unicode_literals

Maël Kervella's avatar
Maël Kervella committed
29 30
import git

31
from django.shortcuts import render
32
from django.template.context_processors import csrf
33
from django.conf import settings
34
from django.utils.translation import ugettext as _
Maël Kervella's avatar
Maël Kervella committed
35 36
from django.views.decorators.cache import cache_page

37 38 39
from preferences.models import (
    Service,
    AssoOption,
Gabriel Detraz's avatar
Gabriel Detraz committed
40
    HomeOption
41
)
Maël Kervella's avatar
Maël Kervella committed
42

43
from .contributors import CONTRIBUTORS
chirac's avatar
chirac committed
44

Maël Kervella's avatar
Maël Kervella committed
45

chirac's avatar
chirac committed
46
def form(ctx, template, request):
chirac's avatar
chirac committed
47
    """Form générique, raccourci importé par les fonctions views du site"""
chirac's avatar
chirac committed
48 49 50
    context = ctx
    context.update(csrf(request))
    return render(request, template, context)
chirac's avatar
chirac committed
51 52 53


def index(request):
chirac's avatar
chirac committed
54
    """Affiche la liste des services sur la page d'accueil de re2o"""
55 56 57
    services = [[], [], []]
    for indice, serv in enumerate(Service.objects.all()):
        services[indice % 3].append(serv)
Gabriel Detraz's avatar
Gabriel Detraz committed
58 59 60
    twitter_url = HomeOption.get_cached_value('twitter_url')
    facebook_url = HomeOption.get_cached_value('facebook_url')
    twitter_account_name = HomeOption.get_cached_value('twitter_account_name')
61 62
    asso_name = AssoOption.get_cached_value('pseudo')
    return form({
63 64 65 66 67 68
        'services_urls': services,
        'twitter_url': twitter_url,
        'twitter_account_name': twitter_account_name,
        'facebook_url': facebook_url,
        'asso_name': asso_name
    }, 're2o/index.html', request)
69

70

Maël Kervella's avatar
Maël Kervella committed
71
@cache_page(7 * 24 * 60 * 60)
72
def about_page(request):
Maël Kervella's avatar
Maël Kervella committed
73 74 75
    """ The view for the about page.
    Fetch some info about the configuration of the project. If it can't
    get the info from the Git repository, fallback to default string """
76
    option = AssoOption.objects.get()
77 78
    git_info_contributors = CONTRIBUTORS
    try:
Maël Kervella's avatar
Maël Kervella committed
79
        git_repo = git.Repo(settings.BASE_DIR)
80 81 82 83 84 85 86
        git_info_remote = ", ".join(git_repo.remote().urls)
        git_info_branch = git_repo.active_branch.name
        last_commit = git_repo.commit()
        git_info_commit = last_commit.hexsha
        git_info_commit_date = last_commit.committed_datetime
    except:
        NO_GIT_MSG = _("Unable to get the information")
87 88 89 90 91
        git_info_remote = NO_GIT_MSG
        git_info_branch = NO_GIT_MSG
        git_info_commit = NO_GIT_MSG
        git_info_commit_date = NO_GIT_MSG

Maël Kervella's avatar
Maël Kervella committed
92
    dependencies = settings.INSTALLED_APPS + settings.MIDDLEWARE_CLASSES
93

94 95 96
    return render(
        request,
        "re2o/about.html",
97
        {
Maël Kervella's avatar
Maël Kervella committed
98 99
            'description': option.description,
            'AssoName': option.name,
100 101 102 103 104 105 106
            'git_info_contributors': git_info_contributors,
            'git_info_remote': git_info_remote,
            'git_info_branch': git_info_branch,
            'git_info_commit': git_info_commit,
            'git_info_commit_date': git_info_commit_date,
            'dependencies': dependencies
        }
107
    )
Maël Kervella's avatar
Maël Kervella committed
108 109 110 111 112


def handler500(request):
    """The handler view for a 500 error"""
    return render(request, 'errors/500.html')