# CCS CS Lab Facebook Models # # Douglas Thrift # # $Id$ from django.db import models from facebook.djangofb import get_facebook_client class UserManager(models.Manager): def get_current(self): return self.get(id = int(get_facebook_client().uid)) class User(models.Model): id = models.BigIntegerField(primary_key = True) person = models.ForeignKey('main.Person', related_name = 'facebook_user_set') session = models.CharField(blank = True, max_length = 128) session_expiry = models.DateTimeField(blank = True, null = True) updated = models.DateTimeField(auto_now = True, auto_now_add = True, blank = True, null = True) objects = UserManager() class Meta: ordering = ('id',) def __unicode__(self): return unicode(self.id) @models.permalink def get_absolute_url(self): return ('facebook_person_view', [self.person.name]) def get_facebook_url(self): from ccscslab.facebook.utils import root return self.get_absolute_url().replace('/facebook', root, 1) class DualUserMixin: def user_full_name(self): from ccscslab.facebook.utils import _facebook import facebook if self.user is not None: try: return _facebook.users.getInfo(self.user.id, 'name')[0]['name'] except (facebook.FacebookError, KeyError): pass return self.person.full_name class DualUserModel(models.Model, DualUserMixin): person = models.ForeignKey('main.Person') user = models.ForeignKey(User, blank = True, null = True) class Meta: abstract = True class Publication(models.Model): user = models.ForeignKey(User) published = models.DateTimeField() class Meta: ordering = ('published', 'user') unique_together = ('user', 'published') def __unicode__(self): return '%s %s' % (self.user, self.published) class TemplateBundle(models.Model): name = models.CharField(max_length = 25, unique = True) bundle_id = models.BigIntegerField() class Meta: ordering = ('name',) def __unicode__(self): return self.name class SectionManager(models.Manager): def actions(self): return self.filter(tab = False) def tabs(self): return self.filter(tab = True) def tab(self, path): return self.get(path = path, tab = True) class Section(models.Model): name = models.CharField(max_length = 25) path = models.CharField(blank = True, max_length = 25) tab = models.BooleanField() position = models.PositiveSmallIntegerField(null = True) objects = SectionManager() class Meta: ordering = ('tab', 'position') unique_together = ('name', 'tab') def __unicode__(self): return self.name def get_absolute_url(self): from ccscslab.facebook.utils import root return root + '/' + self.path