Merge branch 'master' into 'main'
Init See merge request keriks/flask-namedays!1
This commit is contained in:
commit
c94dbe2922
65
app.py
Normal file
65
app.py
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import uuid
|
||||||
|
from collections import defaultdict
|
||||||
|
from io import BytesIO
|
||||||
|
from typing import Iterable, Mapping
|
||||||
|
|
||||||
|
from flask import Flask, request, render_template, send_file
|
||||||
|
from icalendar import Calendar, Event, Alarm
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# Source JSON created from http://vvc.gov.lv/export/sites/default/files/paplasinatais_saraksts.pdf
|
||||||
|
|
||||||
|
|
||||||
|
def generate_ical_for_mapping(cal: Mapping[datetime.date, Iterable[str]]) -> BytesIO:
|
||||||
|
ical = Calendar()
|
||||||
|
ical['VERSION'] = "2.0"
|
||||||
|
ical['PRODID'] = "NameDays"
|
||||||
|
for date, names in sorted(cal.items(), key=lambda x: x[0]):
|
||||||
|
ev = Event()
|
||||||
|
ev.add("SUMMARY", ", ".join(sorted(names)))
|
||||||
|
ev.add("DTSTART", date)
|
||||||
|
ev.add("DTEND", date + datetime.timedelta(days=1))
|
||||||
|
ev.add("DTSTAMP", datetime.datetime(2000, 1, 1))
|
||||||
|
ev.add("RRULE", {"FREQ": "YEARLY"})
|
||||||
|
ev.add("CATEGORY", "Anniversary")
|
||||||
|
ev.add("UID", uuid.uuid4())
|
||||||
|
alert = Alarm()
|
||||||
|
alert.add("action", "DISPLAY")
|
||||||
|
alert.add("TRIGGER", datetime.timedelta(hours=9))
|
||||||
|
alert.add("DESCRIPTION", "Default description")
|
||||||
|
ev.add_component(alert)
|
||||||
|
ical.add_component(ev)
|
||||||
|
return BytesIO(ical.to_ical(True))
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=["POST", "GET"])
|
||||||
|
def calendar():
|
||||||
|
with open("vardadienas.json") as f:
|
||||||
|
vdienas = json.load(f)
|
||||||
|
names_dict = {
|
||||||
|
f"{int(month):02}_{int(day):02}_{name}": name
|
||||||
|
for month, month_data in vdienas.items()
|
||||||
|
for day, day_data in month_data.items()
|
||||||
|
for names in day_data.values()
|
||||||
|
for name in names
|
||||||
|
}
|
||||||
|
if request.method == "POST":
|
||||||
|
cal = defaultdict(list)
|
||||||
|
for selected_name in request.form.getlist("words"):
|
||||||
|
if selected_name in names_dict:
|
||||||
|
month, day, name = selected_name.split("_")
|
||||||
|
date = datetime.date(2000, int(month), int(day))
|
||||||
|
cal[date].append(name)
|
||||||
|
if cal:
|
||||||
|
name = f"{uuid.uuid4().hex}.ics"
|
||||||
|
f = generate_ical_for_mapping(cal)
|
||||||
|
return send_file(f, mimetype="text/calendar", as_attachment=True, download_name=name)
|
||||||
|
words = ((k,v) for k,v in sorted(names_dict.items(), key=lambda x: x[1]))
|
||||||
|
return render_template("namedays.html", words=words)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run("0.0.0.0", 5000, True, False)
|
BIN
paplasinatais_saraksts.pdf
Normal file
BIN
paplasinatais_saraksts.pdf
Normal file
Binary file not shown.
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Flask==2.0.2
|
||||||
|
icalendar==4.0.9
|
43
templates/namedays.html
Normal file
43
templates/namedays.html
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="lv">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
|
||||||
|
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet"/>
|
||||||
|
<title>Namedays!</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-12">
|
||||||
|
<h1>Vārdadienu kalendāra ģenerators</h1>
|
||||||
|
</div>
|
||||||
|
<div class="col-12">
|
||||||
|
{%- if words %}
|
||||||
|
<form method="post">
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="idWordSelect" class="form-label">Atlasi vārdus:</label>
|
||||||
|
<select name="words" class="form-control js-example-basic-multiple" id="idWordSelect" multiple>
|
||||||
|
{%- for key, name in words %}
|
||||||
|
<option value="{{ key }}">{{ name }}</option>
|
||||||
|
{%- endfor -%}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button name="submit" value="submit" class="btn btn-primary">Izveidot</button>
|
||||||
|
</form>
|
||||||
|
{% endif -%}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
|
||||||
|
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js"></script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
|
||||||
|
<script>
|
||||||
|
$(document).ready(function () {
|
||||||
|
$('.js-example-basic-multiple').select2();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
7719
vardadienas.json
Normal file
7719
vardadienas.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user