add basic changelog api
This commit is contained in:
parent
3946875188
commit
2f154aa610
2 changed files with 94 additions and 1 deletions
|
@ -11,3 +11,6 @@ class Changelog(models.Model):
|
|||
|
||||
def __unicode__(self):
|
||||
return u'%s %s' %(self.type, self.created)
|
||||
|
||||
def json(self):
|
||||
return self.value
|
||||
|
|
|
@ -1 +1,91 @@
|
|||
# Create your views here.
|
||||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division
|
||||
|
||||
import ox
|
||||
|
||||
from ox.django.decorators import admin_required_json
|
||||
from ox.django.shortcuts import render_to_json_response, json_response
|
||||
|
||||
from ox.django.api import actions
|
||||
|
||||
from item import utils
|
||||
|
||||
import models
|
||||
|
||||
|
||||
def parse_query(data, user):
|
||||
query = {}
|
||||
query['range'] = [0, 100]
|
||||
query['sort'] = [{'key':'name', 'operator':'+'}]
|
||||
for key in ('keys', 'group', 'list', 'range', 'sort', 'query'):
|
||||
if key in data:
|
||||
query[key] = data[key]
|
||||
#query['qs'] = models.Changelog.objects.find(query, user)
|
||||
query['qs'] = models.Changelog.all()
|
||||
return query
|
||||
|
||||
def order_query(qs, sort):
|
||||
order_by = []
|
||||
for e in sort:
|
||||
operator = e['operator']
|
||||
if operator != '-':
|
||||
operator = ''
|
||||
key = {
|
||||
}.get(e['key'], e['key'])
|
||||
order = '%s%s' % (operator, key)
|
||||
order_by.append(order)
|
||||
if order_by:
|
||||
qs = qs.order_by(*order_by, nulls_last=True)
|
||||
return qs
|
||||
|
||||
@admin_required_json
|
||||
def findChangeLogs(request, data):
|
||||
'''
|
||||
takes {
|
||||
query: {
|
||||
conditions: [
|
||||
{
|
||||
key: 'user',
|
||||
value: 'something',
|
||||
operator: '='
|
||||
}
|
||||
]
|
||||
operator: ","
|
||||
},
|
||||
sort: [{key: 'created', operator: '+'}],
|
||||
range: [int, int]
|
||||
keys: [string]
|
||||
}
|
||||
returns {
|
||||
items: [object]
|
||||
}
|
||||
|
||||
'''
|
||||
response = json_response()
|
||||
|
||||
query = parse_query(data, request.user)
|
||||
qs = order_query(query['qs'], query['sort'])
|
||||
qs = qs.distinct()
|
||||
if 'keys' in data:
|
||||
qs = qs.select_related()
|
||||
qs = qs[query['range'][0]:query['range'][1]]
|
||||
response['data']['items'] = [p.json(data['keys']) for p in qs]
|
||||
elif 'position' in query:
|
||||
ids = [i.get_id() for i in qs]
|
||||
data['conditions'] = data['conditions'] + {
|
||||
'value': data['position'],
|
||||
'key': query['sort'][0]['key'],
|
||||
'operator': '^'
|
||||
}
|
||||
query = parse_query(data, request.user)
|
||||
qs = order_query(query['qs'], query['sort'])
|
||||
if qs.count() > 0:
|
||||
response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0]
|
||||
elif 'positions' in data:
|
||||
ids = [ox.toAZ(i.id) for i in qs]
|
||||
response['data']['positions'] = utils.get_positions(ids, data['positions'])
|
||||
else:
|
||||
response['data']['items'] = qs.count()
|
||||
return render_to_json_response(response)
|
||||
actions.register(findChangeLogs)
|
||||
|
|
Loading…
Reference in a new issue