From a1d6b3c463812f4c82c49dfd554fca07214a3ca9 Mon Sep 17 00:00:00 2001
From: Gregory Koberger
Date: Thu, 02 Sep 2010 01:48:19 +0000
Subject: Added remove link for collection icon (bug 592430)
aka my foray into python
---
diff --git a/apps/bandwagon/forms.py b/apps/bandwagon/forms.py
index a3c9340..7ce311a 100644
--- a/apps/bandwagon/forms.py
+++ b/apps/bandwagon/forms.py
@@ -186,12 +186,13 @@ class CollectionForm(ModelForm):
if default_locale:
c.default_locale = default_locale
+ if icon:
+ c.icontype = 'image/png'
+
c.save()
if icon:
- c.icontype = 'image/png'
- dirname = os.path.join(settings.COLLECTIONS_ICON_PATH,
- str(c.id / 1000), )
+ dirname = c.get_img_dir()
destination = os.path.join(dirname, '%d.png' % c.id)
tmp_destination = os.path.join(dirname,
@@ -208,6 +209,7 @@ class CollectionForm(ModelForm):
tasks.resize_icon.delay(tmp_destination, destination)
c.save()
+
return c
class Meta:
diff --git a/apps/bandwagon/models.py b/apps/bandwagon/models.py
index 51036df..9678234 100644
--- a/apps/bandwagon/models.py
+++ b/apps/bandwagon/models.py
@@ -1,4 +1,4 @@
-import collections
+import collections, os
from datetime import datetime
import hashlib
import time
@@ -169,6 +169,9 @@ class Collection(amo.models.ModelBase):
else:
return '/collection/%s' % self.url_slug
+ def get_img_dir(self):
+ return os.path.join(settings.COLLECTIONS_ICON_PATH, str(self.id / 1000))
+
def upvote_url(self):
return reverse('collections.vote',
args=[self.author_username, self.slug, 'up'])
@@ -189,6 +192,10 @@ class Collection(amo.models.ModelBase):
return reverse('collections.delete',
args=[self.author_username, self.slug])
+ def delete_icon_url(self):
+ return reverse('collections.delete_icon',
+ args=[self.author_username, self.slug])
+
def share_url(self):
return reverse('collections.share',
args=[self.author_username, self.slug])
diff --git a/apps/bandwagon/tasks.py b/apps/bandwagon/tasks.py
index a51c33b..aafae43 100644
--- a/apps/bandwagon/tasks.py
+++ b/apps/bandwagon/tasks.py
@@ -2,6 +2,7 @@ import logging
import math
import os
+from django.conf import settings
from django.db.models import Count
from celeryutils import task
@@ -48,6 +49,20 @@ def resize_icon(src, dst):
except Exception, e:
log.error("Error saving collection icon: %s" % e)
+@task
+def delete_icon(dst):
+ log.info('[1@None] Deleting icon: %s.' % dst)
+
+ if not dst.startswith(settings.COLLECTIONS_ICON_PATH):
+ log.error("Someone tried deleting something they shouldn't: %s"
+ % dst)
+ return
+
+ try:
+ os.remove(dst)
+ except Exception, e:
+ log.error("Error deleting icon: %s" % e)
+
@task
def collection_meta(*ids, **kw):
diff --git a/apps/bandwagon/templates/bandwagon/includes/addedit.html b/apps/bandwagon/templates/bandwagon/includes/addedit.html
index dec6df7..5a47825 100644
--- a/apps/bandwagon/templates/bandwagon/includes/addedit.html
+++ b/apps/bandwagon/templates/bandwagon/includes/addedit.html
@@ -22,23 +22,32 @@
{{ _('(optional)') }}
{{ form.description|safe }}
-
- {{ form.listed|safe }}
+ {{ form.icon|safe }}
+
+
+ {{ form.listed|safe }}
+
-
- {{ _('(optional)') }}
+