def create_ics(sender, instance, **kwargs): c = Calendar() alarm = [DisplayAlarm(trigger=timedelta(minutes=30))] e = Event() e.name = instance.title e.begin = instance.date e.end = instance.dateend e.alarms = alarm if instance.duration != None: e.duration = instance.duration if (instance.cost == 0 or instance.cost == None): cost = 'Бесплатно' e.description = str(instance.intro) + ' Стоимость: ' + str(cost) else: e.description = str(instance.intro) + ' Стоимость: ' + str(instance.cost)+ 'р.' e.location = instance.location if instance.timepad != None: e.url = instance.timepad c.events.add(e) instance.ics.delete(save=False) instance.ics.save(instance.title +'.ics', ContentFile(str(c)), save=True) #Формирование глобального файла со всеми мероприятиями global_ics = EventIndex.objects.all()[0] events = global_ics.get_children() c = Calendar() for event in events: if (event.specific.date < timezone.now()): pass else: e = Event() e.name = event.title e.begin = event.specific.date e.end = event.specific.dateend e.alarms = alarm if event.specific.duration != None: e.duration = event.specific.duration if (event.specific.cost == 0 or event.specific.cost == None): cost = 'Бесплатно' e.description = str(event.specific.intro.strip("<p>*</p>")) + ' Стоимость: ' + str(cost) else: e.description = str(event.specific.intro.strip("<p>*</p>")) + ' Стоимость: ' + str(event.specific.cost)+ 'р.' e.location = event.specific.location if event.specific.timepad != None: e.url = event.specific.timepad c.events.add(e) global_ics.calenadar_file.delete(save=False) global_ics.calenadar_file.save('global.ics', ContentFile(str(c)), save=True)
def ical_content(self): c = Calendar() e = Event() e.alarms = [DisplayAlarm(trigger=self.trigger_datetime)] e.name = self.event_title e.description = self.event_description e.location = self.event_location e.begin = self.start_datetime e.end = self.end_datetime c.events.add(e) ics_str = str(c) ics_str = re.sub(r'DTSTAMP\:(\d+)T(\d+)Z', r'DTSTAMP:\1T\2', ics_str) ics_str = re.sub(r'DTEND\:(\d+)T(\d+)Z', r'DTEND;TZID=%s:\1T\2' % self.timezone, ics_str) ics_str = re.sub(r'DTSTART\:(\d+)T(\d+)Z', r'DTSTART;TZID=%s:\1T\2' % self.timezone, ics_str) return ics_str
e = Event() date_begin_str = f'{init_date} {i["begin"]}+0530' date_end_str = f'{init_date} {i["end"]}+0530' date_begin = datetime.strptime(date_begin_str, "%Y-%m-%d %H%M%z") + timedelta( days.index(i['day'].lower())) date_end = datetime.strptime(date_end_str, "%Y-%m-%d %H%M%z") + timedelta( days.index(i['day'].lower())) e.name = i['name'] e.begin = date_begin.strftime("%Y-%m-%d %H:%M:%S%z") e.end = date_end.strftime("%Y-%m-%d %H:%M:%S%z") e.description = i['desc'] e.alarms = [ DisplayAlarm(trigger=timedelta(minutes=-5)), DisplayAlarm(trigger=date_begin) ] if i['name'] != "Economics B": e.attendees = ['*****@*****.**'] c.events.add(e) print( f'Created {i["name"]} {datetime.strftime(date_begin, "%Y-%m-%d %H:%M:%S")} - {datetime.strftime(date_end, "%Y-%m-%d %H:%M:%S")}' ) with open('out.ics', 'w') as my_file: my_file.writelines(c) print()