return form errors, use site.json user.ui for logged in users for now

This commit is contained in:
j 2010-12-24 14:53:34 +05:30
parent afd981651a
commit 31cf62c752
4 changed files with 196 additions and 75 deletions

View file

@ -36,6 +36,7 @@ from api.actions import actions
import models import models
import tasks import tasks
@login_required_json @login_required_json
def removeVolume(request): def removeVolume(request):
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
@ -44,7 +45,7 @@ def removeVolume(request):
volume = models.Volume.objects.get(user=user, name=data['volume']) volume = models.Volume.objects.get(user=user, name=data['volume'])
volume.files.delete() volume.files.delete()
volume.delete() volume.delete()
response = json_response(status=200, text='ok') response = json_response()
except models.Volume.DoesNotExist: except models.Volume.DoesNotExist:
response = json_response(status=404, text='volume not found') response = json_response(status=404, text='volume not found')
return render_to_json_response(response) return render_to_json_response(response)
@ -124,7 +125,7 @@ def upload(request):
'data': {info: object, rename: object}} 'data': {info: object, rename: object}}
''' '''
user = request.user user = request.user
f = get_object_or_404(models.File, oshash=request.POST['oshash']) f = get_object_or_404_json(models.File, oshash=request.POST['oshash'])
if 'frame' in request.FILES: if 'frame' in request.FILES:
if f.frames.count() == 0: if f.frames.count() == 0:
for frame in request.FILES.getlist('frame'): for frame in request.FILES.getlist('frame'):
@ -142,7 +143,7 @@ def upload(request):
f.data.save('data.raw', request.FILES['file']) f.data.save('data.raw', request.FILES['file'])
f.available = True f.available = True
f.save() f.save()
response = json_response(status=200, text='file saved') response = json_response(text='file saved')
else: else:
response = json_response(status=403, text='permissino denied') response = json_response(status=403, text='permissino denied')
return render_to_json_response(response) return render_to_json_response(response)
@ -215,19 +216,36 @@ def taskStatus(request):
actions.register(taskStatus) actions.register(taskStatus)
@login_required_json @login_required_json
def editFile(request): #FIXME: should this be file.files. or part of update def editFile(request):
''' '''
change file / imdb link change file / item link
param data
oshash hash of file
itemId new itemId
return {'status': {'code': int, 'text': string},
'data': {imdbId:string }}
''' '''
response = json_response(status=501, text='not implemented') #FIXME: permissions, need to be checked
data = json.loads(request.POST['data'])
f = get_object_or_404_json(models.File, oshash=data['oshash'])
response = json_response()
if f.item.id != data['itemId']:
if len(data['itemId']) != 7:
folder = f.instances.all()[0].folder
item_info = parse_path(folder)
item = get_item(item_info)
else:
item = get_item({'imdbId': data['itemId']})
f.item = item
f.save()
#FIXME: other things might need updating here
response = json_response(text='updated')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(editFile) actions.register(editFile)
def lookup_file(request, oshash): def lookup_file(request, oshash):
f = get_object_or_404(models.File, oshash=oshash) f = get_object_or_404(models.File, oshash=oshash)
return redirect(f.item.get_absolute_url()) return redirect(f.item.get_absolute_url())
""" """
def fileInfo(request): def fileInfo(request):

View file

@ -77,8 +77,11 @@ def _get_positions(ids, get_ids):
def find(request): def find(request):
''' '''
param data param data {
{'query': query, 'sort': array, 'range': array} 'query': query,
'sort': array,
'range': array
}
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
@ -102,8 +105,12 @@ def find(request):
'data': {items: array}} 'data': {items: array}}
Groups Groups
param data param data {
{'query': query, 'key': string, 'group': string, 'range': array} 'query': query,
'key': string,
'group': string,
'range': array
}
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
@ -123,8 +130,10 @@ Groups
'data': {items: int}} 'data': {items: int}}
Positions Positions
param data param data {
{'query': query, 'ids': []} 'query': query,
'ids': []
}
query: query object, more on query syntax at query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax https://wiki.0x2620.org/wiki/pandora/QuerySyntax
@ -234,10 +243,14 @@ actions.register(getItem)
@login_required_json @login_required_json
def editItem(request): def editItem(request):
''' '''
param data param data {
{id: string, key: value,..} id: string,
return {'status': {'code': int, 'text': string}, key: value,..
'data': {}} }
return {
status: {'code': int, 'text': string},
data: {}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Item, itemId=data['id']) item = get_object_or_404_json(models.Item, itemId=data['id'])
@ -272,10 +285,13 @@ actions.register(removeItem)
''' '''
def parse(request): #parse path and return info def parse(request): #parse path and return info
''' '''
param data param data {
{path: string} path: string
return {'status': {'code': int, 'text': string}, }
data: {imdb: string}} return {
status: {'code': int, 'text': string},
data: {imdb: string}
}
''' '''
path = json.loads(request.POST['data'])['path'] path = json.loads(request.POST['data'])['path']
response = json_response(utils.parse_path(path)) response = json_response(utils.parse_path(path))
@ -285,10 +301,14 @@ actions.register(parse)
def setPosterFrame(request): #parse path and return info def setPosterFrame(request): #parse path and return info
''' '''
param data param data {
{id: itemId, position: float} id: itemId,
return {'status': {'code': int, 'text': string}, position: float
data: {}} }
return {
status: {'code': int, 'text': string},
data: {}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Item, itemId=data['id']) item = get_object_or_404_json(models.Item, itemId=data['id'])
@ -296,7 +316,7 @@ def setPosterFrame(request): #parse path and return info
#FIXME: some things need to be updated after changing this #FIXME: some things need to be updated after changing this
item.poster_frame = data['position'] item.poster_frame = data['position']
item.save() item.save()
response = json_response(status=200, text='ok') response = json_response()
else: else:
response = json_response(status=403, text='permissino denied') response = json_response(status=403, text='permissino denied')
return render_to_json_response(response) return render_to_json_response(response)
@ -304,10 +324,14 @@ actions.register(setPosterFrame)
def setPoster(request): #parse path and return info def setPoster(request): #parse path and return info
''' '''
param data param data {
{id: itemId, url: string} id: itemId,
return {'status': {'code': int, 'text': string}, url: string
data: {poster: {url,width,height}}} }
return {
status: {'code': int, 'text': string},
data: {poster: {url,width,height}}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
item = get_object_or_404_json(models.Item, itemId=data['id']) item = get_object_or_404_json(models.Item, itemId=data['id'])
@ -319,7 +343,7 @@ def setPoster(request): #parse path and return info
item.poster.delete() item.poster.delete()
item.save() item.save()
tasks.update_poster.delay(item.itemId) tasks.update_poster.delay(item.itemId)
response = json_response(status=200, text='ok') response = json_response()
response['data']['poster'] = item.get_poster() response['data']['poster'] = item.get_poster()
else: else:
response = json_response(status=403, text='invalid poster url') response = json_response(status=403, text='invalid poster url')
@ -330,10 +354,15 @@ actions.register(setPoster)
def getImdbId(request): def getImdbId(request):
''' '''
param data param data {
{title: string, director: string, year: string} title: string,
return {'status': {'code': int, 'text': string}, director: string,
'data': {imdbId:string }} year: string
}
return {
status: {'code': int, 'text': string},
data: {imdbId:string }
}
''' '''
imdbId = ox.web.imdb.guess(search_title, r['director'], timeout=-1) imdbId = ox.web.imdb.guess(search_title, r['director'], timeout=-1)
if imdbId: if imdbId:

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
import os
from datetime import datetime from datetime import datetime
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -15,6 +16,7 @@ class UserProfile(models.Model):
user = models.ForeignKey(User, unique=True) user = models.ForeignKey(User, unique=True)
files_updated = models.DateTimeField(default=datetime.now) files_updated = models.DateTimeField(default=datetime.now)
newsletter = models.BooleanField(default=True)
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)
@ -42,6 +44,10 @@ def get_user_json(user):
return json return json
def get_ui(user): def get_ui(user):
with open(os.path.join(settings.PROJECT_ROOT, 'templates', 'site.json')) as f:
site = json.load(f)
return site['user']['ui']
'''
return { return {
"columns": ["id", "title", "director", "country", "year", "language", "genre"], "columns": ["id", "title", "director", "country", "year", "language", "genre"],
"findQuery": {"conditions": [], "operator": ""}, "findQuery": {"conditions": [], "operator": ""},
@ -59,6 +65,7 @@ def get_ui(user):
"sort": settings.DEFAULT_SORT, "sort": settings.DEFAULT_SORT,
"theme": settings.DEFAULT_THEME "theme": settings.DEFAULT_THEME
} }
'''
def get_preferences(user): def get_preferences(user):
prefs = {} prefs = {}

View file

@ -19,8 +19,6 @@ import models
from api.actions import actions from api.actions import actions
def json_errors(form):
return {'status': {'code': 402, 'text': 'form error', 'data': form.errors}}
class LoginForm(forms.Form): class LoginForm(forms.Form):
username = forms.TextInput() username = forms.TextInput()
@ -28,41 +26,67 @@ class LoginForm(forms.Form):
def api_login(request): def api_login(request):
''' '''
param data param data {
{'username': username, 'password': password} username: 'username',
password: 'password'
}
return {'status': {'code': int, 'text': string}} return {
status: {'code': int, 'text': string}
data: {
errors: {
username: 'Unknown User',
password: 'Incorrect Password'
}
user: {
...
}
}
}
''' '''
response = json_response(status=403, text='login failed')
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = LoginForm(data, request.FILES) form = LoginForm(data, request.FILES)
if form.is_valid(): if form.is_valid():
user = authenticate(username=data['username'], password=data['password']) if models.User.objects.filter(username=form.data['username']).count() == 0:
if user is not None: response = json_response({
if user.is_active: 'errors': {
login(request, user) 'username': 'Unknown User'
user_json = models.get_user_json(user) }
response = json_response({'user': user_json}, })
text='You are logged in.')
else:
response = json_response(status=401,
text='Your account is disabled.')
else: else:
errors = json_errors(form) user = authenticate(username=data['username'], password=data['password'])
response = json_response(errors, if user is not None:
status=401, text='Your username and password were incorrect.') 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'
}
})
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(api_login, 'login') actions.register(api_login, 'login')
def api_logout(request): def api_logout(request):
''' '''
param data param data {
{} }
return {'status': {'code': int, 'text': string}} return {
status: {'code': int, 'text': string}
}
''' '''
response = json_response(text='logged out') response = json_response(text='logged out')
if request.user.is_authenticated(): if request.user.is_authenticated():
@ -77,18 +101,40 @@ class RegisterForm(forms.Form):
def register(request): def register(request):
''' '''
param data param data {
{'username': username, 'password': password, 'email': email} username: 'username',
password: 'password',
email: 'emailaddress'
}
return {'status': {'code': int, 'text': string}} return {
status: {'code': int, 'text': string}
data: {
errors: {
username: 'Unknown User',
password: 'Incorrect Password'
}
user: {
...
}
}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = RegisterForm(data, request.FILES) form = RegisterForm(data, request.FILES)
if form.is_valid(): if form.is_valid():
if models.User.objects.filter(username=form.data['username']).count() > 0: if models.User.objects.filter(username=form.data['username']).count() > 0:
response = json_response(status=400, text='username or email exists') response = json_response({
'errors': {
'username': 'Username taken'
}
})
elif models.User.objects.filter(email=form.data['email']).count() > 0: elif models.User.objects.filter(email=form.data['email']).count() > 0:
response = json_response(status=400, text='username or email exists') response = json_response({
'errors': {
'email': 'Email is used by another account'
}
})
else: else:
first_user = models.User.objects.count() == 0 first_user = models.User.objects.count() == 0
user = models.User(username=form.data['username'], email=form.data['email']) user = models.User(username=form.data['username'], email=form.data['email'])
@ -100,9 +146,12 @@ def register(request):
user = authenticate(username=form.data['username'], user = authenticate(username=form.data['username'],
password=form.data['password']) password=form.data['password'])
login(request, user) login(request, user)
response = json_response(text='account created') user_json = models.get_user_json(user)
response = json_response({
'user': user_json
}, text='account created')
else: else:
response = json_response(status=400, text='username exists') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
actions.register(register) actions.register(register)
@ -111,10 +160,18 @@ class RecoverForm(forms.Form):
def api_recover(request): def api_recover(request):
''' '''
param data param data {
{'username_or_email': username} username_or_email: username
}
return {'status': {'code': int, 'text': string}} return {
status: {'code': int, 'text': string}
data: {
errors: {
username_or_email: 'Username or email not found'
}
}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = RegisterForm(data, request.FILES) form = RegisterForm(data, request.FILES)
@ -144,7 +201,11 @@ def api_recover(request):
user.email_user(subject, message) user.email_user(subject, message)
response = json_response(text='recover email sent') response = json_response(text='recover email sent')
else: else:
response = json_response(status=404, text='username or email not found') response = json_response({
'errors': {
'username_or_email': 'Username or email not found'
}
})
else: else:
response = json_response(status=400, text='invalid data') response = json_response(status=400, text='invalid data')
return render_to_json_response(response) return render_to_json_response(response)
@ -152,8 +213,10 @@ actions.register(api_recover, 'recover')
def findUser(request): def findUser(request):
''' '''
param data param data {
{key: "username", value: "foo", operator: "="} key: "username",
value: "foo", operator: "="
}
return { return {
'status': {'code': int, 'text': string} 'status': {'code': int, 'text': string}
@ -195,10 +258,14 @@ class ContactForm(forms.Form):
def contact(request): def contact(request):
''' '''
param data param data {
{'email': string, 'message': string} 'email': string,
'message': string
}
return {'status': {'code': int, 'text': string}} return {
'status': {'code': int, 'text': string}
}
''' '''
data = json.loads(request.POST['data']) data = json.loads(request.POST['data'])
form = ContactForm(data, request.FILES) form = ContactForm(data, request.FILES)