def ical_string(events): if not isinstance(events, list) and (events is not None): events = [events] cal = iCal() for e in events: cal.add_component(e) sdata = cal.to_ical() return sdata
def item_export_html(self): try: from icalendar import Calendar as iCal from icalendar import Event as iEvent except ImportError: return self.render_error("Event Error", "Event exporting is not supported in this installation") cal = iCal() collection = self.item if self.cur_agent_can_global('do_anything'): recursive_filter = None else: visible_memberships = self.permission_cache.filter_items('view Membership.item', Membership.objects) recursive_filter = Q(child_memberships__in=visible_memberships.values('pk').query) all_members = collection.all_contained_collection_members(recursive_filter) all_events = Event.objects.filter(pk__in=all_members.values('pk').query) all_events = self.permission_cache.filter_items('view Event.start_date', all_events) all_events = self.permission_cache.filter_items('view Event.start_time', all_events) all_events = self.permission_cache.filter_items('view Event.end_date', all_events) cal.add('prodid', '-//Deme//Deme Calendar Module//EN') cal.add('version', '2.0') cal.add('calscale', 'GREGORIAN') for member in all_events: newEvent = iEvent() newEvent.add('summary', member.display_name()) newEvent.add('dtstart', datetime.combine(member.start_date, member.start_time)) newEvent.add('dtend', datetime.combine(member.end_date, member.end_time)) newEvent.add('location', member.location) newEvent.add('description', member.body) cal.add_component(newEvent) response = HttpResponse(cal.as_string(), mimetype='text/calendar') response['Content-Disposition'] = 'attachment; filename=demeCalendar.ics' return response
def create_appointment(self, message): description = formatted_start_date = formatted_start_time = formatted_end_time = None # extract description from input if present if message.data.get("description") != None: description = message.data.get("description") else: # cancel current event creating because of missing description self.speak( "Creation canceled. No description for the new event was specified." ) return # extract start date from input if present if message.data.get("start_date") != None: formatted_start_date = util.parse.extract_datetime( message.data.get("start_date"), anchorDate=datetime.now()) else: # cancel current event creating because of missing start date self.speak( "Creation canceled. No starting date for the new event was specified." ) return # extract start time from input if present if message.data.get("start_time") != None: formatted_start_time = util.parse.extract_number( message.data.get("start_time")) # extract end time from input if present if message.data.get("end_time") != None: formatted_end_time = util.parse.extract_number( message.data.get("end_time")) # create new iCal event from input wildcards new_event = Event() new_event.add("summary", description) if formatted_start_time == None: # no start time specified, creating all-day event new_event.add("dtstart", formatted_start_date[0].date()) new_event.add("dtend", formatted_start_date[0].date() + timedelta(days=1)) else: new_event.add( "dtstart", formatted_start_date[0] + timedelta(hours=formatted_start_time)) if formatted_end_time == None: # no end time specified, creating 1 hour event new_event.add( "dtend", formatted_start_date[0] + timedelta(hours=formatted_start_time + 1)) else: new_event.add( "dtend", formatted_start_date[0] + timedelta(hours=formatted_end_time)) # create wrapping iCal calendar object iCal_wrapper = iCal() iCal_wrapper.add_component(new_event) # save new event with caldav self.cal.save_event(iCal_wrapper) self.speak("New event successfully created.")