Commits

Paul McLanahan  committed 42aac7d

Moved badge registration to a metaclass to avoid reliance on class decorator.
Cleaned up some imports and code style issues.

  • Participants
  • Parent commits 73512b9

Comments (0)

Files changed (5)

File badges/admin.py

 from django.contrib import admin
+
 from badges.models import Badge
 
 class BadgeAdmin(admin.ModelAdmin):

File badges/models.py

-from django.db import models
+from datetime import datetime
+
 from django.contrib.auth.models import User
 from django.core.urlresolvers import reverse
+from django.db import models
 
-from signals import badge_awarded
+from badges.signals import badge_awarded
 from badges.managers import BadgeManager
 
 LEVEL_CHOICES = (
     badge = models.ForeignKey(Badge)
     user = models.ForeignKey(User)
     
-    created = models.DateTimeField(auto_now_add=True)
+    created = models.DateTimeField(default=datetime.now)

File badges/tests.py

 from django.test import TestCase
 from django.contrib.auth.models import User
 
-from utils import MetaBadge, registered_badges
-from utils import register as register_badge
-from signals import badge_awarded
-from models import Badge
+from badges.utils import MetaBadge, registered_badges
+from badges.utils import register as register_badge
+from badges.signals import badge_awarded
+from badges.models import Badge
 
 class BadgeTests(TestCase):
     def setUp(self):
                 return instance
             
             def check_email(self, instance):
-                return instance.email
+                return bool(instance.email)
             
         self.meta_badge = YouveGotMail
     
     def test_badge_creation(self):
-        register_badge(self.meta_badge)
         badge = Badge.objects.get(id=self.meta_badge.id)
         self.assertTrue(isinstance(badge.meta_badge, self.meta_badge))
         
-    def test_badge_registration_decorator(self):
-        meta_badge = register_badge(self.meta_badge)
-        self.assertTrue(meta_badge is self.meta_badge)
-        
+    def test_badge_registration(self):
         meta_badge_instance = registered_badges[self.meta_badge.id]
         self.assertTrue(isinstance(meta_badge_instance, self.meta_badge))
         
         self.assertTrue(registered_badges[meta_badge.id] is meta_badge_instance)
         
     def test_badge_earned_signal(self):
-        register_badge(self.meta_badge)
         test_self = self
         
         signal_handler_kwargs = {}

File badges/urls.py

 from django.conf.urls.defaults import *
-import views
+
+from badges import views
 
 urlpatterns = patterns('',
     url(r'^$', views.overview, name="badges_overview"),

File badges/utils.py

+from django.core.urlresolvers import reverse
+from django.db import models
 from django.db.models.signals import post_save
-from django.db import models
-from django.core.urlresolvers import reverse
-from models import Badge as BadgeModel
-from models import BadgeToUser
+
+from badges.models import Badge as BadgeModel
+from badges.models import BadgeToUser
+
+
+registered_badges = {}
+
+
+class MetaBadgeMeta(type):
+    
+    def __new__(cls, name, bases, attrs):
+        new_badge = super(MetaBadgeMeta, cls).__new__(cls, name, bases, attrs)
+        parents = [b for b in bases if isinstance(b, MetaBadgeMeta)]
+        if not parents:
+            return new_badge
+        return register(new_badge)
 
 
 class MetaBadge(object):
+    __metaclass__ = MetaBadgeMeta
+    
     one_time_only = False
     model = models.Model
     
             user = self.get_user(instance)
             self.badge.award_to(user)
 
-registered_badges = {}
-
-def register(badge): 
-    if not registered_badges.has_key(badge.id):
+def register(badge):
+    if badge.id not in registered_badges:
         registered_badges[badge.id] = badge()
-    return badge
-    
+    return badge