Python pendulum examples

Example use of pendulum_to_date and pendulum_to_datetime, caldav

 1 for absence in absences_employe:
 2     summary = absence.str_agenda()
 3     description = f"SAGE {summary}"
 4     location = "Congés/Absences"
 5     if absence.is_journee_complete():
 6         dtstart = current_day.set(hour=0, minute=0, second=0)
 7         dtend = current_day.add(days=1).set(
 8             hour=0, minute=0, second=0
 9         )
10         # conversion vers date
11         dtstart = pendulum_to_date(dtstart)
12         dtend = pendulum_to_date(dtend)
13     else:
14         if absence.is_demi_journee_matin():
15             dtstart = current_day.set(hour=8)
16             dtend = current_day.set(hour=12)
17         if absence.is_demi_journee_apres_midi():
18             dtstart = current_day.set(hour=13)
19             dtend = current_day.set(hour=17)
20         # conversion vers datetime
21         dtstart = pendulum_to_datetime(dtstart)
22         dtend = pendulum_to_datetime(dtend)
23         logger.info(f"Absence demi-journée {dtstart=} => {dtend=}")
24
25     try:
26         # Ecriture de l'événement
27         new_event = the_calendar.save_event(
28             dtstart=dtstart,
29             dtend=dtend,
30             summary=summary,
31             description=description,
32             location=location,
33         )
34         logger.info(
35             f"Successfull insertion in calendar {summary} {dtstart=} {dtend=}"
36         )
37     except Exception as error:
38         logger.error(f"{error=}")

Example iterate the days in a month

  1 import datetime
  2 import logging
  3 import os
  4
  5 import caldav
  6 import icalendar
  7 import pendulum
  8 import rich
  9 from caldav.lib import error
 10 from caldav.objects import Principal
 11 from django.conf import settings
 12 from django.core.management.base import BaseCommand
 13 from django.db.models import QuerySet
 14 from django.utils import timezone
 15 from employes.models_absence import Absence
 16 from employes.models_employe import Employe
 17 from employes.models_employe import StatusEmploye
 18 from tools.utils_time import pendulum_to_date
 19 from tools.utils_time import pendulum_to_datetime
 20
 21 TODAY = datetime.date.today()
 22 CURRENT_YEAR = TODAY.year
 23 CURRENT_MONTH = TODAY.month
 24
 25 def update_agenda_absences(
 26     *, year: int = CURRENT_YEAR, month: int = CURRENT_MONTH
 27 ) -> None:
 28     employes = Employe.objects.all()
 29     client = caldav.DAVClient(
 30         url=settings.NEXTCLOUD_JUPYTER_CALDAV_URL,
 31         username=settings.NEXTCLOUD_JUPYTER_USERNAME,
 32         password=password,
 33     )
 34     dav_principal = client.principal()
 35     try:
 36         calendar_name = settings.NEXTCLOUD_JUPYTER_CALENDRIER_ABSENCES
 37         logger.info(f"Searching {calendar_name=} ...")
 38         the_calendar = dav_principal.calendar(name=calendar_name)
 39     except Exception as error:
 40         logger.error(f"{calendar_name=} {error}")
 41
 42     dtstart_search = pendulum.datetime(year, month, 1, tz=settings.TIME_ZONE).start_of(
 43         "day"
 44     )
 45     dtend_search = dtstart_search.end_of("month").end_of("day")
 46     # conversion vers datetime
 47     dtstart_search = pendulum_to_datetime(dtstart_search)
 48     dtend_search = pendulum_to_datetime(dtend_search)
 49     liste_events = the_calendar.date_search(
 50         start=dtstart_search, end=dtend_search, expand=True
 51     )
 52     if len(liste_events) > 0:
 53         for event in liste_events:
 54             event.delete()
 55
 56     for employe in employes:
 57         try:
 58             # on se place le 1er du mois
 59             first_day = pendulum.datetime(year, month, 1, tz=settings.TIME_ZONE)
 60             last_day = first_day.end_of("month")
 61             current_day = first_day.set(hour=0, minute=0, second=0)
 62             while (
 63                 current_day.day <= last_day.day and current_day.month == first_day.month
 64             ):
 65                 absences_employe = Absence.objects_conges.filter(
 66                     employe=employe,
 67                     date_absence__year=year,
 68                     date_absence__month=month,
 69                     date_absence__day=current_day.day,
 70                 )
 71                 if absences_employe.exists():
 72                     for absence in absences_employe:
 73                         summary = absence.str_agenda()
 74                         description = f"SAGE {summary}"
 75                         location = "Congés/Absences"
 76                         if absence.is_journee_complete():
 77                             dtstart = current_day.set(hour=0, minute=0, second=0)
 78                             dtend = current_day.add(days=1).set(
 79                                 hour=0, minute=0, second=0
 80                             )
 81                             # conversion vers date
 82                             dtstart = pendulum_to_date(dtstart)
 83                             dtend = pendulum_to_date(dtend)
 84                         else:
 85                             if absence.is_demi_journee_matin():
 86                                 dtstart = current_day.set(hour=8)
 87                                 dtend = current_day.set(hour=12)
 88                             if absence.is_demi_journee_apres_midi():
 89                                 dtstart = current_day.set(hour=13)
 90                                 dtend = current_day.set(hour=17)
 91                             # conversion vers datetime
 92                             dtstart = pendulum_to_datetime(dtstart)
 93                             dtend = pendulum_to_datetime(dtend)
 94                             logger.info(f"Absence demi-journée {dtstart=} => {dtend=}")
 95
 96                         try:
 97                             # Ecriture de l'événement
 98                             new_event = the_calendar.save_event(
 99                                 dtstart=dtstart,
100                                 dtend=dtend,
101                                 summary=summary,
102                                 description=description,
103                                 location=location,
104                             )
105                             logger.info(
106                                 f"Successfull insertion in calendar {summary} {dtstart=} {dtend=}"
107                             )
108                         except Exception as error:
109                             logger.error(f"{error=}")
110
111                 current_day = current_day.add(days=1)
112
113         except Exception as error:
114             logger.info(f"{error=}")