pandora/pandora/log/views.py

133 lines
3.8 KiB
Python
Raw Normal View History

2011-11-01 16:08:09 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, print_function, absolute_import
2011-11-01 16:08:09 +00:00
import ox
from ox.utils import json
2016-02-20 09:06:41 +00:00
from oxdjango.decorators import admin_required_json
from oxdjango.shortcuts import render_to_json_response, json_response
2011-11-01 16:08:09 +00:00
2016-02-20 09:06:41 +00:00
from oxdjango.api import actions
2011-11-01 16:08:09 +00:00
from item import utils
from . import models
2011-11-01 16:08:09 +00:00
2014-12-16 13:44:42 +00:00
def logError(request, data):
2011-11-01 16:08:09 +00:00
'''
2014-12-19 15:05:08 +00:00
Logs an error
takes {
url: string, // URL
line: string, // line number
text: string // error text
}
returns {}
see: findErrorLogs, removeErrorLogs
2011-11-01 16:08:09 +00:00
'''
if request.user.is_authenticated():
user = request.user
else:
user = None
url = data.get('url', '').split('/static/')[-1]
if 'text' in data:
2013-08-23 17:32:07 +00:00
if len(url) > 1000:
url = url[:997] + '...'
2018-02-27 13:41:08 +00:00
text = data.get('url', '') + '\n\n' + \
data['text'] + '\n\n' + request.META.get('HTTP_USER_AGENT', '')[:4096]
log = models.Log(
text=text,
2011-11-01 16:08:09 +00:00
line=int(data.get('line', 0)),
url=url
)
if user:
2018-02-27 13:41:08 +00:00
log.user = user
log.save()
2011-11-01 16:08:09 +00:00
response = json_response()
return render_to_json_response(response)
2014-12-16 13:44:42 +00:00
actions.register(logError, cache=False)
2011-11-01 16:08:09 +00:00
@admin_required_json
2014-12-16 13:44:42 +00:00
def removeErrorLogs(request, data):
2011-11-01 16:08:09 +00:00
'''
2014-12-19 15:05:08 +00:00
Removes entries from the error log
takes {
ids: [string] // list of error ids
}
returns {}
see: findErrorLogs, logError
2011-11-01 16:08:09 +00:00
'''
2011-12-18 09:35:49 +00:00
models.Log.objects.filter(id__in=[ox.fromAZ(i) for i in data['ids']]).delete()
2011-11-01 16:08:09 +00:00
response = json_response()
return render_to_json_response(response)
2014-12-16 13:44:42 +00:00
actions.register(removeErrorLogs, cache=False)
2011-11-01 16:08:09 +00:00
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.Log.objects.find(query, user)
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
2014-12-16 13:44:42 +00:00
def findErrorLogs(request, data):
2011-11-01 16:08:09 +00:00
'''
2014-12-19 15:05:08 +00:00
Finds error logs for a given query
takes {
query: object, // query object, see `find`
sort: [object], // list of sort objects, see `find`
range: [int, int], // range of results to return
keys: [string] // list of properties to return
}
returns {
items: [object]
}
see: find, logError, removeErrorLogs
2011-11-01 16:08:09 +00:00
'''
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:
2014-09-19 12:26:46 +00:00
response['data']['position'] = utils.get_positions(ids, [qs[0].public_id])[0]
2011-11-01 16:08:09 +00:00
elif 'positions' in data:
ids = list(qs.values_list('id', flat=True))
2016-06-30 14:42:56 +00:00
response['data']['positions'] = utils.get_positions(ids, data['positions'], decode_id=True)
2011-11-01 16:08:09 +00:00
else:
response['data']['items'] = qs.count()
return render_to_json_response(response)
2014-12-16 13:44:42 +00:00
actions.register(findErrorLogs)