Open Media Library
This commit is contained in:
commit
2ee2bc178a
228 changed files with 85988 additions and 0 deletions
0
oml/oxflask/__init__.py
Normal file
0
oml/oxflask/__init__.py
Normal file
154
oml/oxflask/api.py
Normal file
154
oml/oxflask/api.py
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import inspect
|
||||
import sys
|
||||
import json
|
||||
|
||||
from flask import request, Blueprint
|
||||
from .shortcuts import render_to_json_response, json_response
|
||||
|
||||
app = Blueprint('oxflask', __name__)
|
||||
|
||||
@app.route('/api/', methods=['POST', 'OPTIONS'])
|
||||
def api():
|
||||
if request.method == "OPTIONS":
|
||||
response = render_to_json_response({'status': {'code': 200, 'text': 'use POST'}})
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
if not 'action' in request.form:
|
||||
methods = actions.keys()
|
||||
api = []
|
||||
for f in sorted(methods):
|
||||
api.append({'name': f,
|
||||
'doc': actions.doc(f).replace('\n', '<br>\n')})
|
||||
return render_to_json_response(api)
|
||||
action = request.form['action']
|
||||
f = actions.get(action)
|
||||
if f:
|
||||
response = f(request)
|
||||
else:
|
||||
response = render_to_json_response(json_response(status=400,
|
||||
text='Unknown action %s' % action))
|
||||
response.headers['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
|
||||
def trim(docstring):
|
||||
if not docstring:
|
||||
return ''
|
||||
# Convert tabs to spaces (following the normal Python rules)
|
||||
# and split into a list of lines:
|
||||
lines = docstring.expandtabs().splitlines()
|
||||
# Determine minimum indentation (first line doesn't count):
|
||||
indent = sys.maxint
|
||||
for line in lines[1:]:
|
||||
stripped = line.lstrip()
|
||||
if stripped:
|
||||
indent = min(indent, len(line) - len(stripped))
|
||||
# Remove indentation (first line is special):
|
||||
trimmed = [lines[0].strip()]
|
||||
if indent < sys.maxint:
|
||||
for line in lines[1:]:
|
||||
trimmed.append(line[indent:].rstrip())
|
||||
# Strip off trailing and leading blank lines:
|
||||
while trimmed and not trimmed[-1]:
|
||||
trimmed.pop()
|
||||
while trimmed and not trimmed[0]:
|
||||
trimmed.pop(0)
|
||||
# Return a single string:
|
||||
return '\n'.join(trimmed)
|
||||
|
||||
|
||||
class ApiActions(dict):
|
||||
properties = {}
|
||||
versions = {}
|
||||
def __init__(self):
|
||||
|
||||
def api(request):
|
||||
'''
|
||||
returns list of all known api actions
|
||||
param data {
|
||||
docs: bool
|
||||
}
|
||||
if docs is true, action properties contain docstrings
|
||||
return {
|
||||
status: {'code': int, 'text': string},
|
||||
data: {
|
||||
actions: {
|
||||
'api': {
|
||||
cache: true,
|
||||
doc: 'recursion'
|
||||
},
|
||||
'hello': {
|
||||
cache: true,
|
||||
..
|
||||
}
|
||||
...
|
||||
}
|
||||
}
|
||||
}
|
||||
'''
|
||||
data = json.loads(request.form.get('data', '{}'))
|
||||
docs = data.get('docs', False)
|
||||
code = data.get('code', False)
|
||||
version = getattr(request, 'version', None)
|
||||
if version:
|
||||
_actions = self.versions.get(version, {}).keys()
|
||||
_actions = list(set(_actions + self.keys()))
|
||||
else:
|
||||
_actions = self.keys()
|
||||
_actions.sort()
|
||||
actions = {}
|
||||
for a in _actions:
|
||||
actions[a] = self.properties[a]
|
||||
if docs:
|
||||
actions[a]['doc'] = self.doc(a, version)
|
||||
if code:
|
||||
actions[a]['code'] = self.code(a, version)
|
||||
response = json_response({'actions': actions})
|
||||
return render_to_json_response(response)
|
||||
self.register(api)
|
||||
|
||||
def doc(self, name, version=None):
|
||||
if version:
|
||||
f = self.versions[version].get(name, self.get(name))
|
||||
else:
|
||||
f = self[name]
|
||||
return trim(f.__doc__)
|
||||
|
||||
def code(self, name, version=None):
|
||||
if version:
|
||||
f = self.versions[version].get(name, self.get(name))
|
||||
else:
|
||||
f = self[name]
|
||||
if name != 'api' and hasattr(f, 'func_closure') and f.func_closure:
|
||||
fc = filter(lambda c: hasattr(c.cell_contents, '__call__'), f.func_closure)
|
||||
f = fc[len(fc)-1].cell_contents
|
||||
info = f.func_code.co_filename
|
||||
info = u'%s:%s' % (info, f.func_code.co_firstlineno)
|
||||
return info, trim(inspect.getsource(f))
|
||||
|
||||
def register(self, method, action=None, cache=True, version=None):
|
||||
if not action:
|
||||
action = method.func_name
|
||||
if version:
|
||||
if not version in self.versions:
|
||||
self.versions[version] = {}
|
||||
self.versions[version][action] = method
|
||||
else:
|
||||
self[action] = method
|
||||
self.properties[action] = {'cache': cache}
|
||||
|
||||
def unregister(self, action):
|
||||
if action in self:
|
||||
del self[action]
|
||||
|
||||
actions = ApiActions()
|
||||
|
||||
def error(request):
|
||||
'''
|
||||
this action is used to test api error codes, it should return a 503 error
|
||||
'''
|
||||
success = error_is_success
|
||||
return render_to_json_response({})
|
||||
actions.register(error)
|
||||
27
oml/oxflask/db.py
Normal file
27
oml/oxflask/db.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from sqlalchemy.ext.mutable import Mutable
|
||||
|
||||
class MutableDict(Mutable, dict):
|
||||
@classmethod
|
||||
def coerce(cls, key, value):
|
||||
"Convert plain dictionaries to MutableDict."
|
||||
|
||||
if not isinstance(value, MutableDict):
|
||||
if isinstance(value, dict):
|
||||
return MutableDict(value)
|
||||
|
||||
# this call will raise ValueError
|
||||
return Mutable.coerce(key, value)
|
||||
else:
|
||||
return value
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
"Detect dictionary set events and emit change events."
|
||||
|
||||
dict.__setitem__(self, key, value)
|
||||
self.changed()
|
||||
|
||||
def __delitem__(self, key):
|
||||
"Detect dictionary del events and emit change events."
|
||||
|
||||
dict.__delitem__(self, key)
|
||||
self.changed()
|
||||
245
oml/oxflask/query.py
Normal file
245
oml/oxflask/query.py
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
from sqlalchemy.sql.expression import and_, not_, or_, ClauseElement
|
||||
from datetime import datetime
|
||||
import unicodedata
|
||||
from sqlalchemy.sql import operators, extract
|
||||
|
||||
import utils
|
||||
import settings
|
||||
|
||||
def get_operator(op, type='str'):
|
||||
return {
|
||||
'str': {
|
||||
'==': operators.ilike_op,
|
||||
'>': operators.gt,
|
||||
'>=': operators.ge,
|
||||
'<': operators.lt,
|
||||
'<=': operators.le,
|
||||
'^': operators.startswith_op,
|
||||
'$': operators.endswith_op,
|
||||
},
|
||||
'int': {
|
||||
'==': operators.eq,
|
||||
'>': operators.gt,
|
||||
'>=': operators.ge,
|
||||
'<': operators.lt,
|
||||
'<=': operators.le,
|
||||
}
|
||||
}[type].get(op, {
|
||||
'str': operators.contains_op,
|
||||
'int': operators.eq
|
||||
}[type])
|
||||
|
||||
|
||||
class Parser(object):
|
||||
|
||||
def __init__(self, model):
|
||||
self._model = model
|
||||
self._find = model.find.mapper.class_
|
||||
self._user = model.users.mapper.class_
|
||||
self._list = model.lists.mapper.class_
|
||||
self.item_keys = model.item_keys
|
||||
self.filter_keys = model.filter_keys
|
||||
|
||||
def parse_condition(self, condition):
|
||||
'''
|
||||
condition: {
|
||||
value: "war"
|
||||
}
|
||||
or
|
||||
condition: {
|
||||
key: "year",
|
||||
value: [1970, 1980],
|
||||
operator: "="
|
||||
}
|
||||
...
|
||||
'''
|
||||
k = condition.get('key', '*')
|
||||
if not k:
|
||||
k = '*'
|
||||
v = condition['value']
|
||||
op = condition.get('operator')
|
||||
if not op:
|
||||
op = '='
|
||||
if op.startswith('!'):
|
||||
op = op[1:]
|
||||
exclude = True
|
||||
else:
|
||||
exclude = False
|
||||
|
||||
key_type = (utils.get_by_id(self.item_keys, k) or {'type': 'string'}).get('type')
|
||||
if isinstance(key_type, list):
|
||||
key_type = key_type[0]
|
||||
if k == 'list':
|
||||
key_type = ''
|
||||
|
||||
|
||||
if (not exclude and op == '=' or op in ('$', '^')) and v == '':
|
||||
return None
|
||||
elif k == 'resolution':
|
||||
q = self.parse_condition({'key': 'width', 'value': v[0], 'operator': op}) \
|
||||
& self.parse_condition({'key': 'height', 'value': v[1], 'operator': op})
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
elif isinstance(v, list) and len(v) == 2 and op == '=':
|
||||
q = self.parse_condition({'key': k, 'value': v[0], 'operator': '>='}) \
|
||||
& self.parse_condition({'key': k, 'value': v[1], 'operator': '<'})
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
elif key_type == 'boolean':
|
||||
q = getattr(self._model, 'find_%s' % k) == v
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
elif key_type in ("string", "text"):
|
||||
if isinstance(v, unicode):
|
||||
v = unicodedata.normalize('NFKD', v).lower()
|
||||
q = get_operator(op)(self._find.value, v.lower())
|
||||
if k != '*':
|
||||
q &= (self._find.key == k)
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
elif k == 'list':
|
||||
'''
|
||||
q = Q(id=0)
|
||||
l = v.split(":")
|
||||
if len(l) == 1:
|
||||
vqs = Volume.objects.filter(name=v, user=user)
|
||||
if vqs.count() == 1:
|
||||
v = vqs[0]
|
||||
q = Q(files__instances__volume__id=v.id)
|
||||
elif len(l) >= 2:
|
||||
l = (l[0], ":".join(l[1:]))
|
||||
lqs = list(List.objects.filter(name=l[1], user__username=l[0]))
|
||||
if len(lqs) == 1 and lqs[0].accessible(user):
|
||||
l = lqs[0]
|
||||
if l.query.get('static', False) == False:
|
||||
data = l.query
|
||||
q = self.parse_conditions(data.get('conditions', []),
|
||||
data.get('operator', '&'),
|
||||
user, l.user)
|
||||
else:
|
||||
q = Q(id__in=l.items.all())
|
||||
if exclude:
|
||||
q = ~q
|
||||
else:
|
||||
q = Q(id=0)
|
||||
'''
|
||||
l = v.split(":")
|
||||
nickname = l[0]
|
||||
name = ':'.join(l[1:])
|
||||
if nickname:
|
||||
p = self._user.query.filter_by(nickname=nickname).first()
|
||||
v = '%s:%s' % (p.id, name)
|
||||
else:
|
||||
p = self._user.query.filter_by(id=settings.USER_ID).first()
|
||||
v = ':%s' % name
|
||||
#print 'get list:', p.id, name, l, v
|
||||
if name:
|
||||
l = self._list.query.filter_by(user_id=p.id, name=name).first()
|
||||
else:
|
||||
l = None
|
||||
if l and l._query:
|
||||
data = l._query
|
||||
q = self.parse_conditions(data.get('conditions', []),
|
||||
data.get('operator', '&'))
|
||||
else:
|
||||
q = (self._find.key == 'list') & (self._find.value == v)
|
||||
return q
|
||||
elif key_type == 'date':
|
||||
def parse_date(d):
|
||||
while len(d) < 3:
|
||||
d.append(1)
|
||||
return datetime(*[int(i) for i in d])
|
||||
#using sort here since find only contains strings
|
||||
v = parse_date(v.split('-'))
|
||||
vk = getattr(self._model, 'sort_%s' % k)
|
||||
q = get_operator(op, 'int')(vk, v)
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
else: #integer, float, time
|
||||
q = get_operator(op, 'int')(getattr(self._model, 'sort_%s'%k), v)
|
||||
if exclude:
|
||||
q = ~q
|
||||
return q
|
||||
|
||||
def parse_conditions(self, conditions, operator):
|
||||
'''
|
||||
conditions: [
|
||||
{
|
||||
value: "war"
|
||||
}
|
||||
{
|
||||
key: "year",
|
||||
value: "1970-1980,
|
||||
operator: "!="
|
||||
},
|
||||
{
|
||||
key: "country",
|
||||
value: "f",
|
||||
operator: "^"
|
||||
}
|
||||
],
|
||||
operator: "&"
|
||||
'''
|
||||
conn = []
|
||||
for condition in conditions:
|
||||
if 'conditions' in condition:
|
||||
q = self.parse_conditions(condition['conditions'],
|
||||
condition.get('operator', '&'))
|
||||
else:
|
||||
q = self.parse_condition(condition)
|
||||
if isinstance(q, list):
|
||||
conn += q
|
||||
else:
|
||||
conn.append(q)
|
||||
conn = [q for q in conn if not isinstance(q, None.__class__)]
|
||||
if conn:
|
||||
if operator == '|':
|
||||
q = conn[0]
|
||||
for c in conn[1:]:
|
||||
q = q | c
|
||||
q = [q]
|
||||
else:
|
||||
q = conn
|
||||
return q
|
||||
return []
|
||||
|
||||
def find(self, data):
|
||||
'''
|
||||
query: {
|
||||
conditions: [
|
||||
{
|
||||
value: "war"
|
||||
}
|
||||
{
|
||||
key: "year",
|
||||
value: "1970-1980,
|
||||
operator: "!="
|
||||
},
|
||||
{
|
||||
key: "country",
|
||||
value: "f",
|
||||
operator: "^"
|
||||
}
|
||||
],
|
||||
operator: "&"
|
||||
}
|
||||
'''
|
||||
|
||||
#join query with operator
|
||||
qs = self._model.query
|
||||
#only include items that have hard metadata
|
||||
conditions = self.parse_conditions(data.get('query', {}).get('conditions', []),
|
||||
data.get('query', {}).get('operator', '&'))
|
||||
for c in conditions:
|
||||
qs = qs.join(self._find).filter(c)
|
||||
qs = qs.group_by(self._model.id)
|
||||
return qs
|
||||
|
||||
34
oml/oxflask/shortcuts.py
Normal file
34
oml/oxflask/shortcuts.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from functools import wraps
|
||||
import datetime
|
||||
import json
|
||||
|
||||
from flask import Response
|
||||
|
||||
def json_response(data=None, status=200, text='ok'):
|
||||
if not data:
|
||||
data = {}
|
||||
return {'status': {'code': status, 'text': text}, 'data': data}
|
||||
|
||||
def _to_json(python_object):
|
||||
if isinstance(python_object, datetime.datetime):
|
||||
if python_object.year < 1900:
|
||||
tt = python_object.timetuple()
|
||||
return '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
|
||||
return python_object.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
raise TypeError(u'%s %s is not JSON serializable' % (repr(python_object), type(python_object)))
|
||||
|
||||
def json_dumps(obj):
|
||||
indent = 2
|
||||
return json.dumps(obj, indent=indent, default=_to_json, ensure_ascii=False).encode('utf-8')
|
||||
|
||||
def render_to_json_response(obj, content_type="text/json", status=200):
|
||||
resp = Response(json_dumps(obj), status=status, content_type=content_type)
|
||||
return resp
|
||||
|
||||
def returns_json(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
r = f(*args, **kwargs)
|
||||
return render_to_json_response(json_response(r))
|
||||
return decorated_function
|
||||
|
||||
8
oml/oxflask/utils.py
Normal file
8
oml/oxflask/utils.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
def get_by_key(objects, key, value):
|
||||
obj = filter(lambda o: o.get(key) == value, objects)
|
||||
return obj and obj[0] or None
|
||||
|
||||
def get_by_id(objects, id):
|
||||
return get_by_key(objects, 'id', id)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue