Explorar el Código

Force client-side purging of CSS/JS when updated.

pull/24/head
Ben Kurtovic hace 8 años
padre
commit
016742402f
Se han modificado 3 ficheros con 27 adiciones y 5 borrados
  1. +19
    -0
      app.py
  2. +5
    -2
      templates/api.mako
  3. +3
    -3
      templates/support/header.mako

+ 19
- 0
app.py Ver fichero

@@ -2,9 +2,11 @@
# -*- coding: utf-8 -*-

from functools import wraps
from hashlib import md5
from json import dumps
from logging import DEBUG, INFO, getLogger
from logging.handlers import TimedRotatingFileHandler
from os import path
from time import asctime
from traceback import format_exc

@@ -27,6 +29,7 @@ hand = TimedRotatingFileHandler("logs/app.log", when="midnight", backupCount=7)
hand.setLevel(DEBUG)
app.logger.addHandler(hand)
app.logger.info(u"Flask server started " + asctime())
app._hash_cache = {}

def catch_errors(func):
@wraps(func)
@@ -77,6 +80,22 @@ def close_databases(error):
if g._db:
g._db.close()

def external_url_handler(error, endpoint, **values):
if endpoint == "static" and "file" in values:
fpath = path.join(app.static_folder, values["file"])
mtime = path.getmtime(fpath)
cache = app._hash_cache.get(fpath)
if cache and cache[0] == mtime:
hashstr = cache[1]
else:
with open(fpath, "rb") as f:
hashstr = md5(f.read()).hexdigest()
app._hash_cache[fpath] = (mtime, hashstr)
return "/static/{0}?v={1}".format(values["file"], hashstr)
raise error

app.build_error_handler = external_url_handler

@app.route("/")
@catch_errors
def index():


+ 5
- 2
templates/api.mako Ver fichero

@@ -1,4 +1,7 @@
<%! from json import dumps %>\
<%!
from json import dumps
from flask import url_for
%>\
<%def name="do_indent(size)">
<br />
% for i in xrange(size):
@@ -31,7 +34,7 @@
<head>
<meta charset="utf-8">
<title>API - Earwig's Copyvio Detector</title>
<link rel="stylesheet" href="${request.script_root}/static/api.min.css" type="text/css" />
<link rel="stylesheet" href="${request.script_root}$(url_for('static', file='api.min.css')" type="text/css" />
</head>
<body>
% if help:


+ 3
- 3
templates/support/header.mako Ver fichero

@@ -1,6 +1,6 @@
<%page args="title"/>\
<%!
from flask import g, request
from flask import g, request, url_for
from copyvios.background import set_background
%>\
<!DOCTYPE html>
@@ -8,9 +8,9 @@
<head>
<meta charset="utf-8">
<title>${title}</title>
<link rel="stylesheet" href="${request.script_root}/static/style.min.css" type="text/css" />
<link rel="stylesheet" href="${request.script_root}$(url_for('static', file='style.min.css')" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script src="${request.script_root}/static/script.min.js" type="text/javascript"></script>
<script src="${request.script_root}/$(url_for('static', file='script.min.js')" type="text/javascript"></script>
</head>
<% selected = g.cookies["CopyviosBackground"].value if "CopyviosBackground" in g.cookies else "list" %>\
% if selected == "plain":


Cargando…
Cancelar
Guardar