def add_source(self, request): id = request.POST.get('id', None) source = request.POST.get('source', None) try: if source is None: raise CalendarOpeningException("Geen bron gevonden") cal_io = default_calendar_io() calendarsource = cal_io.parse_file(source) request.session["source" + str(id)] = calendarsource self.filter_source(request) return HttpResponse("Ok") except CalendarOpeningException: request.session["source" + str(id)] = None return HttpResponse("Ok")
def test1(): d = default_calendar_io() a = d.parse_file("res/1.ics") b = d.parse_file("res/b.ics") c = d.parse_file("res/3-B-R.ics") print() # timeit(a) d.parse_file("/data/web/cal_tool/calendar/tests/res/1.ics") print(d.parse_file("https://alessiotona.duckdns.org/")) print( d.test( "https://uhcal-api.brentchesny.com/calendars/914103e0-2add-11e8-9ccb-e7919f03e688.ics", 'rb').read()) open( "https://uhcal-api.brentchesny.com/calendars/914103e0-2add-11e8-9ccb-e7919f03e688.ics" )
def save_to_database(self, p_cal_id): """ Saving NEW object to database, ONLY FOR NEW OBJECTS For existing objects use .save() """ temp_len = len(ICS_TMP_STORE) a = self.__source_location[:temp_len] if self.__source_location[:len(ICS_SAVED_STORE)] == ICS_SAVED_STORE: temp_len = len(ICS_SAVED_STORE) # Save unfiltered source filename = self.__source_location[temp_len:] shutil.move(self.__source_location, ICS_SAVED_STORE + filename) self.__source_location = ICS_SAVED_STORE + filename # Save filtered source self.apply_filters() io = default_calendar_io() self.__filtered_source_location = CalendarSources.generate_new_file_name( ICS_SAVED_STORE) io.write_to_file(self, self.__filtered_source_location) # Translate type if self.__source_type == CalendarSources.FILE: type = "File" else: type = "URL" if self.__src_id is None: self.__src_id = CalendarSources.generate_new_id() db_cal_source = CalendarSources( cal_source_id=self.__src_id, cal_id=p_cal_id, cal_source_alias=self.__source_name, source_location=self.__source_location, filtered_source_location=self.__filtered_source_location, type=type, url=self.__source_url) db_cal_source.save() for filter in self.__filter_list: filter.save_to_database(self.__src_id)
def load_from_database(self, p_cal_source_id, p_refilter=False): db_cal = CalendarSources.objects.get(cal_source_id=p_cal_source_id) self.__src_id = p_cal_source_id self.__cal_source_id = db_cal.cal_source_id self.__source_name = db_cal.cal_source_alias if db_cal.type == CalendarSources.FILE: self.__source_type = CalendarSources.FILE else: self.__source_type = CalendarSources.URL self.__source_location = db_cal.source_location self.__filtered_source_location = db_cal.filtered_source_location self.__source_url = db_cal.url io = default_calendar_io() # If calendar has to be refilterd, new filters will be applied, else take filtered location if p_refilter: temp_cal_src = io.parse_file(self.__source_location) temp_cal_src.set_filter_list(self.__filter_list) temp_cal_src.apply_filters() else: temp_cal_src = io.parse_file(self.__filtered_source_location) self.__cal_attributes = temp_cal_src.__cal_attributes self.__event_list = temp_cal_src.__event_list db_filters = Filters.get_filters(db_cal.cal_source_id) basefilter = base_filter() for db_filter in db_filters: id = db_filter.filter_id if basefilter.load_base_filter(id): filter = Filter() else: filter = FilterUHasselt() filter.load_from_database(db_filter.filter_id) self.__filter_list.append(filter)
class event_changes: __event_change=default_event_change_strategy() __notifications=NOTIFICATION_APP __calendar_io = default_calendar_io() def send_event_changed_notification(self, p_added, p_changed, p_removed, p_calendar): """ Constructs notification based on changes from lists :param p_added: :param p_changed: :param p_removed: :param p_calendar: calendar-object """ added_events= p_added changed_events= p_changed deleted_events= p_removed notification=self.__get_owner_info(p_calendar.get_owner()) notification['message'] = self.__make_notifications(p_calendar, deleted_events, changed_events, added_events) if notification['message'] is not "": self.__notifications.send_notification(notification) def __get_sources_from_url(self, p_calendar, p_location): calendarID = p_calendar.get_id() urls=CalendarSources.objects.filter(cal_id=calendarID).values() calendar_sources=[] for url in urls: calendar_sources.append(self.__calendar_io.parse_file(url[p_location])) return calendar_sources def __get_owner_info(self, p_owner): #TODO: laad info van de eigenaar van de calendar info = {} #info['token']="xoxp-332875611927-332219201906-340940693570-adde7abc96db270e6773584933196128" #info['channel']="CAC1E3MKQ" info['token']=p_owner.slacktoken info['channel']=p_owner.slackchannel if hasattr(p_owner, 'mail') and p_owner.mail is not None: info['email']=p_owner.mail return info def __make_notifications(self, p_calendar, p_deleted_events, p_changed_events, p_added_events): if len(p_deleted_events) == 0 and len(p_changed_events) == 0 and len(p_added_events) == 0: return "" notifications_message="In calendar "+p_calendar.get_calendar_name()+", there are some new changes\n\n" if len(p_deleted_events) > 0: notifications_message+="Events that are deleted from your calendar:\n" notifications_message+=self.__events_to_string(p_deleted_events) if len(p_changed_events) > 0: notifications_message += "\nEvents that are changed from your calendar:\n" notifications_message += self.__changed_events_to_string(p_changed_events) if len(p_added_events) > 0: notifications_message+="\nEvents that are added to your calendar:\n" notifications_message += self.__events_to_string(p_added_events) return notifications_message def __changed_events_to_string(self, p_changes): event_string = "" for changed_event in p_changes: event = changed_event.get_old() event_string += "\tThe event: " if 'SUMMARY' in event and event['SUMMARY'] is not "": event_string += event['SUMMARY'] event_string += '\n' if 'DESCRIPTION' in event and event['DESCRIPTION'].to_ical().decode("unicode-escape") != "": event_string += '\twith description: ' event_string += event['DESCRIPTION'] event_string += '\n' event_string += "\thas the next attributes added, changed or deleted: \n" attributes = changed_event.get_attributes() for attribute, value in attributes.items(): if attribute not in filter_attributes_uhasselt: continue elif value == AttributeStatus.CHANGED: event_string += "\tThe attribute "+filter_attributes_uhasselt[attribute]+" has changed it's value from: " event_string += self.__value_to_string(attribute, changed_event.get_old()[str(attribute)]) event_string += " to: " event_string += self.__value_to_string(attribute, changed_event.get_old()[str(attribute)]) event_string += '\n' elif value == AttributeStatus.ADDED: event_string += "\t" + str(filter_attributes_uhasselt[str(attribute)]) event_string += " has been added with value: " event_string += self.__value_to_string(attribute, changed_event.get_old()[str(attribute)]) event_string += '\n' else: event_string += "\t" + str(filter_attributes_uhasselt[str(attribute)]) event_string += " has deleted" event_string += '\n' return event_string def __value_to_string(self, p_attribute, p_value): if str(p_attribute) == "DTSTART" or str(p_attribute) == "DTEND": return p_value.dt.strftime("%Y-%m-%d %H:%M") else: return str(p_value.to_ical().decode("unicode-escape")).replace("\\\\", "\\").replace("\,", ", ") @staticmethod def __interpret_attribute_status(p_attribute): if p_attribute == AttributeStatus.ADDED: return "toegevoegd" elif p_attribute == AttributeStatus.CHANGED: return "veranderd" else: return "verwijderd" def __events_to_string(self, p_event_list): event_string="" for event in p_event_list: event_string+="\tThe event: " if 'SUMMARY' in event and event['SUMMARY'] is not "": event_string+=event['SUMMARY'] event_string+='\n' if 'DESCRIPTION' in event and event['DESCRIPTION'] is not "": event_string+='\twith description: ' event_string += event['DESCRIPTION'] event_string += '\n' if 'DTSTART' in event and 'DTEND' in event: event_string += '\tfrom ' event_string += event["DTSTART"].dt.strftime("%H.%M %d.%m.%y") event_string += ' to ' event_string += event["DTEND"].dt.strftime("%H.%M %d.%m.%y") event_string += '\n\n' return event_string
def __init__(self): self.__sources_count = 0 self.__save_location = ICS_SAVED_STORE self.__tmp_save_location = ICS_TMP_STORE self.__io = default_calendar_io() self.__request = None
from cal_tool.calendar.calendar_io import default_calendar_io from cal_tool.event_updates.event_matching import event_matching_factory from cal_tool.utilities.fuzzy_string_matching import string_matching_factory d = default_calendar_io() # cal = d.parse_file("test_calendars/reverse_order.ics") cal = d.parse_file("test_calendars/duplicates.ics") e1 = cal.get_event(0) e2 = cal.get_event(1) strict_string_matching = event_matching_factory.get_strategy(event_matching_factory.FUZZY_EVENT_MATCHING) fuzzy_matching = event_matching_factory.get_strategy(event_matching_factory.FUZZY_EVENT_MATCHING, string_matching_factory.MODIFIED_DAMERAU_LEVENSHTEIN) r = fuzzy_matching.match(e1, e2) print(r) # b = d.parse_file("res/b.ics") # c = d.parse_file("res/3-B-R.ics") # e = Event() # e.add('uid', 1) # e.add('dtstart', datetime(2005,4,4,8,0,0,tzinfo=UTC)) # d = e.items() # # for i in e: # print("{0}: {1}".format(i, e[i])) # print()
def test2(): d = default_calendar_io() a = d.parse_file("/data/web/cal_tool/calendar/tests/res/1.ics") d.write_to_file(a, "/data/web/cal_tool/calendar/tests/res/test")