remember ui state for guests
This commit is contained in:
parent
ce8e012fc3
commit
9f63a45c2e
2 changed files with 85 additions and 71 deletions
|
@ -39,6 +39,20 @@ class UserProfile(models.Model):
|
|||
return prefs
|
||||
|
||||
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 = {}
|
||||
config = copy.deepcopy(settings.CONFIG)
|
||||
ui.update(config['user']['ui'])
|
||||
|
@ -52,7 +66,7 @@ class UserProfile(models.Model):
|
|||
elif isinstance(ui, dict):
|
||||
ui[key] = new[key]
|
||||
return ui
|
||||
ui = updateUI(ui, self.ui)
|
||||
ui = updateUI(ui, user_ui)
|
||||
if not 'lists' in ui:
|
||||
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)
|
||||
created = False
|
||||
except Position.DoesNotExist:
|
||||
pos = Position(list=l, section=section, user=self.user)
|
||||
pos = Position(list=l, section=section, user=user)
|
||||
pos.save()
|
||||
created = True
|
||||
else:
|
||||
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section)
|
||||
qs = qs.filter(user=self.user)
|
||||
pos, created = Position.objects.get_or_create(list=l, user=user, section=section)
|
||||
qs = qs.filter(user=user)
|
||||
if created:
|
||||
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
|
||||
pos.save()
|
||||
|
@ -85,25 +99,15 @@ class UserProfile(models.Model):
|
|||
return ids
|
||||
|
||||
ids = ['']
|
||||
ids += add(self.user.lists.exclude(status="featured"), 'personal')
|
||||
ids += add(self.user.subscribed_lists.filter(status='public'), 'public')
|
||||
if user:
|
||||
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')
|
||||
for i in ui['lists'].keys():
|
||||
if i not in ids:
|
||||
del ui['lists'][i]
|
||||
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):
|
||||
p = user.get_profile()
|
||||
j = {
|
||||
|
|
|
@ -64,6 +64,7 @@ def signin(request):
|
|||
user = authenticate(username=data['username'], password=data['password'])
|
||||
if user is not None:
|
||||
if user.is_active:
|
||||
request.session['ui'] = '{}'
|
||||
login(request, user)
|
||||
user_json = models.init_user(user, request)
|
||||
response = json_response({
|
||||
|
@ -612,10 +613,13 @@ def resetUI(request):
|
|||
'status': {'code': int, 'text': string}
|
||||
}
|
||||
'''
|
||||
response = json_response()
|
||||
if request.user.is_authenticated():
|
||||
profile = request.user.get_profile()
|
||||
profile.ui = {}
|
||||
profile.save()
|
||||
response = json_response()
|
||||
else:
|
||||
request.session['ui'] = '{}'
|
||||
return render_to_json_response(response)
|
||||
actions.register(resetUI, cache=False)
|
||||
|
||||
|
@ -634,10 +638,13 @@ def setUI(request):
|
|||
data = json.loads(request.POST['data'])
|
||||
if request.user.is_authenticated():
|
||||
profile = request.user.get_profile()
|
||||
ui = profile.ui
|
||||
else:
|
||||
ui = json.loads(request.session.get('ui', '{}'))
|
||||
for key in data:
|
||||
keys = re.sub('([^\\\\])\.', '\\1\n', key).split('\n')
|
||||
value = data[key]
|
||||
p = profile.ui
|
||||
p = ui
|
||||
while len(keys)>1:
|
||||
key = keys.pop(0)
|
||||
if isinstance(p, list):
|
||||
|
@ -650,7 +657,10 @@ def setUI(request):
|
|||
del p[keys[0]]
|
||||
else:
|
||||
p[keys[0]] = value
|
||||
if request.user.is_authenticated():
|
||||
profile.save()
|
||||
else:
|
||||
request.session['ui'] = json.dumps(ui)
|
||||
|
||||
if data.get('item', False):
|
||||
item = get_object_or_404_json(Item, itemId=data['item'])
|
||||
|
|
Loading…
Reference in a new issue