def ajax_add_event(request): #decoding the inputted JSON blob json_data = request.POST['data_as_json'] post_data = json_str_to_dict(json_data) # parsing the input dates and times tz = post_data.get('timezone', None) tz = pytz.timezone(tz) start_date = post_data.get('start_date', None) start_time = post_data.get('start_time', None) end_date = post_data.get('end_date', None) end_time = post_data.get('end_time', None) # error out if we didn't get them if not (start_date and start_time and end_date and end_time): print "didn't get the stuff that we wanted" return HttpResponse(simplejson.dumps({ 'status': 400, 'message': 'couldn\'t find the time parameters'}), status=400) start_date_m, start_date_d, start_date_y = map(int, start_date.split('/')) end_date_m, end_date_d, end_date_y = map(int, end_date.split('/')) start_time_h, start_time_m = map(int, start_time.split(':')) end_time_h, end_time_m = map(int, end_time.split(':')) #start_datetime = datetime.datetime(start_date_y, start_date_m, start_date_d, start_time_h, start_time_m, tzinfo=tz) #end_datetime = datetime.datetime(end_date_y, end_date_m, end_date_d, end_time_h, end_time_m, tzinfo=tz) # NOTICE TO THE WORLD: IF YOU DO THE ABOVE, DST WILL BE BASICALLY IGNORED. BUT IF YOU DO THIS: start_datetime = tz.localize(datetime.datetime(start_date_y, start_date_m, start_date_d, start_time_h, start_time_m)) end_datetime = tz.localize(datetime.datetime(end_date_y, end_date_m, end_date_d, end_time_h, end_time_m)) # THEN IT WORKS AS EXPECTED. # WHY? I DONT KNOW. # SPECIFYING THE TZ AFTER INSTANTIATION HAS DIFFERENT BEHAVIOR THAN SPECIFYING DURING # I'M FED UP WITH THIS WORLD # convert to utc and then naiveify # note: we need to naive-ify or else heroku will aggressively try to convert to another timezone at insertion time start_datetime, end_datetime = map(utils.naiveify_datetime, [start_datetime, end_datetime]) # make the calendaritem c = CalendarItem() c.name = post_data.get('title', '') c.location = post_data.get('location', '') c.info = post_data.get('info', '') c.start_datetime = start_datetime c.end_datetime = end_datetime c.save() our_url = c.get_url_for_cal_item() return_dict = {'our_url': our_url} return HttpResponse(simplejson.dumps(return_dict), mimetype='application/x-javascript')
def ajax_add_event(request): #decoding the inputted JSON blob json_data = request.POST['data_as_json'] #escape newlines because they break the JSON parser json_data = '\\n'.join(json_data.splitlines()) print json_data #DEBUG post_data = simplejson.loads(json_data) post_data = map(lambda d: (d['name'], d['value']), post_data) post_data = dict(post_data) #put the newlines back post_data['info'] = post_data['info'].replace('\\n', '\n') # parsing the input dates and times tz = post_data.get('timezone', None) tz = pytz.timezone(tz) start_date = post_data.get('start_date', None) start_time = post_data.get('start_time', None) end_date = post_data.get('end_date', None) end_time = post_data.get('end_time', None) # error out if we didn't get them if not (start_date and start_time and end_date and end_time): print "didn't get the stuff that we wanted" return HttpResponse(simplejson.dumps({ 'status': 400, 'message': 'couldn\'t find the time parameters'}), status=400) start_date_m, start_date_d, start_date_y = map(int, start_date.split('/')) end_date_m, end_date_d, end_date_y = map(int, end_date.split('/')) start_time_h, start_time_m = map(int, start_time.split(':')) end_time_h, end_time_m = map(int, end_time.split(':')) start_datetime = datetime.datetime(start_date_y, start_date_m, start_date_d, start_time_h, start_time_m, tzinfo=tz) end_datetime = datetime.datetime(end_date_y, end_date_m, end_date_d, end_time_h, end_time_m, tzinfo=tz) # convert to utc and then naiveify # note: we need to naive-ify or else heroku will aggressively try to convert to another timezone at insertion time start_datetime, end_datetime = map(lambda dt: dt.astimezone(pytz.utc).replace(tzinfo=None), [start_datetime, end_datetime]) # make the calendaritem c = CalendarItem() c.name = post_data.get('title', '') c.location = post_data.get('location', '') c.info = post_data.get('info', '') c.start_datetime = start_datetime c.end_datetime = end_datetime tries_left = 3 while tries_left >= 0: tries_left-=1 c.slug = generate_hash(c.id) try: c.save() break except IntegrityError: print "slug collision. re-rolling..." continue except: print "trouble putting the calendar item in the database" return HttpResponse(simplejson.dumps({ 'status': 400, 'message': 'couldn\'t put calendar item in the database'}), status=400) #if we tried X times and didn't get a slug that we could use... if tries_left <0: print "rolled X times and couldn't get a good slug" return HttpResponse(simplejson.dumps({ 'status': 400, 'message': 'couldn\'t get a good slug after X rolls'}), status=400) #gcal_url = google_url_from_calendaritem_dict(c.__dict__) our_url = current_site_url() + '/' + c.slug return_dict = {'our_url': our_url} return HttpResponse(simplejson.dumps(return_dict), mimetype='application/x-javascript')