diff options
| author | rubenwardy <rw@rubenwardy.com> | 2018-05-09 18:35:36 +0100 |
|---|---|---|
| committer | rubenwardy <rw@rubenwardy.com> | 2018-05-09 18:35:36 +0100 |
| commit | 1b0dfb2acc9b2f971cf1e2102e6d4faefbc389f4 (patch) | |
| tree | 24e37cad8d66abfcb2011288af3fc3e89d63309a /app/views | |
| parent | 552e35f6aaa4bbbb680aa8fae89fd0904dd5f5f4 (diff) | |
| download | cheatdb-1b0dfb2acc9b2f971cf1e2102e6d4faefbc389f4.tar.xz | |
Add scss, improve homepage
Diffstat (limited to 'app/views')
| -rw-r--r-- | app/views/__init__.py | 5 | ||||
| -rw-r--r-- | app/views/sass.py | 67 |
2 files changed, 70 insertions, 2 deletions
diff --git a/app/views/__init__.py b/app/views/__init__.py index 3c8c009..17b9f00 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -27,6 +27,7 @@ def send_upload(path): @app.route("/") @menu.register_menu(app, ".", "Home") def home_page(): - return render_template("index.html") + packages = Package.query.filter_by(approved=True).all() + return render_template("index.html", packages=packages) -from . import users, githublogin, packages +from . import users, githublogin, packages, sass diff --git a/app/views/sass.py b/app/views/sass.py new file mode 100644 index 0000000..2ae3896 --- /dev/null +++ b/app/views/sass.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +""" +A small Flask extension that makes it easy to use Sass (SCSS) with your +Flask application. + +Code unabashedly adapted from https://github.com/weapp/flask-coffee2js + +:copyright: (c) 2012 by Ivan Miric. +:license: MIT, see LICENSE for more details. +""" + +import os +import os.path +import codecs +from flask import * +from scss import Scss + +from app import app + +def _convert(dir, src, dst): + original_wd = os.getcwd() + os.chdir(dir) + + css = Scss() + source = codecs.open(src, 'r', encoding='utf-8').read() + output = css.compile(source) + + os.chdir(original_wd) + + outfile = codecs.open(dst, 'w', encoding='utf-8') + outfile.write(output) + outfile.close() + +def _getDirPath(originalPath, create=False): + path = originalPath + + if not os.path.isdir(path): + path = os.path.join(app.root_path, path) + + if not os.path.isdir(path): + if create: + os.mkdir(path) + else: + raise IOError("Unable to find " + originalPath) + + return path + +def sass(app, inputDir='scss', outputPath='static', force=False, cacheDir=None): + static_url_path = app.static_url_path + inputDir = _getDirPath(inputDir) + cacheDir = _getDirPath(cacheDir or outputPath, True) + + def _sass(filepath): + sassfile = "%s/%s.scss" % (inputDir, filepath) + cacheFile = "%s/%s.css" % (cacheDir, filepath) + + # Source file exists, and needs regenerating + if os.path.isfile(sassfile) and (force or not os.path.isfile(cacheFile) or \ + os.path.getmtime(sassfile) > os.path.getmtime(cacheFile)): + _convert(inputDir, sassfile, cacheFile) + app.logger.debug('Compiled %s into %s' % (sassfile, cacheFile)) + + return send_from_directory(cacheDir, filepath + ".css") + + app.add_url_rule("/%s/<path:filepath>.css" % (outputPath), 'sass', _sass) + +sass(app) |
