Commit b16adaac authored by buttle's avatar buttle

Added favicon preference

parent 9ee8fb87
......@@ -21,7 +21,7 @@ from flask import Flask
from flask_pymongo import PyMongo
from flask_babel import Babel
from flask_wtf.csrf import CSRFProtect
import sys, os
app = Flask(__name__)
app.config.from_pyfile('config.cfg')
......@@ -31,10 +31,10 @@ babel = Babel(app)
csrf = CSRFProtect()
csrf.init_app(app)
app.config['APP_VERSION'] = 20
app.config['APP_VERSION'] = 21
app.config['SCHEMA_VERSION'] = 10
app.config['RESERVED_SLUGS'] = ['static', 'admin', 'admins', 'user', 'users', 'form', 'forms', 'site', 'sites']
app.config['RESERVED_SLUGS'] = ['static', 'admin', 'admins', 'user', 'users', 'form', 'forms', 'site', 'sites', 'update']
# DPL = Data Protection Law
app.config['RESERVED_FORM_ELEMENT_NAMES'] = ['created', 'csrf_token', 'DPL']
app.config['RESERVED_USERNAMES'] = ['system', 'admin']
......@@ -45,8 +45,8 @@ app.config['LANGUAGES'] = {
'ca': ('Català', 'es-ES'),
'es': ('Castellano', 'es-ES')
}
app.config['FAVICON_FOLDER'] = "%s/static/images/favicon/" % os.path.dirname(os.path.abspath(__file__))
import sys, os
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/form_templates")
from GNGforms import views
......
......@@ -26,6 +26,7 @@ from flask_babel import gettext
from urllib.parse import urlparse
import os, string, random, datetime, json, markdown
import pprint
pp = pprint.PrettyPrinter()
......@@ -676,6 +677,20 @@ class Site(object):
url = "%s:%s" % (url, self.site['port'])
return url+'/'
def faviconURL(self):
path="%s%s_favicon.png" % (app.config['FAVICON_FOLDER'], self.hostname)
if os.path.exists(path):
return "/static/images/favicon/%s_favicon.png" % self.hostname
else:
return "/static/images/favicon/default-favicon.png"
def deleteFavicon(self):
path="%s%s_favicon.png" % (app.config['FAVICON_FOLDER'], self.hostname)
if os.path.exists(path):
os.remove(path)
return True
return False
@property
def blurb(self):
return self.site['blurb']
......
......@@ -3,6 +3,7 @@
<head>
<meta charset="utf-8">
<title>{{ g.site.siteName }}</title>
<link rel="shortcut icon" type="image/png" href="{{g.site.faviconURL()}}"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="/static/jquery/jquery.min.js"></script>
<script src="/static/jquery/jquery-ui.min.js"></script>
......
{% extends "base.html" %}
{% block content %}
<div class="container">
<div class="row col-md-4"></div>
<div class="row col-md-4">
<div style="font-size:1.5em">
{%trans%}Change site favicon{%endtrans%}
<img src="{{g.site.faviconURL()}}" width=24 height=24 />
</div>
<hr />
<p></p>
<form method="POST" enctype=multipart/form-data >
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}" />
<label>{%trans%}Upload image{%endtrans%}</label>
<br />
<span class="hint">{%trans%}A small, square PNG image{%endtrans%}</span>
<br />
<input type=file name=file><br />
<p></p>
<input class="btn-danger btn" type="button" value="{%trans%}Reset{%endtrans%}" onClick="location.href='/site/reset-favicon'">
<input class="btn-success btn" type="submit" value="{%trans%}Submit{%endtrans%}">
</form>
</div>
<div class="row col-md-4"></div>
</div>
{% endblock %}
......@@ -157,6 +157,15 @@
<input class="btn-primary btn btn-xs" type="button" value="{%trans%}Change{%endtrans%}" onClick="location.href='/site/change-sitename'">
</td>
</tr>
<tr>
<td>{%trans%}Favicon{%endtrans%}</td>
<td>
<img src="{{g.site.faviconURL()}}" width=18 height=18 />
</td>
<td class="text-right">
<input class="btn-primary btn btn-xs" type="button" value="{%trans%}Change{%endtrans%}" onClick="location.href='/site/change-favicon'">
</td>
</tr>
<tr>
<td>{%trans%}Email address{%endtrans%}</td>
<td>
......
......@@ -43,9 +43,9 @@ def before_request():
g.current_user=None
g.isRootUser=False
g.isAdmin=False
g.site=Site()
if '/static' in request.path:
return
g.site=Site()
if 'user_id' in session:
g.current_user=User(_id=session["user_id"], hostname=g.site.hostname)
if not g.current_user:
......@@ -1041,6 +1041,32 @@ def change_siteName():
return redirect(make_url_for('user_settings', username=g.current_user.username))
return render_template('change-sitename.html', site=g.site)
@app.route('/site/change-favicon', methods=['GET', 'POST'])
@admin_required
def change_site_favicon():
if request.method == 'POST':
if not request.files['file']:
flash(gettext("Required file is missing"), 'warning')
return render_template('change-site-favicon.html')
file=request.files['file']
if len(file.filename) > 4 and file.filename[-4:] == ".png":
filename="%s_favicon.png" % g.site.hostname
file.save(os.path.join(app.config['FAVICON_FOLDER'], filename))
else:
flash(gettext("Bad file name. PNG only"), 'warning')
return render_template('change-site-favicon.html')
flash(gettext("Favicon changed OK. Refresh with &lt;F5&gt;"), 'success')
return redirect(make_url_for('user_settings', username=g.current_user.username))
return render_template('change-site-favicon.html')
@app.route('/site/reset-favicon', methods=['GET'])
@admin_required
def reset_site_favicon():
if g.site.deleteFavicon():
flash(gettext("Favicon reset OK. Refresh with &lt;F5&gt;"), 'success')
return redirect(make_url_for('user_settings', username=g.current_user.username))
@app.route('/site/test-smtp/<string:email>', methods=['GET'])
@rootuser_required
def test_smtp(email):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment