diff --git a/pandora/archive/views.py b/pandora/archive/views.py index fc466a9..7cc48d9 100644 --- a/pandora/archive/views.py +++ b/pandora/archive/views.py @@ -36,6 +36,7 @@ from api.actions import actions import models import tasks + @login_required_json def removeVolume(request): data = json.loads(request.POST['data']) @@ -44,7 +45,7 @@ def removeVolume(request): volume = models.Volume.objects.get(user=user, name=data['volume']) volume.files.delete() volume.delete() - response = json_response(status=200, text='ok') + response = json_response() except models.Volume.DoesNotExist: response = json_response(status=404, text='volume not found') return render_to_json_response(response) @@ -124,7 +125,7 @@ def upload(request): 'data': {info: object, rename: object}} ''' 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 f.frames.count() == 0: for frame in request.FILES.getlist('frame'): @@ -142,7 +143,7 @@ def upload(request): f.data.save('data.raw', request.FILES['file']) f.available = True f.save() - response = json_response(status=200, text='file saved') + response = json_response(text='file saved') else: response = json_response(status=403, text='permissino denied') return render_to_json_response(response) @@ -215,19 +216,36 @@ def taskStatus(request): actions.register(taskStatus) @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) actions.register(editFile) - def lookup_file(request, oshash): f = get_object_or_404(models.File, oshash=oshash) return redirect(f.item.get_absolute_url()) - """ def fileInfo(request): diff --git a/pandora/item/views.py b/pandora/item/views.py index ea6134a..ab98ad3 100644 --- a/pandora/item/views.py +++ b/pandora/item/views.py @@ -77,8 +77,11 @@ def _get_positions(ids, get_ids): def find(request): ''' - param data - {'query': query, 'sort': array, 'range': array} + param data { + 'query': query, + 'sort': array, + 'range': array + } query: query object, more on query syntax at https://wiki.0x2620.org/wiki/pandora/QuerySyntax @@ -102,8 +105,12 @@ def find(request): 'data': {items: array}} Groups - param data - {'query': query, 'key': string, 'group': string, 'range': array} + param data { + 'query': query, + 'key': string, + 'group': string, + 'range': array + } query: query object, more on query syntax at https://wiki.0x2620.org/wiki/pandora/QuerySyntax @@ -123,8 +130,10 @@ Groups 'data': {items: int}} Positions - param data - {'query': query, 'ids': []} + param data { + 'query': query, + 'ids': [] + } query: query object, more on query syntax at https://wiki.0x2620.org/wiki/pandora/QuerySyntax @@ -234,10 +243,14 @@ actions.register(getItem) @login_required_json def editItem(request): ''' - param data - {id: string, key: value,..} - return {'status': {'code': int, 'text': string}, - 'data': {}} + param data { + id: string, + key: value,.. + } + return { + status: {'code': int, 'text': string}, + data: {} + } ''' data = json.loads(request.POST['data']) 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 ''' - param data - {path: string} - return {'status': {'code': int, 'text': string}, - data: {imdb: string}} + param data { + path: string + } + return { + status: {'code': int, 'text': string}, + data: {imdb: string} + } ''' path = json.loads(request.POST['data'])['path'] response = json_response(utils.parse_path(path)) @@ -285,10 +301,14 @@ actions.register(parse) def setPosterFrame(request): #parse path and return info ''' - param data - {id: itemId, position: float} - return {'status': {'code': int, 'text': string}, - data: {}} + param data { + id: itemId, + position: float + } + return { + status: {'code': int, 'text': string}, + data: {} + } ''' data = json.loads(request.POST['data']) 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 item.poster_frame = data['position'] item.save() - response = json_response(status=200, text='ok') + response = json_response() else: response = json_response(status=403, text='permissino denied') return render_to_json_response(response) @@ -304,10 +324,14 @@ actions.register(setPosterFrame) def setPoster(request): #parse path and return info ''' - param data - {id: itemId, url: string} - return {'status': {'code': int, 'text': string}, - data: {poster: {url,width,height}}} + param data { + id: itemId, + url: string + } + return { + status: {'code': int, 'text': string}, + data: {poster: {url,width,height}} + } ''' data = json.loads(request.POST['data']) 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.save() tasks.update_poster.delay(item.itemId) - response = json_response(status=200, text='ok') + response = json_response() response['data']['poster'] = item.get_poster() else: response = json_response(status=403, text='invalid poster url') @@ -330,10 +354,15 @@ actions.register(setPoster) def getImdbId(request): ''' - param data - {title: string, director: string, year: string} - return {'status': {'code': int, 'text': string}, - 'data': {imdbId:string }} + param data { + title: string, + director: string, + year: string + } + return { + status: {'code': int, 'text': string}, + data: {imdbId:string } + } ''' imdbId = ox.web.imdb.guess(search_title, r['director'], timeout=-1) if imdbId: diff --git a/pandora/user/models.py b/pandora/user/models.py index 375361a..df214a4 100644 --- a/pandora/user/models.py +++ b/pandora/user/models.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- # vi:si:et:sw=4:sts=4:ts=4 +import os from datetime import datetime from django.contrib.auth.models import User @@ -15,6 +16,7 @@ class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) files_updated = models.DateTimeField(default=datetime.now) + newsletter = models.BooleanField(default=True) def user_post_save(sender, instance, **kwargs): profile, new = UserProfile.objects.get_or_create(user=instance) @@ -42,6 +44,10 @@ def get_user_json(user): return json 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 { "columns": ["id", "title", "director", "country", "year", "language", "genre"], "findQuery": {"conditions": [], "operator": ""}, @@ -59,6 +65,7 @@ def get_ui(user): "sort": settings.DEFAULT_SORT, "theme": settings.DEFAULT_THEME } + ''' def get_preferences(user): prefs = {} diff --git a/pandora/user/views.py b/pandora/user/views.py index 4f43164..23b39ef 100644 --- a/pandora/user/views.py +++ b/pandora/user/views.py @@ -19,8 +19,6 @@ import models from api.actions import actions -def json_errors(form): - return {'status': {'code': 402, 'text': 'form error', 'data': form.errors}} class LoginForm(forms.Form): username = forms.TextInput() @@ -28,41 +26,67 @@ class LoginForm(forms.Form): def api_login(request): ''' - param data - {'username': username, 'password': password} + param data { + 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']) 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.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.') + if models.User.objects.filter(username=form.data['username']).count() == 0: + response = json_response({ + 'errors': { + 'username': 'Unknown User' + } + }) else: - errors = json_errors(form) - response = json_response(errors, - status=401, text='Your username and password were incorrect.') + 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' + } + }) else: response = json_response(status=400, text='invalid data') - return render_to_json_response(response) actions.register(api_login, 'login') 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') if request.user.is_authenticated(): @@ -77,18 +101,40 @@ class RegisterForm(forms.Form): def register(request): ''' - param data - {'username': username, 'password': password, 'email': email} + param data { + 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']) form = RegisterForm(data, request.FILES) if form.is_valid(): 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: - response = json_response(status=400, text='username or email exists') + response = json_response({ + 'errors': { + 'email': 'Email is used by another account' + } + }) else: first_user = models.User.objects.count() == 0 user = models.User(username=form.data['username'], email=form.data['email']) @@ -100,9 +146,12 @@ def register(request): user = authenticate(username=form.data['username'], password=form.data['password']) 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: - response = json_response(status=400, text='username exists') + response = json_response(status=400, text='invalid data') return render_to_json_response(response) actions.register(register) @@ -111,10 +160,18 @@ class RecoverForm(forms.Form): def api_recover(request): ''' - param data - {'username_or_email': username} + param data { + 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']) form = RegisterForm(data, request.FILES) @@ -144,7 +201,11 @@ def api_recover(request): user.email_user(subject, message) response = json_response(text='recover email sent') 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: response = json_response(status=400, text='invalid data') return render_to_json_response(response) @@ -152,8 +213,10 @@ actions.register(api_recover, 'recover') def findUser(request): ''' - param data - {key: "username", value: "foo", operator: "="} + param data { + key: "username", + value: "foo", operator: "=" + } return { 'status': {'code': int, 'text': string} @@ -195,10 +258,14 @@ class ContactForm(forms.Form): def contact(request): ''' - param data - {'email': string, 'message': string} + param data { + 'email': string, + 'message': string + } - return {'status': {'code': int, 'text': string}} + return { + 'status': {'code': int, 'text': string} + } ''' data = json.loads(request.POST['data']) form = ContactForm(data, request.FILES)