Mercurial > public > sg101
view gpp/bio/models.py @ 213:65016249bf35
See #31; undid attempts to clear template tag caching when saving news and bulletins, as we don't know the cache key (it is mangled).
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Mon, 10 May 2010 02:58:19 +0000 |
parents | b4305e18d3af |
children | 8c1832b9d815 |
line wrap: on
line source
""" Contains models for the bio application. I would have picked profile for this application, but that is already taken, apparently. """ import os.path from django.db import models from django.contrib import auth from django.conf import settings from django.core.cache import cache from core.markup import SiteMarkup (STA_ACTIVE, STA_RESIGNED, STA_REMOVED, STA_SUSPENDED, STA_SPAMMER) = range(5) USER_STATUS_CHOICES = ( (STA_ACTIVE, "Active"), (STA_RESIGNED, "Resigned"), (STA_REMOVED, "Removed"), (STA_SUSPENDED, "Suspended"), (STA_SPAMMER, "Spammer"), ) class Badge(models.Model): """This model represents badges that users can earn.""" image = models.ImageField(upload_to='badges') name = models.CharField(max_length=64) description = models.TextField(blank=True) order = models.IntegerField() numeric_id = models.IntegerField(db_index=True) class Meta: ordering = ('order', ) def __unicode__(self): return self.name def get_absolute_url(self): return self.image.url def html(self): """Returns a HTML img tag representation of the badge.""" if self.image: return u'<img src="%s" alt="%s" title="%s" />' % ( self.get_absolute_url(), self.name, self.name) return u'' html.allow_tags = True def avatar_file_path_for_user(username, filename): return os.path.join(settings.AVATAR_DIR, 'users', username, filename) def avatar_file_path(instance, filename): return avatar_file_path_for_user(instance.user.username, filename) class UserProfile(models.Model): """model to represent additional information about users""" user = models.ForeignKey(auth.models.User, unique=True) location = models.CharField(max_length=128, blank=True) birthday = models.DateField(blank=True, null=True, help_text='Optional; the year is not shown to others') occupation = models.CharField(max_length=128, blank=True) interests = models.CharField(max_length=255, blank=True) profile_text = models.TextField(blank=True) profile_html = models.TextField(blank=True) hide_email = models.BooleanField(default=True) signature = models.TextField(blank=True) signature_html = models.TextField(blank=True) avatar = models.ImageField(upload_to=avatar_file_path, blank=True) time_zone = models.CharField(max_length=64, blank=True, default='US/Pacific') use_24_time = models.BooleanField(default=False) forum_post_count = models.IntegerField(default=0) status = models.IntegerField(default=STA_ACTIVE, choices=USER_STATUS_CHOICES) status_date = models.DateTimeField(auto_now_add=True) badges = models.ManyToManyField(Badge, through="BadgeOwnership") def __unicode__(self): return self.user.username class Meta: ordering = ('user__username', ) def save(self, *args, **kwargs): sm = SiteMarkup() self.profile_html = sm.convert(self.profile_text) self.signature_html = sm.convert(self.signature) super(UserProfile, self).save(*args, **kwargs) cache.delete('avatar_' + self.user.username) @models.permalink def get_absolute_url(self): return ('bio-view_profile', (), {'username': self.user.username}) def badge_ownership(self): if hasattr(self, '_badges'): return self._badges self._badges = BadgeOwnership.objects.filter(profile=self).select_related( "badge") return self._badges class UserProfileFlag(models.Model): """This model represents a user flagging a profile as inappropriate.""" user = models.ForeignKey(auth.models.User) profile = models.ForeignKey(UserProfile) flag_date = models.DateTimeField(auto_now_add=True) def __unicode__(self): return u"%s's profile flagged by %s" % (self.profile.user.username, self.user.username) class Meta: ordering = ('flag_date', ) def get_profile_url(self): return '<a href="%s">Profile</a>' % self.profile.get_absolute_url() get_profile_url.allow_tags = True class BadgeOwnership(models.Model): """This model represents the ownership of badges by users.""" profile = models.ForeignKey(UserProfile) badge = models.ForeignKey(Badge) count = models.IntegerField(default=1) class Meta: verbose_name_plural = "badge ownership" ordering = ('badge__order', ) def __unicode__(self): if self.count == 1: return u"%s owns 1 %s" % (self.profile.user.username, self.badge.name) else: return u"%s owns %d %s badges" % (self.profile.user.username, self.count, self.badge.name) def badge_count_str(self): if self.count == 1: return u"1 %s" % self.badge.name return u"%d %ss" % (self.count, self.badge.name)