Changement caldav to mycaldav

This commit is contained in:
Ambulance Clerc
2022-06-30 18:08:16 +02:00
parent f86d6a02c9
commit 8f8807fea9
16 changed files with 36 additions and 6 deletions

0
mycaldav/__init__.py Normal file
View File

3
mycaldav/admin.py Normal file
View File

@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
mycaldav/apps.py Normal file
View File

@@ -0,0 +1,6 @@
from django.apps import AppConfig
class CaldavConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'mycaldav'

View File

117
mycaldav/models.py Normal file
View File

@@ -0,0 +1,117 @@
from datetime import datetime, timedelta, time
import datetime as Datetime
from django.db import models
import urllib.request
import ssl
from icalendar import Calendar, Event
import recurring_ical_events
# Create your models here.
class cls_caldav():
url = ""
data = None
items = []
day = []
night = []
def __init__(self, url=""):
self.url = url
def clear_data(self):
self.data = None
self.items = []
self.day = []
self.night = []
#Trie les tableau par odre croissant sur la date de début de l'événement
def sort_array(self):
self.items.sort(key=lambda x: x.dtstamp, reverse=False)
def get_caldav_data(self,periode=1):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
self.clear_data()
o_url = urllib.request.urlopen(self.url, context=ctx)
self.data = o_url.read()
o_url.close()
now = f"{datetime.now().day}.{datetime.now().month}.{datetime.now().year}"
tomorow = datetime.now() + timedelta(days=1)
today = (datetime.now().year,datetime.now().month,datetime.now().day)
events = None
if periode == 1:
events = recurring_ical_events.of(Calendar.from_ical(self.data)).at(today)
elif periode == 2:
events = recurring_ical_events.of(Calendar.from_ical(self.data)).between(today,tomorow)
for event in events:
item = _caldav_item()
item.name = event["SUMMARY"]
if "-" in item.name:
arr = item.name.split("-")
item.key = arr[0]
item.name = arr[1]
if "DESCRIPTION" in event.keys():
item.desc = event["DESCRIPTION"]
if "#" in item.desc:
item.done = True
if '{href=' in item.desc:
temp_str = item.desc.split('{href=')[1]
temp_str = temp_str.split('}')[0]
item.href = temp_str
item.desc = item.desc.replace("{href=" + item.href + "}","")
item.dtstart = event["DTSTART"].dt.strftime("%d.%m.%Y %H:%M")
item.dtstamp = int(event["DTSTART"].dt.strftime("%Y%m%d%H%M"))
print(item.dtstamp)
item.dtend = event["DTEND"].dt.strftime("%d.%m.%Y %H:%M")
item.format_str_date()
self.items.append(item)
start = datetime.strptime(item.dtstart,"%d.%m.%Y %H:%M")
if type(start) is Datetime.date:
start = datetime.combine(start, datetime.min.time())
print("convert Date to datetime")
print(start)
if start < datetime.combine(datetime.today(), time(19,0)):
self.day.append(item)
if start >= datetime.combine(datetime.today(), time(19,0)):
self.night.append(item)
self.sort_array()
return self.data
class _caldav_item():
key = ""
name = ""
desc = ""
dtstart = None
str_start_date = ""
str_start_time = ""
dtend = None
str_end_date = ""
str_end_time = ""
dtstamp = None
done = False
href = ""
def format_str_date(self):
self.str_start_date = datetime.strptime(self.dtstart,"%d.%m.%Y %H:%M").strftime("%d.%m")
self.str_start_time = datetime.strptime(self.dtstart, "%d.%m.%Y %H:%M").strftime("%H:%M")
self.str_end_date = datetime.strptime(self.dtend,"%d.%m.%Y %H:%M").strftime("%d.%m")
self.str_end_time = datetime.strptime(self.dtend, "%d.%m.%Y %H:%M").strftime("%H:%M")

View File

@@ -0,0 +1,21 @@
{% if latest_task_list %}
<table style="font-family: 'Source Sans Pro', sans-serif; font-size: 12pt;">
{% for task in latest_task_list %}
<tr>
<td style="vertical-align: top;" width="150" >
<span style="font-weight: bold;">{{task.key}}</span><br>
{{task.str_start_date}} - {{task.str_end_date}}<br>
</td>&nbsp;&nbsp;&nbsp;&nbsp;
<td style="{% if task.done %}text-decoration:line-through{% endif %}">
<a href="{{task.href}}" target="_blank" style="text-decoration: none;color: black" >{{ task.name }}<br></a>
{{task.desc}}
</td>
</tr>
<tr><td>&nbsp;</td></tr>
{% endfor %}
</table>
{% else %}
<p>Aucune information opérationnelle</p>
{% endif %}

View File

@@ -0,0 +1,21 @@
{% if latest_task_list %}
<table style="font-family: 'Source Sans Pro', sans-serif; font-size: 12pt;">
{% for task in latest_task_list %}
<tr>
<td style="vertical-align: top;" width="150" >
<span style="font-weight: bold;">{{task.key}}</span><br>
{{task.str_start_date}} - {{task.str_end_date}}<br>
</td>&nbsp;&nbsp;&nbsp;&nbsp;
<td style="{% if task.done %}text-decoration:line-through{% endif %}">
<a href="{{task.href}}" target="_blank" style="text-decoration: none;color: black" >{{ task.name }}<br></a>
{{task.desc}}
</td>
</tr>
<tr><td>&nbsp;</td></tr>
{% endfor %}
</table>
{% else %}
<p>Aucune informations véhicule</p>
{% endif %}

View File

@@ -0,0 +1,22 @@
{% if latest_task_list %}
<table style="font-family: 'Source Sans Pro', sans-serif; font-size: 12pt;">
{% for task in latest_task_list %}
<tr>
<td style="vertical-align: top;" width="150" >
<span style="font-weight: bold;">{{task.key}}</span><br>
{{task.str_start_date}} - {{task.str_end_date}}<br>
{{task.str_start_time}} - {{task.str_end_time}}<br>
</td>&nbsp;&nbsp;&nbsp;&nbsp;
<td style="{% if task.done %}text-decoration:line-through{% endif %}">
<a href="{{task.href}}" target="_blank" style="text-decoration: none;color: black" >{{ task.name }}<br></a>
{{task.desc}}
</td>
</tr>
<tr><td>&nbsp;</td></tr>
{% endfor %}
</table>
{% else %}
<p>Aucune info route</p>
{% endif %}

View File

@@ -0,0 +1,18 @@
{% if latest_task_list %}
<table style="font-family: 'Source Sans Pro', sans-serif; font-size: 12pt;">
<td style="font-weight: bold;padding-bottom: 5px; text-align: center;" colspan="2">Jour</td>
{% for task in latest_task_list %}
<tr><td style="font-weight: bold;vertical-align: top;" width="75" >{{task.key}}</td>&nbsp;&nbsp;&nbsp;&nbsp;<td style="{% if task.done %}text-decoration:line-through{% endif %}">{{ task.name }}<br>{{task.desc}}</td></tr>
{% endfor %}
<tr><td>&nbsp;</td></tr>
<tr>
<td style="border-top: 1px dashed black;font-weight: bold;padding: 5px; text-align: center;" colspan="2">Nuit</td>
</tr>
{% for task in night_task_list %}
<tr><td style="font-weight: bold;" width="75" >{{task.key}}</td>&nbsp;&nbsp;&nbsp;&nbsp;<td style="{% if task.done %}text-decoration:line-through{% endif %}">{{ task.name }}<br>{{task.desc}}</td></tr>
{% endfor %}
</table>
{% else %}
<p>Aucune tâche disponible.</p>
{% endif %}

View File

@@ -0,0 +1,22 @@
{% if latest_task_list %}
<table style="font-family: 'Source Sans Pro', sans-serif; font-size: 12pt;">
{% for task in latest_task_list %}
<tr>
<td style="vertical-align: top;padding-bottom: 18px" width="100" >
<span style="font-weight: bold;">{{task.key}}</span><br>
{{task.str_start_date}} - {{task.str_end_date}}<br>
{{task.str_start_time}} - {{task.str_end_time}}<br>
</td>
<td style="{% if task.done %}text-decoration:line-through{% endif %}">
<span style="font-weight: bold;">{{task.name}}</span><br>
{{task.desc}}
</td>
</tr>
<tr><td>&nbsp;</td></tr>
{% endfor %}
</table>
{% else %}
<p>Aucune information véhicule</p>
{% endif %}

3
mycaldav/tests.py Normal file
View File

@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

18
mycaldav/urls.py Normal file
View File

@@ -0,0 +1,18 @@
from django.urls import path
from . import views
app_name = "mycaldav"
urlpatterns = [
path('task', views.view_task_caldav, name='view_task_caldav'),
path('vhc', views.view_vhc_caldav, name='view_vhc_caldav'),
path('rh', views.view_rh_caldav, name='view_rh_caldav'),
path('road', views.view_road_caldav, name='view_road_caldav'),
path('op', views.view_op_caldav, name='view_op_caldav'),
path('dayly', views.view_dayly_caldav, name='view_dayly_caldav'),
]

75
mycaldav/views.py Normal file
View File

@@ -0,0 +1,75 @@
from django.http import HttpResponse
from django.shortcuts import render
from django.views import generic
from django.template import loader
from django.views.decorators.clickjacking import xframe_options_exempt
import caldav as cal
# Create your views here.
from mycaldav.models import *
@xframe_options_exempt
def view_task_caldav(request):
o_caldav = cls_caldav(url="https://sync.infomaniak.com/calendars/AA01593/a4835de4-b3cd-46ca-8f48-3dacd3a2b46e?export")
o_caldav.get_caldav_data()
template = loader.get_template("task/task.html")
context = {'latest_task_list':o_caldav.day, 'night_task_list': o_caldav.night}
return HttpResponse(template.render(context,request))
@xframe_options_exempt
def view_vhc_caldav(request):
o_caldav = cls_caldav(url="https://sync.infomaniak.com/calendars/AA01593/4f7b4591-a4c4-4443-9f78-01df068f58b8?export")
o_caldav.get_caldav_data(periode=2)
template = loader.get_template("vhc/vhc_view.html")
context = {'latest_task_list': o_caldav.items}
return HttpResponse(template.render(context, request))
@xframe_options_exempt
def view_rh_caldav(request):
o_caldav = cls_caldav(url="https://sync.infomaniak.com/calendars/AA01593/c9916d31-c25b-4b0b-8673-81e2164b6f10?export")
o_caldav.get_caldav_data(periode=2)
template = loader.get_template("rh/rh_view.html")
context = {'latest_task_list': o_caldav.items}
return HttpResponse(template.render(context, request))
@xframe_options_exempt
def view_road_caldav(request):
o_caldav = cls_caldav(url="https://sync.infomaniak.com/calendars/AA01593/d608fcce-2f30-42f0-816c-b5524a0672fe?export")
o_caldav.get_caldav_data(periode=2)
template = loader.get_template("road/road_view.html")
context = {'latest_task_list': o_caldav.items}
return HttpResponse(template.render(context, request))
@xframe_options_exempt
def view_op_caldav(request):
o_caldav = cls_caldav(url="https://sync.infomaniak.com/calendars/AA01593/73f3c787-abe1-4cd0-bf2a-c44dbc7015b9?export")
o_caldav.get_caldav_data(periode=1)
template = loader.get_template("road/op_view.html")
context = {'latest_task_list': o_caldav.items}
return HttpResponse(template.render(context, request))
@xframe_options_exempt
def view_dayly_caldav(request):
ret = ""
caldav_url = "https://sync.infomaniak.com"
caldav_user = 'SC01066'
caldav_password = "mc144*1870CLERC"
client = cal.DAVClient(url=caldav_url, username=caldav_user, password=caldav_password)
my_principal = client.principal()
calendars = my_principal.calendars()
if calendars:
## Some calendar servers will include all calendars you have
## access to in this list, and not only the calendars owned by
## this principal.
print("your principal has %i calendars:" % len(calendars))
ret += "your principal has %i calendars:" % len(calendars)
for c in calendars:
print(" Name: %-20s URL: %s" % (c.name, c.url))
ret += " Name: %-20s URL: %s" % (c.name, c.url) + "<br>\n"
else:
print("your principal has no calendars")
return HttpResponse(ret)