diff --git a/pandora/app/config.py b/pandora/app/config.py new file mode 100644 index 00000000..233cf7f7 --- /dev/null +++ b/pandora/app/config.py @@ -0,0 +1,73 @@ + +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +from __future__ import division, with_statement + +import os +import sys +import time +import thread + +from django.conf import settings +import ox.jsonc +from ox.utils import json + +_win = (sys.platform == "win32") + +RUN_RELOADER = True + +def load_config(): + with open(settings.SITE_CONFIG) as f: + config = ox.jsonc.load(f) + + config['site']['id'] = settings.SITEID + config['site']['name'] = settings.SITENAME + config['site']['sectionName'] = settings.SITENAME + config['site']['url'] = settings.URL + + config['keys'] = {} + for key in config['itemKeys']: + config['keys'][key['id']] = key + + settings.CONFIG = config + +def reloader_thread(): + _config_mtime = 0 + while RUN_RELOADER: + stat = os.stat(settings.SITE_CONFIG) + mtime = stat.st_mtime + if _win: + mtime -= stat.st_ctime + if mtime > _config_mtime: + load_config() + _config_mtime = mtime + time.sleep(1) + +def update_static(): + oxjs_build = os.path.join(settings.STATIC_ROOT, 'oxjs/tools/build/build.py') + if os.path.exists(oxjs_build): + os.system(oxjs_build) + + data = '' + js = [] + pandora_js = os.path.join(settings.STATIC_ROOT, 'js/pandora.js') + pandora_json = os.path.join(settings.STATIC_ROOT, 'json/pandora.json') + for root, folders, files in os.walk(os.path.join(settings.STATIC_ROOT, 'js/pandora')): + for f in files: + if f.endswith('.js'): + js.append(os.path.join(root, f)[len(settings.STATIC_ROOT)+1:]) + with open(os.path.join(root, f)) as j: + data += j.read() + '\n' + + print 'write', pandora_js + with open(pandora_js, 'w') as f: + data = ox.js.minify(data) + f.write(data) + + print 'write', pandora_json + with open(pandora_json, 'w') as f: + json.dump(sorted(js), f, indent=2) + +def init(): + load_config() + thread.start_new_thread(reloader_thread, ()) diff --git a/pandora/app/management/commands/update_static.py b/pandora/app/management/commands/update_static.py index f98c2b7d..8769b0a3 100644 --- a/pandora/app/management/commands/update_static.py +++ b/pandora/app/management/commands/update_static.py @@ -2,7 +2,7 @@ # vi:si:et:sw=4:sts=4:ts=4 from django.core.management.base import BaseCommand -from ... import models +from ... import config class Command(BaseCommand): @@ -12,4 +12,4 @@ class Command(BaseCommand): args = '' def handle(self, **options): - models.update_static() + config.update_static() diff --git a/pandora/app/models.py b/pandora/app/models.py index 15303e5a..1f6a8ab6 100644 --- a/pandora/app/models.py +++ b/pandora/app/models.py @@ -2,17 +2,9 @@ # vi:si:et:sw=4:sts=4:ts=4 from __future__ import division, with_statement -import os -import sys -import time -import thread - from django.db import models -from django.conf import settings -import ox.jsonc -from ox.utils import json +from django.contrib.auth.models import User -_win = (sys.platform == "win32") class Page(models.Model): created = models.DateTimeField(auto_now_add=True) @@ -23,59 +15,22 @@ class Page(models.Model): def __unicode__(self): return self.name -RUN_RELOADER = True +class Log(models.Model): + created = models.DateTimeField(auto_now_add=True) + modified = models.DateTimeField(auto_now=True) + user = models.ForeignKey(User, default=None, blank=True, null=True) + url = models.CharField(max_length=1000, default='') + line = models.IntegerField(default=0) + text = models.TextField(blank=True) -def load_config(): - with open(settings.SITE_CONFIG) as f: - config = ox.jsonc.load(f) + def __unicode__(self): + return self.id - config['site']['id'] = settings.SITEID - config['site']['name'] = settings.SITENAME - config['site']['sectionName'] = settings.SITENAME - config['site']['url'] = settings.URL - - config['keys'] = {} - for key in config['itemKeys']: - config['keys'][key['id']] = key - - settings.CONFIG = config - -def reloader_thread(): - _config_mtime = 0 - while RUN_RELOADER: - stat = os.stat(settings.SITE_CONFIG) - mtime = stat.st_mtime - if _win: - mtime -= stat.st_ctime - if mtime > _config_mtime: - load_config() - _config_mtime = mtime - time.sleep(1) - -thread.start_new_thread(reloader_thread, ()) - -def update_static(): - oxjs_build = os.path.join(settings.STATIC_ROOT, 'oxjs/tools/build/build.py') - if os.path.exists(oxjs_build): - os.system(oxjs_build) - - data = '' - js = [] - pandora_js = os.path.join(settings.STATIC_ROOT, 'js/pandora.js') - pandora_json = os.path.join(settings.STATIC_ROOT, 'json/pandora.json') - for root, folders, files in os.walk(os.path.join(settings.STATIC_ROOT, 'js/pandora')): - for f in files: - if f.endswith('.js'): - js.append(os.path.join(root, f)[len(settings.STATIC_ROOT)+1:]) - with open(os.path.join(root, f)) as j: - data += j.read() + '\n' - - print 'write', pandora_js - with open(pandora_js, 'w') as f: - data = ox.js.minify(data) - f.write(data) - - print 'write', pandora_json - with open(pandora_json, 'w') as f: - json.dump(sorted(js), f, indent=2) - + def json(self): + return { + 'created': self.created, + 'modified': self.modified, + 'user': self.user and self.user.username or '', + 'type': self.type, + 'message': self.message, + } diff --git a/pandora/app/views.py b/pandora/app/views.py index 71decff8..350dfcba 100644 --- a/pandora/app/views.py +++ b/pandora/app/views.py @@ -92,3 +92,32 @@ def redirect_url(request, url): else: return HttpResponse(''%json.dumps(url)) +def log(request): + ''' + param data { + type: 'ERROR', 'WARN' + message: text + } + return { + status: ... + data: { + name: + body: + } + } + ''' + data = json.loads(request.POST['data']) + if not request.user.is_authenticated: + user = request.user + else: + user = None + if 'message' in data: + l = models.Log( + user=user, + type=data.get('type', 'ERROR'), + message=data['message'] + ) + l.save() + response = json_response() + return render_to_json_response(response) +actions.register(log) diff --git a/pandora/item/management/commands/rebuildcache.py b/pandora/item/management/commands/rebuildcache.py index 95b03c3a..452b6367 100644 --- a/pandora/item/management/commands/rebuildcache.py +++ b/pandora/item/management/commands/rebuildcache.py @@ -7,7 +7,7 @@ from os.path import join, dirname, basename, splitext, exists from django.core.management.base import BaseCommand, CommandError from django.conf import settings -import app.models +mport monkey_patch.models from ... import models diff --git a/pandora/item/management/commands/sync_itemsort.py b/pandora/item/management/commands/sync_itemsort.py index 579367dd..808e8451 100644 --- a/pandora/item/management/commands/sync_itemsort.py +++ b/pandora/item/management/commands/sync_itemsort.py @@ -4,6 +4,7 @@ from django.core.management.base import BaseCommand from django.db import connection, transaction +import monkey_patch.models from ... import models diff --git a/pandora/monkey_patch/models.py b/pandora/monkey_patch/models.py index 13c5130c..bf86d81e 100644 --- a/pandora/monkey_patch/models.py +++ b/pandora/monkey_patch/models.py @@ -2,8 +2,8 @@ from django.contrib.auth.models import User from django.core.validators import MaxLengthValidator #load config from json -import app.models -app.models.load_config() +import app.config +app.config.init() NEW_LENGTH = { 'username': 255, diff --git a/pandora/urls.py b/pandora/urls.py index 78ecfddf..cd210589 100644 --- a/pandora/urls.py +++ b/pandora/urls.py @@ -7,9 +7,6 @@ from ox.django.http import HttpFileResponse from django.conf import settings -#gunicorn has issues with settings.CONFIG otherwise -import app.models - # Uncomment the next two lines to enable the admin: from django.contrib import admin admin.autodiscover() diff --git a/static/js/init.js b/static/js/init.js index 88912183..b01117e3 100644 --- a/static/js/init.js +++ b/static/js/init.js @@ -20,6 +20,16 @@ appPanel statusbar */ +window.onerror = function(error, url, line) { + try { + pandora.api.log({ + text: error, + url: url, + line: line + }); + } catch(e) {} +}; + (function() { var debug = localStorage && localStorage.debug,