2017-01-15 23:01:18 +00:00
|
|
|
# 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.
|
2017-10-14 02:48:43 +00:00
|
|
|
"""
|
|
|
|
Fonctions de la page d'accueil et diverses fonctions utiles pour tous
|
|
|
|
les views
|
|
|
|
"""
|
2017-01-15 23:01:18 +00:00
|
|
|
|
2017-09-10 23:29:24 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
from itertools import chain
|
|
|
|
import git
|
|
|
|
from reversion.models import Version
|
|
|
|
|
2017-12-27 20:21:04 +00:00
|
|
|
from django.http import Http404
|
2018-01-06 17:51:17 +00:00
|
|
|
from django.urls import reverse
|
2017-12-27 20:21:04 +00:00
|
|
|
from django.shortcuts import render, redirect
|
2016-12-12 12:32:18 +00:00
|
|
|
from django.template.context_processors import csrf
|
2018-04-14 19:29:16 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2017-12-27 20:21:04 +00:00
|
|
|
from django.contrib import messages
|
2018-03-04 16:16:57 +00:00
|
|
|
from django.conf import settings
|
2018-04-10 22:07:26 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-04-11 11:40:55 +00:00
|
|
|
from django.views.decorators.cache import cache_page
|
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
import preferences
|
2018-04-16 04:41:38 +00:00
|
|
|
from preferences.models import (
|
|
|
|
Service,
|
|
|
|
GeneralOption,
|
|
|
|
AssoOption,
|
2018-04-16 16:22:51 +00:00
|
|
|
HomeOption
|
2018-04-16 04:41:38 +00:00
|
|
|
)
|
2018-04-14 19:29:16 +00:00
|
|
|
import users
|
2018-04-13 23:42:22 +00:00
|
|
|
import cotisations
|
|
|
|
import topologie
|
|
|
|
import machines
|
2018-04-11 11:40:55 +00:00
|
|
|
|
2018-04-03 03:00:06 +00:00
|
|
|
from .utils import re2o_paginator
|
2018-04-10 22:07:26 +00:00
|
|
|
from .contributors import CONTRIBUTORS
|
2017-10-14 02:48:43 +00:00
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
|
2016-07-19 01:12:48 +00:00
|
|
|
def form(ctx, template, request):
|
2017-10-14 02:48:43 +00:00
|
|
|
"""Form générique, raccourci importé par les fonctions views du site"""
|
2017-10-13 20:47:32 +00:00
|
|
|
context = ctx
|
|
|
|
context.update(csrf(request))
|
|
|
|
return render(request, template, context)
|
2016-07-19 01:12:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def index(request):
|
2017-10-14 02:48:43 +00:00
|
|
|
"""Affiche la liste des services sur la page d'accueil de re2o"""
|
2017-08-25 02:35:49 +00:00
|
|
|
services = [[], [], []]
|
|
|
|
for indice, serv in enumerate(Service.objects.all()):
|
|
|
|
services[indice % 3].append(serv)
|
2018-04-16 16:22:51 +00:00
|
|
|
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')
|
2018-04-16 04:41:38 +00:00
|
|
|
asso_name = AssoOption.get_cached_value('pseudo')
|
|
|
|
return form({
|
|
|
|
'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)
|
2017-12-27 20:21:04 +00:00
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
|
2017-12-28 13:43:43 +00:00
|
|
|
#: Binding the corresponding char sequence of history url to re2o models.
|
2017-12-27 20:21:04 +00:00
|
|
|
HISTORY_BIND = {
|
2018-04-13 23:42:22 +00:00
|
|
|
'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,
|
|
|
|
'listshell': users.models.ListShell,
|
2018-01-06 18:09:18 +00:00
|
|
|
},
|
2018-04-13 23:42:22 +00:00
|
|
|
'preferences': {
|
|
|
|
'service': preferences.models.Service,
|
2018-05-06 17:11:12 +00:00
|
|
|
'reminder': preferences.models.Reminder,
|
2018-01-06 18:09:18 +00:00
|
|
|
},
|
2018-04-13 23:42:22 +00:00
|
|
|
'cotisations': {
|
|
|
|
'facture': cotisations.models.Facture,
|
|
|
|
'article': cotisations.models.Article,
|
|
|
|
'paiement': cotisations.models.Paiement,
|
|
|
|
'banque': cotisations.models.Banque,
|
2018-01-06 18:09:18 +00:00
|
|
|
},
|
2018-04-13 23:42:22 +00:00
|
|
|
'topologie': {
|
|
|
|
'switch': topologie.models.Switch,
|
|
|
|
'port': topologie.models.Port,
|
|
|
|
'room': topologie.models.Room,
|
|
|
|
'stack': topologie.models.Stack,
|
|
|
|
'modelswitch': topologie.models.ModelSwitch,
|
|
|
|
'constructorswitch': topologie.models.ConstructorSwitch,
|
|
|
|
'accesspoint': topologie.models.AccessPoint,
|
|
|
|
'switchbay': topologie.models.SwitchBay,
|
|
|
|
'building': topologie.models.Building,
|
2018-01-06 18:09:18 +00:00
|
|
|
},
|
2018-04-13 23:42:22 +00:00
|
|
|
'machines': {
|
|
|
|
'machine': machines.models.Machine,
|
|
|
|
'interface': machines.models.Interface,
|
|
|
|
'domain': machines.models.Domain,
|
|
|
|
'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,
|
|
|
|
'nas': machines.models.Nas,
|
|
|
|
'ipv6list': machines.models.Ipv6List,
|
2018-01-06 18:09:18 +00:00
|
|
|
},
|
2017-12-27 20:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-13 23:42:22 +00:00
|
|
|
|
2017-12-27 20:21:04 +00:00
|
|
|
@login_required
|
2018-01-06 18:09:18 +00:00
|
|
|
def history(request, application, object_name, object_id):
|
2017-12-28 13:43:43 +00:00
|
|
|
"""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.
|
|
|
|
"""
|
2017-12-27 20:21:04 +00:00
|
|
|
try:
|
2018-01-06 18:09:18 +00:00
|
|
|
model = HISTORY_BIND[application][object_name]
|
2018-04-14 19:29:16 +00:00
|
|
|
except KeyError:
|
2017-12-27 20:21:04 +00:00
|
|
|
raise Http404(u"Il n'existe pas d'historique pour ce modèle.")
|
2018-03-28 16:04:25 +00:00
|
|
|
object_name_id = object_name + 'id'
|
|
|
|
kwargs = {object_name_id: object_id}
|
2017-12-27 20:21:04 +00:00
|
|
|
try:
|
2018-03-28 16:04:25 +00:00
|
|
|
instance = model.get_instance(**kwargs)
|
2017-12-27 20:21:04 +00:00
|
|
|
except model.DoesNotExist:
|
|
|
|
messages.error(request, u"Entrée inexistante")
|
2018-04-13 23:42:22 +00:00
|
|
|
return redirect(reverse(
|
|
|
|
'users:profil',
|
|
|
|
kwargs={'userid': str(request.user.id)}
|
2017-12-27 20:21:04 +00:00
|
|
|
))
|
|
|
|
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',
|
2018-04-13 23:42:22 +00:00
|
|
|
kwargs={'userid': str(request.user.id)}
|
2017-12-27 20:21:04 +00:00
|
|
|
))
|
2018-01-30 22:07:43 +00:00
|
|
|
pagination_number = GeneralOption.get_cached_value('pagination_number')
|
2017-12-27 20:21:04 +00:00
|
|
|
reversions = Version.objects.get_for_object(instance)
|
2018-04-02 01:36:34 +00:00
|
|
|
if hasattr(instance, 'linked_objects'):
|
|
|
|
for related_object in chain(instance.linked_objects()):
|
2018-04-13 23:42:22 +00:00
|
|
|
reversions = (reversions |
|
|
|
|
Version.objects.get_for_object(related_object))
|
2018-04-03 03:00:06 +00:00
|
|
|
reversions = re2o_paginator(request, reversions, pagination_number)
|
2017-12-27 20:21:04 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
're2o/history.html',
|
|
|
|
{'reversions': reversions, 'object': instance}
|
|
|
|
)
|
|
|
|
|
2018-01-08 15:33:50 +00:00
|
|
|
|
2018-04-11 11:40:55 +00:00
|
|
|
@cache_page(7 * 24 * 60 * 60)
|
2018-01-08 15:33:50 +00:00
|
|
|
def about_page(request):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" 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 """
|
2018-01-08 15:33:50 +00:00
|
|
|
option = AssoOption.objects.get()
|
2018-04-10 22:07:26 +00:00
|
|
|
git_info_contributors = CONTRIBUTORS
|
|
|
|
try:
|
2018-04-14 19:29:16 +00:00
|
|
|
git_repo = git.Repo(settings.BASE_DIR)
|
2018-04-11 13:29:14 +00:00
|
|
|
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")
|
2018-04-10 22:07:26 +00:00
|
|
|
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
|
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
dependencies = settings.INSTALLED_APPS + settings.MIDDLEWARE_CLASSES
|
2018-04-10 22:07:26 +00:00
|
|
|
|
2018-01-08 15:33:50 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"re2o/about.html",
|
2018-04-10 22:07:26 +00:00
|
|
|
{
|
2018-04-13 23:42:22 +00:00
|
|
|
'description': option.description,
|
|
|
|
'AssoName': option.name,
|
2018-04-10 22:07:26 +00:00
|
|
|
'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
|
|
|
|
}
|
2018-01-08 15:33:50 +00:00
|
|
|
)
|
2018-04-15 17:05:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handler500(request):
|
|
|
|
"""The handler view for a 500 error"""
|
|
|
|
return render(request, 'errors/500.html')
|