diff --git a/main.py b/main.py index 684a583..2270f1f 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,10 @@ +# -*- coding: utf-8 -*- + import feedparser -from flask import Flask, render_template +import requests +import string +import random +from flask import Flask, render_template, request, flash, session, abort from time import mktime from datetime import datetime @@ -18,13 +23,68 @@ def pull_feed_entries(): all_entries.sort(key=lambda e: e.updated_parsed, reverse=True) return all_entries[:app.config['MAX_ENTRIES']] -@app.route('/') +def mailman_subscribe(email, mailing_list): + r = requests.post("https://lists.hackerspace.pl/subscribe/" + mailing_list, {"email": email, "fullname": "", "pw": "", "pw-conf": "", "email-button": "Zapisz", "digest": 0}) + if r.status_code != 200: + return False + return True + +@app.route('/', methods=["POST", "GET"]) def main(): + if request.method == "POST": + if "email" in request.form: + email = request.form["email"].strip() + if len(email) > 0: + if "@" in email: + lists = [] + if "mail-waw" in request.form: + lists.append("waw") + if "mail-proj" in request.form: + lists.append("waw-proj") + if "mail-offtopic" in request.form: + lists.append("waw-ot") + subscribed = [] + failed = [] + for maillist in lists: + if mailman_subscribe(email, maillist): + subscribed.append(maillist) + else: + failed.append(maillist) + if len(subscribed) == 1: + flash(u"Pomyślnie zasubskrybowano na listę %s@lists.hackerspace.pl.\ + W celu aktywacji subskrypcji odwiedź odnośnik wysłany mailem na adres %s." % (subscribed[0], email)) + elif len(subscribed) > 1: + flash(u"Pomyślnie zasubskrybowano na listy %s. \ +W celu aktywacji subskrypcji odwiedź odnośniki wysłane mailem na adres %s." % \ + (", ".join(l + "@lists.hackerspace.pl" for l in subscribed), email)) + if len(failed) > 0: + flash(u"Wystąpił problem z zapisaniem na następujące listy: %s. Ups! Napisz na bofh@hackerspace.pl, spróbujemy to naprawić." % ", ".join(failed), "error") + else: + flash(u"Podano nieprawidłowy adres email.", "error") + else: + flash(u"Nie podano adresu email.", "error") + + return render_template('main.html', entries=pull_feed_entries()) @app.route('/about') def about(): return render_template('about.html') +@app.before_request +def csrf_protect(): + if request.method == "POST": + token = session.pop('_csrf_token', None) + if not token or token != request.form.get('_csrf_token'): + abort(403) + generate_csrf_token() + +def generate_csrf_token(): + if '_csrf_token' not in session: + session['_csrf_token'] = "".join(random.choice(string.letters) for _ in range(32)) + return session['_csrf_token'] + +app.jinja_env.globals['csrf_token'] = generate_csrf_token + if __name__ == '__main__': - app.run('0.0.0.0', 8080, debug=True) + app.run('0.0.0.0', 8080, debug=True) \ No newline at end of file diff --git a/static/main.css b/static/main.css index d85cbef..9450021 100644 --- a/static/main.css +++ b/static/main.css @@ -81,7 +81,7 @@ span.author { #left { border-right: 2px groove #fff; - margin-right: 250px; + margin-right: 270px; } #right { @@ -90,6 +90,8 @@ span.author { top: 100px; padding-top: 16px; right: 0; + margin-right: 20px; + font-size: 16px; } .clear { @@ -148,3 +150,66 @@ span.author { font-size: 1.2em; } } + +#right h4 { + margin-bottom: 0px; + margin-left: 0px; + margin-top: 2px; +} + +h1.mail { + margin-bottom: 2px !important; +} + +#right .email-entry { + width: 90%; + border: 1px groove #777; + padding: 2px; + background: none repeat scroll 0% 0% rgb(250, 250, 250); + color: rgb(34, 34, 34); + margin: 10px; + font-size: 14px; +} + +#right .email-submit { + padding: 4px 14px 4px 14px; +} + +.mailcheck { + margin-right: 8px; + position: relative; + top: 2px; +} + +div.mail-desc { + font-size: 14px; + padding-left: 26px; + line-height: 15px; +} + +div.flashes { + padding: 0; + margin-left: auto; + margin-right: auto; + width: 80%; + margin-bottom: 10px; +} + +div.flashes ul { + list-style-type: none; + padding: 0 0 5px 0; +} + +div.flashes li { + width: 90%; + border-radius: 4px; + background-color: #308033; + padding: 5px; + margin-top: 5px; + margin-left: auto; + margin-right: auto; +} + +div.flashes li.error { + background-color: #a01023; +} \ No newline at end of file diff --git a/templates/basic.html b/templates/basic.html index 21582cc..6a0d4d8 100644 --- a/templates/basic.html +++ b/templates/basic.html @@ -33,6 +33,17 @@ {% block header %} {% include 'header.html' %} {% endblock %} + {% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} +
+ +
+ {% endif %} + {% endwith %}
{% block main %}
diff --git a/templates/main.html b/templates/main.html index bf8d38b..99a6d3c 100644 --- a/templates/main.html +++ b/templates/main.html @@ -36,7 +36,21 @@