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

318 lines
13 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
from django.http import Http404
2019-09-12 07:40:43 +00:00
from django.core.mail import mail_admins
2018-11-22 21:52:15 +00:00
from coopeV3.acl import active_required
2018-08-31 12:46:35 +00:00
2019-09-12 07:40:43 +00:00
from .models import GeneralPreferences, Cotisation, PaymentMethod, PriceProfile, Improvement
2018-10-05 22:03:02 +00:00
2019-09-12 07:40:43 +00:00
from .forms import CotisationForm, PaymentMethodForm, GeneralPreferencesForm, PriceProfileForm, ImprovementForm
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
View which displays a :class:`~preferences.forms.GeneralPreferencesForm` to edit the :class:`~preferences.models.GeneralPreferences`.
2018-12-02 15:28:40 +00:00
"""
2018-10-05 22:03:02 +00:00
gp,_ = GeneralPreferences.objects.get_or_create(pk=1)
2019-02-18 18:20:09 +00:00
form = GeneralPreferencesForm(request.POST or None, request.FILES or None, instance=gp)
2018-10-05 22:03:02 +00:00
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
"""
2019-02-28 12:18:41 +00:00
View which lists all the :class:`~preferences.models.Cotisation`.
2018-12-02 15:28:40 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
View which displays a :class:`~preferences.forms.CotisationForm` to create a :class:`~preferences.models.Cotisation`.
2018-12-02 15:28:40 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
View which displays a :class:`~preferences.forms.CotisationForm` to edit a :class:`~preferences.models.Cotisation`.
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the :class:`~preferences.models.Cotisation` to edit.
2018-12-02 15:28:40 +00:00
"""
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')
2019-02-28 12:18:41 +00:00
def deleteCotisation(request, pk):
2018-12-02 15:28:40 +00:00
"""
2019-02-28 12:18:41 +00:00
Delete a :class:`~preferences.models.Cotisation`.
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the :class:`~preferences.models.Cotisation` to delete.
2018-12-02 15:28:40 +00:00
"""
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):
"""
2019-02-28 12:18:41 +00:00
Return the requested :class:`~preferences.models.Cotisation` in json format.
2019-01-22 19:27:18 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the requested :class:`~preferences.models.Cotisation`.
2019-01-22 19:27:18 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
View which lists all the :class:`~preferences.models.PaymentMethod`.
2018-12-02 15:28:40 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
View which displays a :class:`~preferences.forms.PaymentMethodForm` to create a :class:`~preferences.models.PaymentMethod`.
2018-12-02 15:28:40 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
View which displays a :class:`~preferences.forms.PaymentMethodForm` to edit a :class:`~preferences.models.PaymentMethod`.
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the :class:`~preferences.models.PaymentMethod` to edit.
2018-12-02 15:28:40 +00:00
"""
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
"""
2019-02-28 12:18:41 +00:00
Delete a :class:`~preferences.models.PaymentMethod`.
2018-12-02 15:28:40 +00:00
2019-02-28 12:18:41 +00:00
pk
The primary key of the :class:`~preferences.models.PaymentMethod` to delete.
2018-12-02 15:28:40 +00:00
"""
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):
"""
2019-02-28 12:18:41 +00:00
View which displays the inactive message (if the site is inactive).
2018-12-14 08:17:56 +00:00
"""
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):
"""
2019-02-28 12:18:41 +00:00
Load the :class:`~preferences.models.GeneralPreferences` and return it in json format (except for :attr:`~preferences.models.GeneralPreferences.statutes`, :attr:`~preferences.models.GeneralPreferences.rules` and :attr:`~preferences.models.GeneralPreferences.menu`)
2018-12-23 11:06:16 +00:00
"""
gp, _ = GeneralPreferences.objects.defer("statutes", "rules", "menu").get_or_create(pk=1)
gp_dict = model_to_dict(gp)
del gp_dict["statutes"]
del gp_dict["rules"]
del gp_dict["menu"]
2019-04-28 16:35:48 +00:00
del gp_dict["alcohol_charter"]
data = json.dumps(gp_dict)
2018-12-23 11:06:16 +00:00
return HttpResponse(data, content_type='application/json')
2019-06-23 12:53:18 +00:00
########## Price Profiles ##########
@active_required
@login_required
@permission_required('preferences.view_priceprofile')
def price_profiles_index(request):
"""
View which lists all the :class:`~preferences.models.PriceProfile`.
"""
price_profiles = PriceProfile.objects.all()
return render(request, "preferences/price_profiles_index.html", {"price_profiles": price_profiles})
@active_required
@login_required
@permission_required('preferences.add_priceprofile')
def add_price_profile(request):
"""
View which displays a :class:`~preferences.forms.PriceProfileForm` to create a :class:`~preferences.models.PriceProfile`.
"""
form = PriceProfileForm(request.POST or None)
if form.is_valid():
price_profile = form.save()
messages.success(request, "Le profil de prix " + price_profile.name + " a bien été crée")
return redirect(reverse('preferences:priceProfilesIndex'))
return render(request, "form.html", {"form": form, "form_title": "Création d'un profil de prix", "form_button": "Créer", "form_button_icon": "plus-square"})
@active_required
@login_required
@permission_required('preferences.change_priceprofile')
def edit_price_profile(request, pk):
"""
View which displays a :class:`~preferences.forms.PriceProfile` to edit a :class:`~preferences.models.PriceProfile`.
pk
The primary key of the :class:`~preferences.models.PriceProfile` to edit.
"""
price_profile = get_object_or_404(PriceProfile, pk=pk)
form = PriceProfileForm(request.POST or None, instance=price_profile)
if form.is_valid():
price_profile = form.save()
messages.success(request, "Le profil de prix " + price_profile.name + " a bien été modifié")
return redirect(reverse('preferences:priceProfilesIndex'))
return render(request, "form.html", {"form": form, "form_title": "Modification d'un profil de prix", "form_button": "Modifier", "form_button_icon": "pencil-alt"})
@active_required
@login_required
@permission_required('preferences.delete_priceprofile')
def delete_price_profile(request,pk):
"""
Delete a :class:`~preferences.models.PriceProfile`.
pk
The primary key of the :class:`~preferences.models.PriceProfile` to delete.
"""
price_profile = get_object_or_404(PriceProfile, pk=pk)
message = "Le profil de prix " + price_profile.name + " a bien été supprimé"
2019-08-27 16:51:24 +00:00
price_profile.delete()
2019-06-23 12:53:18 +00:00
messages.success(request, message)
return redirect(reverse('preferences:priceProfilesIndex'))
2019-09-12 07:40:43 +00:00
########## Improvements ##########
@active_required
@login_required
def add_improvement(request):
"""
Display a form to create an improvement. Any logged user can access it
"""
form = ImprovementForm(request.POST or None)
if form.is_valid():
improvement = form.save(commit=False)
improvement.coopeman = request.user
improvement.save()
mail_admins("Nouvelle proposition d'amélioration", "Une nouvelle proposition d'amélioration a été postée (" + improvement.title + ", " + improvement.get_mode_display() + "). Le corps est le suivant : " + improvement.description)
messages.success(request, "Votre proposition a bien été envoyée")
return redirect(reverse('home'))
return render(request, "form.html", {"form": form, "form_title": "Proposition d'amélioration", "form_button": "Envoyer", "form_button_icon": "bug"})
@active_required
@login_required
@permission_required('preferences.view_improvement')
def improvements_index(request):
"""
Display all improvements
"""
2019-09-20 20:01:51 +00:00
todo_improvements = Improvement.objects.filter(done=False).order_by('-date')
done_improvements = Improvement.objects.filter(done=True).order_by('-date')
2019-09-12 07:40:43 +00:00
return render(request, "preferences/improvements_index.html", {"todo_improvements": todo_improvements, "done_improvements": done_improvements})
@active_required
@login_required
@permission_required('preferences.view_improvement')
@permission_required('preferences.change_improvement')
def improvement_profile(request, pk):
"""
Display an improvement
"""
improvement = get_object_or_404(Improvement, pk=pk)
improvement.seen = 1
improvement.save()
return render(request, "preferences/improvement_profile.html", {"improvement": improvement})
@active_required
@login_required
@permission_required('preferences.change_improvement')
def change_improvement_state(request, pk):
"""
Change done state of an improvement
"""
improvement = get_object_or_404(Improvement, pk=pk)
improvement.done = 1 - improvement.done
improvement.save()
messages.success(request, "L'état a bien été changé")
return redirect(reverse('preferences:improvementsIndex'))
@active_required
@login_required
@permission_required('preferences.delete_improvement')
def delete_improvement(request, pk):
"""
Delete an improvement
"""
improvement = get_object_or_404(Improvement, pk=pk)
improvement.delete()
messages.success(request, "L'amélioration a bien été supprimée.")
return redirect(reverse('preferences:improvementsIndex'))