8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-25 22:22:26 +00:00
re2o/re2o/views.py
Yoann Piétri f95babce51 fix: 🐛 Quick fix bug with MIDDLEWARE_CLASSES
Support for django 2.22 introduces the use of MIDDLEWARE instead of MIDDLEWARE_CLASSES. This change was not reflected everywhere, about function for instance.
2021-02-28 09:23:53 +00:00

193 lines
6.3 KiB
Python

# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Gabriel Détraz
# Copyright © 2017 Lara 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.
"""
Welcom main page view, and several template widely used in re2o views
"""
from __future__ import unicode_literals
from importlib import import_module
import git
from dal import autocomplete
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render
from django.template.context_processors import csrf
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext as _
from preferences.models import (AssoOption, GeneralOption, HomeOption,
MailContact, Mandate, Service)
from re2o.settings_local import OPTIONNAL_APPS_RE2O
from .contributors import CONTRIBUTORS
def form(ctx, template, request):
"""Global template function, used in all re2o views, for building a render with context,
template and request. Adding csrf.
Parameters:
ctx (dict): Dict of values to transfer to template
template (django template): The django template of this view
request (django request)
Returns:
Django render: Django render complete view with template, context and request
"""
context = ctx
context.update(csrf(request))
return render(request, template, context)
def index(request):
"""Display all services provided on main page
Returns: a form with all services linked and description, and social media
link if provided.
"""
services = [[], [], []]
for indice, serv in enumerate(Service.objects.all()):
services[indice % 3].append(serv)
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")
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,
)
def about_page(request):
"""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"""
option = AssoOption.objects.get()
general = GeneralOption.objects.get()
git_info_contributors = CONTRIBUTORS
try:
git_repo = git.Repo(settings.BASE_DIR)
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.")
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
dependencies = settings.INSTALLED_APPS + settings.MIDDLEWARE
try:
president = Mandate.get_mandate().president.get_full_name()
except Mandate.DoesNotExist:
president = _("Unable to get the information.")
return render(
request,
"re2o/about.html",
{
"option": option,
"gtu": general.GTU,
"president": president,
"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,
},
)
def contact_page(request):
"""The view for the contact page
Send all the objects MailContact
"""
address = MailContact.objects.all()
optionnal_apps = [import_module(app) for app in OPTIONNAL_APPS_RE2O]
optionnal_templates_contact_list = [
app.views.contact(request)
for app in optionnal_apps
if hasattr(app.views, "contact")
]
return render(
request,
"re2o/contact.html",
{
"contacts": address,
"asso_name": AssoOption.objects.first().name,
"optionnal_templates_contact_list": optionnal_templates_contact_list,
},
)
def handler500(request):
"""The handler view for a 500 error"""
return render(request, "errors/500.html", status=500)
def handler404(request, exception):
"""The handler view for a 404 error"""
return render(request, "errors/404.html", status=404)
class AutocompleteLoggedOutViewMixin(autocomplete.Select2QuerySetView):
obj_type = None # This MUST be overridden by child class
query_set = None
query_filter = "name__icontains" # Override this if necessary
def get_queryset(self):
can, reason, _permission, query_set = self.obj_type.can_list(self.request.user)
if query_set:
self.query_set = query_set
else:
self.query_set = self.obj_type.objects.none()
if hasattr(self, "filter_results"):
self.filter_results()
else:
if self.q:
self.query_set = self.query_set.filter(**{self.query_filter: self.q})
return self.query_set
class AutocompleteViewMixin(LoginRequiredMixin, AutocompleteLoggedOutViewMixin):
pass