working on #10
This commit is contained in:
@@ -22,10 +22,12 @@ from .forms import ( RencontreForm, RencontreInlineFormSet,
|
||||
ObservationInlineFormSet, ObservationInlineFormSetNoExtra,
|
||||
MaraudeAutoDateForm, MonthSelectForm, )
|
||||
|
||||
from utilisateurs.models import Maraudeur
|
||||
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
ajax=False,
|
||||
permissions=['maraudes.view_maraudes'],
|
||||
app_users=[Maraudeur],
|
||||
app_menu=["maraudes/menu_dernieres_maraudes.html", "maraudes/menu_administration.html"]
|
||||
)
|
||||
|
||||
|
||||
@@ -7,10 +7,11 @@ from .forms import *
|
||||
from notes.mixins import NoteFormMixin
|
||||
from notes.forms import AutoNoteForm
|
||||
# Create your views here.
|
||||
from utilisateurs.models import Maraudeur
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
ajax=False,
|
||||
permissions=['sujets.view_sujets'],
|
||||
app_users=[Maraudeur],
|
||||
app_menu=["suivi/menu_sujets.html"]
|
||||
)
|
||||
|
||||
|
||||
@@ -5,10 +5,11 @@ from .models import Sujet
|
||||
from .forms import SujetCreateForm
|
||||
|
||||
### Webpage config
|
||||
from utilisateurs.models import Maraudeur
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
ajax=True,
|
||||
permissions=['sujets.view_sujets'],
|
||||
app_users=[Maraudeur],
|
||||
app_name="suivi",
|
||||
app_menu=[]
|
||||
)
|
||||
@@ -70,10 +71,6 @@ class SujetCreateView(generic.edit.CreateView):
|
||||
class PageInfo:
|
||||
title = "Nouveau sujet"
|
||||
header = "Nouveau sujet"
|
||||
# Special permissions
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.permissions += ['sujets.add_sujet']
|
||||
#CreateView
|
||||
template_name = "sujets/sujet_create.html"
|
||||
form_class = SujetCreateForm
|
||||
|
||||
@@ -1,15 +1,25 @@
|
||||
from django.contrib.auth.backends import ModelBackend
|
||||
|
||||
from utilisateurs.models import Maraudeur
|
||||
|
||||
class MyBackend(ModelBackend):
|
||||
|
||||
def authenticate(self, **kwargs):
|
||||
print('use MyBackend')
|
||||
print('authenticate using MyBackend')
|
||||
return super().authenticate(**kwargs)
|
||||
|
||||
def get_user(self, user_id):
|
||||
""" Retourne la classe enfant de l'utilisateur connecté
|
||||
s'il en a une, sinon le User par défaut.
|
||||
"""
|
||||
print('use MyBackend: get_user', user_id)
|
||||
return super().get_user(user_id)
|
||||
try:
|
||||
user = Maraudeur.objects.get(pk=user_id)
|
||||
except Maraudeur.DoesNotExist:
|
||||
print('no Maraudeur found. Using base user class')
|
||||
user = super().get_user(user_id)
|
||||
print("found:", user, user.__class__)
|
||||
return user
|
||||
|
||||
def has_perm(self, *args, **kwargs):
|
||||
print('call has_perm', args, kwargs)
|
||||
|
||||
@@ -14,14 +14,18 @@ def webpage(**options):
|
||||
permissions = options.pop('permissions', [])
|
||||
app_menu = options.pop('app_menu', [])
|
||||
app_name = options.pop('app_name', None)
|
||||
app_users = options.pop('app_users', [])
|
||||
|
||||
new_bases = []
|
||||
if ajax:
|
||||
new_bases.append(WebsiteAjaxTemplateMixin)
|
||||
else:
|
||||
new_bases.append(WebsiteTemplateMixin)
|
||||
|
||||
if permissions:
|
||||
new_bases.append(PermissionRequiredMixin)
|
||||
if app_users:
|
||||
new_bases.append(SpecialUserRequiredMixin)
|
||||
|
||||
def class_decorator(cls):
|
||||
_insert_bases(cls, new_bases)
|
||||
@@ -29,6 +33,7 @@ def webpage(**options):
|
||||
cls.permissions = permissions
|
||||
cls.app_menu = app_menu.copy() #avoid conflict between Views
|
||||
cls.app_name = app_name
|
||||
cls.app_users = app_users.copy()
|
||||
return cls
|
||||
|
||||
return class_decorator
|
||||
|
||||
@@ -2,7 +2,9 @@ import datetime
|
||||
from django.utils import timezone
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.apps import apps
|
||||
#TODO: remove next line
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
from django.template import Template, Context
|
||||
from django.views.generic.base import ContextMixin, TemplateResponseMixin
|
||||
|
||||
@@ -17,6 +19,28 @@ class PermissionRequiredMixin(object):
|
||||
view = super(PermissionRequiredMixin, cls).as_view(**initkwargs)
|
||||
return permission_required(cls.permissions)(view)
|
||||
|
||||
def special_user_required(authorized_users):
|
||||
|
||||
valid_cls = tuple(authorized_users)
|
||||
|
||||
def check_special_user(user):
|
||||
print('check user is instance of', valid_cls)
|
||||
if isinstance(user, valid_cls):
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
return user_passes_test(check_special_user)
|
||||
|
||||
|
||||
class SpecialUserRequiredMixin(object):
|
||||
app_users = []
|
||||
|
||||
@classmethod
|
||||
def as_view(cls, **initkwargs):
|
||||
view = super().as_view(**initkwargs)
|
||||
return special_user_required(cls.app_users)(view)
|
||||
|
||||
|
||||
|
||||
class TemplateFieldsMetaclass(type):
|
||||
@@ -105,10 +129,13 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
||||
def get_active_app(self):
|
||||
if not self.app_name:
|
||||
self.app_name = self.__class__.__module__.split(".")[0]
|
||||
# If app is website, there is no "active" application
|
||||
if self.app_name == "website":
|
||||
return None
|
||||
|
||||
active_app = apps.get_app_config(self.app_name)
|
||||
if not active_app in self.apps: #TODO: how do we deal with this ?
|
||||
print("%s must be registered in Configuration.navbar_apps" % active_app)
|
||||
return None
|
||||
raise ValueError("%s must be registered in Configuration.navbar_apps" % active_app)
|
||||
return active_app
|
||||
|
||||
@property
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<li class="{% if app == active_app %}active{%endif%}">
|
||||
<a href="/{{app.label}}/">{% bootstrap_icon app.menu_icon %} · <strong>{{ app.name|title }}</strong></a>
|
||||
</li>
|
||||
{% if app == active_app %}{% for t in app_menu %}{% include t %}{% endfor %}{% endif %}
|
||||
{% if app == active_app %}{% for template in app_menu %}{% include template %}{% endfor %}{% endif %}
|
||||
{% endif %}{%endfor%}
|
||||
</ul>
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
|
||||
@@ -8,7 +8,7 @@ from django.http import HttpResponseRedirect
|
||||
class Index(WebsiteTemplateMixin, views.generic.TemplateView):
|
||||
|
||||
template_name = "main.html"
|
||||
app_menu = [] #TODO: fix this !
|
||||
app_menu = None
|
||||
login_response = None
|
||||
|
||||
class PageInfo:
|
||||
|
||||
Reference in New Issue
Block a user