Commit b0aeb601 authored by YSelf Tool's avatar YSelf Tool

Added time view, single template for update javascript files

parent d067165e
from flask import Blueprint, render_template, redirect, url_for, request, flash, abort, send_file, Response from flask import Blueprint, redirect, url_for, request, flash, abort, send_file, Response
from flask.ext.login import login_required from flask.ext.login import login_required
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
from models.database import User, Event from models.database import User, Event
from models.forms import AdminUserForm, NewUserForm, NewEventForm from models.forms import AdminUserForm, NewUserForm, NewEventForm
from shared import db, admin_permission from shared import db, admin_permission, render_layout
admin = Blueprint("admin", __name__) admin = Blueprint("admin", __name__)
...@@ -16,14 +16,14 @@ admin = Blueprint("admin", __name__) ...@@ -16,14 +16,14 @@ admin = Blueprint("admin", __name__)
def index(): def index():
users = User.query.limit(10).all() users = User.query.limit(10).all()
events = Event.query.limit(10).all() events = Event.query.limit(10).all()
return render_template("admin_index.html", users=users, events=events) return render_layout("admin_index.html", users=users, events=events)
@admin.route("/user/") @admin.route("/user/")
@login_required @login_required
@admin_permission.require() @admin_permission.require()
def user(): def user():
users = User.query.all() users = User.query.all()
return render_template("admin_user_index.html", users=users) return render_layout("admin_user_index.html", users=users)
@admin.route("/user/edit", methods=["GET", "POST"]) @admin.route("/user/edit", methods=["GET", "POST"])
@login_required @login_required
...@@ -38,7 +38,7 @@ def user_edit(): ...@@ -38,7 +38,7 @@ def user_edit():
db.session.commit() db.session.commit()
return redirect(url_for(".index")) return redirect(url_for(".index"))
else: else:
return render_template("admin_user_edit.html", form=form, id=user_id) return render_layout("admin_user_edit.html", form=form, id=user_id)
else: else:
return redirect(url_for(".index")) return redirect(url_for(".index"))
...@@ -66,7 +66,7 @@ def user_new(): ...@@ -66,7 +66,7 @@ def user_new():
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
return redirect(url_for(".user")) return redirect(url_for(".user"))
return render_template("admin_user_new.html", form=form) return render_layout("admin_user_new.html", form=form)
@admin.route("/event/new", methods=["GET", "POST"]) @admin.route("/event/new", methods=["GET", "POST"])
...@@ -77,12 +77,12 @@ def event_new(): ...@@ -77,12 +77,12 @@ def event_new():
if form.validate_on_submit(): if form.validate_on_submit():
if Event.query.filter_by(name=form.name.data).count() > 0: if Event.query.filter_by(name=form.name.data).count() > 0:
flash("There already is an event with that name.", "alert-error") flash("There already is an event with that name.", "alert-error")
return render_template("admin_event_new.html", form=form) return render_layout("admin_event_new.html", form=form)
event = Event(form.name.data, form.date.data) event = Event(form.name.data, form.date.data)
db.session.add(event) db.session.add(event)
db.session.commit() db.session.commit()
return redirect(url_for(".event")) return redirect(url_for(".event"))
return render_template("admin_event_new.html", form=form) return render_layout("admin_event_new.html", form=form)
@admin.route("/event/delete") @admin.route("/event/delete")
@login_required @login_required
...@@ -109,7 +109,7 @@ def event_edit(): ...@@ -109,7 +109,7 @@ def event_edit():
db.session.commit() db.session.commit()
return redirect(url_for(".index")) return redirect(url_for(".index"))
else: else:
return render_template("admin_event_edit.html", form=form, id=event_id) return render_layout("admin_event_edit.html", form=form, id=event_id)
else: else:
return redirect(url_for(".index")) return redirect(url_for(".index"))
...@@ -119,4 +119,4 @@ def event_edit(): ...@@ -119,4 +119,4 @@ def event_edit():
def event(): def event():
events = Event.query.all() events = Event.query.all()
print(events) print(events)
return render_template("admin_event_index.html", events=events) return render_layout("admin_event_index.html", events=events)
from flask import Blueprint, render_template, redirect, url_for, request, flash, abort, send_file, Response from flask import Blueprint, redirect, url_for, request, flash, abort, send_file, Response
from flask.ext.login import login_required from flask.ext.login import login_required
from models.database import User, Statement, Speaker, Event from models.database import User, Statement, Speaker, Event
from models.forms import AddStatementForm from models.forms import AddStatementForm
from shared import db, admin_permission, user_permission from shared import db, admin_permission, user_permission, render_layout
from datetime import datetime from datetime import datetime
import json import json
import config
speech = Blueprint("speech", __name__) speech = Blueprint("speech", __name__)
def transpose(arr): def transpose(arr):
...@@ -41,7 +43,7 @@ def index(): ...@@ -41,7 +43,7 @@ def index():
form.event.data = event.id form.event.data = event.id
meta.append((query_statements(mode, event.id), form, event)) meta.append((query_statements(mode, event.id), form, event))
event_id = -1 event_id = -1
return render_template("speech_index.html", meta=meta, event_id=event_id) return render_layout("speech_index.html", meta=meta, event_id=event_id)
@speech.route("/show") @speech.route("/show")
def show(): def show():
...@@ -54,7 +56,7 @@ def show(): ...@@ -54,7 +56,7 @@ def show():
else: else:
for event in Event.query.all(): for event in Event.query.all():
meta.append((query_statements(mode, event.id), event)) meta.append((query_statements(mode, event.id), event))
return render_template("speech_show.html", mode=mode, meta=meta, event_id=event_id) return render_layout("speech_show.html", mode=mode, meta=meta, event_id=event_id)
@speech.route("/update") @speech.route("/update")
def update(): def update():
...@@ -67,7 +69,7 @@ def update(): ...@@ -67,7 +69,7 @@ def update():
else: else:
for event in Event.query.all(): for event in Event.query.all():
meta.append((query_statements(mode, event.id), event)) meta.append((query_statements(mode, event.id), event))
return render_template("speech_content_show.html", mode=mode, meta=meta) return render_layout("speech_content_show.html", mode=mode, meta=meta)
@speech.route("/add", methods=["GET", "POST"]) @speech.route("/add", methods=["GET", "POST"])
...@@ -130,7 +132,10 @@ def done(): ...@@ -130,7 +132,10 @@ def done():
@speech.route("/update_show.js") @speech.route("/update_show.js")
def update_show_js(): def update_show_js():
update_interval = config.UPDATE_SHOW_INTERVAL or 1
div = "rede-content-div"
mode = request.args.get("mode", "pending") mode = request.args.get("mode", "pending")
event_id = request.args.get("event", -1) event_id = request.args.get("event", -1)
return render_template("update_show.js", mode=mode, event_id=event_id) target_url = url_for(".update", mode=mode, event=event_id)
return render_layout("update.js", update_interval=update_interval, div=div, target_url=target_url)
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask import Flask, g, current_app, request, render_template, session, flash, redirect, url_for, abort from flask import Flask, g, current_app, request, session, flash, redirect, url_for, abort
from flask.ext.login import login_user, logout_user, login_required, current_user from flask.ext.login import login_user, logout_user, login_required, current_user
from flask.ext.principal import Principal, Identity, AnonymousIdentity, identity_changed, identity_loaded, UserNeed, RoleNeed from flask.ext.principal import Principal, Identity, AnonymousIdentity, identity_changed, identity_loaded, UserNeed, RoleNeed
from passlib.hash import pbkdf2_sha256 from passlib.hash import pbkdf2_sha256
import config import config
from shared import db, login_manager from shared import db, login_manager, render_layout
from models.forms import LoginForm, NewUserForm from models.forms import LoginForm, NewUserForm
from models.database import User, Statement, Speaker, Event from models.database import User, Statement, Speaker, Event
...@@ -41,7 +41,7 @@ def index(): ...@@ -41,7 +41,7 @@ def index():
no_speaker = Speaker("No Speaker", event) no_speaker = Speaker("No Speaker", event)
no_statement = Statement(no_speaker, event) no_statement = Statement(no_speaker, event)
meta.append((ls[0] if len(ls) > 0 else (no_statement, no_speaker, ()), event)) meta.append((ls[0] if len(ls) > 0 else (no_statement, no_speaker, ()), event))
return render_template("index.html", meta=meta) return render_layout("index.html", meta=meta)
@app.route("/update") @app.route("/update")
def update(): def update():
...@@ -52,11 +52,25 @@ def update(): ...@@ -52,11 +52,25 @@ def update():
no_speaker = Speaker("No Speaker", event) no_speaker = Speaker("No Speaker", event)
no_statement = Statement(no_speaker, event) no_statement = Statement(no_speaker, event)
meta.append((ls[0] if len(ls) > 0 else (no_statement, no_speaker, ()), event)) meta.append((ls[0] if len(ls) > 0 else (no_statement, no_speaker, ()), event))
return render_template("content_index.html", meta=meta) return render_layout("content_index.html", meta=meta)
@app.route("/update.js") @app.route("/update.js")
def update_js(): def update_js():
return render_template("update_index.js") update_interval = config.UPDATE_INDEX_INTERVAL or 1
div = "rede-content-div"
target_url = url_for(".update")
return render_layout("update.js", update_interval=update_interval, div=div, target=target_url)
@app.route("/update_time")
def update_time():
return render_layout("content_time.html")
@app.route("/update_time.js")
def update_time_js():
update_interval = config.UPDATE_TIME_INTERVAL or 10
div = "rede-time-div"
target_url = url_for("update_time")
return render_layout("update.js", update_interval=update_interval, div=div, target_url=target_url)
@app.route("/login", methods=["GET", "POST"]) @app.route("/login", methods=["GET", "POST"])
def login(): def login():
...@@ -70,7 +84,7 @@ def login(): ...@@ -70,7 +84,7 @@ def login():
return redirect(request.args.get("next") or url_for(".index")) return redirect(request.args.get("next") or url_for(".index"))
else: else:
flash("Invalid username or wrong password", "alert-error") flash("Invalid username or wrong password", "alert-error")
return render_template("login.html", form=form) return render_layout("login.html", form=form)
@app.route("/logout", methods=["GET", "POST"]) @app.route("/logout", methods=["GET", "POST"])
@login_required @login_required
...@@ -89,14 +103,14 @@ def register(): ...@@ -89,14 +103,14 @@ def register():
length = len(db.session.query(User).filter_by(username=form.username.data).all()) length = len(db.session.query(User).filter_by(username=form.username.data).all())
if length > 0: if length > 0:
flash("There already is a user with that name.") flash("There already is a user with that name.")
return render_template("register.html", form=form) return render_layout("register.html", form=form)
password = pbkdf2_sha256.encrypt(form.password.data, rounds=200000, salt_size=16) password = pbkdf2_sha256.encrypt(form.password.data, rounds=200000, salt_size=16)
user = User(fullname, username, password, []) user = User(fullname, username, password, [])
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
flash("Your account has been created, you may now log in with it.") flash("Your account has been created, you may now log in with it.")
return redirect(url_for(".login")) return redirect(url_for(".login"))
return render_template("register.html", form=form) return render_layout("register.html", form=form)
@identity_loaded.connect_via(app) @identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity): def on_identity_loaded(sender, identity):
......
from flask import abort from flask import abort, render_template
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.login import LoginManager from flask.ext.login import LoginManager
from flask.ext.principal import Permission, RoleNeed from flask.ext.principal import Permission, RoleNeed
from datetime import datetime
db = SQLAlchemy() db = SQLAlchemy()
login_manager = LoginManager() login_manager = LoginManager()
...@@ -9,3 +10,7 @@ login_manager = LoginManager() ...@@ -9,3 +10,7 @@ login_manager = LoginManager()
admin_permission = Permission(RoleNeed("admin")) admin_permission = Permission(RoleNeed("admin"))
user_permission = Permission(RoleNeed("user")) user_permission = Permission(RoleNeed("user"))
roles = ["user", "admin"] roles = ["user", "admin"]
def render_layout(template, **kwargs):
current_time = datetime.now()
return render_template(template, current_time=current_time, **kwargs)
{{ current_time.strftime("%H:%M") }}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
<meta name="mobile-web-app-capable" content="yes"> <meta name="mobile-web-app-capable" content="yes">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" /> <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />
<title>{% block title %}Unknown Page{% endblock %} - Redeleitsystem</title> <title>{% block title %}Unknown Page{% endblock %} - Redeleitsystem</title>
<script src="{{ url_for("update_time_js") }}" async></script>
{% block additional_js %} {% block additional_js %}
<!-- Place to insert more .js-files --> <!-- Place to insert more .js-files -->
{% endblock %} {% endblock %}
...@@ -23,6 +24,10 @@ ...@@ -23,6 +24,10 @@
<div class="mdl-layout__header-row"> <div class="mdl-layout__header-row">
<a href="{{ url_for('index') }}"><span class="mdl-layout-title">Redeleitsystem</span></a> <a href="{{ url_for('index') }}"><span class="mdl-layout-title">Redeleitsystem</span></a>
<div class="mdl-layout-spacer"></div> <div class="mdl-layout-spacer"></div>
<span id="rede-time-div" class="mdl-layout-title">
{% include "content_time.html" %}
</span>
<div class="mdl-layout-spacer"></div>
<button class="mdl-button mdl-js-button mdl-js-ripple-effet mdl-button--icon" id="hdrbtn"> <button class="mdl-button mdl-js-button mdl-js-ripple-effet mdl-button--icon" id="hdrbtn">
<i class="material-icons">more_vert</i> <i class="material-icons">more_vert</i>
</button> </button>
......
...@@ -11,20 +11,19 @@ function request() { ...@@ -11,20 +11,19 @@ function request() {
update(xmlhttp.responseText); update(xmlhttp.responseText);
} }
}; };
var target = "{{ url_for('.update') }}"; var target = "{{ target_url }}";
xmlhttp.open("GET", target, true); xmlhttp.open("GET", target, true);
xmlhttp.send(); xmlhttp.send();
} }
function update(data) { function update(data) {
if (data != last_content) { if (data != last_content) {
document.getElementById("rede-content-div").innerHTML = data; document.getElementById("{{ div }}").innerHTML = data;
last_content = data; last_content = data;
} }
} }
window.onload=function() { window.onload=function() {
window.setInterval(request, 1000); window.setInterval(request, 1000 * {{ update_interval }});
} }
var returned = true;
var last_content = "";
function request() {
if (!returned)
return;
returned = false;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
returned = true;
update(xmlhttp.responseText);
}
};
var target = "{{ url_for('.update', mode=mode, event=event_id) }}";
xmlhttp.open("GET", target, true);
xmlhttp.send();
}
function update(data) {
if (data != last_content) {
document.getElementById("rede-content-div").innerHTML = data;
last_content = data;
}
}
window.onload=function() {
window.setInterval(request, 1000);
}
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