Commit d9a7dc25b22b8eb7763e324185b8ea9d7f1d57d8
1 parent
1d346cf0
Exists in
master
and in
39 other branches
closes #113
Showing
1 changed file
with
13 additions
and
7 deletions
Show diff stats
src/badger/forms.py
| ... | ... | @@ -10,7 +10,7 @@ from .models import Badge |
| 10 | 10 | |
| 11 | 11 | |
| 12 | 12 | class BadgeForm(forms.ModelForm): |
| 13 | - image = forms.ImageField(label=_(u'Image')) | |
| 13 | + image = forms.ImageField(label=_(u'Image'), required=False) | |
| 14 | 14 | |
| 15 | 15 | class Meta: |
| 16 | 16 | model = Badge |
| ... | ... | @@ -19,16 +19,22 @@ class BadgeForm(forms.ModelForm): |
| 19 | 19 | 'value', 'awardees' |
| 20 | 20 | ) |
| 21 | 21 | |
| 22 | + def clean_image(self): | |
| 23 | + if not self.instance.pk and not self.cleaned_data['image']: | |
| 24 | + raise forms.ValidationError(_(u'You must add an Image')) | |
| 25 | + return self.cleaned_data['image'] | |
| 26 | + | |
| 22 | 27 | def save(self, commit=True): |
| 23 | 28 | |
| 24 | 29 | instance = super(BadgeForm, self).save(commit=False) |
| 25 | 30 | |
| 26 | - img = Image.open(self.cleaned_data['image']) | |
| 27 | - img = img.resize((50, 50), Image.ANTIALIAS) | |
| 28 | - f = StringIO.StringIO() | |
| 29 | - img.save(f, 'png') | |
| 30 | - instance.image_base64 = f.getvalue().encode('base64') | |
| 31 | - f.close() | |
| 31 | + if self.cleaned_data['image']: | |
| 32 | + img = Image.open(self.cleaned_data['image']) | |
| 33 | + img = img.resize((50, 50), Image.ANTIALIAS) | |
| 34 | + f = StringIO.StringIO() | |
| 35 | + img.save(f, 'png') | |
| 36 | + instance.image_base64 = f.getvalue().encode('base64') | |
| 37 | + f.close() | |
| 32 | 38 | |
| 33 | 39 | if commit: |
| 34 | 40 | instance.save() | ... | ... |