Permalink
Browse files

Splitting badger app out of badger2 project

  • Loading branch information...
1 parent 446733f commit 2b99b6ac740e0d81addf45dacada246181cb8e89 @lmorchard committed Jul 22, 2011
View
@@ -0,0 +1,2 @@
+*.sw*
+*.pyc
View
File renamed without changes.
View
No changes.
View
No changes.
View
@@ -0,0 +1,62 @@
+import logging
+
+from django.conf import settings
+
+from django import forms
+from django.db import models
+from django.contrib.auth.models import User, AnonymousUser
+
+try:
+ from tower import ugettext_lazy as _
+except ImportError, e:
+ from django.utils.translation import ugettext_lazy as _
+
+from .models import ( Badge, Award, Nomination)
+
+
+class MyModelForm(forms.ModelForm):
+ def as_ul(self):
+ "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
+ return self._html_output(
+ normal_row = u'<li%(html_class_attr)s>%(label)s %(field)s%(help_text)s%(errors)s</li>',
+ error_row = u'<li>%s</li>',
+ row_ender = '</li>',
+ help_text_html = u' <p class="help">%s</p>',
+ errors_on_separate_row = False)
+
+
+class MyForm(forms.Form):
+ def as_ul(self):
+ "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
+ return self._html_output(
+ normal_row = u'<li%(html_class_attr)s>%(label)s %(field)s%(help_text)s%(errors)s</li>',
+ error_row = u'<li>%s</li>',
+ row_ender = '</li>',
+ help_text_html = u' <p class="help">%s</p>',
+ errors_on_separate_row = False)
+
+
+class BadgeEditForm(MyModelForm):
+
+ class Meta:
+ model = Badge
+ fields = ( 'title', 'description', )
+
+ required_css_class = "required"
+ error_css_class = "error"
+
+
+class BadgeNewForm(BadgeEditForm):
+
+ class Meta(BadgeEditForm.Meta):
+ pass
+ #fields = SubmissionEditForm.Meta.fields + ( 'captcha', 'accept_terms', )
+
+ #captcha = ReCaptchaField(label=_("Show us you're human"))
+ #accept_terms = forms.BooleanField(initial=False, required=True)
+
+ def __init__(self, *args, **kwargs):
+ super(BadgeNewForm, self).__init__(*args, **kwargs)
+ #if not settings.RECAPTCHA_PRIVATE_KEY:
+ # del self.fields['captcha']
+
View
@@ -0,0 +1,175 @@
+from django.db import models
+from django.contrib.auth.models import User
+
+from django.template.defaultfilters import slugify
+
+
+def get_permissions_for(self, user):
+ """Mixin method to collect permissions for a model instance"""
+ pre = 'allows_'
+ pre_len = len(pre)
+ methods = ( m for m in dir(self) if m.startswith(pre) )
+ perms = dict(
+ ( m[pre_len:], getattr(self, m)(user) )
+ for m in methods
+ )
+ return perms
+
+class BadgerException(Exception):
+ """General Badger model exception"""
+
+
+class BadgeManager(models.Manager):
+ """Manager for Badge model objects"""
+
+class BadgeException(BadgerException):
+ """Badge model exception"""
+class BadgeAwardNotAllowedException(BadgeException):
+ """Attempt to award a badge not allowed."""
+
+class Badge(models.Model):
+ """Representation of a badge"""
+ objects = BadgeManager()
+
+ title = models.CharField(max_length=255, blank=False, unique=True)
+ slug = models.SlugField(blank=False, unique=True)
+ description = models.TextField(blank=True)
+ creator = models.ForeignKey(User, blank=False)
+ created = models.DateTimeField(auto_now_add=True, blank=False)
+ modified = models.DateTimeField(auto_now=True, blank=False)
+
+ class Meta:
+ unique_together = ('title','slug')
+
+ get_permissions_for = get_permissions_for
+
+ def save(self, **kwargs):
+ """Save the submission, updating slug and screenshot thumbnails"""
+ if not self.slug:
+ self.slug = slugify(self.title)
+ super(Badge,self).save(**kwargs)
+
+ def allows_award_to(self, user):
+ """Is award_to() allowed for this user?"""
+ if user.is_staff or user.is_superuser:
+ return True
+ if user == self.creator:
+ return True
+ return False
+
+ def award_to(self, awarder, awardee, nomination=None):
+ """Award this badge to the awardee on the awarder's behalf, with an
+ optional nomination involved"""
+ if not self.allows_award_to(awarder):
+ raise BadgeAwardNotAllowedException()
+ award = Award(user=awardee, badge=self, creator=awarder,
+ nomination=nomination)
+ award.save()
+ return award
+
+ def is_awarded_to(self, user):
+ """Has this badge been awarded to the user?"""
+ return Award.objects.filter(user=user, badge=self).count() > 0
+
+ def nominate_for(self, nominator, nominee):
+ """Nominate a nominee for this badge on the nominator's behalf"""
+ nomination = Nomination(badge=self, creator=nominator, nominee=nominee)
+ nomination.save()
+ return nomination
+
+ def is_nominated_for(self, user):
+ return Nomination.objects.filter(nominee=user, badge=self).count() > 0
+
+
+class NominationManager(models.Manager):
+ pass
+
+
+class NominationException(BadgerException):
+ """Nomination model exception"""
+class NominationApproveNotAllowedException(NominationException):
+ """Attempt to approve a nomination was disallowed"""
+class NominationAcceptNotAllowedException(NominationException):
+ """Attempt to accept a nomination was disallowed"""
+
+
+class Nomination(models.Model):
+ """Representation of a user nominated by another user for a badge"""
+ objects = NominationManager()
+
+ badge = models.ForeignKey(Badge)
+ nominee = models.ForeignKey(User, related_name="nomination_nominee",
+ blank=False, null=False)
+ accepted = models.BooleanField(default=False)
+ creator = models.ForeignKey(User, related_name="nomination_creator",
+ blank=False, null=False)
+ approver = models.ForeignKey(User, related_name="nomination_approver",
+ blank=True, null=True)
+ created = models.DateTimeField(auto_now_add=True, blank=False)
+ modified = models.DateTimeField(auto_now=True, blank=False)
+
+ get_permissions_for = get_permissions_for
+
+ def allows_approve_by(self, user):
+ if user.is_staff or user.is_superuser:
+ return True
+ if user == self.badge.creator:
+ return True
+ return False
+
+ def approve_by(self, approver):
+ """Approve this nomination.
+ Also awards, if already accepted."""
+ if not self.allows_approve_by(approver):
+ raise NominationApproveNotAllowedException()
+ self.approver = approver
+ self.save()
+ self._award_if_ready()
+
+ def is_approved(self):
+ """Has this nomination been approved?"""
+ return self.approver is not None
+
+ def allows_accept(self, user):
+ if user.is_staff or user.is_superuser:
+ return True
+ if user == self.nominee:
+ return True
+ return False
+
+ def accept(self, user):
+ """Accept this nomination for the nominee.
+ Also awards, if already approved."""
+ if not self.allows_accept(user):
+ raise NominationAcceptNotAllowedException()
+ self.accepted = True
+ self.save()
+ self._award_if_ready()
+
+ def is_accepted(self):
+ """Has this nomination been accepted?"""
+ return self.accepted
+
+ def _award_if_ready(self):
+ """If approved and accepted, award the badge to nominee on behalf of
+ approver."""
+ if self.is_approved() and self.is_accepted():
+ self.badge.award_to(self.approver, self.nominee, self)
+
+
+class AwardManager(models.Manager):
+ pass
+
+
+class Award(models.Model):
+ """Representation of a badge awarded to a user"""
+ objects = AwardManager()
+
+ badge = models.ForeignKey(Badge)
+ user = models.ForeignKey(User, related_name="award_user")
+ nomination = models.ForeignKey(Nomination, blank=True, null=True)
+ creator = models.ForeignKey(User, related_name="award_creator")
+ created = models.DateTimeField(auto_now_add=True, blank=False)
+ modified = models.DateTimeField(auto_now=True, blank=False)
+
+ get_permissions_for = get_permissions_for
@@ -0,0 +1,15 @@
+{% extends "badger/base.html" %}
+
+{% block content %}
+
+<h2>Create a badge</h2>
+
+<form id="create_badge" method="POST" action="">
+ {{ csrf() }}
+ <ul>
+ {{ form.as_ul() }}
+ <li><input type="submit" class="submit" value="Create"></li>
+ </ul>
+</form>
+
+{% endblock %}
@@ -0,0 +1,7 @@
+{% extends "badger/base.html" %}
+
+{% block content %}
+
+<h2>Badge detail</h2>
+
+{% endblock %}
@@ -0,0 +1 @@
+{% extends "base.html" %}
@@ -0,0 +1,8 @@
+{% extends "badger/base.html" %}
+
+{% block content %}
+
+<h1>Badger</h1>
+<p>Welcome to the Badger home page</p>
+
+{% endblock %}
View
No changes.
Oops, something went wrong.

0 comments on commit 2b99b6a

Please sign in to comment.