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)') }} +
+ {{ _('(optional)') }} +
+ {% if collection %} +
+ {{ _('Collection Icon') }} + {% if collection.icontype %} + {{ _('Reset') }} + {% endif %} +
+ {% endif %} + {{ form.errors['icon']|safe }} + {{ form.icon|safe }} -
- {% if collection %} - - {% endif %} - {{ form.errors['icon']|safe }} - {{ form.icon|safe }} -

- {{ _('PNG and JPG supported. Image will be resized to 32x32.') }} -

+

+ {{ _('PNG and JPG supported. Image will be resized to 32x32.') }} +

+
- + diff --git a/apps/bandwagon/urls.py b/apps/bandwagon/urls.py index 29c0218..f3c8d26 100644 --- a/apps/bandwagon/urls.py +++ b/apps/bandwagon/urls.py @@ -16,6 +16,7 @@ detail_urls = patterns('', name='collections.vote'), url('^edit/', include(edit_urls)), url('^delete$', views.delete, name='collections.delete'), + url('^delete_icon$', views.delete_icon, name='collections.delete_icon'), url('^(?Padd|remove)$', views.collection_alter, name='collections.alter'), url('^watch$', views.watch, name='collections.watch'), diff --git a/apps/bandwagon/views.py b/apps/bandwagon/views.py index 88eb744..0e9a0e5 100644 --- a/apps/bandwagon/views.py +++ b/apps/bandwagon/views.py @@ -1,4 +1,5 @@ import functools +import os from django import http from django.db.models import Q @@ -22,7 +23,7 @@ from translations.query import order_by_translation from users.models import UserProfile from .models import (Collection, CollectionAddon, CollectionWatcher, CollectionVote, SPECIAL_SLUGS) -from . import forms +from . import forms, tasks log = commonware.log.getLogger('z.collections') @@ -478,6 +479,24 @@ def delete(request, username, slug): @write @login_required +@owner_required +@json_view +def delete_icon(request, collection, username, slug): + + log.debug(u"User deleted collection (%s) icon " % slug) + tasks.delete_icon(os.path.join(collection.get_img_dir(), + '%d.png' % collection.id)) + + collection.icontype = '' + collection.save() + + if request.is_ajax(): + return {'icon': collection.icon_url} + else: + messages.success(request, _('Icon Deleted')) + return redirect(collection.edit_url()) + +@login_required @post_required @json_view def watch(request, username, slug): diff --git a/locale b/locale -Subproject d757b7433cfe9986ba0a05aa2dc9e87b2261959 +Subproject b46512ee0abb7fb97acb78848a975fdef7f6f13 diff --git a/media/css/zamboni/zamboni.css b/media/css/zamboni/zamboni.css index 0525330..0b95b38 100644 --- a/media/css/zamboni/zamboni.css +++ b/media/css/zamboni/zamboni.css @@ -2527,17 +2527,33 @@ label > .optional { width: 100%; } +#collection_listed { + margin-bottom: 1.5em; +} + +#collection_listed ul label { + font-weight: normal; +} + #icon_upload { - overflow:auto; - padding-top:5px; + padding-top: 5px; } #icon_upload .icon_preview { - -moz-border-radius:5px 5px 5px 5px; - border:1px solid #C1E0EA; - float:left; - margin-right:10px; - padding:8px; + float: left; + text-align: center; + margin-right: 10px; + line-height: 1em; +} + +#icon_upload .icon_preview img { + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + border: 1px solid #C1E0EA; + padding: 8px; + width: 32px; + height: 32px; + display: block; } #ajax_collections_list { diff --git a/media/js/zamboni/collections.js b/media/js/zamboni/collections.js index a6137fb..e1ed9e3 100644 --- a/media/js/zamboni/collections.js +++ b/media/js/zamboni/collections.js @@ -498,6 +498,17 @@ if ($('body.collections-contributors')) { }); } +$(document).ready(function() { + + $('#remove_icon').click(function(){ + $.post($(this).attr('href'), {}, function(d){ + $('#icon_upload .icon_preview img').attr('src', d.icon); + }); + $(this).hide(); + return false; + }); + +}); $(document).ready(function () { -- cgit v0.9.1