def i_cal_feed(request, token): try: token = ICalToken.objects.get(pk=token) try: student = Student.objects.get(id=token.user.id) occupancy_list = Occupancy.objects.filter( subject___class=student._class) except Student.DoesNotExist: try: teacher = Teacher.objects.get(id=token.user.id) occupancy_list = Occupancy.objects.filter(teacher=teacher) except Teacher.DoesNotExist: return HttpResponse('Invalid token', status=403) calendar = Calendar() for occ in occupancy_list: occ_mod_created = OccupancyModification.objects.get( occupancy=occ, modification_type='INSERT') organizer = Organizer( common_name=f'{occ.teacher.first_name} {occ.teacher.last_name}', email=occ.teacher.email, ) if occ.group_number: attendee_name = f'{occ.subject.name} - Groupe {occ.group_number}' else: attendee_name = f'{occ.subject._class.name}' attendees = [Attendee( common_name=attendee_name, email='', )] e = Event( name=occ.name, begin=occ.start_datetime, duration=occ.duration, created=occ_mod_created.modification_date, location=occ.classroom.name, organizer=organizer, attendees=attendees, ) occ_last_modification = OccupancyModification.objects.filter( occupancy=occ, modification_type='EDIT').order_by('-modification_date') if len(occ_last_modification) > 0: e.last_modified = occ_last_modification[0].modification_date calendar.events.add(e) response = HttpResponse(str(calendar), content_type='text/calendar') response['Content-Disposition'] = 'attachment; filename="calendar.ics"' return response except ICalToken.DoesNotExist: return HttpResponse('Token does not exist', status=403)
def calendar_view(request, whatever): from ics import Calendar, Event actions, ctx = _get_actions(request, include_future=True, include_past=30) thecal = Calendar() thecal.creator = 'XR Mass Events' for action in actions: evt = Event() evt.uid = '{}@{}'.format(action.id, request.get_host()) evt.name = action.html_title evt.description = action.description evt.categories = action.tags.names() evt.last_modified = action.modified evt.url = request.build_absolute_uri(action.get_absolute_url()) evt.begin = action.when evt.duration = timedelta(hours=1) # evt.end = action.when + timedelta(hours=1) evt.location = action.location thecal.events.add(evt) response = HttpResponse(thecal, content_type='text/calendar') return response
for race in races: event = Event() today = datetime.datetime.now() gp, circuit, coords, name = race.split(',') if gp != 'JAPAN': dates = get_dates(gp) event.name = f'{name} Grand Prix' event.begin = dates['R'] # event.end = '' event.location = circuit event.geo = [float(i) for i in coords.split()] event.transparent = False event.created = today event.last_modified = today event.extra.extend([ContentLine(name='SEQUENCE', value='0')]) else: event.name = f'{name} Grand Prix' event.begin = '2020-10-11 00:00:00' # event.end = '' event.location = circuit event.geo = [float(i) for i in coords.split()] event.transparent = False event.created = today event.last_modified = today event.extra.extend([ContentLine(name='SEQUENCE', value='0')]) calendar.events.add(event) print(f'Added {event.name !r} to calendar')