pandora/pandora/document/views.py

325 lines
11 KiB
Python
Raw Normal View History

2013-03-24 12:28:57 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
from ox.utils import json
from ox.django.api import actions
from ox.django.decorators import login_required_json
from ox.django.http import HttpFileResponse
2014-02-16 10:15:45 +00:00
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json, json_response, HttpErrorJson
2013-03-24 12:28:57 +00:00
from django import forms
2014-01-07 11:05:10 +00:00
from django.db.models import Sum
2013-03-24 12:28:57 +00:00
from item import utils
from item.models import Item
from itemlist.models import List
from archive.chunk import process_chunk
from changelog.models import add_changelog
2013-03-24 12:28:57 +00:00
import models
def get_document_or_404_json(id):
2014-02-16 10:15:45 +00:00
try:
return models.Document.get(id)
except models.Document.DoesNotExist:
response = {'status': {'code': 404,
'text': 'Document not found'}}
raise HttpErrorJson(response)
2013-03-24 12:28:57 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def addDocument(request, data):
'''
2014-12-18 18:18:18 +00:00
Adds one or more documents to one or more items
takes {
item: string or [string], // one or more item ids
id: string or [string] // one or more document ids
}
returns {}
see: editDocument, findDocument, getDocument, removeDocument, sortDocuments
'''
response = json_response()
if 'ids' in data:
ids = data['ids']
else:
ids = [data['id']]
if 'item' in data:
if isinstance(data['item'], basestring):
2014-09-19 12:26:46 +00:00
item = Item.objects.get(public_id=data['item'])
if item.editable(request.user):
for id in ids:
document = models.Document.get(id)
document.add(item)
add_changelog(request, data, item.public_id)
else:
response = json_response(status=403, text='permission denied')
else:
2014-09-19 12:26:46 +00:00
for item in Item.objects.filter(public_id__in=data['item']):
if item.editable(request.user):
for id in ids:
document = models.Document.get(id)
document.add(item)
add_changelog(request, data, data['item'])
return render_to_json_response(response)
actions.register(addDocument, cache=False)
@login_required_json
2014-10-06 08:26:43 +00:00
def editDocument(request, data):
2013-03-24 12:28:57 +00:00
'''
2014-12-19 15:05:08 +00:00
Edits data for a document
2014-12-18 18:18:18 +00:00
takes {
id: string, // document id
name: string, // new document name
description: string // new document description
item: string // item id (optional)
}
returns {
2014-12-19 12:59:10 +00:00
id: string, // document id
... // more document properties
2014-12-18 18:18:18 +00:00
}
notes: If `item` is present, this will not edit the global description of
the document, but its specific description in the context of the given
item.
see: addDocument, findDocument, getDocument, removeDocument, sortDocuments
2013-03-24 12:28:57 +00:00
'''
response = json_response()
2014-09-19 12:26:46 +00:00
item = 'item' in data and Item.objects.get(public_id=data['item']) or None
2013-03-24 12:28:57 +00:00
if data['id']:
2013-05-27 11:21:08 +00:00
document = models.Document.get(data['id'])
if document.editable(request.user, item):
add_changelog(request, data)
document.edit(data, request.user, item)
2013-05-27 11:21:08 +00:00
document.save()
response['data'] = document.json(user=request.user, item=item)
2013-03-24 12:28:57 +00:00
else:
2013-05-27 11:21:08 +00:00
response = json_response(status=403, text='permission denied')
2013-03-24 12:28:57 +00:00
else:
2013-05-27 11:21:08 +00:00
response = json_response(status=500, text='invalid request')
2013-03-24 12:28:57 +00:00
return render_to_json_response(response)
actions.register(editDocument, cache=False)
2013-03-24 12:28:57 +00:00
2014-01-08 04:32:57 +00:00
def _order_query(qs, sort, item=None):
2013-03-24 12:28:57 +00:00
order_by = []
for e in sort:
operator = e['operator']
if operator != '-':
operator = ''
key = {
'name': 'name_sort',
2014-01-08 09:12:44 +00:00
'description': 'descriptions__description_sort'
2014-01-08 04:32:57 +00:00
if item else 'description_sort',
2014-01-07 11:05:10 +00:00
'dimensions': 'dimensions_sort',
'index': 'items__itemproperties__index',
2013-03-24 12:28:57 +00:00
}.get(e['key'], e['key'])
if key == 'resolution':
order_by.append('%swidth'%operator)
order_by.append('%sheight'%operator)
else:
order = '%s%s' % (operator, key)
order_by.append(order)
2013-03-24 12:28:57 +00:00
if order_by:
2014-01-07 11:05:10 +00:00
qs = qs.order_by(*order_by, nulls_last=True)
2013-03-24 12:28:57 +00:00
qs = qs.distinct()
return qs
2014-01-08 04:32:57 +00:00
def get_item(query):
for c in query.get('conditions'):
if c.get('key') == 'item':
return c.get('value')
return None
2013-03-24 12:28:57 +00:00
def parse_query(data, user):
query = {}
query['range'] = [0, 100]
query['sort'] = [{'key':'user', 'operator':'+'}, {'key':'name', 'operator':'+'}]
for key in ('keys', 'group', 'file', 'range', 'position', 'positions', 'sort'):
if key in data:
query[key] = data[key]
query['qs'] = models.Document.objects.find(data, user).exclude(name='')
2014-01-08 04:32:57 +00:00
query['item'] = get_item(data['query'])
2013-03-24 12:28:57 +00:00
return query
2014-10-06 08:26:43 +00:00
def findDocuments(request, data):
2013-03-24 12:28:57 +00:00
'''
2014-12-19 15:05:08 +00:00
Finds documents for a given query
2014-12-18 18:18:18 +00:00
takes {
query: object, // query object, see `find`
sort: [object], // list of sort objects, see `find`
range: [int, int], // range of results, per current sort order
keys: [string] // list of keys to return
}
returns {
items: [{ // list of documents
key: value, // item key and value
... // more key/value pairs
}]
}
Notes: Query keys and keys to be returned can be 'extension', 'name',
'size' and 'user'.
see: addDocument, editDocument, find, getDocument, removeDocument,
sortDocuments
2013-03-24 12:28:57 +00:00
'''
query = parse_query(data, request.user)
#order
2014-01-08 04:32:57 +00:00
qs = _order_query(query['qs'], query['sort'], query['item'])
2013-03-24 12:28:57 +00:00
response = json_response()
if 'keys' in data:
qs = qs[query['range'][0]:query['range'][1]]
2014-01-08 04:32:57 +00:00
response['data']['items'] = [l.json(data['keys'], request.user, query['item']) for l in qs]
2013-03-24 12:28:57 +00:00
elif 'position' in data:
#FIXME: actually implement position requests
response['data']['position'] = 0
elif 'positions' in data:
ids = [i.get_id() for i in qs]
response['data']['positions'] = utils.get_positions(ids, query['positions'])
else:
2014-01-07 11:05:10 +00:00
r = qs.aggregate(
Sum('size')
)
2013-03-24 12:28:57 +00:00
response['data']['items'] = qs.count()
2014-01-08 08:35:02 +00:00
response['data']['size'] = r['size__sum'] or 0
2013-03-24 12:28:57 +00:00
return render_to_json_response(response)
actions.register(findDocuments)
2013-03-24 12:28:57 +00:00
2014-10-06 08:26:43 +00:00
def getDocument(request, data):
2014-02-16 10:15:45 +00:00
'''
2014-12-18 18:18:18 +00:00
Gets a document by id
takes {
id: string, // document id
keys: [string] // list of properties to return
}
returns {
key: value, // document key and value
... // more key/value pairs
}
see: addDocument, editDocument, findDocument, removeDocument, sortDocuments
2014-02-16 10:15:45 +00:00
'''
response = json_response({})
data['keys'] = data.get('keys', [])
document = get_document_or_404_json(data['id'])
response['data'] = document.json(keys=data['keys'], user=request.user)
return render_to_json_response(response)
actions.register(getDocument)
2013-03-24 12:28:57 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def removeDocument(request, data):
2013-03-24 12:28:57 +00:00
'''
2014-12-18 18:18:18 +00:00
Removes one or more documents, either from an item or from the database
takes {
id or ids: string or [string], // one or more document ids
item: string // item id (optional)
}
returns {}
notes: If `item` is present, this removes the documents from that item.
Otherwise, it removes the documents from the database.
see: addDocument, editDocument, findDocument, getDocument, sortDocuments
2013-03-24 12:28:57 +00:00
'''
response = json_response()
if 'ids' in data:
ids = data['ids']
2013-03-24 12:28:57 +00:00
else:
ids = [data['id']]
2014-09-19 12:26:46 +00:00
item = 'item' in data and Item.objects.get(public_id=data['item']) or None
2013-05-27 11:21:08 +00:00
if item:
if item.editable(request.user):
add_changelog(request, data, item.public_id)
2013-05-27 11:21:08 +00:00
for id in ids:
document = models.Document.get(id)
document.remove(item)
else:
2013-05-27 11:21:08 +00:00
response = json_response(status=403, text='not allowed')
else:
add_changelog(request, data, ids)
2013-05-27 11:21:08 +00:00
for id in ids:
document = models.Document.get(id)
if document.editable(request.user):
document.delete()
else:
response = json_response(status=403, text='not allowed')
break
2013-03-24 12:28:57 +00:00
return render_to_json_response(response)
actions.register(removeDocument, cache=False)
2013-03-24 12:28:57 +00:00
2013-05-27 11:21:08 +00:00
@login_required_json
2014-10-06 08:26:43 +00:00
def sortDocuments(request, data):
2013-05-27 11:21:08 +00:00
'''
Sets the sort order for the documents associated with a given item
2014-12-18 18:18:18 +00:00
takes {
item: string, // item id
2014-12-19 15:31:57 +00:00
ids: [string] // ordered list of document ids
2014-12-18 18:18:18 +00:00
}
returns {}
see: addDocument, editDocument, findDocument, removeDocument, sortDocuments
2013-05-27 11:21:08 +00:00
'''
index = 0
2014-09-19 12:26:46 +00:00
item = Item.objects.get(public_id=data['item'])
2013-05-27 11:21:08 +00:00
ids = data['ids']
if item.editable(request.user):
for i in ids:
document = models.Document.get(i)
models.ItemProperties.objects.filter(item=item, document=document).update(index=index)
index += 1
response = json_response()
add_changelog(request, data, item.public_id)
2013-05-27 11:21:08 +00:00
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
actions.register(sortDocuments, cache=False)
def file(request, id, name=None):
document = models.Document.get(id)
return HttpFileResponse(document.file.path)
2013-03-24 12:28:57 +00:00
2014-02-02 06:30:58 +00:00
def thumbnail(request, id, size=256, page=None):
size = int(size)
document = get_document_or_404_json(id)
2014-02-02 06:30:58 +00:00
return HttpFileResponse(document.thumbnail(size, page=page))
2013-03-24 12:28:57 +00:00
@login_required_json
def upload(request):
if 'id' in request.GET:
file = models.Document.get(request.GET['id'])
2013-03-24 12:28:57 +00:00
else:
file = None
2013-03-24 12:28:57 +00:00
extension = request.POST['filename'].split('.')
name = '.'.join(extension[:-1])
extension = extension[-1].lower()
response = json_response(status=400, text='this request requires POST')
if 'chunk' in request.FILES:
if file.editable(request.user):
response = process_chunk(request, file.save_chunk)
response['resultUrl'] = request.build_absolute_uri(file.get_absolute_url())
2013-03-24 12:28:57 +00:00
return render_to_json_response(response)
#init upload
else:
if not file:
created = False
num = 1
_name = name
while not created:
file, created = models.Document.objects.get_or_create(
user=request.user, name=name, extension=extension)
if not created:
num += 1
name = _name + ' [%d]' % num
file.name = name
file.extension = extension
file.uploading = True
file.save()
else:
#replace existing file
file.file.delete()
file.uploading = True
file.save()
upload_url = request.build_absolute_uri('/api/upload/document?id=%s' % file.get_id())
2013-03-24 12:28:57 +00:00
return render_to_json_response({
'uploadUrl': upload_url,
'url': request.build_absolute_uri(file.get_absolute_url()),
'result': 1
})
return render_to_json_response(response)