2009-10-09 13:58:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
2010-02-03 11:59:11 +00:00
|
|
|
from django.contrib.auth.models import User
|
2010-02-03 12:05:38 +00:00
|
|
|
from django.db import models
|
2010-02-03 11:59:11 +00:00
|
|
|
from django.db.models import signals
|
|
|
|
from django.dispatch import dispatcher
|
2010-02-03 12:05:38 +00:00
|
|
|
from django.utils import simplejson as json
|
2010-02-03 11:59:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
|
|
|
recover_key = models.TextField()
|
|
|
|
user = models.ForeignKey(User, unique=True)
|
|
|
|
|
|
|
|
def user_post_save(sender, instance, **kwargs):
|
|
|
|
profile, new = UserProfile.objects.get_or_create(user=instance)
|
2009-10-09 13:58:38 +00:00
|
|
|
|
2010-02-03 11:59:11 +00:00
|
|
|
models.signals.post_save.connect(user_post_save, sender=User)
|
|
|
|
|
2009-10-09 13:58:38 +00:00
|
|
|
class Preference(models.Model):
|
2010-01-26 13:11:57 +00:00
|
|
|
user = models.ForeignKey(User, related_name='preferences')
|
2009-10-09 13:58:38 +00:00
|
|
|
created = models.DateTimeField(auto_now_add=True)
|
|
|
|
modified = models.DateTimeField(auto_now=True)
|
|
|
|
key = models.CharField(blank=True, max_length=255)
|
|
|
|
value = models.TextField(blank=True)
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
def getUserJSON(user):
|
|
|
|
json = {}
|
|
|
|
for key in ('username', 'email'):
|
|
|
|
json[key] = getattr(user, key)
|
|
|
|
json['preferences'] = getPreferences(user)
|
|
|
|
return json
|
|
|
|
|
|
|
|
def getPreferences(user):
|
|
|
|
prefs = {}
|
|
|
|
for p in Preference.objects.filter(user=user):
|
2010-01-27 05:36:21 +00:00
|
|
|
prefs[p.key] = json.loads(p.value)
|
2010-01-26 13:11:57 +00:00
|
|
|
return prefs
|
|
|
|
|
2010-01-25 09:20:21 +00:00
|
|
|
def getPreference(user, key, value=None):
|
|
|
|
q = Preference.objects.filter(user=user, key=key)
|
|
|
|
if q.count() > 0:
|
|
|
|
value = json.loads(q[0].value)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def setPreference(user, key, value):
|
|
|
|
value = json.dumps(value)
|
|
|
|
q = Preference.objects.filter(user=user, key=key)
|
|
|
|
if q.count() > 0:
|
|
|
|
q[0].value = value
|
|
|
|
else:
|
|
|
|
p = Preference(user=user, key=key, value=value)
|
|
|
|
p.save()
|