From ad401da4f1eae23518e90fe5a455dec8f2c54de3 Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Wed, 3 Sep 2014 21:29:47 -0500 Subject: [PATCH] worked on assets bundling, added admin field --- install.sh | 2 +- realms/__init__.py | 19 +++++++++++++------ realms/config/__init__.py | 6 ++++-- realms/modules/auth/models.py | 2 ++ realms/modules/wiki/assets.py | 2 +- realms/modules/wiki/views.py | 4 ++-- realms/static/js/wmd.js | 10 ++++++---- realms/templates/layout.html | 14 ++++++++------ 8 files changed, 37 insertions(+), 22 deletions(-) diff --git a/install.sh b/install.sh index e5e0c14..4ecad1b 100644 --- a/install.sh +++ b/install.sh @@ -9,7 +9,7 @@ add-apt-repository -y ppa:chris-lea/node.js apt-get update apt-get install -y python build-essential git libpcre3-dev python-software-properties \ python-pip python-virtualenv python-dev pkg-config curl libxml2-dev libxslt1-dev zlib1g-dev \ -libffi-dev nodejs screen +libffi-dev nodejs screen node-cleancss # Default cache is memoization diff --git a/realms/__init__.py b/realms/__init__.py index f0d4ac7..4d34f9d 100644 --- a/realms/__init__.py +++ b/realms/__init__.py @@ -88,7 +88,7 @@ class Application(Flask): class Assets(Environment): - default_filters = {'js': 'jsmin', 'css': 'cssmin'} + default_filters = {'js': 'rjsmin', 'css': 'cleancss'} default_output = {'js': 'assets/%(version)s.js', 'css': 'assets/%(version)s.css'} def register(self, name, *args, **kwargs): @@ -96,7 +96,7 @@ class Assets(Environment): filters = kwargs.get('filters', self.default_filters[ext]) output = kwargs.get('output', self.default_output[ext]) - super(Assets, self).register(name, Bundle(*args, filters=filters, output=output)) + return super(Assets, self).register(name, Bundle(*args, filters=filters, output=output)) class RegexConverter(BaseConverter): @@ -123,7 +123,7 @@ def error_handler(e): else: status_code = httplib.INTERNAL_SERVER_ERROR message = None - tb = traceback.format_exc() if current_user.staff else None + tb = traceback.format_exc() if current_user.admin else None if request.is_xhr or request.accept_mimetypes.best in ['application/json', 'text/javascript']: response = { @@ -154,7 +154,7 @@ def create_app(): @app.before_request def init_g(): - g.assets = ['main'] + g.assets = dict(css=['main.css'], js=['main.js']) @app.template_filter('datetime') def _jinja2_filter_datetime(ts): @@ -182,8 +182,8 @@ login_manager.login_view = 'auth.login' db = SQLAlchemy(app) cache = Cache(app) -assets = Environment(app) -assets.register('main', +assets = Assets(app) +assets.register('main.js', 'vendor/jquery/jquery.js', 'vendor/components-underscore/underscore.js', 'vendor/components-bootstrap/js/bootstrap.js', @@ -196,8 +196,15 @@ assets.register('main', 'vendor/parsleyjs/dist/parsley.js', 'js/main.js') +assets.register('main.css', + 'css/bootstrap/flatly.css', + 'css/font-awesome.min.css', + 'vendor/highlightjs/styles/github.css', + 'css/style.css') + app.discover() +# Should be called explicitly during install? db.create_all() diff --git a/realms/config/__init__.py b/realms/config/__init__.py index 65e8306..b1cc08f 100644 --- a/realms/config/__init__.py +++ b/realms/config/__init__.py @@ -9,11 +9,11 @@ ENV = 'DEV' DEBUG = True ASSETS_DEBUG = True - SQLALCHEMY_ECHO = True -PORT = 80 +PORT = 5000 BASE_URL = 'http://realms.dev' +SITE_TITLE = "Realms" DB_URI = 'sqlite:///%s/wiki.db' % USER_HOME @@ -27,6 +27,8 @@ CACHE_REDIS_PORT = 6379 CACHE_REDIS_DB = '0' """ +# Get ReCaptcha Keys for your domain here: +# https://www.google.com/recaptcha/admin#whyrecaptcha RECAPTCHA_ENABLE = False RECAPTCHA_USE_SSL = False RECAPTCHA_PUBLIC_KEY = "6LfYbPkSAAAAAB4a2lG2Y_Yjik7MG9l4TDzyKUao" diff --git a/realms/modules/auth/models.py b/realms/modules/auth/models.py index 86ce9a5..a596880 100644 --- a/realms/modules/auth/models.py +++ b/realms/modules/auth/models.py @@ -38,6 +38,7 @@ def load_token(token): class AnonUser(AnonymousUserMixin): username = 'Anon' email = '' + admin = False class User(Model, UserMixin): @@ -46,6 +47,7 @@ class User(Model, UserMixin): username = db.Column(db.String, unique=True) email = db.Column(db.String, unique=True) password = db.Column(db.String) + admin = False hidden_fields = ['password'] readonly_fields = ['email', 'password'] diff --git a/realms/modules/wiki/assets.py b/realms/modules/wiki/assets.py index a6c26b9..a0981b7 100644 --- a/realms/modules/wiki/assets.py +++ b/realms/modules/wiki/assets.py @@ -1,6 +1,6 @@ from realms import assets -assets.register('editor', +assets.register('editor.js', 'js/ace/ace.js', 'js/ace/mode-markdown.js', 'vendor/keymaster/keymaster.js', diff --git a/realms/modules/wiki/views.py b/realms/modules/wiki/views.py index 8176654..f5c576c 100644 --- a/realms/modules/wiki/views.py +++ b/realms/modules/wiki/views.py @@ -62,7 +62,7 @@ def edit(name): if data: name = remove_ext(data['name']) content = data['data'] - g.assets.append('editor') + g.assets['js'].append('editor.js') return render_template('wiki/edit.html', name=name, content=content) else: return redirect(url_for('wiki.create', name=cname)) @@ -90,7 +90,7 @@ def create(name): # Page exists, edit instead return redirect(url_for('wiki.edit', name=cname)) - g.assets.append('editor') + g.assets['js'].append('editor.js') return render_template('wiki/edit.html', name=cname, content="") diff --git a/realms/static/js/wmd.js b/realms/static/js/wmd.js index 07450c8..86f767a 100644 --- a/realms/static/js/wmd.js +++ b/realms/static/js/wmd.js @@ -41,9 +41,11 @@ WMD.convert = function(content, options) { }; -function gsub(str, re, fn, /*optional*/newstr) { +function gsub(str, regex, fn, /*optional*/newstr) { newstr = newstr || ''; + var re = new RegExp(regex); var match = re.exec(str); + var remaining; if (match) { newstr += str.slice(0, match.index); newstr += fn.apply(null, match); @@ -51,7 +53,7 @@ function gsub(str, re, fn, /*optional*/newstr) { return gsub(remaining, re, fn, newstr); } return newstr + str; -}; +} WMD.showdown = new Showdown.converter({extensions: ['table']}); WMD.processor = WMD.showdown.makeHtml; @@ -61,7 +63,7 @@ WMD.preprocessors = { underscores: function (doc) { // prevent foo_bar_baz from ending up with an italic word in the middle doc.markdown = gsub(doc.markdown, - /(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/, function (match) { + "/(^(?! {4}|\t)\w+_\w+_\w[\w_]*)/", function (match) { var count = 0; for (var i = 0; i < match.length; i++) { if (match[i] == '_') count++; @@ -83,7 +85,7 @@ WMD.preprocessors = { while (lines.length) { var match = /^(\S+):\s+(.*)$/.exec(lines[0]); if (match) { - var key = match[1]; + key = match[1]; doc.metadata[key] = match[2]; lines.shift(); } diff --git a/realms/templates/layout.html b/realms/templates/layout.html index e8a7789..2878871 100644 --- a/realms/templates/layout.html +++ b/realms/templates/layout.html @@ -8,10 +8,12 @@ Realms - - - - + + {% for bundle in g.assets['css'] %} + {% assets bundle %} + + {% endassets %} + {% endfor %} {% block css %}{% endblock %} @@ -32,7 +34,7 @@ - Realms + {{ config.SITE_TITLE }} - {% for bundle in g.assets %} + {% for bundle in g.assets['js'] %} {% assets bundle %} {% endassets %}