add a check to limit picture uploads
with the option max_pictures it's now possible to limit the amount of pictures a user is allowed to upload to an article.
This commit is contained in:
parent
fabdc48492
commit
35de9bf7a5
|
@ -7,6 +7,8 @@ from webshop.models import (Article, Order, OrderPosition,
|
|||
Person, City, Picture, OrderOfGoods,
|
||||
Category, Option)
|
||||
|
||||
from webshop.forms import PictureForm
|
||||
|
||||
|
||||
class PersonInline(admin.StackedInline):
|
||||
model = Person
|
||||
|
@ -14,9 +16,15 @@ class PersonInline(admin.StackedInline):
|
|||
verbose_name_plural = 'person'
|
||||
|
||||
|
||||
class PictureAdmin(admin.ModelAdmin):
|
||||
form = PictureForm
|
||||
ordering = ('name',)
|
||||
list_display = ('name', 'article',)
|
||||
|
||||
|
||||
class PictureInline(admin.StackedInline):
|
||||
model = Picture
|
||||
can_delete = False
|
||||
form = PictureForm
|
||||
verbose_name_plural = 'pictures'
|
||||
|
||||
|
||||
|
@ -74,6 +82,7 @@ admin.site.register(User, UserAdmin)
|
|||
|
||||
admin.site.register(Article, ArticleAdmin)
|
||||
admin.site.register(Order, OrderAdmin)
|
||||
admin.site.register(Picture, PictureAdmin)
|
||||
admin.site.register(City)
|
||||
admin.site.register(OrderOfGoods, OrderOfGoodsAdmin)
|
||||
admin.site.register(Category)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from django import forms
|
||||
from webshop.models import Salutation, City
|
||||
from webshop.models import Salutation, City, Picture, Article, Option
|
||||
|
||||
|
||||
class RegistrationForm(forms.Form):
|
||||
|
@ -22,3 +22,32 @@ class RegistrationForm(forms.Form):
|
|||
raise forms.ValidationError(
|
||||
"The zip code and the city don't match.")
|
||||
return city
|
||||
|
||||
|
||||
class PictureForm(forms.ModelForm):
|
||||
def max_pictures(self):
|
||||
try:
|
||||
option = Option.objects.get(name='max_pictures')
|
||||
if option.enabled:
|
||||
return option.value
|
||||
else:
|
||||
return False
|
||||
except:
|
||||
return False
|
||||
|
||||
def count_pictures(self, _article):
|
||||
count = Picture.objects.filter(article=_article.id).count()
|
||||
return count
|
||||
|
||||
def clean(self):
|
||||
article = self.cleaned_data.get('article')
|
||||
print(self.max_pictures())
|
||||
if self.max_pictures():
|
||||
if (self.count_pictures(article) >= self.max_pictures()):
|
||||
raise forms.ValidationError("Only " + str(self.max_pictures())
|
||||
+ " pictures per article allowed.")
|
||||
return self.cleaned_data
|
||||
|
||||
class Meta:
|
||||
model = Picture
|
||||
fields = ['name', 'article', 'image']
|
||||
|
|
Loading…
Reference in New Issue