def test_get_occurrence_raises(self): with self.assertRaises(Http404): get_occurrence(self.event.pk, year=2007, month=1, day=5, hour=8, minute=0, second=0, tzinfo=pytz.utc)
def test_get_occurrence_persisted(self): date = timezone.make_aware(datetime.datetime(year=2008, month=1, day=5, hour=8, minute=0, second=0), pytz.utc) occurrence = self.event.get_occurrence(date) occurrence.save() with self.assertRaises(Http404): get_occurrence(self.event.pk, occurrence_id=100) event, persisted_occ = get_occurrence(self.event.pk, occurrence_id=occurrence.pk) self.assertEqual(persisted_occ, occurrence)
def tour_booker(request): if request.is_ajax(): event_id = request.GET.get('eventID') booking_date = decimal.Decimal(request.GET.get('bookingDate')) hour = int(request.GET.get('hour')) tour_date = datetime.datetime.fromtimestamp( float(booking_date) / 1000.0) date = pytz.utc.localize( datetime.datetime(tour_date.year, tour_date.month, tour_date.day, hour, tour_date.minute, tour_date.second)) try: new_occ = Occurrence.objects.get(event=event_id, start=date) data = {"url": new_occ.get_absolute_url()} return JsonResponse(data) except: event, occurrence = get_occurrence(event_id, year=tour_date.year, month=tour_date.month, day=tour_date.day, hour=hour, minute=tour_date.minute, second=tour_date.second, tzinfo=pytz.utc) occurrence.save() data = {"url": occurrence.get_absolute_url()} return JsonResponse(data)
def calendar_event_creator(request): if request.is_ajax(): new_date = request.GET.get('newDate') new_date_array = new_date.split("/") event_id = int(new_date_array[3]) year = int(new_date_array[4]) month = int(new_date_array[5]) day = int(new_date_array[6]) hour = int(new_date_array[7]) minute = int(new_date_array[8]) second = int(new_date_array[9]) date = pytz.utc.localize( datetime.datetime(year, month, day, hour, minute, second)) try: new_occ = Occurrence.objects.get(event=event_id, start=date) data = {"url": new_occ.get_absolute_url()} print(new_occ) return JsonResponse(data) except: event, occurrence = get_occurrence(event_id, year=year, month=month, day=day, hour=hour, minute=minute, second=second, tzinfo=pytz.utc) occurrence.save() print(occurrence) data = {"url": occurrence.get_absolute_url()} return JsonResponse(data)
def test_get_occurrence(self): event, occurrence = get_occurrence(self.event.pk, year=2008, month=1, day=5, hour=8, minute=0, second=0, tzinfo=pytz.utc) self.assertEqual(event, self.event) self.assertEqual(occurrence.start, self.event.start) self.assertEqual(occurrence.end, self.event.end)
def test_get_occurrence_raises_wrong_tz(self): # Montevideo is 3 hours behind UTC with self.assertRaises(Http404): event, occurrence = get_occurrence(self.event.pk, year=2008, month=1, day=5, hour=8, minute=0, second=0)
def calendar_update_event_dates(request): id = request.POST.get('id', None) start = request.POST.get('start', None) end = request.POST.get('end', None) id = request.REQUEST.get('id', None) try: all_day = JSONDecoder().decode(request.POST.get('allDay', 'null')) except ValueError: all_day = None start = datetime.datetime.fromtimestamp(float(start) / 1000) end = datetime.datetime.fromtimestamp(float(end) / 1000) if id: kwargs = decode_occurrence(id) event_id = kwargs.pop('event_id') instance_event, occurrence = get_occurrence(event_id, **kwargs) calendars = get_user_calendars(request.user, ['manager', 'creator']) if instance_event.calendar.pk not in [ c.calendar_id for c in calendars ]: return HttpResponse("error", mimetype="text/javascript", status=403) if all_day: ostart = start.strftime('%Y-%m-%d 00:00:00') oend = end.strftime('%Y-%m-%d 23:59:59') else: ostart = start.strftime('%Y-%m-%d %H:%M:%S') oend = end.strftime('%Y-%m-%d %H:%M:%S') if occurrence.pk: occurrence.start = ostart occurrence.end = oend occurrence.save() else: occurrence.title = instance_event.title occurrence.description = instance_event.description occurrence.start = ostart occurrence.end = oend occurrence.save() ied = EventDetails.objects.get_eventdetails_for_object( instance_event) EventDetails.objects.create_details( occurrence, location=ied.location, free_busy=ied.free_busy, privacy=ied.privacy, bgcolor=ied.bgcolor, category=ied.category, ) return HttpResponse("success", mimetype="text/javascript") else: return HttpResponse("error", mimetype="text/javascript", status=400)
def rsvp_by_iso_date(request, event_id, iso_date): """ Make an rsvp for an occurrence that hasn't yet been persisted. """ t = datetime.datetime.strptime(iso_date, "%Y-%m-%dT%H:%M:%S") event, occurrence = get_occurrence(event_id, year=t.year, month=t.month, day=t.day, hour=t.hour, minute=t.minute, second=t.second) occurrence.save() return rsvp(request, occurrence.pk)
def ajax_move_or_resize_by_code(request): id = request.REQUEST.get('id') kwargs = decode_occurrence(id) event_id = kwargs.pop('event_id') event, occurrence = get_occurrence(event_id, **kwargs) dayDelta = int(request.REQUEST.get('dayDelta')) minuteDelta = int(request.REQUEST.get('minuteDelta')) dt = timedelta(days=dayDelta, minutes=minuteDelta) resize = bool(request.REQUEST.get('resize', False)) resp = {} if occurrence.event.rule: if occurrence.id: #Direct move/resize occurrence new_start = occurrence.start if not resize: new_start += dt occurrence.move(new_start, occurrence.end + dt) resp['status'] = "OK" else: #Either move/resize event or occurrences. Need to ask to the user if 'target' in request.REQUEST: target = request.REQUEST.get('target') if target == 'this': new_start = occurrence.start if not resize: new_start += dt occurrence.move(new_start, occurrence.end + dt) resp['status'] = "OK" elif target == 'all': if not resize: event.start += dt event.end = event.end + dt event.save() resp['status'] = "OK" else: resp['status'] = "FUZZY" resp[ 'move_or_resize_url'] = "%s?id=%s&dayDelta=%s&minuteDelta=%s" % ( reverse("ajax_move_or_resize"), id, dayDelta, minuteDelta) if resize: resp['move_or_resize_url'] += "&resize=1" else: #Direct move/resize event if not resize: event.start += dt event.end = event.end + dt event.save() resp['status'] = "OK" return HttpResponse(simplejson.dumps(resp))
def calendar_update_event_dates(request): id = request.POST.get('id', None) start = request.POST.get('start', None) end = request.POST.get('end', None) id = request.REQUEST.get('id', None) try: all_day = JSONDecoder().decode(request.POST.get('allDay', 'null')) except ValueError: all_day = None start = datetime.datetime.fromtimestamp(float(start) / 1000) end = datetime.datetime.fromtimestamp(float(end) / 1000) if id: kwargs = decode_occurrence(id) event_id = kwargs.pop('event_id') instance_event, occurrence = get_occurrence(event_id, **kwargs) calendars = get_user_calendars(request.user, ['manager', 'creator']) if instance_event.calendar.pk not in [c.calendar_id for c in calendars]: return HttpResponse("error", mimetype="text/javascript", status=403) if all_day: ostart = start.strftime('%Y-%m-%d 00:00:00') oend = end.strftime('%Y-%m-%d 23:59:59') else: ostart = start.strftime('%Y-%m-%d %H:%M:%S') oend = end.strftime('%Y-%m-%d %H:%M:%S') if occurrence.pk: occurrence.start = ostart occurrence.end = oend occurrence.save() else: occurrence.title = instance_event.title occurrence.description = instance_event.description occurrence.start = ostart occurrence.end = oend occurrence.save() ied = EventDetails.objects.get_eventdetails_for_object( instance_event) EventDetails.objects.create_details( occurrence, location=ied.location, free_busy=ied.free_busy, privacy=ied.privacy, bgcolor=ied.bgcolor, category=ied.category, ) return HttpResponse("success", mimetype="text/javascript") else: return HttpResponse("error", mimetype="text/javascript", status=400)
def signup_by_iso_date(request, event_id, iso_date): """ Make a signup for an occurrence that hasn't yet been persisted. """ t = datetime.datetime.strptime(iso_date, "%Y-%m-%dT%H:%M:%S") event, occurrence = get_occurrence(event_id, year=t.year, month=t.month, day=t.day, hour=t.hour, minute=t.minute, second=t.second) occurrence.save() return signup(request, occurrence.pk)
def edit_calendar_event(request): id = request.REQUEST.get('id', None) if id: kwargs = decode_occurrence(id) event_id = kwargs.pop('event_id') instance_event, occurrence = get_occurrence(event_id, **kwargs) else: return HttpResponseRedirect('/') if occurrence.pk: return edit_calendar_occurrence(request, event_id, instance_event, occurrence) else: if instance_event.rule is None: return edit_calendar_simple_event(request, event_id, instance_event, occurrence) else: return edit_calendar_reccurrent_event(request, event_id, instance_event, occurrence)