#-*- coding:utf-8 -*-

from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from common.decorators import render
from accounts.forms import SettingsForm
from accounts.models import UserSettings

@login_required
@render() 
def settings(request):
  user_settings, created = UserSettings.objects.get_or_create(user=request.user)
  if request.method == 'POST':
    form = SettingsForm(data=request.POST, instance=user_settings)
    if form.is_valid():
      form.save()
      return HttpResponseRedirect(request.POST['next'])
  else:
    form = SettingsForm(instance=user_settings)
  return {'form': form, 'next': request.META.get('HTTP_REFERER', '')}
