2013-03-24 12:28:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-08-23 10:27:06 +00:00
|
|
|
|
2015-10-25 21:08:13 +00:00
|
|
|
import os
|
2016-10-04 22:00:03 +00:00
|
|
|
import re
|
2015-10-25 21:08:13 +00:00
|
|
|
from glob import glob
|
2016-10-04 22:00:03 +00:00
|
|
|
import unicodedata
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2015-02-13 11:06:09 +00:00
|
|
|
import ox
|
2013-03-24 12:28:57 +00:00
|
|
|
from ox.utils import json
|
2016-02-20 09:06:41 +00:00
|
|
|
from oxdjango.api import actions
|
|
|
|
from oxdjango.decorators import login_required_json
|
|
|
|
from oxdjango.http import HttpFileResponse
|
|
|
|
from oxdjango.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
|
2016-10-04 22:00:03 +00:00
|
|
|
from django.db.models import Count, Sum
|
|
|
|
from django.conf import settings
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
from item import utils
|
2013-05-27 10:13:59 +00:00
|
|
|
from item.models import Item
|
2014-01-09 08:05:35 +00:00
|
|
|
from itemlist.models import List
|
2015-02-13 11:06:09 +00:00
|
|
|
from entity.models import Entity
|
2014-04-11 16:56:22 +00:00
|
|
|
from archive.chunk import process_chunk
|
2014-12-17 13:45:46 +00:00
|
|
|
from changelog.models import add_changelog
|
2014-04-11 16:56:22 +00:00
|
|
|
|
2016-08-23 10:27:06 +00:00
|
|
|
from . import models
|
2020-09-11 11:55:01 +00:00
|
|
|
from . import tasks
|
2021-11-15 15:20:08 +00:00
|
|
|
from . import page_views
|
2013-03-24 12:28:57 +00:00
|
|
|
|
2017-01-24 23:07:04 +00:00
|
|
|
def get_document_or_404_json(request, id):
|
|
|
|
response = {'status': {'code': 404,
|
|
|
|
'text': 'Document not found'}}
|
2014-02-16 10:15:45 +00:00
|
|
|
try:
|
2017-01-24 23:07:04 +00:00
|
|
|
document = models.Document.get(id)
|
2015-02-20 15:13:23 +00:00
|
|
|
except:
|
2014-02-16 10:15:45 +00:00
|
|
|
raise HttpErrorJson(response)
|
2017-01-24 23:07:04 +00:00
|
|
|
if not document.access(request.user):
|
|
|
|
raise HttpErrorJson(response)
|
|
|
|
return document
|
2013-03-24 12:28:57 +00:00
|
|
|
|
|
|
|
@login_required_json
|
2014-10-06 08:26:43 +00:00
|
|
|
def addDocument(request, data):
|
2013-05-27 10:13:59 +00:00
|
|
|
'''
|
2016-10-04 22:00:03 +00:00
|
|
|
Create new html document
|
|
|
|
takes {
|
|
|
|
title: string
|
|
|
|
}
|
|
|
|
|
|
|
|
or
|
|
|
|
|
2014-12-18 18:18:18 +00:00
|
|
|
Adds one or more documents to one or more items
|
|
|
|
takes {
|
2015-02-13 11:06:09 +00:00
|
|
|
item: string or [string], // one or more item ids (optional)
|
|
|
|
entity: string or [string], // one or more entity ids (optional)
|
2014-12-18 18:18:18 +00:00
|
|
|
id: string or [string] // one or more document ids
|
|
|
|
}
|
2015-02-13 11:06:09 +00:00
|
|
|
notes: either `item` or `entity` must be provided
|
2014-12-18 18:18:18 +00:00
|
|
|
returns {}
|
2015-05-20 10:14:21 +00:00
|
|
|
see: editDocument, findDocuments, getDocument, removeDocument, sortDocuments
|
2013-05-27 10:13:59 +00:00
|
|
|
'''
|
|
|
|
response = json_response()
|
2016-10-04 22:00:03 +00:00
|
|
|
if 'title' in data:
|
|
|
|
doc = models.Document(user=request.user, extension='html')
|
|
|
|
doc.data['title'] = data['title']
|
|
|
|
doc.save()
|
|
|
|
response = json_response(status=200, text='created')
|
|
|
|
response['data'] = doc.json(user=request.user)
|
|
|
|
add_changelog(request, data, doc.get_id())
|
2013-05-27 10:13:59 +00:00
|
|
|
else:
|
2016-10-04 22:00:03 +00:00
|
|
|
if 'ids' in data:
|
|
|
|
ids = data['ids']
|
2014-01-16 05:30:54 +00:00
|
|
|
else:
|
2016-10-04 22:00:03 +00:00
|
|
|
ids = [data['id']]
|
|
|
|
if 'item' in data:
|
2020-05-29 10:17:18 +00:00
|
|
|
if isinstance(data['item'], str):
|
2016-10-04 22:00:03 +00:00
|
|
|
item = Item.objects.get(public_id=data['item'])
|
2014-01-16 05:30:54 +00:00
|
|
|
if item.editable(request.user):
|
|
|
|
for id in ids:
|
|
|
|
document = models.Document.get(id)
|
|
|
|
document.add(item)
|
2016-10-04 22:00:03 +00:00
|
|
|
add_changelog(request, data, item.public_id)
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
2015-02-13 11:06:09 +00:00
|
|
|
else:
|
2016-10-04 22:00:03 +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'])
|
|
|
|
elif 'entity' in data:
|
2020-05-29 10:17:18 +00:00
|
|
|
if isinstance(data['entity'], str):
|
2016-10-04 22:00:03 +00:00
|
|
|
entity = Entity.get(data['entity'])
|
2015-02-13 11:06:09 +00:00
|
|
|
if entity.editable(request.user):
|
|
|
|
for id in ids:
|
|
|
|
document = models.Document.get(id)
|
|
|
|
entity.add(document)
|
2016-10-04 22:00:03 +00:00
|
|
|
add_changelog(request, data, entity.get_id())
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='permission denied')
|
|
|
|
else:
|
|
|
|
for entity in Entity.objects.filter(id__in=map(ox.fromAZ, data['entity'])):
|
|
|
|
if entity.editable(request.user):
|
|
|
|
for id in ids:
|
|
|
|
document = models.Document.get(id)
|
|
|
|
entity.add(document)
|
|
|
|
add_changelog(request, data, data['entity'])
|
|
|
|
else:
|
|
|
|
response = json_response(status=500, text='invalid request')
|
2013-05-27 10:13:59 +00:00
|
|
|
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
|
2016-10-04 22:00:03 +00:00
|
|
|
|
|
|
|
key: value, // set new data
|
2014-12-18 18:18:18 +00:00
|
|
|
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.
|
2015-05-20 10:14:21 +00:00
|
|
|
see: addDocument, findDocuments, 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']:
|
2018-09-18 21:17:32 +00:00
|
|
|
if isinstance(data['id'], list):
|
2020-09-11 11:55:01 +00:00
|
|
|
add_changelog(request, data)
|
2020-09-11 11:57:54 +00:00
|
|
|
t = tasks.bulk_edit.delay(data, request.user.username)
|
2020-09-11 11:55:01 +00:00
|
|
|
response['data']['taskId'] = t.task_id
|
2013-03-24 12:28:57 +00:00
|
|
|
else:
|
2020-09-11 11:55:01 +00:00
|
|
|
document = models.Document.get(data['id'])
|
2018-09-18 21:17:32 +00:00
|
|
|
if document.editable(request.user, item):
|
2020-09-11 11:55:01 +00:00
|
|
|
add_changelog(request, data)
|
2018-09-18 21:17:32 +00:00
|
|
|
document.edit(data, request.user, item)
|
|
|
|
document.save()
|
|
|
|
response['data'] = document.json(user=request.user, item=item)
|
|
|
|
else:
|
|
|
|
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)
|
2013-05-27 10:13:59 +00:00
|
|
|
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):
|
2016-10-04 22:00:03 +00:00
|
|
|
prefix = 'sort__'
|
2013-03-24 12:28:57 +00:00
|
|
|
order_by = []
|
|
|
|
for e in sort:
|
|
|
|
operator = e['operator']
|
|
|
|
if operator != '-':
|
|
|
|
operator = ''
|
|
|
|
key = {
|
2014-01-08 09:12:44 +00:00
|
|
|
'description': 'descriptions__description_sort'
|
2016-10-04 22:00:03 +00:00
|
|
|
if item else 'description',
|
2014-01-07 11:05:10 +00:00
|
|
|
'index': 'items__itemproperties__index',
|
2016-10-04 22:00:03 +00:00
|
|
|
#fixme:
|
|
|
|
'position': 'id',
|
|
|
|
'name': 'title',
|
2013-03-24 12:28:57 +00:00
|
|
|
}.get(e['key'], e['key'])
|
2014-01-04 12:29:11 +00:00
|
|
|
if key == 'resolution':
|
2017-03-02 11:37:53 +00:00
|
|
|
order_by.append('%swidth' % operator)
|
|
|
|
order_by.append('%sheight' % operator)
|
2014-01-04 12:29:11 +00:00
|
|
|
else:
|
2017-03-02 11:37:53 +00:00
|
|
|
if '__' not in key and key not in ('created', 'modified'):
|
2016-10-04 22:00:03 +00:00
|
|
|
key = "%s%s" % (prefix, key)
|
2014-01-04 12:29:11 +00:00
|
|
|
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
|
|
|
|
|
2016-10-04 22:00:03 +00:00
|
|
|
def _order_by_group(query):
|
|
|
|
if 'sort' in query:
|
|
|
|
if len(query['sort']) == 1 and query['sort'][0]['key'] == 'items':
|
|
|
|
order_by = query['sort'][0]['operator'] == '-' and '-items' or 'items'
|
|
|
|
if query['group'] == "year":
|
|
|
|
secondary = query['sort'][0]['operator'] == '-' and '-sortvalue' or 'sortvalue'
|
|
|
|
order_by = (order_by, secondary)
|
|
|
|
elif query['group'] != "keyword":
|
|
|
|
order_by = (order_by, 'sortvalue')
|
|
|
|
else:
|
|
|
|
order_by = (order_by, 'value')
|
|
|
|
else:
|
|
|
|
order_by = query['sort'][0]['operator'] == '-' and '-sortvalue' or 'sortvalue'
|
|
|
|
order_by = (order_by, 'items')
|
|
|
|
else:
|
|
|
|
order_by = ('-sortvalue', 'items')
|
|
|
|
return order_by
|
|
|
|
|
2014-01-08 04:32:57 +00:00
|
|
|
def get_item(query):
|
2016-02-15 14:21:36 +00:00
|
|
|
for c in query.get('conditions', []):
|
2014-01-08 04:32:57 +00:00
|
|
|
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]
|
2017-03-02 11:37:53 +00:00
|
|
|
query['sort'] = [{'key': 'user', 'operator': '+'}, {'key': 'name', 'operator': '+'}]
|
2013-03-24 12:28:57 +00:00
|
|
|
for key in ('keys', 'group', 'file', 'range', 'position', 'positions', 'sort'):
|
|
|
|
if key in data:
|
|
|
|
query[key] = data[key]
|
2019-10-19 11:44:57 +00:00
|
|
|
#print(query.get('sort'), data.get('sort'))
|
2016-10-04 22:00:03 +00:00
|
|
|
query['qs'] = models.Document.objects.find(data, user)
|
2016-02-15 14:21:36 +00:00
|
|
|
query['item'] = get_item(data.get('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()
|
2016-10-04 22:00:03 +00:00
|
|
|
if 'group' in query:
|
|
|
|
response['data']['items'] = []
|
|
|
|
items = 'items'
|
|
|
|
document_qs = query['qs']
|
|
|
|
order_by = _order_by_group(query)
|
|
|
|
qs = models.Facet.objects.filter(key=query['group']).filter(document__id__in=document_qs)
|
|
|
|
qs = qs.values('value').annotate(items=Count('id')).order_by(*order_by)
|
|
|
|
|
|
|
|
if 'positions' in query:
|
|
|
|
response['data']['positions'] = {}
|
|
|
|
ids = [j['value'] for j in qs]
|
|
|
|
response['data']['positions'] = utils.get_positions(ids, query['positions'])
|
|
|
|
elif 'range' in data:
|
|
|
|
qs = qs[query['range'][0]:query['range'][1]]
|
|
|
|
response['data']['items'] = [{'name': i['value'], 'items': i[items]} for i in qs]
|
|
|
|
else:
|
|
|
|
response['data']['items'] = qs.count()
|
|
|
|
elif 'keys' in data:
|
2013-03-24 12:28:57 +00:00
|
|
|
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:
|
2016-06-30 14:18:07 +00:00
|
|
|
ids = list(qs.values_list('id', flat=True))
|
|
|
|
response['data']['positions'] = utils.get_positions(ids, query['positions'], decode_id=True)
|
2013-03-24 12:28:57 +00:00
|
|
|
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)
|
2013-05-27 10:13:59 +00:00
|
|
|
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
|
|
|
|
}
|
2015-05-20 10:14:21 +00:00
|
|
|
see: addDocument, editDocument, findDocuments, removeDocument, sortDocuments
|
2014-02-16 10:15:45 +00:00
|
|
|
'''
|
|
|
|
response = json_response({})
|
|
|
|
data['keys'] = data.get('keys', [])
|
2017-01-24 23:07:04 +00:00
|
|
|
document = get_document_or_404_json(request, data['id'])
|
2014-02-16 10:15:45 +00:00
|
|
|
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
|
|
|
'''
|
2015-02-13 11:06:09 +00:00
|
|
|
Removes one or more documents, either from an item/entity or from the database
|
2014-12-18 18:18:18 +00:00
|
|
|
takes {
|
|
|
|
id or ids: string or [string], // one or more document ids
|
|
|
|
item: string // item id (optional)
|
2015-02-13 11:06:09 +00:00
|
|
|
entity: string // entity id (optional)
|
2014-12-18 18:18:18 +00:00
|
|
|
}
|
|
|
|
returns {}
|
|
|
|
notes: If `item` is present, this removes the documents from that item.
|
2015-02-13 11:06:09 +00:00
|
|
|
If `entity` is present, this removes the documents from that entity.
|
2014-12-18 18:18:18 +00:00
|
|
|
Otherwise, it removes the documents from the database.
|
2015-05-20 10:14:21 +00:00
|
|
|
see: addDocument, editDocument, findDocuments, getDocument, sortDocuments
|
2013-03-24 12:28:57 +00:00
|
|
|
'''
|
|
|
|
response = json_response()
|
2013-05-27 10:13:59 +00:00
|
|
|
|
|
|
|
if 'ids' in data:
|
|
|
|
ids = data['ids']
|
2013-03-24 12:28:57 +00:00
|
|
|
else:
|
2013-05-27 10:13:59 +00:00
|
|
|
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
|
2015-02-13 11:06:09 +00:00
|
|
|
entity = 'entity' in data and Entity.objects.get(id=ox.fromAZ(data['entity'])) or None
|
2013-05-27 11:21:08 +00:00
|
|
|
if item:
|
|
|
|
if item.editable(request.user):
|
2014-12-17 13:45:46 +00:00
|
|
|
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)
|
2013-05-27 10:13:59 +00:00
|
|
|
else:
|
2013-05-27 11:21:08 +00:00
|
|
|
response = json_response(status=403, text='not allowed')
|
2015-02-13 11:06:09 +00:00
|
|
|
elif entity:
|
|
|
|
if entity.editable(request.user):
|
|
|
|
add_changelog(request, data, entity.get_id())
|
|
|
|
for id in ids:
|
|
|
|
document = models.Document.get(id)
|
|
|
|
entity.remove(document)
|
|
|
|
else:
|
|
|
|
response = json_response(status=403, text='not allowed')
|
2013-05-27 11:21:08 +00:00
|
|
|
else:
|
2014-12-17 13:45:46 +00:00
|
|
|
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)
|
2013-05-27 10:13:59 +00:00
|
|
|
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
|
|
|
'''
|
2014-12-21 13:03:49 +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 {}
|
2015-05-20 10:14:21 +00:00
|
|
|
see: addDocument, editDocument, findDocuments, 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()
|
2014-12-17 13:45:46 +00:00
|
|
|
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)
|
|
|
|
|
2013-12-23 11:26:48 +00:00
|
|
|
def file(request, id, name=None):
|
2017-01-24 23:07:04 +00:00
|
|
|
document = get_document_or_404_json(request, id)
|
2013-05-27 10:13:59 +00:00
|
|
|
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):
|
2013-12-23 11:26:48 +00:00
|
|
|
size = int(size)
|
2017-01-24 23:07:04 +00:00
|
|
|
document = get_document_or_404_json(request, 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:
|
2013-05-27 10:13:59 +00:00
|
|
|
file = models.Document.get(request.GET['id'])
|
2015-10-25 21:08:13 +00:00
|
|
|
elif 'id' in request.POST:
|
|
|
|
file = models.Document.get(request.POST['id'])
|
2013-03-24 12:28:57 +00:00
|
|
|
else:
|
2014-01-09 08:05:35 +00:00
|
|
|
file = None
|
2015-10-25 21:08:13 +00:00
|
|
|
name, extension = request.POST['filename'].rsplit('.', 1)
|
2017-05-28 11:40:56 +00:00
|
|
|
extension = extension.lower()
|
2013-03-24 12:28:57 +00:00
|
|
|
response = json_response(status=400, text='this request requires POST')
|
|
|
|
if 'chunk' in request.FILES:
|
2014-04-11 16:56:22 +00:00
|
|
|
if file.editable(request.user):
|
|
|
|
response = process_chunk(request, file.save_chunk)
|
2016-10-04 22:00:03 +00:00
|
|
|
response['resultUrl'] = file.get_absolute_url()
|
2015-02-14 18:29:58 +00:00
|
|
|
# id is used to select document in dialog after upload
|
|
|
|
response['id'] = file.get_id()
|
2013-03-24 12:28:57 +00:00
|
|
|
return render_to_json_response(response)
|
|
|
|
#init upload
|
|
|
|
else:
|
2014-01-09 08:05:35 +00:00
|
|
|
if not file:
|
2017-05-28 11:40:56 +00:00
|
|
|
file = models.Document(user=request.user)
|
2016-10-04 22:00:03 +00:00
|
|
|
file.data['title'] = name
|
2014-01-09 08:05:35 +00:00
|
|
|
file.extension = extension
|
|
|
|
file.uploading = True
|
|
|
|
file.save()
|
2017-01-24 23:07:04 +00:00
|
|
|
elif file.editable(request.user):
|
2014-01-09 08:05:35 +00:00
|
|
|
#replace existing file
|
2015-10-25 21:08:13 +00:00
|
|
|
if file.file:
|
2016-09-23 11:38:00 +00:00
|
|
|
file.delete_cache()
|
2015-10-25 21:08:13 +00:00
|
|
|
file.file.delete()
|
2014-01-09 08:05:35 +00:00
|
|
|
file.uploading = True
|
2015-10-25 21:08:13 +00:00
|
|
|
name, extension = request.POST['filename'].rsplit('.', 1)
|
2017-05-28 11:40:56 +00:00
|
|
|
file.extension = extension
|
2015-10-26 15:10:12 +00:00
|
|
|
file.width = -1
|
|
|
|
file.pages = -1
|
2014-01-09 08:05:35 +00:00
|
|
|
file.save()
|
2017-01-24 23:07:04 +00:00
|
|
|
else:
|
|
|
|
return render_to_json_response(response)
|
2017-03-12 20:01:32 +00:00
|
|
|
add_changelog({
|
|
|
|
'user': request.user,
|
|
|
|
'action': 'uploadDocument',
|
|
|
|
}, {
|
|
|
|
'name': name,
|
|
|
|
'extension': extension
|
|
|
|
}, file.get_id())
|
2016-10-04 22:00:03 +00:00
|
|
|
upload_url = '/api/upload/document?id=%s' % file.get_id()
|
2013-03-24 12:28:57 +00:00
|
|
|
return render_to_json_response({
|
|
|
|
'uploadUrl': upload_url,
|
2016-10-04 22:00:03 +00:00
|
|
|
'url': file.get_absolute_url(),
|
2013-03-24 12:28:57 +00:00
|
|
|
'result': 1
|
|
|
|
})
|
|
|
|
return render_to_json_response(response)
|
2016-10-04 22:00:03 +00:00
|
|
|
|
|
|
|
def autocompleteDocuments(request, data):
|
|
|
|
'''
|
2019-12-08 15:46:08 +00:00
|
|
|
Returns autocomplete strings for a given document key and search string
|
2016-10-04 22:00:03 +00:00
|
|
|
takes {
|
|
|
|
key: string, // document key
|
|
|
|
value: string, // search string
|
|
|
|
operator: string, // '=', '==', '^', '$'
|
|
|
|
query: object, // document query to limit results, see `find`
|
|
|
|
range: [int, int] // range of results to return
|
|
|
|
}
|
|
|
|
returns {
|
|
|
|
items: [string, ...] // list of matching strings
|
|
|
|
}
|
|
|
|
see: autocomplete, autocompleteEntities
|
|
|
|
'''
|
|
|
|
if 'range' not in data:
|
|
|
|
data['range'] = [0, 10]
|
|
|
|
op = data.get('operator', '=')
|
|
|
|
|
|
|
|
key = utils.get_by_id(settings.CONFIG['documentKeys'], data['key'])
|
|
|
|
order_by = key.get('autocompleteSort', False)
|
|
|
|
if order_by:
|
|
|
|
for o in order_by:
|
|
|
|
if o['operator'] != '-':
|
|
|
|
o['operator'] = ''
|
|
|
|
order_by = ['%(operator)ssort__%(key)s' % o for o in order_by]
|
|
|
|
else:
|
|
|
|
order_by = ['-items']
|
|
|
|
|
|
|
|
qs = parse_query({'query': data.get('query', {})}, request.user)['qs']
|
|
|
|
response = json_response({})
|
|
|
|
response['data']['items'] = []
|
|
|
|
'''
|
|
|
|
for d in qs:
|
|
|
|
value = d.json().get(data['key'])
|
|
|
|
add = False
|
|
|
|
if value:
|
|
|
|
if op == '=' and data['value'] in value:
|
|
|
|
add = True
|
|
|
|
elif op == '==' and data['value'].lower() == value.lower():
|
|
|
|
add = True
|
|
|
|
elif op == '^' and value.lower().startswith(data['value'].lower()):
|
|
|
|
add = True
|
|
|
|
if add and value not in response['data']['items']:
|
|
|
|
response['data']['items'].append(value)
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
sort_type = key.get('sortType', key.get('type', 'string'))
|
|
|
|
qs = models.Facet.objects.filter(key=data['key'])
|
|
|
|
if data['value']:
|
|
|
|
value = unicodedata.normalize('NFKD', data['value']).lower()
|
|
|
|
if op == '=':
|
|
|
|
qs = qs.filter(value__icontains=value)
|
|
|
|
elif op == '==':
|
|
|
|
qs = qs.filter(value__iexact=value)
|
|
|
|
elif op == '^':
|
|
|
|
qs = qs.filter(value__istartswith=value)
|
|
|
|
elif op == '$':
|
|
|
|
qs = qs.filter(value__iendswith=value)
|
|
|
|
if 'query' in data:
|
|
|
|
document_query = parse_query({'query': data.get('query', {})}, request.user)['qs']
|
|
|
|
qs = qs.filter(document__in=document_query)
|
|
|
|
qs = qs.values('value').annotate(items=Count('id'))
|
|
|
|
qs = qs.order_by(*order_by)
|
|
|
|
qs = qs[data['range'][0]:data['range'][1]]
|
|
|
|
response = json_response({})
|
|
|
|
response['data']['items'] = [i['value'] for i in qs]
|
|
|
|
return render_to_json_response(response)
|
|
|
|
actions.register(autocompleteDocuments)
|