remember ui state for guests

This commit is contained in:
j 2011-10-09 15:39:31 +02:00
parent ce8e012fc3
commit 9f63a45c2e
2 changed files with 85 additions and 71 deletions

View file

@ -39,6 +39,20 @@ class UserProfile(models.Model):
return prefs return prefs
def get_ui(self): def get_ui(self):
return get_ui(self.ui, self.user)
def set_level(self, level):
self.level = settings.CONFIG['userLevels'].index(level)
def get_level(self):
return settings.CONFIG['userLevels'][self.level]
def user_post_save(sender, instance, **kwargs):
profile, new = UserProfile.objects.get_or_create(user=instance)
models.signals.post_save.connect(user_post_save, sender=User)
def get_ui(user_ui, user=None):
ui = {} ui = {}
config = copy.deepcopy(settings.CONFIG) config = copy.deepcopy(settings.CONFIG)
ui.update(config['user']['ui']) ui.update(config['user']['ui'])
@ -52,7 +66,7 @@ class UserProfile(models.Model):
elif isinstance(ui, dict): elif isinstance(ui, dict):
ui[key] = new[key] ui[key] = new[key]
return ui return ui
ui = updateUI(ui, self.ui) ui = updateUI(ui, user_ui)
if not 'lists' in ui: if not 'lists' in ui:
ui['lists'] = {} ui['lists'] = {}
ui['lists'][''] = copy.deepcopy(config['user']['ui']['lists']['']) ui['lists'][''] = copy.deepcopy(config['user']['ui']['lists'][''])
@ -66,12 +80,12 @@ class UserProfile(models.Model):
pos = Position.objects.get(list=l, section=section) pos = Position.objects.get(list=l, section=section)
created = False created = False
except Position.DoesNotExist: except Position.DoesNotExist:
pos = Position(list=l, section=section, user=self.user) pos = Position(list=l, section=section, user=user)
pos.save() pos.save()
created = True created = True
else: else:
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section) pos, created = Position.objects.get_or_create(list=l, user=user, section=section)
qs = qs.filter(user=self.user) qs = qs.filter(user=user)
if created: if created:
pos.position = qs.aggregate(Max('position'))['position__max'] + 1 pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save() pos.save()
@ -85,25 +99,15 @@ class UserProfile(models.Model):
return ids return ids
ids = [''] ids = ['']
ids += add(self.user.lists.exclude(status="featured"), 'personal') if user:
ids += add(self.user.subscribed_lists.filter(status='public'), 'public') ids += add(user.lists.exclude(status="featured"), 'personal')
ids += add(user.subscribed_lists.filter(status='public'), 'public')
ids += add(List.objects.filter(status='featured'), 'featured') ids += add(List.objects.filter(status='featured'), 'featured')
for i in ui['lists'].keys(): for i in ui['lists'].keys():
if i not in ids: if i not in ids:
del ui['lists'][i] del ui['lists'][i]
return ui return ui
def set_level(self, level):
self.level = settings.CONFIG['userLevels'].index(level)
def get_level(self):
return settings.CONFIG['userLevels'][self.level]
def user_post_save(sender, instance, **kwargs):
profile, new = UserProfile.objects.get_or_create(user=instance)
models.signals.post_save.connect(user_post_save, sender=User)
def user_json(user, keys=None, request_user=None): def user_json(user, keys=None, request_user=None):
p = user.get_profile() p = user.get_profile()
j = { j = {

View file

@ -64,6 +64,7 @@ def signin(request):
user = authenticate(username=data['username'], password=data['password']) user = authenticate(username=data['username'], password=data['password'])
if user is not None: if user is not None:
if user.is_active: if user.is_active:
request.session['ui'] = '{}'
login(request, user) login(request, user)
user_json = models.init_user(user, request) user_json = models.init_user(user, request)
response = json_response({ response = json_response({
@ -612,10 +613,13 @@ def resetUI(request):
'status': {'code': int, 'text': string} 'status': {'code': int, 'text': string}
} }
''' '''
response = json_response()
if request.user.is_authenticated():
profile = request.user.get_profile() profile = request.user.get_profile()
profile.ui = {} profile.ui = {}
profile.save() profile.save()
response = json_response() else:
request.session['ui'] = '{}'
return render_to_json_response(response) return render_to_json_response(response)
actions.register(resetUI, cache=False) actions.register(resetUI, cache=False)
@ -634,10 +638,13 @@ def setUI(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
if request.user.is_authenticated(): if request.user.is_authenticated():
profile = request.user.get_profile() profile = request.user.get_profile()
ui = profile.ui
else:
ui = json.loads(request.session.get('ui', '{}'))
for key in data: for key in data:
keys = re.sub('([^\\\\])\.', '\\1\n', key).split('\n') keys = re.sub('([^\\\\])\.', '\\1\n', key).split('\n')
value = data[key] value = data[key]
p = profile.ui p = ui
while len(keys)>1: while len(keys)>1:
key = keys.pop(0) key = keys.pop(0)
if isinstance(p, list): if isinstance(p, list):
@ -650,7 +657,10 @@ def setUI(request):
del p[keys[0]] del p[keys[0]]
else: else:
p[keys[0]] = value p[keys[0]] = value
if request.user.is_authenticated():
profile.save() profile.save()
else:
request.session['ui'] = json.dumps(ui)
if data.get('item', False): if data.get('item', False):
item = get_object_or_404_json(Item, itemId=data['item']) item = get_object_or_404_json(Item, itemId=data['item'])