Commits

Lee Begg  committed ea307ff

Work with Django 1.4 and generic relationships

To work with Django 1.4, removed the setup of a message using
user.message_set that doesn't exist in 1.4

Generic relationships need a integer id/pk to work. Moved id string of the
name to name (unique and indexed), leaving the id to be integer primary key.

Tests pass and using the app appears to work fine.

  • Participants
  • Parent commits 07526b7

Comments (0)

Files changed (6)

File badges/admin.py

 
 class BadgeAdmin(admin.ModelAdmin):
     fields = ('icon',)
-    list_display = ('id','level')
+    list_display = ('name','level')
 
 admin.site.register(Badge, BadgeAdmin)

File badges/managers.py

 class BadgeManager(models.Manager):
     def active(self):
         import badges
-        return self.get_query_set().filter(id__in=badges.registered_badges.keys())
+        return self.get_query_set().filter(name__in=badges.registered_badges.keys())
         

File badges/models.py

     )
 
 class Badge(models.Model):
-    id = models.CharField(max_length=255, primary_key=True)
+    #id = models.CharField(max_length=255, primary_key=True)
+    name = models.CharField(max_length=255, unique=True, db_index=True)
     user = models.ManyToManyField(User, related_name="badges", through='BadgeToUser')
     level = models.CharField(max_length=1, choices=LEVEL_CHOICES)
     
     @property
     def meta_badge(self):
         from utils import registered_badges
-        return registered_badges[self.id]
+        return registered_badges[self.name]
     
     @property
     def title(self):
         return u"%s" % self.title
     
     def get_absolute_url(self):
-        return reverse('badge_detail', kwargs={'slug': self.id})
+        return reverse('badge_detail', kwargs={'slug': self.name})
     
     def award_to(self, user):
         has_badge = self in user.badges.all()
                 
         badge_awarded.send(sender=self.meta_badge, user=user, badge=self)
         
-        message_template = "You just got the %s Badge!"
-        user.message_set.create(message = message_template % self.title)
-        
         return BadgeToUser.objects.filter(badge=self, user=user).count()
 
     def number_awarded(self, user_or_qs=None):

File badges/utils.py

     def _keep_badge_updated(self):
         if getattr(self, 'badge', False):
             return False
-        badge, created = BadgeModel.objects.get_or_create(id=self.id)
+        badge, created = BadgeModel.objects.get_or_create(name=self.id)
         if badge.level != self.level:
             badge.level = self.level
             badge.save()

File badges/views.py

     return render_to_response("badges/overview.html", context, context_instance=RequestContext(request))
 
 def detail(request, slug, extra_context={}):
-    badge = get_object_or_404(Badge, id=slug)
+    badge = get_object_or_404(Badge, name=slug)
     users = badge.user.all()
     
     context = locals()

File test_project/badges_tests/tests.py

         self.meta_badge = YouveGotMail
     
     def test_badge_creation(self):
-        badge = Badge.objects.get(id=self.meta_badge.id)
+        badge = Badge.objects.get(name=self.meta_badge.id)
         self.assertTrue(isinstance(badge.meta_badge, self.meta_badge))
         
     def test_badge_registration(self):
         self.assertTrue(registered_badges[meta_badge.id] is meta_badge_instance)
 
     def test_badge_progress(self):
-        badge = Badge.objects.get(id=self.meta_badge.id)
+        badge = Badge.objects.get(name=self.meta_badge.id)
 
         user = User(username='zodiac', first_name='john', last_name='doe')
         user.save()
         self.assertEqual(signal_handler_kwargs.get('badge'), Badge.objects.all()[0])
 
     def test_template_tags(self):
-        badge = Badge.objects.get(id=self.meta_badge.id)
+        badge = Badge.objects.get(name=self.meta_badge.id)
 
         self.assertEqual(level_count(Badge.objects.all(), '1'), 1)
         self.assertEqual(level_count(Badge.objects.all(), '2'), 0)