changeset 1023:a5ebc74dc3f3

Perform image_check on downloads.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 Dec 2015 20:57:50 -0600
parents 82f1f6f905eb
children 6164cc091649
files downloads/forms.py downloads/models.py downloads/views.py
diffstat 3 files changed, 30 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- 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]
--- 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
--- 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.