log js errors

This commit is contained in:
j 2011-10-25 15:30:14 +02:00
parent 1af3139e16
commit d9e4d26b95
9 changed files with 136 additions and 71 deletions

73
pandora/app/config.py Normal file
View file

@ -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, ())

View file

@ -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()

View file

@ -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,
}

View file

@ -92,3 +92,32 @@ def redirect_url(request, url):
else:
return HttpResponse('<script>document.location.href=%s;</script>'%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)

View file

@ -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

View file

@ -4,6 +4,7 @@
from django.core.management.base import BaseCommand
from django.db import connection, transaction
import monkey_patch.models
from ... import models

View file

@ -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,

View file

@ -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()

View file

@ -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,