modified website.decorators, webpage->app_config, created new configs
This commit is contained in:
54
maraudes/templates/compte_rendu/menu/creation.html
Normal file
54
maraudes/templates/compte_rendu/menu/creation.html
Normal file
@@ -0,0 +1,54 @@
|
||||
{% load bootstrap3 %}
|
||||
<li class="dropdown app-menu"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Création<b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="#" id="new-sujet">{% bootstrap_icon "user" %} Nouveau sujet</a></li>
|
||||
<li><a href="#" id="new-lieu">{% bootstrap_icon "globe" %} Nouveau lieu</a></li>
|
||||
</ul>
|
||||
|
||||
<script type="text/javascript">
|
||||
/* Lier les boutons de création
|
||||
* Thanks to Derek Morgan, https://dmorgan.info/posts/django-views-bootstrap-modals/
|
||||
*/
|
||||
$(function() {
|
||||
|
||||
var formAjaxSubmit = function(form, modal) {
|
||||
$(form).submit(function (e) {
|
||||
e.preventDefault();
|
||||
$.ajax({
|
||||
type: $(this).attr('method'),
|
||||
url: $(this).attr('action'),
|
||||
data: $(this).serialize(),
|
||||
success: function (xhr, ajaxOptions, thrownError) {
|
||||
if ( $(xhr).find('.has-error').length > 0 || $(xhr).find('.alert-danger').length > 0) {
|
||||
$(modal).find('.modal-body').html(xhr);
|
||||
formAjaxSubmit(form, modal);
|
||||
} else {
|
||||
$(modal).modal('toggle');
|
||||
// Reload page ?
|
||||
location.reload(true)
|
||||
}
|
||||
},
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
// handle response errors here
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
/* TODO: Use formAjaxSubmit above, but reload page on form success */
|
||||
$('#new-sujet').click(function() {
|
||||
$('#form-modal-body').load('{% url "sujets:create" %}?next={% url "maraudes:create" pk=maraude.id %}', function () {
|
||||
$('.modal-title').text("Nouveau sujet");
|
||||
$('#form-modal').modal('toggle');
|
||||
formAjaxSubmit("#form-modal-body form", "#form-modal");
|
||||
});
|
||||
});
|
||||
$('#new-lieu').click(function() {
|
||||
|
||||
$('#form-modal-body').load('{% url "maraudes:lieu-create" %}?next={% url "maraudes:create" pk=maraude.id %}', function () {
|
||||
$('.modal-title').text("Nouveau lieu");
|
||||
$('#form-modal').modal('toggle');
|
||||
formAjaxSubmit("#form-modal-body form", "#form-modal");
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
@@ -1,56 +0,0 @@
|
||||
{% if user.is_superuser %}{% load bootstrap3 %}
|
||||
<li class="dropdown app-menu"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Création<b class="caret"></b></a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="#" id="new-sujet">{% bootstrap_icon "user" %} Nouveau sujet</a></li>
|
||||
<li><a href="#" id="new-lieu">{% bootstrap_icon "globe" %} Nouveau lieu</a></li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
<script type="text/javascript">
|
||||
/* Lier les boutons de création
|
||||
* Thanks to Derek Morgan, https://dmorgan.info/posts/django-views-bootstrap-modals/
|
||||
*/
|
||||
$(function() {
|
||||
|
||||
var formAjaxSubmit = function(form, modal) {
|
||||
$(form).submit(function (e) {
|
||||
e.preventDefault();
|
||||
$.ajax({
|
||||
type: $(this).attr('method'),
|
||||
url: $(this).attr('action'),
|
||||
data: $(this).serialize(),
|
||||
success: function (xhr, ajaxOptions, thrownError) {
|
||||
if ( $(xhr).find('.has-error').length > 0 || $(xhr).find('.alert-danger').length > 0) {
|
||||
$(modal).find('.modal-body').html(xhr);
|
||||
formAjaxSubmit(form, modal);
|
||||
} else {
|
||||
$(modal).modal('toggle');
|
||||
// Reload page ?
|
||||
location.reload(true)
|
||||
}
|
||||
},
|
||||
error: function (xhr, ajaxOptions, thrownError) {
|
||||
// handle response errors here
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
/* TODO: Use formAjaxSubmit above, but reload page on form success */
|
||||
$('#new-sujet').click(function() {
|
||||
$('#form-modal-body').load('{% url "sujets:create" %}?next={% url "maraudes:create" pk=maraude.id %}', function () {
|
||||
$('.modal-title').text("Nouveau sujet");
|
||||
$('#form-modal').modal('toggle');
|
||||
formAjaxSubmit("#form-modal-body form", "#form-modal");
|
||||
});
|
||||
});
|
||||
$('#new-lieu').click(function() {
|
||||
|
||||
$('#form-modal-body').load('{% url "maraudes:lieu-create" %}?next={% url "maraudes:create" pk=maraude.id %}', function () {
|
||||
$('.modal-title').text("Nouveau lieu");
|
||||
$('#form-modal').modal('toggle');
|
||||
formAjaxSubmit("#form-modal-body form", "#form-modal");
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ from django.test import TestCase
|
||||
from .models import Maraude, Maraudeur, ReferentMaraude
|
||||
# Create your tests here.
|
||||
|
||||
from alsa.base_data import MARAUDEURS
|
||||
from maraudes_project.base_data import MARAUDEURS
|
||||
|
||||
MARAUDE_DAYS = [
|
||||
True, True, False, True, True, False, False
|
||||
|
||||
@@ -25,11 +25,24 @@ from .forms import ( RencontreForm, RencontreInlineFormSet,
|
||||
from utilisateurs.models import Maraudeur
|
||||
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
maraudes = website.app_config(
|
||||
name="maraudes",
|
||||
groups=[Maraudeur],
|
||||
menu=["maraudes/menu/dernieres_maraudes.html"],
|
||||
admin_menu=["maraudes/menu/admin_menu.html"],
|
||||
ajax=False,
|
||||
app_users=[Maraudeur],
|
||||
app_menu=["maraudes/menu_dernieres_maraudes.html", "maraudes/menu_administration.html"]
|
||||
)
|
||||
compte_rendu = website.app_config(
|
||||
name="maraudes",
|
||||
groups=[Maraudeur],
|
||||
menu=["compte_rendu/menu/creation.html"],
|
||||
ajax=False,
|
||||
)
|
||||
maraudes_ajax = website.app_config(
|
||||
name="maraudes",
|
||||
groups=[Maraudeur],
|
||||
ajax=True,
|
||||
)
|
||||
|
||||
from django.core.mail import send_mail
|
||||
|
||||
@@ -51,7 +64,7 @@ class DerniereMaraudeMixin(object):
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
@maraudes
|
||||
class IndexView(DerniereMaraudeMixin, generic.TemplateView):
|
||||
|
||||
class PageInfo:
|
||||
@@ -88,7 +101,7 @@ class IndexView(DerniereMaraudeMixin, generic.TemplateView):
|
||||
return Maraude.objects.next
|
||||
|
||||
## MARAUDES
|
||||
@webpage
|
||||
@maraudes
|
||||
class MaraudeDetailsView(DerniereMaraudeMixin, generic.DetailView):
|
||||
""" Vue détaillé d'un compte-rendu de maraude """
|
||||
|
||||
@@ -109,7 +122,7 @@ class MaraudeDetailsView(DerniereMaraudeMixin, generic.DetailView):
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
@maraudes
|
||||
class MaraudeListView(DerniereMaraudeMixin, generic.ListView):
|
||||
""" Vue de la liste des compte-rendus de maraude """
|
||||
|
||||
@@ -129,7 +142,7 @@ class MaraudeListView(DerniereMaraudeMixin, generic.ListView):
|
||||
|
||||
|
||||
## COMPTE-RENDU DE MARAUDE
|
||||
@webpage
|
||||
@compte_rendu
|
||||
class CompteRenduCreateView(generic.DetailView):
|
||||
""" Vue pour la création d'un compte-rendu de maraude """
|
||||
|
||||
@@ -147,8 +160,8 @@ class CompteRenduCreateView(generic.DetailView):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# Overrides app_menu and replace it
|
||||
self.app_menu = ["compte_rendu/menu_creation.html"]
|
||||
#WARNING: Overrides app_menu and replace it
|
||||
self._user_menu = ["compte_rendu/menu/creation.html"]
|
||||
|
||||
def get_forms(self, *args, initial=None):
|
||||
self.form = RencontreForm(*args,
|
||||
@@ -235,7 +248,7 @@ class CompteRenduCreateView(generic.DetailView):
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
@compte_rendu
|
||||
class CompteRenduUpdateView(generic.DetailView):
|
||||
""" Vue pour mettre à jour le compte-rendu de la maraude """
|
||||
|
||||
@@ -301,7 +314,7 @@ class CompteRenduUpdateView(generic.DetailView):
|
||||
|
||||
|
||||
## PLANNING
|
||||
@webpage
|
||||
@maraudes
|
||||
class PlanningView(generic.TemplateView):
|
||||
""" Display and edit the planning of next Maraudes """
|
||||
|
||||
@@ -371,7 +384,7 @@ class PlanningView(generic.TemplateView):
|
||||
|
||||
## LIEU
|
||||
|
||||
@website.webpage(ajax=True, permissions=['maraudes.add_lieu'])
|
||||
@maraudes_ajax
|
||||
class LieuCreateView(generic.edit.CreateView):
|
||||
model = Lieu
|
||||
template_name = "maraudes/lieu_create.html"
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
{% if user.is_superuser %}
|
||||
{% load bootstrap3 %}
|
||||
<li class="dropdown app-menu">
|
||||
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
|
||||
@@ -9,4 +8,3 @@
|
||||
<li><a href="{% url 'admin:app_list' app_label='sujets' %}">
|
||||
{% bootstrap_icon "wrench" %} Gérer les sujets</a></li>
|
||||
</ul>
|
||||
{% endif %}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{% load bootstrap3 %}
|
||||
<li class="app-menu">
|
||||
<a href="{% url 'sujets:liste' %}">Liste des sujets
|
||||
<a href="{% url 'suivi:liste' %}">Liste des sujets
|
||||
<span class="pull-right">{% bootstrap_icon "list" %}</span>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
from django.conf.urls import url
|
||||
|
||||
from . import views
|
||||
from sujets import views as sujets_views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', views.IndexView.as_view(), name="index"),
|
||||
url(r'liste/$', views.SujetListView.as_view(), name="liste"),
|
||||
url(r'(?P<pk>[0-9]+)/$', views.SuiviSujetView.as_view(), name="details"),
|
||||
]
|
||||
|
||||
@@ -9,15 +9,17 @@ from notes.forms import AutoNoteForm
|
||||
# Create your views here.
|
||||
from utilisateurs.models import Maraudeur
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
suivi = website.app_config(
|
||||
name="suivi",
|
||||
groups=[Maraudeur],
|
||||
menu=["suivi/menu/sujets.html"],
|
||||
admin_menu=["suivi/menu/admin_sujets.html"],
|
||||
ajax=False,
|
||||
app_users=[Maraudeur],
|
||||
app_menu=["suivi/menu/sujets.html", "suivi/menu/admin_sujets.html"]
|
||||
)
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
@suivi
|
||||
class IndexView(NoteFormMixin, generic.TemplateView):
|
||||
class PageInfo:
|
||||
title = "Suivi des bénéficiaires"
|
||||
@@ -36,10 +38,33 @@ class IndexView(NoteFormMixin, generic.TemplateView):
|
||||
#TemplateView
|
||||
template_name = "suivi/index.html"
|
||||
|
||||
@suivi
|
||||
class SujetListView(generic.ListView):
|
||||
class PageInfo:
|
||||
title = "Sujet - Liste des sujets"
|
||||
header = "Liste des sujets"
|
||||
#ListView
|
||||
model = Sujet
|
||||
template_name = "sujets/sujet_liste.html"
|
||||
paginate_by = 30
|
||||
|
||||
def post(self, request, **kwargs):
|
||||
from watson import search as watson
|
||||
search_text = request.POST.get('q')
|
||||
results = watson.filter(Sujet, search_text)
|
||||
if results.count() == 1:
|
||||
return redirect(results[0].get_absolute_url())
|
||||
self.queryset = results
|
||||
return self.get(request, **kwargs)
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['query_text'] = self.request.POST.get('q', None)
|
||||
return context
|
||||
|
||||
|
||||
@webpage
|
||||
# Import app_config from 'sujets' application, using
|
||||
# its admin_menu option
|
||||
from sujets.views import sujets
|
||||
@sujets
|
||||
class SuiviSujetView(NoteFormMixin, generic.DetailView):
|
||||
class PageInfo:
|
||||
title = "Sujet - {{sujet}}"
|
||||
@@ -59,9 +84,6 @@ class SuiviSujetView(NoteFormMixin, generic.DetailView):
|
||||
model = Sujet
|
||||
template_name = "suivi/details.html"
|
||||
context_object_name = "sujet"
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.insert_menu("sujets/menu_sujet.html")
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
context = super().get_context_data(*args, **kwargs)
|
||||
context['notes'] = self.object.notes.by_date(reverse=True)
|
||||
|
||||
@@ -5,6 +5,5 @@ from . import views
|
||||
urlpatterns = [
|
||||
url(r'(?P<pk>[0-9]+)/$', views.SujetDetailsView.as_view(), name="details"),
|
||||
url(r'(?P<pk>[0-9]+)/update/$', views.SujetUpdateView.as_view(), name="update"),
|
||||
url(r'liste/$', views.SujetListView.as_view(), name="liste"),
|
||||
url(r'create/$', views.SujetCreateView.as_view(), name="create"),
|
||||
]
|
||||
|
||||
@@ -7,15 +7,16 @@ from .forms import SujetCreateForm
|
||||
### Webpage config
|
||||
from utilisateurs.models import Maraudeur
|
||||
from website import decorators as website
|
||||
webpage = website.webpage(
|
||||
sujets = website.app_config(
|
||||
name="suivi",
|
||||
groups=[Maraudeur],
|
||||
menu=["suivi/menu/sujets.html"],
|
||||
admin_menu=["sujets/menu/admin_sujet.html"],
|
||||
ajax=True,
|
||||
app_users=[Maraudeur],
|
||||
app_name="suivi",
|
||||
app_menu=["sujets/menu/admin_sujet.html"]
|
||||
)
|
||||
### Views
|
||||
|
||||
@webpage
|
||||
@sujets
|
||||
class SujetDetailsView(generic.DetailView):
|
||||
class PageInfo:
|
||||
title = "Sujet - {{ sujet }}"
|
||||
@@ -27,33 +28,10 @@ class SujetDetailsView(generic.DetailView):
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
class SujetListView(generic.ListView):
|
||||
class PageInfo:
|
||||
title = "Sujet - Liste des sujets"
|
||||
header = "Liste des sujets"
|
||||
#ListView
|
||||
model = Sujet
|
||||
template_name = "sujets/sujet_liste.html"
|
||||
paginate_by = 30
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.insert_menu("sujets/menu/admin_sujet.html")
|
||||
def post(self, request, **kwargs):
|
||||
from watson import search as watson
|
||||
search_text = request.POST.get('q')
|
||||
results = watson.filter(Sujet, search_text)
|
||||
if results.count() == 1:
|
||||
return redirect(results[0].get_absolute_url())
|
||||
self.queryset = results
|
||||
return self.get(request, **kwargs)
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['query_text'] = self.request.POST.get('q', None)
|
||||
return context
|
||||
|
||||
|
||||
@webpage
|
||||
|
||||
@sujets
|
||||
class SujetUpdateView(generic.edit.UpdateView):
|
||||
class PageInfo:
|
||||
title = "Mise à jour - {{sujet}}"
|
||||
@@ -66,7 +44,7 @@ class SujetUpdateView(generic.edit.UpdateView):
|
||||
|
||||
|
||||
|
||||
@webpage
|
||||
@sujets
|
||||
class SujetCreateView(generic.edit.CreateView):
|
||||
class PageInfo:
|
||||
title = "Nouveau sujet"
|
||||
|
||||
@@ -5,16 +5,19 @@ def _insert_bases(cls, bases):
|
||||
new_bases = tuple(bases) + old_bases
|
||||
cls.__bases__ = new_bases
|
||||
|
||||
def webpage(**options):
|
||||
""" Class decorators that insert needed bases according to options :
|
||||
def app_config(**options):
|
||||
""" Insert per-application configuration options :
|
||||
-- name : name of the app to register under in navbar
|
||||
-- groups : user groups needed to access this application
|
||||
-- menu : user menu templates to be used
|
||||
-- admin_menu : admin menu templates, only appear for superuser
|
||||
-- ajax : view will return content_template for Ajax requests
|
||||
-- permissions : list of permissions needed to access view
|
||||
"""
|
||||
name = options.pop('name', None)
|
||||
groups = options.pop('groups', []) #Transition from app_users
|
||||
menu = options.pop('menu', [])
|
||||
admin_menu = options.pop('admin_menu', [])
|
||||
ajax = options.pop('ajax', False)
|
||||
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:
|
||||
@@ -22,18 +25,15 @@ def webpage(**options):
|
||||
else:
|
||||
new_bases.append(WebsiteTemplateMixin)
|
||||
|
||||
if permissions:
|
||||
new_bases.append(PermissionRequiredMixin)
|
||||
if app_users:
|
||||
if groups: #TODO: use group instaed of user class
|
||||
new_bases.append(SpecialUserRequiredMixin)
|
||||
|
||||
def class_decorator(cls):
|
||||
_insert_bases(cls, new_bases)
|
||||
if permissions:
|
||||
cls.permissions = permissions
|
||||
cls.app_menu = app_menu.copy() #avoid conflict between Views
|
||||
cls.app_name = app_name
|
||||
cls.app_users = app_users.copy()
|
||||
cls._user_menu = menu
|
||||
cls._admin_menu = admin_menu
|
||||
cls.app_name = name
|
||||
cls.app_users = groups.copy()
|
||||
return cls
|
||||
|
||||
return class_decorator
|
||||
|
||||
@@ -70,7 +70,12 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
||||
"""
|
||||
base_template = "base_site.html"
|
||||
content_template = None
|
||||
|
||||
app_name = None
|
||||
_user_menu = []
|
||||
_admin_menu = []
|
||||
_groups = []
|
||||
|
||||
|
||||
class Configuration:
|
||||
stylesheets = ['base.css']
|
||||
@@ -147,16 +152,15 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
||||
self._active_app = self.get_active_app()
|
||||
return self._active_app
|
||||
|
||||
def get_menu(self):
|
||||
@property
|
||||
def menu(self):
|
||||
""" Renvoie la liste des templates utilisés comme menu pour l'application
|
||||
active
|
||||
"""
|
||||
return self.app_menu
|
||||
if not self.request.user.is_superuser:
|
||||
return self._user_menu
|
||||
return self._user_menu + self._admin_menu
|
||||
|
||||
def insert_menu(self, template_name):
|
||||
""" Insert menu at beginning of self.app_menu """
|
||||
if not template_name in self.app_menu:
|
||||
self.app_menu.insert(0, template_name)
|
||||
|
||||
def _update_context_with_rendered_blocks(self, context):
|
||||
""" Render text for existing PageInfo attributes.
|
||||
@@ -178,7 +182,7 @@ class WebsiteTemplateMixin(TemplateResponseMixin):
|
||||
context = user_processor(self.request, context)
|
||||
#Webpage
|
||||
context['content_template'] = self.get_content_template()
|
||||
context['app_menu'] = self.get_menu()
|
||||
context['app_menu'] = self.menu
|
||||
return context
|
||||
|
||||
class WebsiteAjaxTemplateMixin(WebsiteTemplateMixin):
|
||||
|
||||
Reference in New Issue
Block a user