def prefs(request, key=None): """Get or set preferences.""" if key is None: d = dict( (x.key, x.value) for x in UserPreferences.objects.filter(user=request.user)) return render_json(d) else: if "set" in request.REQUEST: try: x = UserPreferences.objects.get(user=request.user, key=key) except UserPreferences.DoesNotExist: x = UserPreferences(user=request.user, key=key) x.value = request.REQUEST["set"] x.save() return render_json(True) if "delete" in request.REQUEST: try: x = UserPreferences.objects.get(user=request.user, key=key) x.delete() return render_json(True) except UserPreferences.DoesNotExist: return render_json(False) else: try: x = UserPreferences.objects.get(user=request.user, key=key) return render_json(x.value) except UserPreferences.DoesNotExist: return render_json(None)
def user_preferences(request, key=None): response = {'status': 0, 'data': {}} if request.method != "POST": if key is not None: try: x = UserPreferences.objects.get(user=request.user, key=key) response['data'] = {key: x.value} except UserPreferences.DoesNotExist: response['data'] = None else: response['data'] = dict( (x.key, x.value) for x in UserPreferences.objects.filter(user=request.user)) else: if "set" in request.POST: try: x = UserPreferences.objects.get(user=request.user, key=key) except UserPreferences.DoesNotExist: x = UserPreferences(user=request.user, key=key) x.value = request.POST["set"] x.save() response['data'] = {key: x.value} elif "delete" in request.POST: try: x = UserPreferences.objects.get(user=request.user, key=key) x.delete() except UserPreferences.DoesNotExist: pass return JsonResponse(response)