forked from DGNum/gestioCOF
Ajout nouvelle catégorie/article + auth/article
- Lors de la création ou modification d'un article, il est possible de sélectionner une catégorie existante ou d'en créer une nouvelle. - Autorisations pour la création/modif d'article prises en compte correctement. Si l'utilisateur identifié n'a pas la permission add(ou change)_accoount, input password apparait et l'utilisateur est identifié temporairement pour la validation de l'ajout/modif.
This commit is contained in:
parent
a6b961d2ab
commit
74c3f07c66
4 changed files with 32 additions and 5 deletions
|
@ -5,7 +5,7 @@ from django.contrib.contenttypes.models import ContentType
|
|||
from django.contrib.admin.widgets import FilteredSelectMultiple
|
||||
from django.forms import modelformset_factory
|
||||
from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
|
||||
CheckoutStatement)
|
||||
CheckoutStatement, ArticleCategory)
|
||||
from gestioncof.models import CofProfile
|
||||
|
||||
# -----
|
||||
|
@ -129,6 +129,24 @@ class CheckoutStatementForm(forms.ModelForm):
|
|||
# -----
|
||||
|
||||
class ArticleForm(forms.ModelForm):
|
||||
category_new = forms.CharField(
|
||||
max_length=45,
|
||||
required = False)
|
||||
category = forms.ModelChoiceField(
|
||||
queryset = ArticleCategory.objects.all(),
|
||||
required = False)
|
||||
|
||||
def clean(self):
|
||||
category = self.cleaned_data.get('category')
|
||||
category_new = self.cleaned_data.get('category_new')
|
||||
|
||||
if not category and not category_new:
|
||||
raise ValidationError('Sélectionnez une catégorie ou créez en une')
|
||||
elif not category:
|
||||
category, _ = ArticleCategory.objects.get_or_create(name=category_new)
|
||||
self.cleaned_data['category'] = category
|
||||
super(ArticleForm, self).clean()
|
||||
|
||||
class Meta:
|
||||
model = Article
|
||||
fields = ['name', 'is_sold', 'price', 'stock', 'category']
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
<form submit="" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
{% if not perms.kfet.add_article %}
|
||||
<input type="password" name="KFETPASSWORD">
|
||||
{% endif %}
|
||||
<input type="submit" value="Enregistrer">
|
||||
</form>
|
||||
|
||||
|
|
|
@ -8,6 +8,9 @@
|
|||
<form action="" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
{% if not perms.kfet.change_article %}
|
||||
<input type="password" name="KFETPASSWORD">
|
||||
{% endif %}
|
||||
<input type="submit" value="Mettre à jour">
|
||||
</form>
|
||||
|
||||
|
|
|
@ -432,8 +432,10 @@ class ArticleCreate(SuccessMessageMixin, CreateView):
|
|||
# Surcharge de la validation
|
||||
def form_valid(self, form):
|
||||
# Checking permission
|
||||
if not self.request.user.has_perm('add_article'):
|
||||
raise PermissionDenied
|
||||
if not self.request.user.has_perm('kfet.add_article'):
|
||||
form.add_error(None, 'Permission refusée')
|
||||
return self.form_invalid(form)
|
||||
|
||||
# Creating
|
||||
return super(ArticleCreate, self).form_valid(form)
|
||||
|
||||
|
@ -455,8 +457,9 @@ class ArticleUpdate(UpdateView):
|
|||
# Surcharge de la validation
|
||||
def form_valid(self, form):
|
||||
# Checking permission
|
||||
if not self.request.user.has_perm('change_article'):
|
||||
raise PermissionDenied
|
||||
if not self.request.user.has_perm('kfet.change_article'):
|
||||
form.add_error(None, 'Permission refusée')
|
||||
return self.form_invalid(form)
|
||||
# Updating
|
||||
return super(ArticleUpdate, self).form_valid(form)
|
||||
|
||||
|
|
Loading…
Reference in a new issue