def createEvent(request): """ Creates a new event mapped to calendar (through attendance). If success, returns JSON object with success = true and event ID. Otherwise, return JSON object with success = false @param POST + AJAX request from client @return JSON object (success, eventID) """ if request.is_ajax() and request.method == 'POST': obj = request.POST # Read in event object try: title = obj['title'] location = obj.get('location', '') allday = obj['allDay'] allday = get_boolean(allday) # Get date and adjust timezone offsets user_timezone = pytz.timezone(request.user.get_profile().timezone) start_date = adjustDateStringToTimeZone( user_timezone=user_timezone, date_string=obj['start'], allday=allday) end_date = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=obj['end'], allday=allday) # If end date is before start date (i.e. some sort of error), # default to 60 mins if calendar.timegm(end_date.utctimetuple()) < calendar.timegm( start_date.utctimetuple()): end_date = start_date + timedelta(hours=1) print "A newly created event has defaulted end time to 60 mins" color = obj['color'].lower() notes = obj.get('notes', '') except KeyError: message = {'success': False} print "Error reading values from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get repeating details try: repeating = obj['repeating'] except KeyError: repeating = False if repeating: try: repeatStartDate = adjustDateStringToTimeZone( user_timezone=user_timezone, date_string=obj['repeatStartDate'], allday=allday) repeatEndDateRaw = obj['repeatEndDate'] if repeatEndDateRaw == "0": repeatEndDate = None else: repeatEndDate = adjustDateStringToTimeZone( user_timezone=user_timezone, date_string=obj['repeatEndDate'], allday=allday) except KeyError: message = {'success': False} print "Error reading start and end dates of repeating event from JSON" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Save event if repeating: newEvent = RepeatingEvent(title=title, location=location, allday=allday, repeating=True, start_date=repeatStartDate, end_date=repeatEndDate, repeat_interval=repeating) newEvent.instance_start_time = start_date.time() newEvent.instance_length_in_min = ( calendar.timegm(end_date.utctimetuple()) - calendar.timegm(start_date.utctimetuple())) / 60 else: newEvent = Event(title=title, location=location, allday=allday, start_date=start_date, end_date=end_date) newEvent.save() # Create attendance (map event to calendar) try: userCalendar = request.user.calendar except ObjectDoesNotExist: print "Calendar doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') attendance = Attendance(calendar=userCalendar, event=newEvent, color=color, notes=notes) attendance.save() message = {'success': True, 'eventID': newEvent.id} else: message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json')
def createEvent(request): """ Creates a new event mapped to calendar (through attendance). If success, returns JSON object with success = true and event ID. Otherwise, return JSON object with success = false @param POST + AJAX request from client @return JSON object (success, eventID) """ if request.is_ajax() and request.method == 'POST': obj = request.POST # Read in event object try: title = obj['title'] location = obj.get('location','') allday = obj['allDay'] allday = get_boolean(allday) # Get date and adjust timezone offsets user_timezone = pytz.timezone(request.user.get_profile().timezone) start_date = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=obj['start'], allday=allday) end_date = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=obj['end'], allday=allday) # If end date is before start date (i.e. some sort of error), # default to 60 mins if calendar.timegm(end_date.utctimetuple()) < calendar.timegm(start_date.utctimetuple()): end_date = start_date + timedelta(hours=1) print "A newly created event has defaulted end time to 60 mins" color = obj['color'].lower() notes = obj.get('notes', '') except KeyError: message = {'success': False} print "Error reading values from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get repeating details try: repeating = obj['repeating'] except KeyError: repeating = False if repeating: try: repeatStartDate = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=obj['repeatStartDate'], allday=allday) repeatEndDateRaw = obj['repeatEndDate'] if repeatEndDateRaw == "0": repeatEndDate = None else: repeatEndDate = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=obj['repeatEndDate'], allday=allday) except KeyError: message = {'success': False} print "Error reading start and end dates of repeating event from JSON" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Save event if repeating: newEvent = RepeatingEvent(title=title, location=location, allday=allday, repeating=True, start_date=repeatStartDate, end_date=repeatEndDate, repeat_interval=repeating) newEvent.instance_start_time = start_date.time() newEvent.instance_length_in_min = (calendar.timegm(end_date.utctimetuple()) - calendar.timegm(start_date.utctimetuple()))/60 else: newEvent = Event(title=title, location=location, allday=allday, start_date=start_date, end_date=end_date) newEvent.save() # Create attendance (map event to calendar) try: userCalendar = request.user.calendar except ObjectDoesNotExist: print "Calendar doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') attendance = Attendance(calendar=userCalendar, event=newEvent, color=color, notes=notes) attendance.save() message = {'success': True, 'eventID': newEvent.id} else: message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json')
def splitRepeatingEvents(request): """ Split one repeating event series into two, given the JSON object from request that indicates the old start time (i.e. repeat end date for first event series), the new start time (i.e. repeat start date for the second event series), the new end time (i.e. to calculate event instance length) and all day boolean (to check whether or not the new event series has changed to allday). This function is called when a repeating event (that is not the first instance in the repeating series) is changed such that the user want all future events to be changed as well @param POST + AJAX request from client @return JSON object (success) """ if request.is_ajax() and request.method == 'POST': obj = request.POST # Get event try: eventID = obj['eventID'] except KeyError: message = {'success': False} print "Error reading event id from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') try: newEvent = RepeatingEvent.objects.get(id=eventID) except ObjectDoesNotExist: message = {'success': False} print "Event doesn't exist in database" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get values from request object user_timezone = pytz.timezone(request.user.get_profile().timezone) try: allday = obj['allDay'] allday = get_boolean(allday) oldStart = obj['oldStart'] oldStart = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=oldStart, allday=newEvent.allday) newStart = obj['newStart'] newStart = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=newStart, allday=allday) newEnd = obj['newEnd'] newEnd = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=newEnd, allday=allday) except KeyError: message = {'success': False} print "Error reading event start dates from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get current attendance object # TODO - do this for each user attending event try: userCalendar = request.user.calendar except ObjectDoesNotExist: print "Calendar doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') try: attendance = Attendance.objects.get(calendar=userCalendar, event=newEvent) except ObjectDoesNotExist: print "Attendance doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Create old event if newEvent.repeating == 1: oldRepeatEndDate = oldStart - timedelta(days=1) elif newEvent.repeating == 2: oldRepeatEndDate = oldStart - timedelta(days=7) elif newEvent.repeating == 3: oldRepeatEndDate = oldStart - timedelta(months=1) else: oldRepeatEndDate = oldStart - timedelta(years=1) oldEvent = RepeatingEvent(title=newEvent.title, location=newEvent.location, allday=newEvent.allday, repeating=True, start_date=newEvent.start_date, end_date=oldRepeatEndDate, repeat_interval=newEvent.repeating) oldEvent.instance_start_time = newEvent.instance_start_time oldEvent.instance_length_in_min = newEvent.instance_length_in_min oldEvent.save() # Update new event newEvent.start_date = newStart if not newEvent.allday: newStart.replace(hour=12, minute=0) newEnd.replace(hour=13, minute=0) newEvent.allday = allday newEvent.instance_start_date = newStart.time() newEvent.instance_length_in_min = ( calendar.timegm(newEnd.utctimetuple()) - calendar.timegm(newStart.utctimetuple())) / 60 newEvent.save() # Map user to old event # TODO - do this for each user attending event oldAttendance = Attendance(calendar=userCalendar, event=oldEvent, color=attendance.color, notes=attendance.notes) oldAttendance.save() message = {'success': True} else: message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json')
def splitRepeatingEvents(request): """ Split one repeating event series into two, given the JSON object from request that indicates the old start time (i.e. repeat end date for first event series), the new start time (i.e. repeat start date for the second event series), the new end time (i.e. to calculate event instance length) and all day boolean (to check whether or not the new event series has changed to allday). This function is called when a repeating event (that is not the first instance in the repeating series) is changed such that the user want all future events to be changed as well @param POST + AJAX request from client @return JSON object (success) """ if request.is_ajax() and request.method == 'POST': obj = request.POST # Get event try: eventID = obj['eventID'] except KeyError: message = {'success': False} print "Error reading event id from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') try: newEvent = RepeatingEvent.objects.get(id=eventID) except ObjectDoesNotExist: message = {'success': False} print "Event doesn't exist in database" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get values from request object user_timezone = pytz.timezone(request.user.get_profile().timezone) try: allday = obj['allDay'] allday = get_boolean(allday) oldStart = obj['oldStart'] oldStart = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=oldStart, allday=newEvent.allday) newStart = obj['newStart'] newStart = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=newStart, allday=allday) newEnd = obj['newEnd'] newEnd = adjustDateStringToTimeZone(user_timezone=user_timezone, date_string=newEnd, allday=allday) except KeyError: message = {'success': False} print "Error reading event start dates from event json" return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Get current attendance object # TODO - do this for each user attending event try: userCalendar = request.user.calendar except ObjectDoesNotExist: print "Calendar doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') try: attendance = Attendance.objects.get(calendar=userCalendar, event=newEvent) except ObjectDoesNotExist: print "Attendance doesn't exist" message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json') # Create old event if newEvent.repeating == 1: oldRepeatEndDate = oldStart - timedelta(days=1) elif newEvent.repeating == 2: oldRepeatEndDate = oldStart - timedelta(days=7) elif newEvent.repeating == 3: oldRepeatEndDate = oldStart - timedelta(months=1) else: oldRepeatEndDate = oldStart - timedelta(years=1) oldEvent = RepeatingEvent(title=newEvent.title, location=newEvent.location, allday=newEvent.allday, repeating=True, start_date=newEvent.start_date, end_date=oldRepeatEndDate, repeat_interval=newEvent.repeating) oldEvent.instance_start_time = newEvent.instance_start_time oldEvent.instance_length_in_min = newEvent.instance_length_in_min oldEvent.save() # Update new event newEvent.start_date = newStart if not newEvent.allday: newStart.replace(hour=12, minute=0) newEnd.replace(hour=13, minute=0) newEvent.allday = allday newEvent.instance_start_date = newStart.time() newEvent.instance_length_in_min = (calendar.timegm(newEnd.utctimetuple()) - calendar.timegm(newStart.utctimetuple()))/60 newEvent.save() # Map user to old event # TODO - do this for each user attending event oldAttendance = Attendance(calendar=userCalendar, event=oldEvent, color=attendance.color, notes=attendance.notes) oldAttendance.save() message = {'success': True} else: message = {'success': False} return HttpResponse(simplejson.dumps(message), mimetype='application/json')