comparison gpp/bio/models.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents 152d77265da6
children 8c1832b9d815
comparison
equal deleted inserted replaced
203:40e5903903e1 204:b4305e18d3af
20 (STA_RESIGNED, "Resigned"), 20 (STA_RESIGNED, "Resigned"),
21 (STA_REMOVED, "Removed"), 21 (STA_REMOVED, "Removed"),
22 (STA_SUSPENDED, "Suspended"), 22 (STA_SUSPENDED, "Suspended"),
23 (STA_SPAMMER, "Spammer"), 23 (STA_SPAMMER, "Spammer"),
24 ) 24 )
25
26
27 class Badge(models.Model):
28 """This model represents badges that users can earn."""
29 image = models.ImageField(upload_to='badges')
30 name = models.CharField(max_length=64)
31 description = models.TextField(blank=True)
32 order = models.IntegerField()
33 numeric_id = models.IntegerField(db_index=True)
34
35 class Meta:
36 ordering = ('order', )
37
38 def __unicode__(self):
39 return self.name
40
41 def get_absolute_url(self):
42 return self.image.url
43
44 def html(self):
45 """Returns a HTML img tag representation of the badge."""
46 if self.image:
47 return u'<img src="%s" alt="%s" title="%s" />' % (
48 self.get_absolute_url(), self.name, self.name)
49 return u''
50 html.allow_tags = True
51
25 52
26 def avatar_file_path_for_user(username, filename): 53 def avatar_file_path_for_user(username, filename):
27 return os.path.join(settings.AVATAR_DIR, 'users', username, filename) 54 return os.path.join(settings.AVATAR_DIR, 'users', username, filename)
28 55
29 def avatar_file_path(instance, filename): 56 def avatar_file_path(instance, filename):
50 use_24_time = models.BooleanField(default=False) 77 use_24_time = models.BooleanField(default=False)
51 forum_post_count = models.IntegerField(default=0) 78 forum_post_count = models.IntegerField(default=0)
52 status = models.IntegerField(default=STA_ACTIVE, 79 status = models.IntegerField(default=STA_ACTIVE,
53 choices=USER_STATUS_CHOICES) 80 choices=USER_STATUS_CHOICES)
54 status_date = models.DateTimeField(auto_now_add=True) 81 status_date = models.DateTimeField(auto_now_add=True)
82 badges = models.ManyToManyField(Badge, through="BadgeOwnership")
55 83
56 def __unicode__(self): 84 def __unicode__(self):
57 return self.user.username 85 return self.user.username
58 86
59 class Meta: 87 class Meta:
68 96
69 @models.permalink 97 @models.permalink
70 def get_absolute_url(self): 98 def get_absolute_url(self):
71 return ('bio-view_profile', (), {'username': self.user.username}) 99 return ('bio-view_profile', (), {'username': self.user.username})
72 100
101 def badge_ownership(self):
102 if hasattr(self, '_badges'):
103 return self._badges
104 self._badges = BadgeOwnership.objects.filter(profile=self).select_related(
105 "badge")
106 return self._badges
107
73 108
74 class UserProfileFlag(models.Model): 109 class UserProfileFlag(models.Model):
75 """This model represents a user flagging a profile as inappropriate.""" 110 """This model represents a user flagging a profile as inappropriate."""
76 user = models.ForeignKey(auth.models.User) 111 user = models.ForeignKey(auth.models.User)
77 profile = models.ForeignKey(UserProfile) 112 profile = models.ForeignKey(UserProfile)
85 ordering = ('flag_date', ) 120 ordering = ('flag_date', )
86 121
87 def get_profile_url(self): 122 def get_profile_url(self):
88 return '<a href="%s">Profile</a>' % self.profile.get_absolute_url() 123 return '<a href="%s">Profile</a>' % self.profile.get_absolute_url()
89 get_profile_url.allow_tags = True 124 get_profile_url.allow_tags = True
125
126
127 class BadgeOwnership(models.Model):
128 """This model represents the ownership of badges by users."""
129 profile = models.ForeignKey(UserProfile)
130 badge = models.ForeignKey(Badge)
131 count = models.IntegerField(default=1)
132
133 class Meta:
134 verbose_name_plural = "badge ownership"
135 ordering = ('badge__order', )
136
137 def __unicode__(self):
138 if self.count == 1:
139 return u"%s owns 1 %s" % (self.profile.user.username,
140 self.badge.name)
141 else:
142 return u"%s owns %d %s badges" % (self.profile.user.username,
143 self.count, self.badge.name)
144
145 def badge_count_str(self):
146 if self.count == 1:
147 return u"1 %s" % self.badge.name
148 return u"%d %ss" % (self.count, self.badge.name)