Switch to python3
This commit is contained in:
parent
531041e89a
commit
9ba4b6a91a
5286 changed files with 677347 additions and 576888 deletions
0
Shared/lib/python3.4/site-packages/ox/django/__init__.py
Normal file
0
Shared/lib/python3.4/site-packages/ox/django/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from actions import actions
|
||||
143
Shared/lib/python3.4/site-packages/ox/django/api/actions.py
Normal file
143
Shared/lib/python3.4/site-packages/ox/django/api/actions.py
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
import inspect
|
||||
import sys
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from ..shortcuts import render_to_json_response, json_response
|
||||
from ...utils import json
|
||||
|
||||
def autodiscover():
|
||||
#register api actions from all installed apps
|
||||
from django.utils.importlib import import_module
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
for app in settings.INSTALLED_APPS:
|
||||
if app != 'api':
|
||||
mod = import_module(app)
|
||||
try:
|
||||
import_module('%s.views'%app)
|
||||
except:
|
||||
if module_has_submodule(mod, 'views'):
|
||||
raise
|
||||
|
||||
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.POST.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[len(settings.PROJECT_ROOT)+1:]
|
||||
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)
|
||||
13
Shared/lib/python3.4/site-packages/ox/django/api/urls.py
Normal file
13
Shared/lib/python3.4/site-packages/ox/django/api/urls.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
from django.conf.urls import patterns
|
||||
|
||||
import views
|
||||
|
||||
import actions
|
||||
actions.autodiscover()
|
||||
|
||||
urlpatterns = patterns("",
|
||||
(r'^$', views.api),
|
||||
)
|
||||
44
Shared/lib/python3.4/site-packages/ox/django/api/views.py
Normal file
44
Shared/lib/python3.4/site-packages/ox/django/api/views.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from __future__ import division, with_statement
|
||||
|
||||
from django.shortcuts import render_to_response
|
||||
from django.template import RequestContext
|
||||
from django.conf import settings
|
||||
|
||||
from ..shortcuts import render_to_json_response, json_response
|
||||
|
||||
from actions import actions
|
||||
|
||||
def api(request):
|
||||
if request.META['REQUEST_METHOD'] == "OPTIONS":
|
||||
response = render_to_json_response({'status': {'code': 200,
|
||||
'text': 'use POST'}})
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
if not 'action' in request.POST:
|
||||
methods = actions.keys()
|
||||
api = []
|
||||
for f in sorted(methods):
|
||||
api.append({'name': f,
|
||||
'doc': actions.doc(f).replace('\n', '<br>\n')})
|
||||
context = RequestContext(request, {
|
||||
'api': api,
|
||||
'settings': settings,
|
||||
'sitename': settings.SITENAME
|
||||
})
|
||||
return render_to_response('api.html', context)
|
||||
action = request.POST['action']
|
||||
version = getattr(request, 'version', None)
|
||||
if version:
|
||||
f = actions.versions.get(version, {}).get(action, actions.get(action))
|
||||
else:
|
||||
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['Access-Control-Allow-Origin'] = '*'
|
||||
return response
|
||||
|
||||
32
Shared/lib/python3.4/site-packages/ox/django/decorators.py
Normal file
32
Shared/lib/python3.4/site-packages/ox/django/decorators.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
try:
|
||||
from django.contrib.auth.decorators import wraps
|
||||
except:
|
||||
from django.utils.functional import wraps
|
||||
from shortcuts import render_to_json_response
|
||||
|
||||
def login_required_json(function=None):
|
||||
"""
|
||||
Decorator for views that checks that the user is logged in
|
||||
return json error if not logged in.
|
||||
"""
|
||||
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated():
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 401, 'text': 'login required'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
|
||||
def admin_required_json(function=None):
|
||||
"""
|
||||
Decorator for views that checks that the user is logged in
|
||||
return json error if not logged in.
|
||||
"""
|
||||
|
||||
def _wrapped_view(request, *args, **kwargs):
|
||||
if request.user.is_authenticated() and request.user.get_profile().get_level() == 'admin':
|
||||
return function(request, *args, **kwargs)
|
||||
return render_to_json_response({'status': {'code': 403, 'text': 'permission denied'}})
|
||||
return wraps(function)(_wrapped_view)
|
||||
108
Shared/lib/python3.4/site-packages/ox/django/fields.py
Normal file
108
Shared/lib/python3.4/site-packages/ox/django/fields.py
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import time
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils import datetime_safe
|
||||
|
||||
from ox.utils import json
|
||||
|
||||
|
||||
def to_json(python_object):
|
||||
if isinstance(python_object, datetime.datetime):
|
||||
if python_object.year < 1900:
|
||||
tt = python_object.timetuple()
|
||||
value = '%d-%02d-%02dT%02d:%02d%02dZ' % tuple(list(tt)[:6])
|
||||
else:
|
||||
value = python_object.strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
return {'__class__': 'datetime.datetime',
|
||||
'__value__': value}
|
||||
if isinstance(python_object, datetime_safe.datetime):
|
||||
return {'__class__': 'datetime.datetime',
|
||||
'__value__': python_object.strftime('%Y-%m-%dT%H:%M:%SZ')}
|
||||
if isinstance(python_object, time.struct_time):
|
||||
return {'__class__': 'time.asctime',
|
||||
'__value__': time.asctime(python_object)}
|
||||
try:
|
||||
if isinstance(python_object, bytes):
|
||||
return {'__class__': 'bytes',
|
||||
'__value__': list(python_object)}
|
||||
except:
|
||||
pass
|
||||
raise TypeError(repr(python_object) + ' is not JSON serializable')
|
||||
|
||||
def from_json(json_object):
|
||||
if '__class__' in json_object:
|
||||
if json_object['__class__'] == 'bytes':
|
||||
return bytes(json_object['__value__'])
|
||||
if json_object['__class__'] == 'datetime_safe.datetime' \
|
||||
or json_object['__class__'] == 'datetime.datetime':
|
||||
return datetime_safe.datetime.strptime(json_object['__value__'], '%Y-%m-%dT%H:%M:%SZ')
|
||||
if json_object['__class__'] == 'time.asctime':
|
||||
return time.strptime(json_object['__value__'])
|
||||
return json_object
|
||||
|
||||
class DictField(models.TextField):
|
||||
"""DictField is a textfield that contains JSON-serialized dictionaries."""
|
||||
|
||||
# Used so to_python() is called
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
def to_python(self, value):
|
||||
"""Convert our string value to python after we load it from the DB"""
|
||||
if value == None:
|
||||
return value
|
||||
if isinstance(value, dict):
|
||||
return value
|
||||
try:
|
||||
value = json.loads(value, object_hook=from_json)
|
||||
except: #this is required to load fixtures
|
||||
value = eval(value)
|
||||
assert isinstance(value, dict)
|
||||
return value
|
||||
|
||||
def get_db_prep_save(self, value, connection):
|
||||
"""Convert our JSON object to a string before we save"""
|
||||
if value == None:
|
||||
return value
|
||||
if isinstance(value, basestring):
|
||||
value = eval(value)
|
||||
assert isinstance(value, dict)
|
||||
value = json.dumps(value, default=to_json)
|
||||
return super(DictField, self).get_db_prep_save(value, connection=connection)
|
||||
|
||||
class TupleField(models.TextField):
|
||||
"""TupleField is a textfield that contains JSON-serialized tuples."""
|
||||
|
||||
# Used so to_python() is called
|
||||
__metaclass__ = models.SubfieldBase
|
||||
|
||||
def to_python(self, value):
|
||||
"""Convert our string value to JSON after we load it from the DB"""
|
||||
if isinstance(value, tuple):
|
||||
return value
|
||||
|
||||
try:
|
||||
value = json.loads(value, object_hook=from_json)
|
||||
except: #this is required to load fixtures
|
||||
value = eval(value)
|
||||
assert isinstance(value, list)
|
||||
return tuple(value)
|
||||
|
||||
def get_db_prep_save(self, value, connection):
|
||||
"""Convert our JSON object to a string before we save"""
|
||||
if isinstance(value, basestring):
|
||||
value = eval(value)
|
||||
if isinstance(value, list):
|
||||
value = tuple(value)
|
||||
assert isinstance(value, tuple)
|
||||
value = json.dumps(value, default=to_json)
|
||||
return super(TupleField, self).get_db_prep_save(value, connection=connection)
|
||||
|
||||
try:
|
||||
from south.modelsinspector import add_introspection_rules
|
||||
add_introspection_rules([], ["^ox.django\.fields\.DictField"])
|
||||
add_introspection_rules([], ["^ox.django\.fields\.TupleField"])
|
||||
except:
|
||||
pass
|
||||
58
Shared/lib/python3.4/site-packages/ox/django/http.py
Normal file
58
Shared/lib/python3.4/site-packages/ox/django/http.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import os
|
||||
import mimetypes
|
||||
from datetime import datetime, timedelta
|
||||
from urllib import quote
|
||||
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
def HttpFileResponse(path, content_type=None, filename=None):
|
||||
if not os.path.exists(path):
|
||||
raise Http404
|
||||
if not content_type:
|
||||
content_type = mimetypes.guess_type(path)[0]
|
||||
if not content_type:
|
||||
content_type = 'application/octet-stream'
|
||||
|
||||
if getattr(settings, 'XACCELREDIRECT', False):
|
||||
response = HttpResponse()
|
||||
response['Content-Length'] = os.stat(path).st_size
|
||||
|
||||
for PREFIX in ('STATIC', 'MEDIA'):
|
||||
root = getattr(settings, PREFIX+'_ROOT', '')
|
||||
url = getattr(settings, PREFIX+'_URL', '')
|
||||
if root and path.startswith(root):
|
||||
path = url + path[len(root)+1:]
|
||||
if isinstance(path, unicode):
|
||||
path = path.encode('utf-8')
|
||||
response['X-Accel-Redirect'] = path
|
||||
if content_type:
|
||||
response['Content-Type'] = content_type
|
||||
elif getattr(settings, 'XSENDFILE', False):
|
||||
response = HttpResponse()
|
||||
if isinstance(path, unicode):
|
||||
path = path.encode('utf-8')
|
||||
response['X-Sendfile'] = path
|
||||
if content_type:
|
||||
response['Content-Type'] = content_type
|
||||
response['Content-Length'] = os.stat(path).st_size
|
||||
else:
|
||||
response = HttpResponse(open(path), content_type=content_type)
|
||||
if filename:
|
||||
if isinstance(filename, unicode):
|
||||
filename = filename.encode('utf-8')
|
||||
response['Content-Disposition'] = "attachment; filename*=UTF=8''%s" % quote(filename)
|
||||
|
||||
response['Expires'] = datetime.strftime(datetime.utcnow() + timedelta(days=1), "%a, %d-%b-%Y %H:%M:%S GMT")
|
||||
|
||||
def allow_access():
|
||||
for key in ('X-Accel-Redirect', 'X-Sendfile'):
|
||||
if key in response:
|
||||
del response[key]
|
||||
response['Access-Control-Allow-Origin'] = '*'
|
||||
response.allow_access = allow_access
|
||||
return response
|
||||
|
||||
15
Shared/lib/python3.4/site-packages/ox/django/middleware.py
Normal file
15
Shared/lib/python3.4/site-packages/ox/django/middleware.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
from shortcuts import HttpErrorJson, render_to_json_response
|
||||
|
||||
class ExceptionMiddleware(object):
|
||||
def process_exception(self, request, exception):
|
||||
if isinstance(exception, HttpErrorJson):
|
||||
return render_to_json_response(exception.response)
|
||||
return None
|
||||
|
||||
class ChromeFrameMiddleware(object):
|
||||
def process_response(self, request, response):
|
||||
response['X-UA-Compatible'] = 'chrome=1'
|
||||
return response
|
||||
113
Shared/lib/python3.4/site-packages/ox/django/monitor.py
Normal file
113
Shared/lib/python3.4/site-packages/ox/django/monitor.py
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import signal
|
||||
import threading
|
||||
import atexit
|
||||
import Queue
|
||||
|
||||
_interval = 1.0
|
||||
_times = {}
|
||||
_files = []
|
||||
|
||||
_running = False
|
||||
_queue = Queue.Queue()
|
||||
_lock = threading.Lock()
|
||||
|
||||
def _restart(path):
|
||||
_queue.put(True)
|
||||
prefix = 'monitor (pid=%d):' % os.getpid()
|
||||
print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
|
||||
print >> sys.stderr, '%s Triggering process restart.' % prefix
|
||||
os.kill(os.getpid(), signal.SIGINT)
|
||||
|
||||
def _modified(path):
|
||||
try:
|
||||
# If path doesn't denote a file and were previously
|
||||
# tracking it, then it has been removed or the file type
|
||||
# has changed so force a restart. If not previously
|
||||
# tracking the file then we can ignore it as probably
|
||||
# pseudo reference such as when file extracted from a
|
||||
# collection of modules contained in a zip file.
|
||||
|
||||
if not os.path.isfile(path):
|
||||
return path in _times
|
||||
|
||||
# Check for when file last modified.
|
||||
|
||||
mtime = os.stat(path).st_mtime
|
||||
if path not in _times:
|
||||
_times[path] = mtime
|
||||
|
||||
# Force restart when modification time has changed, even
|
||||
# if time now older, as that could indicate older file
|
||||
# has been restored.
|
||||
|
||||
if mtime != _times[path]:
|
||||
return True
|
||||
except:
|
||||
# If any exception occured, likely that file has been
|
||||
# been removed just before stat(), so force a restart.
|
||||
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def _monitor():
|
||||
while 1:
|
||||
# Check modification times on all files in sys.modules.
|
||||
|
||||
for module in sys.modules.values():
|
||||
if not hasattr(module, '__file__'):
|
||||
continue
|
||||
path = getattr(module, '__file__')
|
||||
if not path:
|
||||
continue
|
||||
if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
|
||||
path = path[:-1]
|
||||
if _modified(path):
|
||||
return _restart(path)
|
||||
|
||||
# Check modification times on files which have
|
||||
# specifically been registered for monitoring.
|
||||
|
||||
for path in _files:
|
||||
if _modified(path):
|
||||
return _restart(path)
|
||||
|
||||
# Go to sleep for specified interval.
|
||||
|
||||
try:
|
||||
return _queue.get(timeout=_interval)
|
||||
except:
|
||||
pass
|
||||
|
||||
_thread = threading.Thread(target=_monitor)
|
||||
_thread.setDaemon(True)
|
||||
|
||||
def _exiting():
|
||||
try:
|
||||
_queue.put(True)
|
||||
except:
|
||||
pass
|
||||
_thread.join()
|
||||
|
||||
atexit.register(_exiting)
|
||||
|
||||
def track(path):
|
||||
if not path in _files:
|
||||
_files.append(path)
|
||||
|
||||
def start(interval=1.0):
|
||||
global _interval
|
||||
if interval < _interval:
|
||||
_interval = interval
|
||||
|
||||
global _running
|
||||
_lock.acquire()
|
||||
if not _running:
|
||||
_running = True
|
||||
_thread.start()
|
||||
_lock.release()
|
||||
73
Shared/lib/python3.4/site-packages/ox/django/query.py
Normal file
73
Shared/lib/python3.4/site-packages/ox/django/query.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
|
||||
from django.db.models.sql import Query
|
||||
from django.db.models.sql.compiler import SQLCompiler
|
||||
from django.db import connections
|
||||
import django.db.models.query
|
||||
|
||||
'''
|
||||
models.py:
|
||||
-----------------------------------
|
||||
from ox.django.query import QuerySet
|
||||
|
||||
class Manager(models.Manager):
|
||||
def get_query_set(self):
|
||||
return QuerySet(self.model)
|
||||
|
||||
class Model(models.Model):
|
||||
...
|
||||
objects = Manager()
|
||||
'''
|
||||
|
||||
class SQLCompiler(SQLCompiler):
|
||||
|
||||
def get_ordering(self):
|
||||
result, group_by = super(SQLCompiler, self).get_ordering()
|
||||
if self.query.nulls_last and len(result):
|
||||
if self.connection.vendor == 'sqlite':
|
||||
_result = []
|
||||
for r in result:
|
||||
if r.endswith(' DESC'):
|
||||
_r = r[:-len(' DESC')]
|
||||
elif r.endswith(' ASC'):
|
||||
_r = r[:-len(' ASC')]
|
||||
_result.append(_r + ' IS NULL')
|
||||
_result.append(r)
|
||||
|
||||
result = _result
|
||||
else:
|
||||
result = map(lambda e: e + ' NULLS LAST', result)
|
||||
return result, group_by
|
||||
|
||||
class Query(Query):
|
||||
nulls_last = False
|
||||
|
||||
def clone(self, *args, **kwargs):
|
||||
obj = super(Query, self).clone(*args, **kwargs)
|
||||
obj.nulls_last = self.nulls_last
|
||||
return obj
|
||||
|
||||
def get_compiler(self, using=None, connection=None):
|
||||
if using is None and connection is None:
|
||||
raise ValueError("Need either using or connection")
|
||||
if using:
|
||||
connection = connections[using]
|
||||
# Check that the compiler will be able to execute the query
|
||||
for alias, aggregate in self.aggregate_select.items():
|
||||
connection.ops.check_aggregate_support(aggregate)
|
||||
|
||||
return SQLCompiler(self, connection, using)
|
||||
|
||||
|
||||
class QuerySet(django.db.models.query.QuerySet):
|
||||
|
||||
def __init__(self, model=None, query=None, using=None, **kwargs):
|
||||
super(QuerySet, self).__init__(model=model, query=query, using=None, **kwargs)
|
||||
self.query = query or Query(self.model)
|
||||
|
||||
def order_by(self, *args, **kwargs):
|
||||
nulls_last = kwargs.pop('nulls_last', False)
|
||||
obj = super(QuerySet, self).order_by(*args, **kwargs)
|
||||
obj.query.nulls_last = nulls_last
|
||||
return obj
|
||||
51
Shared/lib/python3.4/site-packages/ox/django/shortcuts.py
Normal file
51
Shared/lib/python3.4/site-packages/ox/django/shortcuts.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import datetime
|
||||
from django.utils import datetime_safe
|
||||
from django.http import HttpResponse, Http404
|
||||
try:
|
||||
import simplejson as json
|
||||
except ImportError:
|
||||
from django.utils import simplejson as json
|
||||
from django.conf import settings
|
||||
|
||||
class HttpErrorJson(Http404):
|
||||
def __init__(self, response):
|
||||
self.response = 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')
|
||||
if isinstance(python_object, datetime_safe.datetime):
|
||||
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 render_to_json_response(dictionary, content_type="text/json", status=200):
|
||||
indent=None
|
||||
if settings.DEBUG:
|
||||
content_type = "text/javascript"
|
||||
indent = 2
|
||||
if getattr(settings, 'JSON_DEBUG', False):
|
||||
print json.dumps(dictionary, indent=2, default=_to_json, ensure_ascii=False).encode('utf-8')
|
||||
|
||||
return HttpResponse(json.dumps(dictionary, indent=indent, default=_to_json,
|
||||
ensure_ascii=False).encode('utf-8'), content_type=content_type, status=status)
|
||||
|
||||
def get_object_or_404_json(klass, *args, **kwargs):
|
||||
from django.shortcuts import _get_queryset
|
||||
queryset = _get_queryset(klass)
|
||||
try:
|
||||
return queryset.get(*args, **kwargs)
|
||||
except queryset.model.DoesNotExist:
|
||||
response = {'status': {'code': 404,
|
||||
'text': '%s not found' % queryset.model._meta.object_name}}
|
||||
raise HttpErrorJson(response)
|
||||
|
||||
48
Shared/lib/python3.4/site-packages/ox/django/utils.py
Normal file
48
Shared/lib/python3.4/site-packages/ox/django/utils.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
from django.utils.datetime_safe import datetime
|
||||
from django.http import HttpResponse,Http404
|
||||
from django.core.servers.basehttp import FileWrapper
|
||||
from django.conf import settings
|
||||
|
||||
import mimetypes
|
||||
import os
|
||||
|
||||
def basic_sendfile(fname,download_name=None):
|
||||
if not os.path.exists(fname):
|
||||
raise Http404
|
||||
|
||||
wrapper = FileWrapper(open(fname,"r"))
|
||||
|
||||
content_type = mimetypes.guess_type(fname)[0]
|
||||
response = HttpResponse(wrapper, content_type=content_type)
|
||||
response['Content-Length'] = os.path.getsize(fname)
|
||||
|
||||
if download_name:
|
||||
response['Content-Disposition'] = "attachment; filename=%s"%download_name
|
||||
|
||||
return response
|
||||
|
||||
def x_sendfile(fname,download_name=None):
|
||||
if not os.path.exists(fname):
|
||||
raise Http404
|
||||
|
||||
content_type = mimetypes.guess_type(fname)[0]
|
||||
response = HttpResponse('', content_type=content_type)
|
||||
response['Content-Length'] = os.path.getsize(fname)
|
||||
response['X-Sendfile'] = fname
|
||||
|
||||
if download_name:
|
||||
response['Content-Disposition'] = "attachment; filename=%s"%download_name
|
||||
|
||||
return response
|
||||
|
||||
try:
|
||||
__sendfile = getattr(settings,'SENDFILE',False) == 'x_sendfile'
|
||||
except:
|
||||
__sendfile = False
|
||||
if __sendfile == 'x_sendfile':
|
||||
sendfile = x_sendfile
|
||||
else:
|
||||
sendfile = basic_sendfile
|
||||
|
||||
67
Shared/lib/python3.4/site-packages/ox/django/views.py
Normal file
67
Shared/lib/python3.4/site-packages/ox/django/views.py
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# vi:si:et:sw=4:sts=4:ts=4
|
||||
import cookielib
|
||||
import urllib2
|
||||
from StringIO import StringIO
|
||||
|
||||
from celery.utils import get_full_cls_name
|
||||
from celery.backends import default_backend
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.conf import settings
|
||||
|
||||
from shortcuts import json_response
|
||||
import ox
|
||||
|
||||
|
||||
def task_status(request, task_id):
|
||||
response = json_response(status=200, text='ok')
|
||||
status = default_backend.get_status(task_id)
|
||||
res = default_backend.get_result(task_id)
|
||||
response['data'] = {
|
||||
'id': task_id,
|
||||
'status': status,
|
||||
'result': res
|
||||
}
|
||||
if status in default_backend.EXCEPTION_STATES:
|
||||
traceback = default_backend.get_traceback(task_id)
|
||||
response['data'].update({'result': str(res.args[0]),
|
||||
'exc': get_full_cls_name(res.__class__),
|
||||
'traceback': traceback})
|
||||
return response
|
||||
|
||||
class SessionCookieJar(cookielib.LWPCookieJar):
|
||||
def save(self):
|
||||
return "#LWP-Cookies-2.0\n" + self.as_lwp_str()
|
||||
|
||||
def load(self, data, ignore_discard=True, ignore_expires=True):
|
||||
f = StringIO(data)
|
||||
self._really_load(f, 'memory', ignore_discard, ignore_expires)
|
||||
|
||||
def api_proxy(request):
|
||||
'''
|
||||
settings.OXAPI_URL =...
|
||||
from ox.django.views import api_proxy
|
||||
urlpatterns = patterns('',
|
||||
url(r'^api/$', api_proxy)
|
||||
'''
|
||||
url = settings.OXAPI_URL
|
||||
cj = SessionCookieJar()
|
||||
if 'cj' in request.session:
|
||||
cj.load(request.session['cj'])
|
||||
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
||||
opener.addheaders = [
|
||||
('User-Agent', request.META.get('HTTP_USER_AGENT'))
|
||||
]
|
||||
form = ox.MultiPartForm()
|
||||
for key in request.POST:
|
||||
form.add_field(key, request.POST[key])
|
||||
r = urllib2.Request(url)
|
||||
body = str(form)
|
||||
r.add_header('Content-type', form.get_content_type())
|
||||
r.add_header('Content-length', len(body))
|
||||
r.add_data(body)
|
||||
f = opener.open(r)
|
||||
response = HttpResponse(f.read())
|
||||
request.session['cj'] = cj.save()
|
||||
return response
|
||||
9
Shared/lib/python3.4/site-packages/ox/django/widgets.py
Normal file
9
Shared/lib/python3.4/site-packages/ox/django/widgets.py
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import django.newforms as forms
|
||||
from string import Template
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
class FirefoggWidget(forms.FileInput):
|
||||
def render(self, name, value, attrs=None):
|
||||
tpl = Template(u"""<h1>This should be a Firefogg widget for $name, current value: $value</h1>""")
|
||||
return mark_safe(tpl.substitute(name=name, value=value))
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue