initial commit
This commit is contained in:
54
app/views.py
Normal file
54
app/views.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from app import app
|
||||
from datetime import datetime, timedelta
|
||||
import os
|
||||
import caldav
|
||||
import datetime
|
||||
from icalendar import cal, Event
|
||||
import requests
|
||||
url = os.getenv('caldav_url')
|
||||
username = os.getenv('username')
|
||||
password = os.getenv('password')
|
||||
cal_id = os.getenv('cal_id')
|
||||
|
||||
|
||||
def daterange(start_date, end_date):
|
||||
for n in range(int((end_date - start_date).days)):
|
||||
yield datetime.datetime.date(start_date + timedelta(n))
|
||||
|
||||
@app.route('/')
|
||||
def hello_world():
|
||||
date_obj = datetime.datetime.now()
|
||||
|
||||
start_of_week = date_obj - timedelta(days=date_obj.weekday()) # Monday
|
||||
end_of_week = start_of_week + timedelta(days=7) # Sunday
|
||||
|
||||
with caldav.DAVClient(url=url, username=username, password=password) as client:
|
||||
my_principal = client.principal()
|
||||
|
||||
calendars = my_principal.calendars()
|
||||
calendar = my_principal.calendar(cal_id=cal_id)
|
||||
events_fetched = calendar.date_search(
|
||||
start=start_of_week, end=end_of_week, expand=False)
|
||||
|
||||
events_fetched_by_date = {}
|
||||
for event in events_fetched:
|
||||
value = event.vobject_instance.vevent.dtstart.value
|
||||
if isinstance(value, datetime.datetime):
|
||||
events_fetched_by_date[datetime.datetime.date(value)] = event
|
||||
elif isinstance(value, datetime.date):
|
||||
events_fetched_by_date[value] = event
|
||||
else:
|
||||
raise Exception
|
||||
|
||||
days = []
|
||||
for single_date in daterange(start_of_week, end_of_week):
|
||||
event = events_fetched_by_date.get(single_date)
|
||||
if event:
|
||||
days.append((single_date, event.vobject_instance.vevent))
|
||||
else:
|
||||
days.append((single_date,[]))
|
||||
|
||||
# breakpoint()
|
||||
pass
|
||||
# r = "<br>".join([event.vobject_instance.vevent.summary.value for event in events_fetched if event.vobject_instance.vevent.dtstart.value < datetime.now()])
|
||||
return render_template("dashboard.html", days=days, today=datetime.datetime.now())
|
||||
Reference in New Issue
Block a user