3
0
Fork 0
mirror of https://github.com/nanoy42/coope synced 2024-11-25 22:22:27 +00:00
coope/preferences/views.py

269 lines
7.8 KiB
Python
Raw Normal View History

2019-01-22 19:27:18 +00:00
import simplejson as json
2018-12-23 11:06:16 +00:00
2018-10-05 22:03:02 +00:00
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib import messages
from django.urls import reverse
2018-11-22 21:52:15 +00:00
from django.contrib.auth.decorators import login_required, permission_required
2018-12-23 11:06:16 +00:00
from django.http import HttpResponse
from django.forms.models import model_to_dict
2018-11-22 21:52:15 +00:00
from coopeV3.acl import active_required
2018-08-31 12:46:35 +00:00
2018-10-05 22:03:02 +00:00
from .models import GeneralPreferences, Cotisation, PaymentMethod
from .forms import CotisationForm, PaymentMethodForm, GeneralPreferencesForm
2018-11-22 21:52:15 +00:00
@active_required
@login_required
2018-12-02 15:28:40 +00:00
@permission_required('preferences.change_generalpreferences')
2018-10-05 22:03:02 +00:00
def generalPreferences(request):
2018-12-02 15:28:40 +00:00
"""
Display form to edit the general preferences
**Context**
``form``
The GeneralPreferences form instance
**Template**
:template:`preferences/general_preferences.html`
"""
2018-10-05 22:03:02 +00:00
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
form = GeneralPreferencesForm(request.POST or None, instance=gp)
if(form.is_valid()):
form.save()
2019-01-06 04:18:31 +00:00
messages.success(request, "Les préférences générales ont bien été mises à jour")
2018-10-05 22:03:02 +00:00
return render(request, "preferences/general_preferences.html", {"form": form})
########## Cotisations ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.view_cotisation')
2018-10-05 22:03:02 +00:00
def cotisationsIndex(request):
2018-12-02 15:28:40 +00:00
"""
Lists the cotisations
**Context**
``cotisations``
List of cotisations
**Template**
:template:`preferences/cotisations_index.html`
"""
2018-10-05 22:03:02 +00:00
cotisations = Cotisation.objects.all()
return render(request, "preferences/cotisations_index.html", {"cotisations": cotisations})
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.add_cotisation')
2018-10-05 22:03:02 +00:00
def addCotisation(request):
2018-12-02 15:28:40 +00:00
"""
Form to add a cotisation
**Context**
``form``
The CotisationForm form instance
``form_title``
The title of the form
``form_button``
The text of the form button
**Template**
:template:`form.html`
"""
2018-10-05 22:03:02 +00:00
form = CotisationForm(request.POST or None)
if(form.is_valid()):
cotisation = form.save()
messages.success(request, "La cotisation (" + str(cotisation.duration) + " jours, " + str(cotisation.amount) + "€) a bien été créée")
return redirect(reverse('preferences:cotisationsIndex'))
2019-01-17 22:16:43 +00:00
return render(request, "form.html", {"form": form, "form_title": "Création d'une cotisation", "form_button": "Créer", "form_button_icon": "plus-square"})
2018-10-05 22:03:02 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.change_cotisation')
2018-10-05 22:03:02 +00:00
def editCotisation(request, pk):
2018-12-02 15:28:40 +00:00
"""
Form to edit a cotisation
``pk``
The primary key of the cotisation
**Context**
``form``
The CotisationForm form instance
``form_title``
The title of the form
``form_button``
The text of the form button
**Template**
:template:`form.html`
"""
2018-10-05 22:03:02 +00:00
cotisation = get_object_or_404(Cotisation, pk=pk)
form = CotisationForm(request.POST or None, instance=cotisation)
if(form.is_valid()):
cotisation = form.save()
messages.success(request, "La cotisation (" + str(cotisation.duration) + " jours, " + str(cotisation.amount) + "€) a bien été modifiée")
return redirect(reverse('preferences:cotisationsIndex'))
2019-01-17 22:16:43 +00:00
return render(request, "form.html", {"form": form, "form_title": "Modification d'une cotisation", "form_button": "Modifier", "form_button_icon": "pencil-alt"})
2018-10-05 22:03:02 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.delete_cotisation')
2018-10-05 22:03:02 +00:00
def deleteCotisation(request,pk):
2018-12-02 15:28:40 +00:00
"""
Delete a cotisation
``pk``
The primary key of the cotisation to delete
"""
2018-10-05 22:03:02 +00:00
cotisation = get_object_or_404(Cotisation, pk=pk)
message = "La cotisation (" + str(cotisation.duration) + " jours, " + str(cotisation.amount) + "€) a bien été supprimée"
cotisation.delete()
messages.success(request, message)
return redirect(reverse('preferences:cotisationsIndex'))
2019-01-22 19:27:18 +00:00
@active_required
@login_required
@permission_required('preferences.view_cotisation')
def get_cotisation(request, pk):
"""
Get a cotisation by pk
``pk``
The primary key of the cotisation
"""
cotisation = get_object_or_404(Cotisation, pk=pk)
data = json.dumps({"pk": cotisation.pk, "duration": cotisation.duration, "amount" : cotisation.amount, "needQuantityButton": False})
return HttpResponse(data, content_type='application/json')
2018-10-05 22:03:02 +00:00
########## Payment Methods ##########
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.view_paymentmethod')
2018-10-05 22:03:02 +00:00
def paymentMethodsIndex(request):
2018-12-02 15:28:40 +00:00
"""
Lists the paymentMethods
**Context**
``paymentMethods``
List of paymentMethods
**Template**
:template:`preferences/payment_methods_index.html`
"""
2018-10-05 22:03:02 +00:00
paymentMethods = PaymentMethod.objects.all()
return render(request, "preferences/payment_methods_index.html", {"paymentMethods": paymentMethods})
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.add_paymentmethod')
2018-10-05 22:03:02 +00:00
def addPaymentMethod(request):
2018-12-02 15:28:40 +00:00
"""
Form to add a paymentMethod
**Context**
``form``
The CotisationForm form paymentMethod
``form_title``
The title of the form
``form_button``
The text of the form button
**Template**
:template:`form.html`
"""
2018-10-05 22:03:02 +00:00
form = PaymentMethodForm(request.POST or None)
if(form.is_valid()):
paymentMethod = form.save()
messages.success(request, "Le moyen de paiement " + paymentMethod.name + " a bien été crée")
return redirect(reverse('preferences:paymentMethodsIndex'))
2019-01-17 22:16:43 +00:00
return render(request, "form.html", {"form": form, "form_title": "Création d'un moyen de paiement", "form_button": "Créer", "form_button_icon": "plus-square"})
2018-10-05 22:03:02 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.change_paymentmethod')
2018-10-05 22:03:02 +00:00
def editPaymentMethod(request, pk):
2018-12-02 15:28:40 +00:00
"""
Form to edit a paymentMethod
``pk``
The primary key of the paymentMethod
**Context**
``form``
The PaymentMethodForm form instance
``form_title``
The title of the form
``form_button``
The text of the form button
**Template**
:template:`form.html`
"""
2018-10-05 22:03:02 +00:00
paymentMethod = get_object_or_404(PaymentMethod, pk=pk)
form = PaymentMethodForm(request.POST or None, instance=paymentMethod)
if(form.is_valid()):
paymentMethod = form.save()
messages.success(request, "Le moyen de paiment " + paymentMethod.name + " a bien été modifié")
return redirect(reverse('preferences:paymentMethodsIndex'))
2019-01-17 22:16:43 +00:00
return render(request, "form.html", {"form": form, "form_title": "Modification d'un moyen de paiement", "form_button": "Modifier", "form_button_icon": "pencil-alt"})
2018-10-05 22:03:02 +00:00
2018-11-22 21:52:15 +00:00
@active_required
@login_required
@permission_required('preferences.delete_paymentmethod')
2018-10-05 22:03:02 +00:00
def deletePaymentMethod(request,pk):
2018-12-02 15:28:40 +00:00
"""
Delete a paymentMethod
``pk``
The primary key of the paymentMethod to delete
"""
2018-10-05 22:03:02 +00:00
paymentMethod = get_object_or_404(PaymentMethod, pk=pk)
message = "Le moyen de paiement " + paymentMethod.name + " a bien été supprimé"
paymentMethod.delete()
messages.success(request, message)
2018-12-02 15:28:40 +00:00
return redirect(reverse('preferences:paymentMethodsIndex'))
2018-12-14 08:17:56 +00:00
########## Active Site ##########
def inactive(request):
"""
Displays inactive view
"""
gp, _ = GeneralPreferences.objects.get_or_create(pk=1)
return render(request, 'preferences/inactive.html', {"message": gp.active_message})
2018-12-23 11:06:16 +00:00
########## Config ##########
def get_config(request):
"""
Load the config and return it in a json format
"""
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
data = json.dumps(model_to_dict(gp))
return HttpResponse(data, content_type='application/json')
2018-12-14 08:17:56 +00:00