Sélection fournisseurs depuis détail d'un article

This commit is contained in:
Aurélien Delobelle 2016-08-27 00:14:49 +02:00
parent ab9bbac34f
commit cd436faf9b

View file

@ -548,11 +548,14 @@ class ArticleCreate(SuccessMessageMixin, CreateView):
form.add_error(None, 'Permission refusée') form.add_error(None, 'Permission refusée')
return self.form_invalid(form) return self.form_invalid(form)
# Save ici pour save le manytomany suppliers
article = form.save() article = form.save()
# Save des suppliers déjà existant
for supplier in form.cleaned_data['suppliers']: for supplier in form.cleaned_data['suppliers']:
SupplierArticle.objects.create( SupplierArticle.objects.create(
article = article, supplier = supplier) article = article, supplier = supplier)
# Nouveau supplier
supplier_new = form.cleaned_data['supplier_new'].strip() supplier_new = form.cleaned_data['supplier_new'].strip()
if supplier_new: if supplier_new:
supplier, created = Supplier.objects.get_or_create( supplier, created = Supplier.objects.get_or_create(
@ -585,6 +588,30 @@ class ArticleUpdate(SuccessMessageMixin, UpdateView):
if not self.request.user.has_perm('kfet.change_article'): if not self.request.user.has_perm('kfet.change_article'):
form.add_error(None, 'Permission refusée') form.add_error(None, 'Permission refusée')
return self.form_invalid(form) return self.form_invalid(form)
# Save ici pour save le manytomany suppliers
article = form.save()
# Save des suppliers déjà existant
for supplier in form.cleaned_data['suppliers']:
if supplier not in article.suppliers.all():
SupplierArticle.objects.create(
article = article, supplier = supplier)
# On vire les suppliers désélectionnés
for supplier in article.suppliers.all():
if supplier not in form.cleaned_data['suppliers']:
SupplierArticle.objects.filter(
article = article, supplier = supplier).delete()
# Nouveau supplier
supplier_new = form.cleaned_data['supplier_new'].strip()
if supplier_new:
supplier, created = Supplier.objects.get_or_create(
name=supplier_new)
if created:
SupplierArticle.objects.create(
article = article, supplier = supplier)
# Updating # Updating
return super(ArticleUpdate, self).form_valid(form) return super(ArticleUpdate, self).form_valid(form)