2018-12-30 09:24:29 +01:00
|
|
|
from flask import Blueprint, render_template, send_file
|
2018-12-29 18:31:10 +01:00
|
|
|
from icalendar import Calendar, Alarm
|
|
|
|
from flask_wtf import FlaskForm
|
2018-12-30 09:24:29 +01:00
|
|
|
from flask_wtf.file import FileField, FileRequired
|
|
|
|
from wtforms import IntegerField, SubmitField
|
2023-11-07 20:28:19 +01:00
|
|
|
from wtforms.widgets import NumberInput
|
2018-12-29 18:31:10 +01:00
|
|
|
import tempfile
|
|
|
|
|
2018-12-30 09:24:29 +01:00
|
|
|
index = Blueprint('index', __name__, template_folder='templates')
|
2018-12-29 18:31:10 +01:00
|
|
|
|
|
|
|
|
2018-12-30 09:24:29 +01:00
|
|
|
@index.route("/", methods=['GET', 'POST'])
|
2018-12-29 18:31:10 +01:00
|
|
|
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:
|
2023-11-08 23:10:56 +01:00
|
|
|
# try to read ics file
|
2018-12-29 18:31:10 +01:00
|
|
|
calendar = Calendar.from_ical(file.read())
|
|
|
|
except ValueError:
|
|
|
|
error = 'can\'t read file'
|
|
|
|
return render_template('index.html', form=form, error=error)
|
|
|
|
|
2023-11-08 22:50:50 +01:00
|
|
|
# iterate over every event in ics file
|
2018-12-29 18:31:10 +01:00
|
|
|
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
|
|
|
|
|
2023-11-08 22:49:17 +01:00
|
|
|
# add alarm if not already set
|
2018-12-29 18:31:10 +01:00
|
|
|
if not valarm_found:
|
2023-11-08 23:23:53 +01:00
|
|
|
# add alarms with given times
|
2018-12-29 18:31:10 +01:00
|
|
|
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)
|
2023-11-07 20:28:19 +01:00
|
|
|
new_filename = file.filename.rstrip('.ics') + '_with_reminders' + '.ics'
|
2018-12-29 18:31:10 +01:00
|
|
|
return send_file(new_ics, as_attachment=True,
|
|
|
|
attachment_filename=new_filename)
|
|
|
|
|
|
|
|
|
|
|
|
class IcsForm(FlaskForm):
|
2018-12-30 09:24:29 +01:00
|
|
|
icsfile = FileField('ICS File', validators=[FileRequired()])
|
2018-12-29 18:31:10 +01:00
|
|
|
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')
|