setUI, setPreferences

This commit is contained in:
j 2011-01-11 17:21:09 +05:30
parent e700020707
commit 68fa703eb8
2 changed files with 43 additions and 52 deletions

View file

@ -21,6 +21,7 @@ class UserProfile(models.Model):
files_updated = models.DateTimeField(default=datetime.now) files_updated = models.DateTimeField(default=datetime.now)
newsletter = models.BooleanField(default=True) newsletter = models.BooleanField(default=True)
ui = DictField(default={}) ui = DictField(default={})
preferences = DictField(default={})
def user_post_save(sender, instance, **kwargs): def user_post_save(sender, instance, **kwargs):
profile, new = UserProfile.objects.get_or_create(user=instance) profile, new = UserProfile.objects.get_or_create(user=instance)
@ -28,17 +29,6 @@ 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)
class Preference(models.Model):
user = models.ForeignKey(User, related_name='preferences')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
key = models.CharField(blank=True, max_length=255)
value = models.TextField(blank=True)
def __unicode__(self):
return u"%s/%s=%s" % (self.user, self.key, self.value)
def get_user_json(user): def get_user_json(user):
result = {} result = {}
for key in ('username', ): for key in ('username', ):
@ -77,9 +67,7 @@ def get_ui(user):
def get_preferences(user): def get_preferences(user):
prefs = {} prefs = user.get_profile().preferences
for p in Preference.objects.filter(user=user):
prefs[p.key] = json.loads(p.value)
prefs['email'] = user.email prefs['email'] = user.email
return prefs return prefs

View file

@ -369,58 +369,61 @@ def contact(request):
actions.register(contact) actions.register(contact)
@login_required_json def getPositionById(list, key):
def preferences(request): for i in range(0, len(list)):
''' if list[i]['id'] == key:
param data return i
string return -1
array
object
return
if data is empy or {} @login_required_json
if data is string: def setPreferences(request):
return preference with name '''
if data is array: param data {
return preferences with names key.subkey: value
if data is object: }
set key values in dict as preferences return
''' '''
response = json_response()
if 'data' not in request.POST:
response['data']['preferences'] = models.get_preferences(request.user)
else:
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
if isinstance(data, basestring): keys = data.keys()[0].split('.')
response['data']['preferences'] = {} value = data.values()[0]
response['data']['preferences'][data] = models.get_preference(request.user, data) profile = request.user.get_profile()
elif isinstance(data, list): p = profile.preferences
response['data']['preferences'] = {} while len(keys)>1:
for preference in data: key = keys.pop(0)
response['preferences'][preference] = models.get_preference(request.user, preference) if isinstance(p, list):
elif isinstance(data, dict): p = p[getPositionById(p, key)]
if not data:
response['data']['preferences'] = models.get_preferences(request.user)
else: else:
del response['data'] p = p[key]
for key in data: p[keys[0]] = value
models.set_preference(request.user, key, data[key]) profile.save()
response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(preferences) actions.register(setPreferences)
@login_required_json @login_required_json
def setUI(request): def setUI(request):
''' '''
param data {
key.subkey: value
}
return
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
keys = data.keys()[0].split('.') keys = data.keys()[0].split('.')
value = data.values()[0] value = data.values()[0]
ui = user.profile.ui profile = request.user.get_profile()
while keys: p = profile.ui
while len(keys)>1:
key = keys.pop(0) key = keys.pop(0)
if isinstance(p, list):
p = p[getPositionById(p, key)]
else:
p = p[key]
p[keys[0]] = value
profile.save()
response = json_response() response = json_response()
return render_to_json_response(response) return render_to_json_response(response)
actions.register(preferences) actions.register(setUI)