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 # vi:si:et:sw=4:sts=4:ts=4
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from ... import models from ... import config
class Command(BaseCommand): class Command(BaseCommand):
@ -12,4 +12,4 @@ class Command(BaseCommand):
args = '' args = ''
def handle(self, **options): 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 # vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement from __future__ import division, with_statement
import os
import sys
import time
import thread
from django.db import models from django.db import models
from django.conf import settings from django.contrib.auth.models import User
import ox.jsonc
from ox.utils import json
_win = (sys.platform == "win32")
class Page(models.Model): class Page(models.Model):
created = models.DateTimeField(auto_now_add=True) created = models.DateTimeField(auto_now_add=True)
@ -23,59 +15,22 @@ class Page(models.Model):
def __unicode__(self): def __unicode__(self):
return self.name 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(): def __unicode__(self):
with open(settings.SITE_CONFIG) as f: return self.id
config = ox.jsonc.load(f)
config['site']['id'] = settings.SITEID def json(self):
config['site']['name'] = settings.SITENAME return {
config['site']['sectionName'] = settings.SITENAME 'created': self.created,
config['site']['url'] = settings.URL 'modified': self.modified,
'user': self.user and self.user.username or '',
config['keys'] = {} 'type': self.type,
for key in config['itemKeys']: 'message': self.message,
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)

View File

@ -92,3 +92,32 @@ def redirect_url(request, url):
else: else:
return HttpResponse('<script>document.location.href=%s;</script>'%json.dumps(url)) 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.core.management.base import BaseCommand, CommandError
from django.conf import settings from django.conf import settings
import app.models mport monkey_patch.models
from ... import models from ... import models

View File

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

View File

@ -2,8 +2,8 @@ from django.contrib.auth.models import User
from django.core.validators import MaxLengthValidator from django.core.validators import MaxLengthValidator
#load config from json #load config from json
import app.models import app.config
app.models.load_config() app.config.init()
NEW_LENGTH = { NEW_LENGTH = {
'username': 255, 'username': 255,

View File

@ -7,9 +7,6 @@ from ox.django.http import HttpFileResponse
from django.conf import settings from django.conf import settings
#gunicorn has issues with settings.CONFIG otherwise
import app.models
# Uncomment the next two lines to enable the admin: # Uncomment the next two lines to enable the admin:
from django.contrib import admin from django.contrib import admin
admin.autodiscover() admin.autodiscover()

View File

@ -20,6 +20,16 @@ appPanel
statusbar statusbar
*/ */
window.onerror = function(error, url, line) {
try {
pandora.api.log({
text: error,
url: url,
line: line
});
} catch(e) {}
};
(function() { (function() {
var debug = localStorage && localStorage.debug, var debug = localStorage && localStorage.debug,