diff --git a/badger_multiplayer/admin.py b/badger_multiplayer/admin.py index ac2c0df..7876c30 100644 --- a/badger_multiplayer/admin.py +++ b/badger_multiplayer/admin.py @@ -3,27 +3,33 @@ from django.contrib import admin from django import forms from django.db import models +try: + from funfactory.urlresolvers import reverse +except ImportError, e: + from django.core.urlresolvers import reverse + import badger.models -from .models import (Badge, Nomination) +from .models import (Badge, Award, Nomination) +from badger.admin import (BadgeAdmin, AwardAdmin, show_unicode, + build_related_link) -class BadgerAdmin(admin.ModelAdmin): - list_display = ("title", ) +def award_link(self): + url = reverse('admin:badger_award_change', args=[self.award.id]) + return '%s' % (url, self.award) - filter_horizontal = ('prerequisites', ) - - formfield_overrides = { - models.ManyToManyField: { - "widget": forms.widgets.SelectMultiple(attrs={"size": 25}) - } - } - - -admin.site.register(Badge, BadgerAdmin) +award_link.allow_tags = True +award_link.short_description = 'award' class NominationAdmin(admin.ModelAdmin): - pass + list_display = ('id', show_unicode, award_link, 'accepted', 'nominee', + 'approver', 'creator', 'created', 'modified',) + list_filter = ('accepted',) + search_fields = ('badge__title', 'badge__slug', 'badge__description',) -admin.site.register(Nomination, NominationAdmin) +for x in ((Badge, BadgeAdmin), + (Award, AwardAdmin), + (Nomination, NominationAdmin),): + admin.site.register(*x) diff --git a/badger_multiplayer/forms.py b/badger_multiplayer/forms.py index 5ed5458..551f7ab 100644 --- a/badger_multiplayer/forms.py +++ b/badger_multiplayer/forms.py @@ -12,7 +12,7 @@ except ImportError, e: from django.utils.translation import ugettext_lazy as _ from badger.models import (Award) -from badger.forms import (MyModelForm, MyForm) +from badger.forms import (MyModelForm, MyForm, MultiEmailField) from badger_multiplayer.models import (Badge, Nomination) @@ -20,7 +20,7 @@ class BadgeEditForm(MyModelForm): class Meta: model = Badge - fields = ('title', 'image', 'description',) + fields = ('title', 'image', 'description', 'unique',) required_css_class = "required" error_css_class = "error" @@ -41,9 +41,9 @@ class BadgeNewForm(BadgeEditForm): #if not settings.RECAPTCHA_PRIVATE_KEY: # del self.fields['captcha'] -class BadgeSubmitNominationForm(MyModelForm): - - class Meta: - model = Nomination - fields = ('nominee', ) +class BadgeSubmitNominationForm(MyForm): + """Form to submit badge nominations""" + emails = MultiEmailField(max_emails=10, + help_text="Enter up to 10 email addresses for badge award " + "nominees") diff --git a/badger_multiplayer/models.py b/badger_multiplayer/models.py index 2c761f3..5e13fcc 100644 --- a/badger_multiplayer/models.py +++ b/badger_multiplayer/models.py @@ -75,6 +75,11 @@ class Award(badger.models.Award): class Meta: proxy = True + def delete(self): + """Make sure nominations get deleted along with awards""" + Nomination.objects.filter(award=self).delete() + super(Award, self).delete() + @property def badge(self): """Property that wraps the related badge in a multiplayer upgrade""" diff --git a/badger_multiplayer/views.py b/badger_multiplayer/views.py index af3089a..3470215 100644 --- a/badger_multiplayer/views.py +++ b/badger_multiplayer/views.py @@ -26,10 +26,12 @@ from django.views.generic.list_detail import object_list from django.views.decorators.http import (require_GET, require_POST, require_http_methods) +from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from badger.models import (Award, Progress, + BadgerException, BadgeAlreadyAwardedException, BadgeAwardNotAllowedException) from badger_multiplayer.models import (Badge, Nomination, @@ -115,12 +117,12 @@ def nomination_detail(request, slug, id, format="html"): elif action == 'accept': nomination.accept(request.user) return HttpResponseRedirect(reverse( - 'badger_multiplayer.views.nomination_detail', + 'badger_multiplayer.views.nomination_detail', args=(slug, id))) - return render_to_response('badger_multiplayer/nomination_detail.html', dict( - badge=badge, nomination=nomination, - ), context_instance=RequestContext(request)) + return render_to_response('badger_multiplayer/nomination_detail.html', + dict(badge=badge, nomination=nomination,), + context_instance=RequestContext(request)) @require_http_methods(['GET', 'POST']) @@ -136,12 +138,29 @@ def nominate_for(request, slug): else: form = BadgeSubmitNominationForm(request.POST, request.FILES) if form.is_valid(): - award = badge.nominate_for(form.cleaned_data['nominee'], - request.user) - return HttpResponseRedirect(reverse( - 'badger_multiplayer.views.nomination_detail', - args=(badge.slug, award.id, ))) + emails = form.cleaned_data['emails'] + for email in emails: + users = User.objects.filter(email=email) + if not users: + # TODO: Need a deferred nomination mechanism for + # non-registered users. + pass + else: + nominee = users[0] + try: + award = badge.nominate_for(nominee, request.user) + messages.info(request, + _('Nomination submitted for %s') % email) + except BadgeAlreadyAwardedException, e: + messages.info(request, + _('Badge already awarded to %s') % email) + except Exception, e: + messages.info(request, + _('Nomination failed for %s') % email) - return render_to_response('badger_multiplayer/badge_nominate_for.html', dict( - form=form, badge=badge, - ), context_instance=RequestContext(request)) + return HttpResponseRedirect(reverse('badger.views.detail', + args=(badge.slug,))) + + return render_to_response('badger_multiplayer/badge_nominate_for.html', + dict(form=form, badge=badge,), + context_instance=RequestContext(request))