2009-10-09 13:58:38 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-12-24 09:23:34 +00:00
|
|
|
import os
|
2010-11-28 16:46:20 +00:00
|
|
|
from datetime import datetime
|
2009-10-09 13:58:38 +00:00
|
|
|
|
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-11-28 16:46:20 +00:00
|
|
|
from django.conf import settings
|
2010-02-03 11:59:11 +00:00
|
|
|
|
2010-11-28 16:46:20 +00:00
|
|
|
from ox.utils import json
|
2010-02-03 11:59:11 +00:00
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
2010-12-24 12:54:35 +00:00
|
|
|
reset_token = models.TextField(blank=True, null=True, unique=True)
|
2010-02-03 11:59:11 +00:00
|
|
|
user = models.ForeignKey(User, unique=True)
|
2010-08-07 14:31:20 +00:00
|
|
|
|
2010-08-10 21:59:20 +00:00
|
|
|
files_updated = models.DateTimeField(default=datetime.now)
|
2010-12-24 09:23:34 +00:00
|
|
|
newsletter = models.BooleanField(default=True)
|
2010-02-03 11:59:11 +00:00
|
|
|
|
|
|
|
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-11-27 12:12:53 +00:00
|
|
|
def get_user_json(user):
|
2010-01-26 13:11:57 +00:00
|
|
|
json = {}
|
2010-02-06 08:24:39 +00:00
|
|
|
for key in ('username', ):
|
2010-01-26 13:11:57 +00:00
|
|
|
json[key] = getattr(user, key)
|
2010-02-08 10:26:25 +00:00
|
|
|
json['group'] = 'user'
|
|
|
|
if user.is_staff:
|
|
|
|
json['group'] = 'admin'
|
|
|
|
elif user.has_perm('0x.vip'): #FIXME: permissions
|
|
|
|
json['group'] = 'vip'
|
2010-11-27 12:12:53 +00:00
|
|
|
json['preferences'] = get_preferences(user)
|
|
|
|
json['ui'] = get_ui(user)
|
2010-01-26 13:11:57 +00:00
|
|
|
return json
|
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def get_ui(user):
|
2010-12-24 09:23:34 +00:00
|
|
|
with open(os.path.join(settings.PROJECT_ROOT, 'templates', 'site.json')) as f:
|
|
|
|
site = json.load(f)
|
|
|
|
return site['user']['ui']
|
|
|
|
'''
|
2010-09-05 21:07:41 +00:00
|
|
|
return {
|
|
|
|
"columns": ["id", "title", "director", "country", "year", "language", "genre"],
|
2010-11-28 16:43:35 +00:00
|
|
|
"findQuery": {"conditions": [], "operator": ""},
|
2010-09-05 21:07:41 +00:00
|
|
|
"groupsQuery": {"conditions": [], "operator": "|"},
|
|
|
|
"groupsSize": 128,
|
2010-12-06 22:32:17 +00:00
|
|
|
"itemView": "timeline",
|
2010-09-05 21:07:41 +00:00
|
|
|
"listQuery": {"conditions": [], "operator": ""},
|
|
|
|
"listsSize": 192,
|
2010-11-28 16:43:35 +00:00
|
|
|
"listView": "icons",
|
2010-09-05 21:07:41 +00:00
|
|
|
"sections": ["history", "lists", "public", "featured"],
|
|
|
|
"showGroups": True,
|
|
|
|
"showInfo": True,
|
|
|
|
"showLists": True,
|
|
|
|
"showMovies": True,
|
2010-11-28 16:43:35 +00:00
|
|
|
"sort": settings.DEFAULT_SORT,
|
|
|
|
"theme": settings.DEFAULT_THEME
|
2010-09-05 21:07:41 +00:00
|
|
|
}
|
2010-12-24 09:23:34 +00:00
|
|
|
'''
|
2010-09-05 21:07:41 +00:00
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def get_preferences(user):
|
2010-01-26 13:11:57 +00:00
|
|
|
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-02-06 08:24:39 +00:00
|
|
|
prefs['email'] = user.email
|
2010-01-26 13:11:57 +00:00
|
|
|
return prefs
|
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def get_preference(user, key, value=None):
|
2010-02-06 08:24:39 +00:00
|
|
|
if key in ('email', ):
|
|
|
|
value = getattr(user, key)
|
|
|
|
else:
|
|
|
|
q = Preference.objects.filter(user=user, key=key)
|
|
|
|
if q.count() > 0:
|
|
|
|
value = json.loads(q[0].value)
|
2010-01-25 09:20:21 +00:00
|
|
|
return value
|
|
|
|
|
2010-11-27 12:12:53 +00:00
|
|
|
def set_preference(user, key, value):
|
2010-02-06 08:24:39 +00:00
|
|
|
if key in ('email', ):
|
|
|
|
setattr(user, key, value)
|
|
|
|
user.save()
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-02-06 08:24:39 +00:00
|
|
|
value = json.dumps(value)
|
|
|
|
q = Preference.objects.filter(user=user, key=key)
|
|
|
|
if q.count() > 0:
|
|
|
|
p = q[0]
|
|
|
|
p.value = value
|
|
|
|
p.save()
|
|
|
|
else:
|
|
|
|
p = Preference(user=user, key=key, value=value)
|
|
|
|
p.save()
|