Permalink
Browse files

support django 2.0

  • Loading branch information...
jiaaro committed Jan 9, 2018
1 parent c186bcb commit 23c3c25a3eebb4cbe6d44fc6eaaa7523956a2b26
Showing with 19 additions and 16 deletions.
  1. +2 −2 badges/migrations/0002_auto_20150513_0429.py
  2. +17 −14 badges/models.py
@@ -27,8 +27,8 @@ class Migration(migrations.Migration):
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('created', models.DateTimeField(default=datetime.datetime.now)),
('badge', models.ForeignKey(to='badges.Badge')),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
('badge', models.ForeignKey(to='badges.Badge', on_delete=models.CASCADE)),
('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE)),
],
),
migrations.AddField(
View
@@ -2,7 +2,10 @@
from datetime import datetime
from django.contrib.auth import get_user_model
from django.core.urlresolvers import reverse
try:
from django.urls import reverse
except ImportError:
from django.core.urlresolvers import reverse
from django.db import models
from django.conf import settings
@@ -23,24 +26,24 @@ class Badge(models.Model):
id = models.CharField(max_length=255, primary_key=True)
user = models.ManyToManyField(settings.AUTH_USER_MODEL, related_name="badges", through='BadgeToUser')
level = models.CharField(max_length=1, choices=LEVEL_CHOICES)
icon = models.ImageField(upload_to='badge_images')
objects = BadgeManager()
@property
def meta_badge(self):
from .utils import registered_badges
return registered_badges[self.id]
@property
def title(self):
return str(self.meta_badge.title)
@property
def description(self):
return self.meta_badge.description
def __unicode__(self):
return "%s" % self.title
@@ -49,16 +52,16 @@ def __str__(self):
def get_absolute_url(self):
return reverse('badge_detail', kwargs={'slug': self.id})
def award_to(self, user):
has_badge = self in user.badges.all()
if self.meta_badge.one_time_only and has_badge:
return False
BadgeToUser.objects.create(badge=self, user=user)
badge_awarded.send(sender=self.meta_badge, user=user, badge=self)
return BadgeToUser.objects.filter(badge=self, user=user).count()
def number_awarded(self, user_or_qs=None):
@@ -77,9 +80,9 @@ def number_awarded(self, user_or_qs=None):
class BadgeToUser(models.Model):
badge = models.ForeignKey(Badge)
user = models.ForeignKey(settings.AUTH_USER_MODEL)
badge = models.ForeignKey(Badge, on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
created = models.DateTimeField(default=datetime.now)

0 comments on commit 23c3c25

Please sign in to comment.