cablegates/pandora/user/models.py

84 lines
2.6 KiB
Python
Raw Normal View History

2009-10-09 13:58:38 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
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-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
2011-01-11 10:18:18 +00:00
from ox.django.fields import DictField
2010-02-03 11:59:11 +00:00
2011-01-03 14:25:51 +00:00
from app.models import site_config
2011-01-11 16:19:27 +00:00
from itemlist.models import List, Position
2011-01-11 10:18:18 +00:00
2011-01-01 11:44:42 +00:00
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)
2011-01-01 11:44:42 +00:00
2010-08-10 21:59:20 +00:00
files_updated = models.DateTimeField(default=datetime.now)
newsletter = models.BooleanField(default=True)
2011-01-11 10:18:18 +00:00
ui = DictField(default={})
2011-01-11 11:51:09 +00:00
preferences = DictField(default={})
2011-01-01 11:44:42 +00:00
2011-01-11 14:56:08 +00:00
def get_preferences(self):
prefs = self.preferences
prefs['email'] = self.user.email
return prefs
def get_ui(self):
ui = {}
ui.update(site_config['user']['ui'])
ui.update(self.ui)
if not 'lists' in ui:
ui['lists'] = {}
ui['lists'][''] = site_config['uiDefaults']['list']
2011-01-11 16:19:27 +00:00
def add(lists, section):
2011-01-13 11:54:52 +00:00
ids = []
2011-01-11 16:19:27 +00:00
for l in lists:
2011-01-11 20:12:35 +00:00
if section == 'featured':
pos, created = Position.objects.get_or_create(list=l, section=section)
else:
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section)
2011-01-11 16:19:27 +00:00
if created:
pos.position = len(in_list)
pos.save()
id = l.get_id()
2011-01-13 11:54:52 +00:00
if id not in ui['lists']:
2011-01-11 16:19:27 +00:00
ui['lists'][id] = {}
ui['lists'][id].update(ui['lists'][''])
2011-01-13 11:54:52 +00:00
ids.append(id)
2011-01-11 16:19:27 +00:00
return ids
ids = ['']
ids += add(self.user.lists.exclude(status="featured"), 'my')
ids += add(self.user.subscribed_lists.all(), 'public')
ids += add(List.objects.filter(status='featured'), 'featured')
for i in ui['lists'].keys():
if i not in ids:
del ui['lists'][i]
2011-01-11 14:56:08 +00:00
return ui
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)
2011-01-01 11:44:42 +00:00
def get_user_json(user):
2011-01-11 14:56:08 +00:00
profile = user.get_profile()
2011-01-01 11:44:42 +00:00
result = {}
2010-02-06 08:24:39 +00:00
for key in ('username', ):
2011-01-01 11:44:42 +00:00
result[key] = getattr(user, key)
result['group'] = 'user'
2010-02-08 10:26:25 +00:00
if user.is_staff:
2011-01-01 11:44:42 +00:00
result['group'] = 'admin'
2010-02-08 10:26:25 +00:00
elif user.has_perm('0x.vip'): #FIXME: permissions
2011-01-01 11:44:42 +00:00
result['group'] = 'vip'
2011-01-11 14:56:08 +00:00
result['preferences'] = profile.get_preferences()
result['ui'] = profile.get_ui()
2011-01-01 11:44:42 +00:00
return result