# HG changeset patch # User Brian Neal # Date 1449802670 21600 # Node ID a5ebc74dc3f341d21140f329a8c1e6bcc086e32d # Parent 82f1f6f905eba5e7bc2abba4b223fa2e00a36d8d Perform image_check on downloads. diff -r 82f1f6f905eb -r a5ebc74dc3f3 downloads/forms.py --- a/downloads/forms.py Wed Dec 09 21:16:04 2015 -0600 +++ b/downloads/forms.py Thu Dec 10 20:57:50 2015 -0600 @@ -5,6 +5,9 @@ from django import forms +from core.html import ImageCheckError +from core.html import image_check +from core.markup import site_markup from downloads.models import PendingDownload from downloads.models import AllowedExtension @@ -16,6 +19,20 @@ description = forms.CharField(required=False, widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) + def clean_description(self): + description = self.cleaned_data['description'].strip() + self.html = None + if not description: + raise forms.ValidationError("Please enter a description") + + self.html = site_markup(description) + try: + image_check(self.html) + except ImageCheckError as ex: + raise forms.ValidationError(str(ex)) + + return description + def clean_file(self): file = self.cleaned_data['file'] ext = os.path.splitext(file.name)[1] diff -r 82f1f6f905eb -r a5ebc74dc3f3 downloads/models.py --- a/downloads/models.py Wed Dec 09 21:16:04 2015 -0600 +++ b/downloads/models.py Thu Dec 10 20:57:50 2015 -0600 @@ -69,6 +69,18 @@ except OSError: return '?' + def save(self, *args, **kwargs): + if not self.pk: + self.date_added = datetime.datetime.now() + self.update_date = self.date_added + else: + self.update_date = datetime.datetime.now() + + self.html = kwargs.pop('html', None) + if not self.html and self.description: + self.html = site_markup(self.description) + super(DownloadBase, self).save(*args, **kwargs) + class PendingDownload(DownloadBase): """This model represents pending downloads created by users. These pending @@ -81,16 +93,6 @@ def __unicode__(self): return self.title - def save(self, *args, **kwargs): - if not self.pk: - self.date_added = datetime.datetime.now() - self.update_date = self.date_added - else: - self.update_date = datetime.datetime.now() - - self.html = site_markup(self.description) - super(PendingDownload, self).save(*args, **kwargs) - class Download(DownloadBase): """Model to represent a download.""" @@ -110,16 +112,6 @@ def get_absolute_url(self): return ('downloads-details', [str(self.id)]) - def save(self, *args, **kwargs): - if not self.pk: - self.date_added = datetime.datetime.now() - self.update_date = self.date_added - else: - self.update_date = datetime.datetime.now() - - self.html = site_markup(self.description) - super(Download, self).save(*args, **kwargs) - def vote(self, vote_value): """receives a vote_value and updates internal score accordingly""" total_score = self.average_score * self.total_votes diff -r 82f1f6f905eb -r a5ebc74dc3f3 downloads/views.py --- a/downloads/views.py Wed Dec 09 21:16:04 2015 -0600 +++ b/downloads/views.py Thu Dec 10 20:57:50 2015 -0600 @@ -162,7 +162,7 @@ dl = form.save(commit=False) dl.user = request.user dl.ip_address = request.META.get('REMOTE_ADDR', None) - dl.save() + dl.save(html=form.html) email_admins('New download for approval', """Hello, A user has uploaded a new download for your approval.