2009-10-09 15:58:38 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-11-28 17:46:20 +01:00
|
|
|
from datetime import datetime
|
2009-10-09 15:58:38 +02:00
|
|
|
|
2010-02-03 17:29:11 +05:30
|
|
|
from django.contrib.auth.models import User
|
2010-02-03 17:35:38 +05:30
|
|
|
from django.db import models
|
2011-01-14 09:54:35 +00:00
|
|
|
from django.db.models import Max
|
2010-02-03 17:29:11 +05:30
|
|
|
|
2011-01-11 15:48:18 +05:30
|
|
|
from ox.django.fields import DictField
|
2010-02-03 17:29:11 +05:30
|
|
|
|
2011-01-03 19:55:51 +05:30
|
|
|
from app.models import site_config
|
2011-01-11 21:49:27 +05:30
|
|
|
from itemlist.models import List, Position
|
2011-01-11 15:48:18 +05:30
|
|
|
|
2011-01-01 17:14:42 +05:30
|
|
|
|
2010-02-03 17:29:11 +05:30
|
|
|
class UserProfile(models.Model):
|
2010-12-24 18:24:35 +05:30
|
|
|
reset_token = models.TextField(blank=True, null=True, unique=True)
|
2010-02-03 17:29:11 +05:30
|
|
|
user = models.ForeignKey(User, unique=True)
|
2011-01-01 17:14:42 +05:30
|
|
|
|
2010-08-10 23:59:20 +02:00
|
|
|
files_updated = models.DateTimeField(default=datetime.now)
|
2010-12-24 14:53:34 +05:30
|
|
|
newsletter = models.BooleanField(default=True)
|
2011-01-11 15:48:18 +05:30
|
|
|
ui = DictField(default={})
|
2011-01-11 17:21:09 +05:30
|
|
|
preferences = DictField(default={})
|
2011-01-01 17:14:42 +05:30
|
|
|
|
2011-01-11 20:26:08 +05:30
|
|
|
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 20:26:08 +05:30
|
|
|
if not 'lists' in ui:
|
|
|
|
ui['lists'] = {}
|
2011-01-14 14:32:48 +00:00
|
|
|
ui['lists'][''] = config['uiDefaults']['list']
|
2011-01-11 21:49:27 +05:30
|
|
|
|
|
|
|
def add(lists, section):
|
2011-01-13 11:54:52 +00:00
|
|
|
ids = []
|
2011-01-11 21:49:27 +05:30
|
|
|
for l in lists:
|
2011-01-14 09:54:35 +00:00
|
|
|
qs = Position.objects.filter(section=section)
|
2011-01-12 01:42:35 +05:30
|
|
|
if section == 'featured':
|
2011-01-21 05:13:41 +00:00
|
|
|
try:
|
|
|
|
pos = Position.objects.get(list=l, section=section)
|
|
|
|
created = False
|
|
|
|
except Position.DoesNotExist:
|
|
|
|
pos = Position(list=l, section=section, user=self.user)
|
|
|
|
pos.save()
|
|
|
|
created = True
|
2011-01-12 01:42:35 +05:30
|
|
|
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 21:49:27 +05:30
|
|
|
if created:
|
2011-01-14 09:54:35 +00:00
|
|
|
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
2011-01-11 21:49:27 +05:30
|
|
|
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 21:49:27 +05:30
|
|
|
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 21:49:27 +05:30
|
|
|
return ids
|
|
|
|
|
|
|
|
ids = ['']
|
2011-01-21 05:13:41 +00:00
|
|
|
ids += add(self.user.lists.exclude(status="featured"), 'personal')
|
|
|
|
ids += add(self.user.subscribed_lists.filter(status='public'), 'public')
|
2011-01-11 21:49:27 +05:30
|
|
|
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 20:26:08 +05:30
|
|
|
return ui
|
|
|
|
|
2010-02-03 17:29:11 +05:30
|
|
|
def user_post_save(sender, instance, **kwargs):
|
|
|
|
profile, new = UserProfile.objects.get_or_create(user=instance)
|
2009-10-09 15:58:38 +02:00
|
|
|
|
2010-02-03 17:29:11 +05:30
|
|
|
models.signals.post_save.connect(user_post_save, sender=User)
|
2011-01-01 17:14:42 +05:30
|
|
|
|
|
|
|
|
2010-11-27 13:12:53 +01:00
|
|
|
def get_user_json(user):
|
2011-01-11 20:26:08 +05:30
|
|
|
profile = user.get_profile()
|
2011-01-01 17:14:42 +05:30
|
|
|
result = {}
|
2010-02-06 13:54:39 +05:30
|
|
|
for key in ('username', ):
|
2011-01-01 17:14:42 +05:30
|
|
|
result[key] = getattr(user, key)
|
2011-01-25 20:15:07 +05:30
|
|
|
if user.is_superuser:
|
2011-01-21 19:10:42 +00:00
|
|
|
result['level'] = 'admin'
|
2011-01-25 20:15:07 +05:30
|
|
|
elif user.is_staff:
|
|
|
|
result['level'] = 'staff'
|
2011-01-21 19:10:42 +00:00
|
|
|
else:
|
2011-01-25 20:15:07 +05:30
|
|
|
result['level'] = 'member'
|
2011-01-21 15:01:49 +05:30
|
|
|
result['groups'] = [g.name for g in user.groups.all()]
|
2011-01-11 20:26:08 +05:30
|
|
|
result['preferences'] = profile.get_preferences()
|
|
|
|
result['ui'] = profile.get_ui()
|
2011-01-01 17:14:42 +05:30
|
|
|
return result
|