gremmie@1: """
gremmie@1: Contains models for the bio application.
gremmie@1: I would have picked profile for this application, but that is already taken, apparently.
gremmie@1: """
gremmie@1: 
gremmie@1: import os.path
gremmie@1: 
gremmie@1: from django.db import models
gremmie@1: from django.contrib import auth
gremmie@1: from django.conf import settings
bgneal@54: from django.core.cache import cache
gremmie@1: 
bgneal@128: from core.markup import SiteMarkup
bgneal@124: 
gremmie@1: 
bgneal@147: (STA_ACTIVE, STA_RESIGNED, STA_REMOVED, STA_SUSPENDED, STA_SPAMMER) = range(5)
bgneal@147: 
bgneal@147: USER_STATUS_CHOICES = (
bgneal@147:     (STA_ACTIVE, "Active"),
bgneal@147:     (STA_RESIGNED, "Resigned"),
bgneal@147:     (STA_REMOVED, "Removed"),
bgneal@147:     (STA_SUSPENDED, "Suspended"),
bgneal@147:     (STA_SPAMMER, "Spammer"),
bgneal@147: )
bgneal@147: 
gremmie@1: def avatar_file_path_for_user(username, filename):
gremmie@1:     return os.path.join(settings.AVATAR_DIR, 'users', username, filename)
gremmie@1: 
gremmie@1: def avatar_file_path(instance, filename):
gremmie@1:     return avatar_file_path_for_user(instance.user.username, filename)
gremmie@1: 
gremmie@1: 
gremmie@1: class UserProfile(models.Model):
gremmie@1:     """model to represent additional information about users"""
gremmie@1: 
gremmie@1:     user = models.ForeignKey(auth.models.User, unique=True)
gremmie@1:     location = models.CharField(max_length=128, blank=True)
gremmie@1:     birthday = models.DateField(blank=True, null=True,
gremmie@1:             help_text='Optional; the year is not shown to others')
gremmie@1:     occupation = models.CharField(max_length=128, blank=True)
gremmie@1:     interests = models.CharField(max_length=255, blank=True)
gremmie@1:     profile_text = models.TextField(blank=True)
gremmie@1:     profile_html = models.TextField(blank=True)
gremmie@1:     hide_email = models.BooleanField(default=True)
gremmie@1:     signature = models.TextField(blank=True)
gremmie@1:     signature_html = models.TextField(blank=True)
gremmie@1:     avatar = models.ImageField(upload_to=avatar_file_path, blank=True)
bgneal@70:     time_zone = models.CharField(max_length=64, blank=True,
bgneal@70:             default='US/Pacific')
bgneal@120:     use_24_time = models.BooleanField(default=False)
bgneal@96:     forum_post_count = models.IntegerField(default=0)
bgneal@147:     status = models.IntegerField(default=STA_ACTIVE,
bgneal@147:             choices=USER_STATUS_CHOICES)
bgneal@147:     status_date = models.DateTimeField(auto_now_add=True)
gremmie@1: 
gremmie@1:     def __unicode__(self):
gremmie@1:         return self.user.username
gremmie@1: 
gremmie@1:     class Meta:
gremmie@1:         ordering = ('user__username', )
gremmie@1: 
gremmie@1:     def save(self, *args, **kwargs):
bgneal@128:         sm = SiteMarkup()
bgneal@128:         self.profile_html = sm.convert(self.profile_text)
bgneal@128:         self.signature_html = sm.convert(self.signature)
gremmie@1:         super(UserProfile, self).save(*args, **kwargs)
bgneal@54:         cache.delete('avatar_' + self.user.username)
bgneal@138: 
bgneal@138:     @models.permalink
bgneal@138:     def get_absolute_url(self):
bgneal@138:         return ('bio-view_profile', (), {'username': self.user.username})
bgneal@138: 
bgneal@138: 
bgneal@138: class UserProfileFlag(models.Model):
bgneal@138:     """This model represents a user flagging a profile as inappropriate."""
bgneal@138:     user = models.ForeignKey(auth.models.User)
bgneal@138:     profile = models.ForeignKey(UserProfile)
bgneal@138:     flag_date = models.DateTimeField(auto_now_add=True)
bgneal@138: 
bgneal@138:     def __unicode__(self):
bgneal@138:         return u"%s's profile flagged by %s" % (self.profile.user.username,
bgneal@138:                 self.user.username)
bgneal@138: 
bgneal@138:     class Meta:
bgneal@138:         ordering = ('flag_date', )
bgneal@138: 
bgneal@138:     def get_profile_url(self):
bgneal@138:         return '<a href="%s">Profile</a>' % self.profile.get_absolute_url()
bgneal@138:     get_profile_url.allow_tags = True