isolated NavBarMixin
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from django.shortcuts import render, reverse
|
from django.shortcuts import render, reverse, redirect
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import datetime
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User, AnonymousUser
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
class SingletonModel(models.Model):
|
class SingletonModel(models.Model):
|
||||||
@@ -20,6 +20,13 @@ class SingletonModel(models.Model):
|
|||||||
except cls.DoesNotExist:
|
except cls.DoesNotExist:
|
||||||
return cls()
|
return cls()
|
||||||
|
|
||||||
|
## Visiteur
|
||||||
|
|
||||||
|
class Visiteur(AnonymousUser):
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Visiteur"
|
||||||
|
|
||||||
|
|
||||||
class Organisme(models.Model):
|
class Organisme(models.Model):
|
||||||
""" Organisme : Association, Entreprise, Service public, ..."""
|
""" Organisme : Association, Entreprise, Service public, ..."""
|
||||||
|
|||||||
@@ -57,54 +57,13 @@ def user_processor(request, context):
|
|||||||
context['user_group'] = request.user.groups.first()
|
context['user_group'] = request.user.groups.first()
|
||||||
return context
|
return context
|
||||||
|
|
||||||
class WebsiteTemplateMixin(TemplateResponseMixin):
|
|
||||||
""" Mixin for easy integration of 'website' templates
|
|
||||||
|
|
||||||
Each child can specify:
|
class NavbarMixin(object):
|
||||||
- title : title of the page
|
|
||||||
- header : header of the page
|
|
||||||
- header_small : sub-header of the page
|
|
||||||
|
|
||||||
If 'content_template' is not defined, value will fallback to template_name
|
|
||||||
in child view.
|
|
||||||
"""
|
|
||||||
base_template = "base_site.html"
|
|
||||||
content_template = None
|
|
||||||
|
|
||||||
|
registered_apps = ['maraudes', 'suivi']
|
||||||
app_name = None
|
app_name = None
|
||||||
_user_menu = []
|
|
||||||
_admin_menu = []
|
|
||||||
_groups = []
|
|
||||||
|
|
||||||
|
|
||||||
class Configuration:
|
|
||||||
stylesheets = ['base.css']
|
|
||||||
navbar_apps = ['maraudes', 'suivi']
|
|
||||||
page_blocks = ['header', 'header_small', 'title']
|
|
||||||
|
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.user = None
|
|
||||||
self._page_blocks = []
|
|
||||||
if not hasattr(self, "PageInfo"):
|
|
||||||
raise ImproperlyConfigured("You must define a PageInfo on ", self)
|
|
||||||
for attr, val in self.PageInfo.__dict__.items():
|
|
||||||
if attr[0] is not "_" and type(val) is str:
|
|
||||||
setattr(self, attr, Template(val))
|
|
||||||
self._page_blocks.append(attr)
|
|
||||||
|
|
||||||
def get_template_names(self):
|
|
||||||
""" Ensure same template for all children views. """
|
|
||||||
return [self.base_template]
|
|
||||||
|
|
||||||
def get_content_template(self):
|
|
||||||
# Ensure easy integration with generic views
|
|
||||||
if hasattr(self, 'template_name'):
|
|
||||||
self.content_template = self.template_name
|
|
||||||
else:
|
|
||||||
raise ImproperlyConfigured(self, "has no template defined !")
|
|
||||||
return self.content_template
|
|
||||||
|
|
||||||
def get_apps_config(self):
|
def get_apps_config(self):
|
||||||
""" Load additionnal config data on each app registered in navbar
|
""" Load additionnal config data on each app registered in navbar
|
||||||
@@ -117,7 +76,7 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
|||||||
'maraudes': 'road',
|
'maraudes': 'road',
|
||||||
'suivi': 'eye-open',
|
'suivi': 'eye-open',
|
||||||
}
|
}
|
||||||
app_names = self.Configuration.navbar_apps
|
app_names = self.registered_apps
|
||||||
self._apps = []
|
self._apps = []
|
||||||
for name in app_names:
|
for name in app_names:
|
||||||
app_config = apps.get_app_config(name)
|
app_config = apps.get_app_config(name)
|
||||||
@@ -162,6 +121,55 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
|||||||
return self._user_menu + self._admin_menu
|
return self._user_menu + self._admin_menu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class WebsiteTemplateMixin(NavbarMixin, TemplateResponseMixin):
|
||||||
|
""" Mixin for easy integration of 'website' templates
|
||||||
|
|
||||||
|
Each child can specify:
|
||||||
|
- title : title of the page
|
||||||
|
- header : header of the page
|
||||||
|
- header_small : sub-header of the page
|
||||||
|
|
||||||
|
If 'content_template' is not defined, value will fallback to template_name
|
||||||
|
in child view.
|
||||||
|
"""
|
||||||
|
base_template = "base_site.html"
|
||||||
|
content_template = None
|
||||||
|
|
||||||
|
_user_menu = []
|
||||||
|
_admin_menu = []
|
||||||
|
_groups = []
|
||||||
|
|
||||||
|
|
||||||
|
class Configuration:
|
||||||
|
stylesheets = ['base.css']
|
||||||
|
page_blocks = ['header', 'header_small', 'title']
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.user = None
|
||||||
|
self._page_blocks = []
|
||||||
|
if not hasattr(self, "PageInfo"):
|
||||||
|
raise ImproperlyConfigured("You must define a PageInfo on ", self)
|
||||||
|
for attr, val in self.PageInfo.__dict__.items():
|
||||||
|
if attr[0] is not "_" and type(val) is str:
|
||||||
|
setattr(self, attr, Template(val))
|
||||||
|
self._page_blocks.append(attr)
|
||||||
|
|
||||||
|
def get_template_names(self):
|
||||||
|
""" Ensure same template for all children views. """
|
||||||
|
return [self.base_template]
|
||||||
|
|
||||||
|
def get_content_template(self):
|
||||||
|
# Ensure easy integration with generic views
|
||||||
|
if hasattr(self, 'template_name'):
|
||||||
|
self.content_template = self.template_name
|
||||||
|
else:
|
||||||
|
raise ImproperlyConfigured(self, "has no template defined !")
|
||||||
|
return self.content_template
|
||||||
|
|
||||||
|
|
||||||
def _update_context_with_rendered_blocks(self, context):
|
def _update_context_with_rendered_blocks(self, context):
|
||||||
""" Render text for existing PageInfo attributes.
|
""" Render text for existing PageInfo attributes.
|
||||||
See Configuration.page_blocks for valid attribute names """
|
See Configuration.page_blocks for valid attribute names """
|
||||||
@@ -205,9 +213,4 @@ class WebsiteAjaxTemplateMixin(WebsiteTemplateMixin):
|
|||||||
return [self.ajax_template]
|
return [self.ajax_template]
|
||||||
return super().get_template_names()
|
return super().get_template_names()
|
||||||
|
|
||||||
class WebsiteProtectedMixin(WebsiteTemplateMixin, PermissionRequiredMixin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
class WebsiteProtectedWithAjaxMixin(WebsiteAjaxTemplateMixin, PermissionRequiredMixin):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|||||||
@@ -40,11 +40,9 @@ def login_view(request):
|
|||||||
user = authenticate(username=username, password=password)
|
user = authenticate(username=username, password=password)
|
||||||
if user is not None:
|
if user is not None:
|
||||||
login(request, user)
|
login(request, user)
|
||||||
|
|
||||||
next = request.POST.get('next', None)
|
next = request.POST.get('next', None)
|
||||||
if not next:
|
if not next:
|
||||||
next = _get_entry_point(user)
|
next = _get_entry_point(user)
|
||||||
return HttpResponseRedirect(next)
|
return HttpResponseRedirect(next)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return HttpResponseRedirect('/')
|
return HttpResponseRedirect('/')
|
||||||
|
|||||||
Reference in New Issue
Block a user