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,59 +39,7 @@ class UserProfile(models.Model):
return prefs return prefs
def get_ui(self): def get_ui(self):
ui = {} return get_ui(self.ui, self.user)
config = copy.deepcopy(settings.CONFIG)
ui.update(config['user']['ui'])
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])
elif isinstance(ui, dict):
ui[key] = new[key]
return ui
ui = updateUI(ui, self.ui)
if not 'lists' in ui:
ui['lists'] = {}
ui['lists'][''] = copy.deepcopy(config['user']['ui']['lists'][''])
def add(lists, section):
ids = []
for l in lists:
qs = Position.objects.filter(section=section)
if section == 'featured':
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
else:
pos, created = Position.objects.get_or_create(list=l, user=self.user, section=section)
qs = qs.filter(user=self.user)
if created:
pos.position = qs.aggregate(Max('position'))['position__max'] + 1
pos.save()
id = l.get_id()
'''
if id not in ui['lists']:
ui['lists'][id] = {}
ui['lists'][id].update(ui['lists'][''])
'''
ids.append(id)
return ids
ids = ['']
ids += add(self.user.lists.exclude(status="featured"), 'personal')
ids += add(self.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): def set_level(self, level):
self.level = settings.CONFIG['userLevels'].index(level) self.level = settings.CONFIG['userLevels'].index(level)
@ -104,6 +52,62 @@ def user_post_save(sender, instance, **kwargs):
models.signals.post_save.connect(user_post_save, sender=User) 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'])
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])
elif isinstance(ui, dict):
ui[key] = new[key]
return ui
ui = updateUI(ui, user_ui)
if not 'lists' in ui:
ui['lists'] = {}
ui['lists'][''] = copy.deepcopy(config['user']['ui']['lists'][''])
def add(lists, section):
ids = []
for l in lists:
qs = Position.objects.filter(section=section)
if section == 'featured':
try:
pos = Position.objects.get(list=l, section=section)
created = False
except Position.DoesNotExist:
pos = Position(list=l, section=section, user=user)
pos.save()
created = True
else:
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()
id = l.get_id()
'''
if id not in ui['lists']:
ui['lists'][id] = {}
ui['lists'][id].update(ui['lists'][''])
'''
ids.append(id)
return ids
ids = ['']
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 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}
} }
''' '''
profile = request.user.get_profile()
profile.ui = {}
profile.save()
response = json_response() response = json_response()
if request.user.is_authenticated():
profile = request.user.get_profile()
profile.ui = {}
profile.save()
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,23 +638,29 @@ 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()
for key in data: ui = profile.ui
keys = re.sub('([^\\\\])\.', '\\1\n', key).split('\n') else:
value = data[key] ui = json.loads(request.session.get('ui', '{}'))
p = profile.ui for key in data:
while len(keys)>1: keys = re.sub('([^\\\\])\.', '\\1\n', key).split('\n')
key = keys.pop(0) value = data[key]
if isinstance(p, list): p = ui
p = p[getPositionById(p, key)] while len(keys)>1:
else: key = keys.pop(0)
if key not in p: if isinstance(p, list):
p[key] = {} p = p[getPositionById(p, key)]
p = p[key]
if value == None and keys[0] in p:
del p[keys[0]]
else: else:
p[keys[0]] = value if key not in p:
p[key] = {}
p = p[key]
if value == None and keys[0] in p:
del p[keys[0]]
else:
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'])