Ajout lecture/édition des paramètres généraux

This commit is contained in:
Aurélien Delobelle 2016-08-22 03:57:13 +02:00
parent b628808493
commit ee998f8a24
6 changed files with 114 additions and 3 deletions

View file

@ -6,7 +6,7 @@ from django.contrib.admin.widgets import FilteredSelectMultiple
from django.forms import modelformset_factory from django.forms import modelformset_factory
from django.utils import timezone from django.utils import timezone
from kfet.models import (Account, Checkout, Article, OperationGroup, Operation, from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
CheckoutStatement, ArticleCategory) CheckoutStatement, ArticleCategory, Settings)
from gestioncof.models import CofProfile from gestioncof.models import CofProfile
# ----- # -----
@ -236,3 +236,38 @@ KPsulOperationFormSet = modelformset_factory(
can_delete = True, can_delete = True,
extra = 0, extra = 0,
min_num = 1, validate_min = True) min_num = 1, validate_min = True)
# -----
# Settings forms
# -----
class SettingsForm(forms.ModelForm):
class Meta:
model = Settings
fields = ['value_decimal', 'value_account', 'value_duration']
def clean(self):
name = self.instance.name
value_decimal = self.cleaned_data.get('value_decimal')
value_account = self.cleaned_data.get('value_account')
value_duration = self.cleaned_data.get('value_duration')
type_decimal = ['SUBVENTION_COF', 'ADDCOST_AMOUNT', 'OVERDRAFT_AMOUNT']
type_account = ['ADDCOST_FOR']
type_duration = ['OVERDRAFT_DURATION', 'CANCEL_DURATION']
self.cleaned_data['name'] = name
if name in type_decimal:
if not value_decimal:
raise ValidationError('Renseignez une valeur décimale')
self.cleaned_data['value_account'] = None
self.cleaned_data['value_duration'] = None
elif name in type_account:
self.cleaned_data['value_decimal'] = None
self.cleaned_data['value_duration'] = None
elif name in type_duration:
if not value_duration:
raise ValidationError('Renseignez une durée')
self.cleaned_data['value_decimal'] = None
self.cleaned_data['value_account'] = None
super(SettingsForm, self).clean()

View file

@ -37,6 +37,9 @@
{% if user.username != 'kfet_genericteam' %} {% if user.username != 'kfet_genericteam' %}
<li><a href="{% url 'kfet.login.genericteam' %}" target="_blank" id="genericteam">Connexion standard</a></li> <li><a href="{% url 'kfet.login.genericteam' %}" target="_blank" id="genericteam">Connexion standard</a></li>
{% endif %} {% endif %}
{% if perms.kfet.change_settings %}
<li><a href="{% url 'kfet.settings' %}">Paramètres</a></li>
{% endif %}
</ul> </ul>
</li> </li>
{% endif %} {% endif %}

View file

@ -0,0 +1,24 @@
{% extends 'kfet/base.html' %}
{% block title %}Paramètres{% endblock %}
{% block content-header-title %}Paramètres{% endblock %}
{% block content %}
{% include 'kfet/base_messages.html' %}
<table>
<tr>
<td></td>
<td>Nom</td>
<td>Valeur</td>
</tr>
{% for setting in settings %}
<tr>
<td><a href="{% url 'kfet.settings.update' setting.pk %}">Modifier</a></td>
<td>{{ setting.name }}</td>
<td>{% firstof setting.value_decimal setting.value_duration setting.value_account %}</td>
</tr>
{% endfor %}
</table>
{% endblock %}

View file

@ -0,0 +1,14 @@
{% extends 'kfet/base.html' %}
{% block title %}Modification de {{ settings.name }}{% endblock %}
{% block content-header-title %}Modification de {{ settings.name }}{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Mettre à jour">
</form>
{% endblock %}

View file

@ -118,5 +118,15 @@ urlpatterns = [
url('^k-psul/history$', views.kpsul_history, url('^k-psul/history$', views.kpsul_history,
name = 'kfet.kpsul.history'), name = 'kfet.kpsul.history'),
url('^k-psul/articles_data', views.kpsul_articles_data, url('^k-psul/articles_data', views.kpsul_articles_data,
name = 'kfet.kpsul.articles_data') name = 'kfet.kpsul.articles_data'),
# -----
# Settings urls
url('^settings/$',
permission_required('kfet.change_settings')(views.SettingsList.as_view()),
name = 'kfet.settings'),
url('^settings/(?P<pk>\d+)/edit$',
permission_required('kfet.change_settings')(views.SettingsUpdate.as_view()),
name = 'kfet.settings.update'),
] ]

View file

@ -451,7 +451,7 @@ class ArticleRead(DetailView):
# Article - Update # Article - Update
class ArticleUpdate(UpdateView): class ArticleUpdate(SuccessMessageMixin, UpdateView):
model = Article model = Article
template_name = 'kfet/article_update.html' template_name = 'kfet/article_update.html'
form_class = ArticleRestrictForm form_class = ArticleRestrictForm
@ -868,3 +868,28 @@ def kpsul_articles_data(request):
.values('id', 'name', 'price', 'stock', 'category_id', 'category__name') .values('id', 'name', 'price', 'stock', 'category_id', 'category__name')
.filter(is_sold=True)) .filter(is_sold=True))
return JsonResponse({ 'articles': list(articles) }) return JsonResponse({ 'articles': list(articles) })
# -----
# Settings views
# -----
class SettingsList(ListView):
model = Settings
context_object_name = 'settings'
template_name = 'kfet/settings.html'
class SettingsUpdate(SuccessMessageMixin, UpdateView):
model = Settings
form_class = SettingsForm
template_name = 'kfet/settings_update.html'
success_message = 'Paramètre %(name)s mis à jour'
success_url = reverse_lazy('kfet.settings')
def form_valid(self, form):
# Checking permission
if not self.request.user.has_perm('kfet.change_settings'):
form.add_error(None, 'Permission refusée')
return self.form_invalid(form)
# Creating
return super(SettingsUpdate, self).form_valid(form)