some cleanup and refactoring

This commit is contained in:
Finn Christiansen 2018-12-30 09:24:29 +01:00
parent 0c4fed2a2b
commit fcd783ef26
7 changed files with 32 additions and 9 deletions

13
app/__init__.py Normal file
View file

@ -0,0 +1,13 @@
from flask import Flask
from flask_wtf.csrf import CSRFProtect
from app.index import index
cp = CSRFProtect()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SECRET_KEY'
app.register_blueprint(index)
cp.init_app(app)
return app

BIN
app/__init__.pyc Normal file

Binary file not shown.

55
app/index.py Normal file
View file

@ -0,0 +1,55 @@
from flask import Blueprint, render_template, send_file
from icalendar import Calendar, Alarm
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileRequired
from wtforms import IntegerField, SubmitField
from wtforms.widgets.html5 import NumberInput
import tempfile
index = Blueprint('index', __name__, template_folder='templates')
@index.route("/", methods=['GET', 'POST'])
def addreminder():
form = IcsForm()
content = render_template('index.html', form=form)
file = form.icsfile.data
if not file:
return content
hours = form.hours.data
minutes = form.minutes.data
try:
calendar = Calendar.from_ical(file.read())
except ValueError:
error = 'can\'t read file'
return render_template('index.html', form=form, error=error)
for component in calendar.walk('VEVENT'):
valarm_found = False
for k, v in component.property_items():
if k == 'BEGIN' and v == 'VALARM':
valarm_found = True
if not valarm_found:
alarm = Alarm()
alarm.add('ACTION', 'DISPLAY')
alarm.add('DESCRIPTION', component.get('SUMMARY'))
alarm.add('TRIGGER;VALUE=DURATION', '-PT%dH%dM' % (hours, minutes))
component.add_component(alarm)
new_ics = tempfile.TemporaryFile()
new_ics.write(calendar.to_ical())
new_ics.seek(0)
new_filename = file.filename.rstrip('.ics')+'_with_reminders'+'.ics'
return send_file(new_ics, as_attachment=True,
attachment_filename=new_filename)
class IcsForm(FlaskForm):
icsfile = FileField('ICS File', validators=[FileRequired()])
hours = IntegerField('Hours', default=0,
widget=NumberInput(step=1, min=0, max=24))
minutes = IntegerField('Minutes', default=0,
widget=NumberInput(step=5, min=0, max=55))
submit = SubmitField('Submit')

21
app/templates/base.html Normal file
View file

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<title>Add reminders to ICS files</title>
<link rel="stylesheet" href="/static/css/style.css">
<script type="text/javascript" src="/static/js/reminder.js"></script>
</head>
<body>
<main>
{% block body %}
{% endblock %}
</main>
<footer>
<a href="https://github.com/Finn10111/reminder">source code on github</a>
</footer>
</body>
</html>

34
app/templates/index.html Normal file
View file

@ -0,0 +1,34 @@
{% extends 'base.html' %}
{% block body %}
<h1>Add reminders to ICS files</h1>
<form method="POST" action="/" enctype=multipart/form-data>
<div class="form">
{{ form.csrf_token }}
<ol>
<li>Select ICS file</li>
<li>Set reminder time</li>
<li>Submit and download your ICS file with reminders added</li>
</ol>
<div>
<label class="file">
{{ form.icsfile }}
<span id="filename" class="file-custom" data-content="Choose file..."></span>
</label>
</div>
<div class="form-group">
{{ form.hours.label }}
{{ form.hours() }}
</div>
<div class="form-group">
{{ form.minutes.label }}
{{ form.minutes() }}
</div>
<div>
{{ form.submit() }}
</div>
{% if error %}
<div class="error">{{ error }}</div>
{% endif %}
</div>
</form>
{% endblock %}