views.py 4.32 KB
Newer Older
Gabriel Detraz's avatar
Gabriel Detraz committed
1
# -*- mode: python; coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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
23 24 25 26
"""
Fonctions de la page d'accueil et diverses fonctions utiles pour tous
les views
"""
27

28 29
from __future__ import unicode_literals

30 31
import git

32
from django.shortcuts import render
33
from django.template.context_processors import csrf
34
from django.conf import settings
35
from django.utils.translation import ugettext as _
36 37
from django.views.decorators.cache import cache_page

38 39
from preferences.models import (
    Service,
40
    MailContact,
41
    AssoOption,
Gabriel Detraz's avatar
Gabriel Detraz committed
42
    HomeOption
43
)
44

45
from .contributors import CONTRIBUTORS
chirac's avatar
chirac committed
46

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

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


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


73
def about_page(request):
74 75 76
    """ 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 """
77
    option = AssoOption.objects.get()
78 79
    git_info_contributors = CONTRIBUTORS
    try:
80
        git_repo = git.Repo(settings.BASE_DIR)
81 82 83 84 85 86 87
        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")
88 89 90 91 92
        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

93
    dependencies = settings.INSTALLED_APPS + settings.MIDDLEWARE_CLASSES
94

95 96 97
    return render(
        request,
        "re2o/about.html",
98
        {
Maël Kervella's avatar
Maël Kervella committed
99 100
            'description': option.description,
            'AssoName': option.name,
101 102 103 104 105 106 107
            '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
        }
108
    )
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
def contact_page(request):
    """The view for the contact page
    Send all the objects MailContact
    """
    address = MailContact.objects.all()

    return render(
        request,
        "re2o/contact.html",
        {
            'contacts': address,
            'asso_name': AssoOption.objects.first().name
        }
    )

125 126 127 128

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


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