2010-01-25 09:20:21 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
|
|
|
|
|
|
|
from django.contrib.auth.models import User
|
2010-01-26 13:11:57 +00:00
|
|
|
from django.contrib.auth import authenticate, login, logout
|
2010-01-25 09:20:21 +00:00
|
|
|
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.utils import simplejson as json
|
2010-01-26 13:11:57 +00:00
|
|
|
from django import forms
|
2010-01-25 09:20:21 +00:00
|
|
|
|
|
|
|
from oxdjango.shortcuts import render_to_json_response
|
|
|
|
from oxdjango.decorators import login_required_json
|
|
|
|
|
|
|
|
import models
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
class LoginForm(forms.Form):
|
|
|
|
username = forms.TextInput()
|
|
|
|
password = forms.TextInput()
|
2010-01-25 09:20:21 +00:00
|
|
|
|
|
|
|
def api_login(request):
|
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
param data
|
|
|
|
{'username': username, 'password': password}
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
return {'status': {'code': int, 'text': string}}
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-26 13:24:50 +00:00
|
|
|
print "lgin"
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code': 403, 'text': 'login failed'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2010-01-26 13:11:57 +00:00
|
|
|
form = LoginForm(data, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
user = authenticate(username=data['username'], password=data['password'])
|
|
|
|
if user is not None:
|
|
|
|
if user.is_active:
|
|
|
|
login(request, user)
|
|
|
|
user_json = models.getUserJSON(user)
|
|
|
|
response = {'status': {'code': 200, 'message': 'You are logged in.', 'user': user_json}}
|
|
|
|
else:
|
|
|
|
response = {'status': {'code': 403, 'text': 'Your account is disabled.'}}
|
|
|
|
print "Your account has been disabled!"
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code': 403, 'text': 'Your username and password were incorrect.'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code':422, 'text': 'invalid data'}}
|
|
|
|
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
|
|
|
def api_logout(request):
|
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
param data
|
|
|
|
{}
|
|
|
|
|
|
|
|
return {'status': {'code': int, 'text': string}}
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code': 200, 'text': 'logged out'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
if request.user.is_authenticated():
|
2010-01-26 13:11:57 +00:00
|
|
|
logout(request)
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
class RegisterForm(forms.Form):
|
|
|
|
username = forms.TextInput()
|
|
|
|
password = forms.TextInput()
|
|
|
|
email = forms.TextInput()
|
|
|
|
|
2010-01-25 09:20:21 +00:00
|
|
|
def api_register(request):
|
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
param data
|
|
|
|
{'username': username, 'password': password, 'email': email}
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
return {'status': {'code': int, 'text': string}}
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-26 13:24:50 +00:00
|
|
|
print "register"
|
2010-01-25 09:20:21 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2010-01-26 13:11:57 +00:00
|
|
|
form = RegisterForm(data, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
if models.User.objects.filter(username=form.data['username']).count() > 0:
|
2010-01-26 13:24:50 +00:00
|
|
|
print "username taken", form.data['username']
|
|
|
|
response = {'status': {'code':422, 'text': 'username or email exists'}}
|
2010-01-26 13:11:57 +00:00
|
|
|
elif models.User.objects.filter(email=form.data['email']).count() > 0:
|
2010-01-26 13:24:50 +00:00
|
|
|
print "username taken", form.data['email']
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code':422, 'text': 'username or email exists'}}
|
|
|
|
else:
|
|
|
|
user = models.User(username=form.data['username'], email=form.data['email'])
|
|
|
|
user.set_password(form.data['password'])
|
2010-01-26 13:24:50 +00:00
|
|
|
user.save()
|
2010-01-26 13:11:57 +00:00
|
|
|
user = authenticate(username=form.data['username'],
|
|
|
|
password=form.data['password'])
|
|
|
|
login(request, user)
|
|
|
|
response = {'status': {'code':200, 'text': 'account created'}}
|
|
|
|
else:
|
2010-01-26 13:24:50 +00:00
|
|
|
print "form invalid"
|
|
|
|
print form.errors
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code':422, 'text': 'username exists'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
class RecoverForm(forms.Form):
|
|
|
|
username_or_email = forms.TextInput()
|
|
|
|
|
2010-01-25 09:20:21 +00:00
|
|
|
def api_recover(request):
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
|
|
|
param data
|
|
|
|
{'username_or_email': username}
|
|
|
|
|
|
|
|
return {'status': {'code': int, 'text': string}}
|
|
|
|
'''
|
2010-01-25 09:20:21 +00:00
|
|
|
data = json.loads(request.POST['data'])
|
2010-01-26 13:11:57 +00:00
|
|
|
form = RegisterForm(data, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
username = data['username_or_email']
|
|
|
|
user = None
|
|
|
|
q = models.User.objects.filter(username=username)
|
2010-01-25 09:20:21 +00:00
|
|
|
if q.count() > 0:
|
|
|
|
user = q[0]
|
2010-01-26 13:11:57 +00:00
|
|
|
else:
|
|
|
|
q = models.User.objects.filter(email=username)
|
|
|
|
if q.count() > 0:
|
|
|
|
user = q[0]
|
|
|
|
if user:
|
2010-01-27 05:36:21 +00:00
|
|
|
user.email_user('recovert','not yest, but soon you will be able to recover')
|
2010-01-26 13:11:57 +00:00
|
|
|
#user.sendmail(...) #FIXME: send recovery mail
|
|
|
|
response = {'status': {'code': 200, 'text': 'recover email sent.'}}
|
|
|
|
else:
|
|
|
|
response = {'status': {'code': 404, 'text': 'user or email not found.'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-01-26 13:11:57 +00:00
|
|
|
response = {'status': {'code':422, 'text': 'username exists'}}
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|
|
|
|
@login_required_json
|
|
|
|
def api_preferences(request):
|
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
param data
|
|
|
|
string
|
|
|
|
list
|
|
|
|
dict
|
|
|
|
return
|
|
|
|
if data is string:
|
|
|
|
return preference with name
|
|
|
|
if data is list:
|
|
|
|
return preferences with names
|
|
|
|
if data is dict:
|
|
|
|
set key values in dict as preferences
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-27 05:36:21 +00:00
|
|
|
response = {'status': {'code': 200, 'text': 'ok'}, 'data':{}}
|
2010-01-25 09:20:21 +00:00
|
|
|
if 'data' not in request.POST:
|
2010-01-27 05:36:21 +00:00
|
|
|
response['data']['preferences'] = models.getPreferences(request.user)
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
if isinstance(data, basestring):
|
2010-01-27 05:36:21 +00:00
|
|
|
response['data']['preferences'] = {}
|
|
|
|
response['data']['preferences'][data] = models.getPreference(request.user, data)
|
2010-01-25 09:20:21 +00:00
|
|
|
elif isinstance(data, list):
|
2010-01-27 05:36:21 +00:00
|
|
|
response['data']['preferences'] = {}
|
2010-01-25 09:20:21 +00:00
|
|
|
for preference in data:
|
2010-01-27 05:36:21 +00:00
|
|
|
response['preferences'][preference] = models.getPreference(request.user, preference)
|
2010-01-25 09:20:21 +00:00
|
|
|
elif isinstance(data, dict):
|
2010-01-27 05:36:21 +00:00
|
|
|
del response['data']
|
2010-01-25 09:20:21 +00:00
|
|
|
for key in data:
|
2010-01-27 05:36:21 +00:00
|
|
|
models.setPreference(request.user, key, data[key])
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
|