cablegates/pandora/user/models.py

97 lines
3.1 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
2011-01-14 09:54:35 +00:00
from django.db.models import Max
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 = {}
2011-01-14 14:32:48 +00:00
config = site_config()
ui.update(config['user']['ui'])
2011-01-15 14:22:29 +00:00
def updateUI(ui, new):
'''
only update set keys in dicts
'''
for key in new:
if isinstance(new[key], dict) and key in ui:
ui[key] = updateUI(ui[key], new[key])
else:
ui[key] = new[key]
return ui
ui = updateUI(ui, self.ui)
2011-01-11 14:56:08 +00:00
if not 'lists' in ui:
ui['lists'] = {}
2011-01-14 14:32:48 +00:00
ui['lists'][''] = 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-14 09:54:35 +00:00
qs = Position.objects.filter(section=section)
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-14 09:54:35 +00:00
qs = qs.filter(user=self.user)
2011-01-11 16:19:27 +00:00
if created:
2011-01-14 09:54:35 +00:00
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
2011-01-11 16:19:27 +00:00
pos.save()
id = l.get_id()
2011-01-16 01:06:37 +00:00
'''
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-16 01:06:37 +00:00
'''
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)
2011-01-21 09:31:49 +00:00
result['admin'] = user.is_staff
result['groups'] = [g.name for g in user.groups.all()]
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