views.py 6.61 KB
Newer Older
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
"""
26

27 28
from __future__ import unicode_literals

29
from django.http import Http404
LEVY-FALK Hugo's avatar
LEVY-FALK Hugo committed
30
from django.urls import reverse
31
from django.shortcuts import render, redirect
32
from django.template.context_processors import csrf
33 34 35 36
from django.contrib.auth.decorators import login_required, permission_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from reversion.models import Version
from django.contrib import messages
37
from preferences.models import Service
38
from preferences.models import OptionalUser, GeneralOption, AssoOption
39 40 41 42
from django.conf import settings
from contributors import contributeurs
import os
import time
43
from itertools import chain
44
import users, preferences, cotisations, topologie, machines
chirac's avatar
chirac 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)
lhark's avatar
lhark committed
58
    return form({'services_urls': services}, 're2o/index.html', request)
59

60
#: Binding the corresponding char sequence of history url to re2o models.
61
HISTORY_BIND = {
62 63 64 65 66 67 68
    'users' : {
        'user' : users.models.User,
        'ban' : users.models.Ban,
        'whitelist' : users.models.Whitelist,
        'school' : users.models.School,
        'listright' : users.models.ListRight,
        'serviceuser' : users.models.ServiceUser,
Gabriel Detraz's avatar
Gabriel Detraz committed
69
        'listshell' : users.models.ListShell,
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    },
    'preferences' : {
        'service' : preferences.models.Service,
    },
    'cotisations' : {
        'facture' : cotisations.models.Facture,
        'article' : cotisations.models.Article,
        'paiement' : cotisations.models.Paiement,
        'banque' : cotisations.models.Banque,
    },
    'topologie' : {
        'switch' : topologie.models.Switch,
        'port' : topologie.models.Port,
        'room' : topologie.models.Room,
        'stack' : topologie.models.Stack,
Gabriel Detraz's avatar
Gabriel Detraz committed
85 86
        'modelswitch' : topologie.models.ModelSwitch,
        'constructorswitch' : topologie.models.ConstructorSwitch,
Gabriel Detraz's avatar
Gabriel Detraz committed
87
        'accesspoint' : topologie.models.AccessPoint,
88 89 90 91
    },
    'machines' : {
        'machine' : machines.models.Machine,
        'interface' : machines.models.Interface,
Gabriel Detraz's avatar
Gabriel Detraz committed
92
        'domain' : machines.models.Domain,
93 94 95 96 97 98 99 100 101 102
        'machinetype' : machines.models.MachineType,
        'iptype' : machines.models.IpType,
        'extension' : machines.models.Extension,
        'soa' : machines.models.SOA,
        'mx' : machines.models.Mx,
        'txt' : machines.models.Txt,
        'srv' : machines.models.Srv,
        'ns' : machines.models.Ns,
        'service' : machines.models.Service,
        'vlan' : machines.models.Vlan,
Gabriel Detraz's avatar
Gabriel Detraz committed
103
        'nas' : machines.models.Nas,
104
        'ipv6list' : machines.models.Ipv6List,
105
    },
106 107 108
}

@login_required
109
def history(request, application, object_name, object_id):
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
    """Render history for a model.

    The model is determined using the `HISTORY_BIND` dictionnary if none is
    found, raises a Http404. The view checks if the user is allowed to see the
    history using the `can_view` method of the model.

    Args:
        request: The request sent by the user.
        object_name: Name of the model.
        object_id: Id of the object you want to acces history.

    Returns:
        The rendered page of history if access is granted, else the user is
        redirected to their profile page, with an error message.

    Raises:
        Http404: This kind of models doesn't have history.
    """
128
    try:
129
        model = HISTORY_BIND[application][object_name]
130 131
    except KeyError as e:
        raise Http404(u"Il n'existe pas d'historique pour ce modèle.")
132 133
    object_name_id = object_name + 'id'
    kwargs = {object_name_id: object_id}
134
    try:
135
        instance = model.get_instance(**kwargs)
136 137 138 139 140 141 142 143 144 145 146 147
    except model.DoesNotExist:
        messages.error(request, u"Entrée inexistante")
        return redirect(reverse('users:profil',
            kwargs={'userid':str(request.user.id)}
        ))
    can, msg = instance.can_view(request.user)
    if not can:
        messages.error(request, msg or "Vous ne pouvez pas accéder à ce menu")
        return redirect(reverse(
            'users:profil',
            kwargs={'userid':str(request.user.id)}
        ))
148
    pagination_number = GeneralOption.get_cached_value('pagination_number')
149
    reversions = Version.objects.get_for_object(instance)
150 151 152
    if hasattr(instance, 'linked_objects'):
        for related_object in chain(instance.linked_objects()):
            reversions = reversions | Version.objects.get_for_object(related_object)
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    paginator = Paginator(reversions, pagination_number)
    page = request.GET.get('page')
    try:
        reversions = paginator.page(page)
    except PageNotAnInteger:
        # If page is not an integer, deliver first page.
        reversions = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of result
        reversions = paginator.page(paginator.num_pages)
    return render(
        request,
        're2o/history.html',
        {'reversions': reversions, 'object': instance}
    )

169 170 171

def about_page(request):
    option = AssoOption.objects.get()
172 173 174
    n = len(contributeurs)
    contrib_1 = contributeurs[:n//2]
    contrib_2 = contributeurs[n//2:]
175 176 177
    return render(
        request,
        "re2o/about.html",
178
        {'description': option.description , 'AssoName' : option.name , 'contrib_1' : contrib_1 , 'contrib_2' : contrib_2}
179
    )
180