2010-12-24 12:54:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-01-25 09:20:21 +00:00
|
|
|
# vi:si:et:sw=4:sts=4:ts=4
|
2010-12-24 12:54:35 +00:00
|
|
|
import random
|
|
|
|
random.seed()
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-02-03 12:05:38 +00:00
|
|
|
from django import forms
|
2010-01-25 09:20:21 +00:00
|
|
|
from django.contrib.auth.models import User
|
2010-01-26 13:11:57 +00:00
|
|
|
from django.contrib.auth import authenticate, login, logout
|
2010-02-06 08:24:39 +00:00
|
|
|
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404, redirect
|
2010-02-03 11:59:11 +00:00
|
|
|
from django.template import RequestContext, loader, Context
|
2010-01-25 09:20:21 +00:00
|
|
|
from django.utils import simplejson as json
|
2010-02-06 08:24:39 +00:00
|
|
|
from django.conf import settings
|
2010-02-16 10:10:53 +00:00
|
|
|
from django.core.mail import send_mail, BadHeaderError
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-11-23 09:53:12 +00:00
|
|
|
from ox.django.shortcuts import render_to_json_response, json_response
|
|
|
|
from ox.django.decorators import login_required_json
|
2010-12-24 12:54:35 +00:00
|
|
|
import ox
|
2010-01-25 09:20:21 +00:00
|
|
|
|
|
|
|
import models
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
from api.actions import actions
|
|
|
|
|
2010-02-27 10:37:00 +00:00
|
|
|
|
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
|
|
|
|
2010-12-22 18:37:20 +00:00
|
|
|
def api_login(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
username: 'username',
|
|
|
|
password: 'password'
|
|
|
|
}
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string}
|
|
|
|
data: {
|
|
|
|
errors: {
|
2010-12-24 10:14:13 +00:00
|
|
|
username: 'Unknown Username',
|
2010-12-24 09:23:34 +00:00
|
|
|
password: 'Incorrect Password'
|
|
|
|
}
|
|
|
|
user: {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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():
|
2010-12-24 09:23:34 +00:00
|
|
|
if models.User.objects.filter(username=form.data['username']).count() == 0:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
2010-12-24 10:14:13 +00:00
|
|
|
'username': 'Unknown Username'
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
})
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-12-24 09:23:34 +00:00
|
|
|
user = authenticate(username=data['username'], password=data['password'])
|
|
|
|
if user is not None:
|
|
|
|
if user.is_active:
|
|
|
|
login(request, user)
|
|
|
|
user_json = models.get_user_json(user)
|
|
|
|
response = json_response({
|
|
|
|
'user': user_json
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
|
|
|
'username': 'User Disabled'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
|
|
|
'password': 'Incorrect Password'
|
|
|
|
}
|
|
|
|
})
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response(status=400, text='invalid data')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 18:37:20 +00:00
|
|
|
actions.register(api_login, 'login')
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-12-22 18:39:19 +00:00
|
|
|
def api_logout(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
}
|
2010-01-26 13:11:57 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string}
|
|
|
|
}
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-12-24 10:14:13 +00:00
|
|
|
response = json_response(text='ok')
|
2010-01-25 09:20:21 +00:00
|
|
|
if request.user.is_authenticated():
|
2010-12-24 10:14:13 +00:00
|
|
|
response = json_response(text='logged out')
|
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-12-22 18:37:20 +00:00
|
|
|
actions.register(api_logout, 'logout')
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
class RegisterForm(forms.Form):
|
|
|
|
username = forms.TextInput()
|
|
|
|
password = forms.TextInput()
|
|
|
|
email = forms.TextInput()
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def register(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
username: 'username',
|
|
|
|
password: 'password',
|
|
|
|
email: 'emailaddress'
|
|
|
|
}
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string}
|
|
|
|
data: {
|
|
|
|
errors: {
|
2010-12-24 10:14:13 +00:00
|
|
|
username: 'Unknown Username',
|
2010-12-24 09:23:34 +00:00
|
|
|
password: 'Incorrect Password'
|
|
|
|
}
|
|
|
|
user: {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-12-24 09:23:34 +00:00
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
2010-12-24 10:14:13 +00:00
|
|
|
'username': 'Username already exists'
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
})
|
2010-01-26 13:11:57 +00:00
|
|
|
elif models.User.objects.filter(email=form.data['email']).count() > 0:
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
2010-12-24 10:14:13 +00:00
|
|
|
'email': 'Email address already exits'
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
})
|
2010-12-24 12:54:35 +00:00
|
|
|
elif not form.data['password']:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
|
|
|
'password': 'Password can not be empty'
|
|
|
|
}
|
|
|
|
})
|
2010-01-26 13:11:57 +00:00
|
|
|
else:
|
2010-12-22 07:45:37 +00:00
|
|
|
first_user = models.User.objects.count() == 0
|
2010-01-26 13:11:57 +00:00
|
|
|
user = models.User(username=form.data['username'], email=form.data['email'])
|
|
|
|
user.set_password(form.data['password'])
|
2010-12-22 07:45:37 +00:00
|
|
|
#make first user admin
|
|
|
|
user.is_superuser = first_user
|
|
|
|
user.is_staff = first_user
|
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)
|
2010-12-24 09:23:34 +00:00
|
|
|
user_json = models.get_user_json(user)
|
|
|
|
response = json_response({
|
|
|
|
'user': user_json
|
|
|
|
}, text='account created')
|
2010-01-26 13:11:57 +00:00
|
|
|
else:
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response(status=400, text='invalid data')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(register)
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-12-24 12:54:35 +00:00
|
|
|
def resetPassword(request):
|
|
|
|
'''
|
|
|
|
param data {
|
|
|
|
token: reset token
|
|
|
|
password: new password
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string}
|
|
|
|
data: {
|
|
|
|
errors: {
|
|
|
|
token: 'Invalid token'
|
|
|
|
}
|
|
|
|
user {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
if 'token' in data and 'password' in data:
|
|
|
|
if not data['password']:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
|
|
|
'password': 'Password can not be empty'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
qs = models.UserProfile.objects.filter(reset_token=data['token'])
|
|
|
|
if qs.count() == 1:
|
|
|
|
user = qs[0].user
|
|
|
|
user.set_password(data['password'])
|
|
|
|
user.save()
|
|
|
|
user_profile = user.get_profile()
|
|
|
|
user_profile.reset_token = None
|
|
|
|
user_profile.save()
|
|
|
|
user = authenticate(username=user.username, password=data['password'])
|
|
|
|
login(request, user)
|
|
|
|
|
|
|
|
user_json = models.get_user_json(user)
|
|
|
|
response = json_response({
|
|
|
|
'user': user_json
|
|
|
|
}, text='password reset')
|
|
|
|
else:
|
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
|
|
|
'token': 'Invalid token'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
else:
|
|
|
|
response = json_response(status=400, text='invalid data')
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(resetPassword)
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
class RecoverForm(forms.Form):
|
|
|
|
username_or_email = forms.TextInput()
|
|
|
|
|
2010-12-24 12:54:35 +00:00
|
|
|
def requestToken(request):
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
username_or_email: username
|
|
|
|
}
|
2010-01-26 13:11:57 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
return {
|
|
|
|
status: {'code': int, 'text': string}
|
|
|
|
data: {
|
|
|
|
errors: {
|
2010-12-24 10:14:13 +00:00
|
|
|
username_or_email: 'Username or email address not found'
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
2010-12-24 12:54:35 +00:00
|
|
|
username: user
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-26 13:11:57 +00:00
|
|
|
'''
|
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-12-24 12:54:35 +00:00
|
|
|
while True:
|
2010-12-24 13:27:36 +00:00
|
|
|
token = ox.to32(random.randint(32768, 1048575))
|
2010-12-24 12:54:35 +00:00
|
|
|
if models.UserProfile.objects.filter(reset_token=token).count() == 0:
|
|
|
|
break
|
2010-02-03 11:59:11 +00:00
|
|
|
user_profile = user.get_profile()
|
2010-12-24 12:54:35 +00:00
|
|
|
user_profile.reset_token = token
|
2010-02-03 11:59:11 +00:00
|
|
|
user_profile.save()
|
|
|
|
|
2010-12-24 12:54:35 +00:00
|
|
|
template = loader.get_template('password_reset_email.txt')
|
2010-02-06 08:24:39 +00:00
|
|
|
context = RequestContext(request, {
|
2010-12-24 12:54:35 +00:00
|
|
|
'url': request.build_absolute_uri("/"),
|
|
|
|
'token': token,
|
2010-02-03 11:59:11 +00:00
|
|
|
'sitename': settings.SITENAME,
|
|
|
|
})
|
|
|
|
message = template.render(context)
|
2010-12-24 12:54:35 +00:00
|
|
|
subject = '%s password reset' % settings.SITENAME
|
2010-02-03 11:59:11 +00:00
|
|
|
user.email_user(subject, message)
|
2010-12-24 12:54:35 +00:00
|
|
|
response = json_response({
|
|
|
|
'username': user.username
|
|
|
|
}, text='recover email sent')
|
2010-01-26 13:11:57 +00:00
|
|
|
else:
|
2010-12-24 09:23:34 +00:00
|
|
|
response = json_response({
|
|
|
|
'errors': {
|
2010-12-24 10:14:13 +00:00
|
|
|
'username_or_email': 'Username or email address not found'
|
2010-12-24 09:23:34 +00:00
|
|
|
}
|
|
|
|
})
|
2010-01-25 09:20:21 +00:00
|
|
|
else:
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response(status=400, text='invalid data')
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-24 12:54:35 +00:00
|
|
|
actions.register(requestToken)
|
2010-07-22 13:03:16 +00:00
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def findUser(request):
|
2010-07-22 13:03:16 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
key: "username",
|
|
|
|
value: "foo", operator: "="
|
|
|
|
}
|
2010-07-22 13:03:16 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'status': {'code': int, 'text': string}
|
|
|
|
'data': {
|
|
|
|
users = ['user1', 'user2']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
#FIXME: support other operators and keys
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
response = json_response(status=200, text='ok')
|
|
|
|
response['data']['users'] = [u.username for u in User.objects.filter(username__iexact=data['value'])]
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(findUser)
|
2010-01-25 09:20:21 +00:00
|
|
|
|
2010-02-16 10:10:53 +00:00
|
|
|
class ContactForm(forms.Form):
|
|
|
|
email = forms.EmailField()
|
|
|
|
subject = forms.TextInput()
|
|
|
|
message = forms.TextInput()
|
|
|
|
|
2010-12-22 07:45:37 +00:00
|
|
|
def contact(request):
|
2010-02-16 10:10:53 +00:00
|
|
|
'''
|
2010-12-24 09:23:34 +00:00
|
|
|
param data {
|
|
|
|
'email': string,
|
|
|
|
'message': string
|
|
|
|
}
|
2010-02-16 10:10:53 +00:00
|
|
|
|
2010-12-24 09:23:34 +00:00
|
|
|
return {
|
|
|
|
'status': {'code': int, 'text': string}
|
|
|
|
}
|
2010-02-16 10:10:53 +00:00
|
|
|
'''
|
|
|
|
data = json.loads(request.POST['data'])
|
|
|
|
form = ContactForm(data, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
email = data['email']
|
|
|
|
template = loader.get_template('contact_email.txt')
|
|
|
|
context = RequestContext(request, {
|
|
|
|
'sitename': settings.SITENAME,
|
|
|
|
'email': email,
|
|
|
|
'message': data['message'],
|
|
|
|
})
|
|
|
|
message = template.render(context)
|
|
|
|
subject = '%s contact: %s' % (settings.SITENAME, data['subject'])
|
|
|
|
response = json_response(text='message sent')
|
|
|
|
try:
|
|
|
|
send_mail(subject, message, email, [settings.DEFAULT_FROM_EMAIL, ])
|
|
|
|
except BadHeaderError:
|
|
|
|
response = json_response(status=400, text='invalid data')
|
|
|
|
else:
|
|
|
|
response = json_response(status=400, text='invalid data')
|
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(contact)
|
2010-02-16 10:10:53 +00:00
|
|
|
|
2010-01-25 09:20:21 +00:00
|
|
|
@login_required_json
|
2010-12-22 07:45:37 +00:00
|
|
|
def preferences(request):
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-01-26 13:11:57 +00:00
|
|
|
param data
|
|
|
|
string
|
2010-01-29 11:03:45 +00:00
|
|
|
array
|
|
|
|
object
|
|
|
|
|
2010-01-26 13:11:57 +00:00
|
|
|
return
|
2010-01-29 11:03:45 +00:00
|
|
|
if data is empy or {}
|
2010-01-26 13:11:57 +00:00
|
|
|
if data is string:
|
|
|
|
return preference with name
|
2010-01-29 11:03:45 +00:00
|
|
|
if data is array:
|
2010-01-26 13:11:57 +00:00
|
|
|
return preferences with names
|
2010-01-29 11:03:45 +00:00
|
|
|
if data is object:
|
2010-01-26 13:11:57 +00:00
|
|
|
set key values in dict as preferences
|
2010-01-25 09:20:21 +00:00
|
|
|
'''
|
2010-02-10 13:10:28 +00:00
|
|
|
response = json_response()
|
2010-01-25 09:20:21 +00:00
|
|
|
if 'data' not in request.POST:
|
2010-11-27 12:12:53 +00:00
|
|
|
response['data']['preferences'] = models.get_preferences(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'] = {}
|
2010-11-27 12:12:53 +00:00
|
|
|
response['data']['preferences'][data] = models.get_preference(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-11-27 12:12:53 +00:00
|
|
|
response['preferences'][preference] = models.get_preference(request.user, preference)
|
2010-01-25 09:20:21 +00:00
|
|
|
elif isinstance(data, dict):
|
2010-01-29 11:03:45 +00:00
|
|
|
if not data:
|
2010-11-27 12:12:53 +00:00
|
|
|
response['data']['preferences'] = models.get_preferences(request.user)
|
2010-01-29 11:03:45 +00:00
|
|
|
else:
|
|
|
|
del response['data']
|
|
|
|
for key in data:
|
2010-11-27 12:12:53 +00:00
|
|
|
models.set_preference(request.user, key, data[key])
|
2010-01-25 09:20:21 +00:00
|
|
|
return render_to_json_response(response)
|
2010-12-22 07:45:37 +00:00
|
|
|
actions.register(preferences)
|
|
|
|
|