def save(self, year, user, playa_event_id): if(playa_event_id is not None): existing_event = True logging.debug("existing_event = True") playa_event = self.instance else: existing_event = False logging.debug("existing_event = False") playa_event = PlayaEvent() data = self.cleaned_data playa_event.year=self.year playa_event.creator=user playa_event.title = data['title'] playa_event.slug = slugify(data['title']) playa_event.description = data['description'].strip() playa_event.print_description = data['print_description'].strip() playa_event.event_type = data['event_type'] playa_event.url=data['url'] playa_event.contact_email=data['contact_email'] playa_event.hosted_by_camp=data['hosted_by_camp'] playa_event.located_at_art = data['located_at_art'] playa_event.other_location=data['other_location'] playa_event.check_location=data['check_location'] playa_event.all_day = data['all_day'] playa_event.list_online=data['list_online'] playa_event.list_contact_online=data['list_contact_online'] playa_event.list_contact_online=data['list_contact_online'] playa_event.speaker_series=data['speaker_series'] playa_event.password=data['password'] playa_event.password_hint=data['password_hint'] playa_event.save() if existing_event: # delete the existing occurrences, they will be replaced for occurrence in Occurrence.objects.filter(event=self.instance).all(): occurrence.delete() # add occurrences if data['repeats']: if(data['all_day']): start_time = datetime.strptime("1/1/01 00:00", "%d/%m/%y %H:%M").time() end_time = datetime.strptime("1/1/01 23:59", "%d/%m/%y %H:%M").time() else: start_time = data['start_time'].time() end_time = data['end_time'].time() for day in data['repeat_days'] : event_start = datetime.combine(datetime.strptime(day, "%Y-%m-%d"), start_time) event_end = datetime.combine(datetime.strptime(day, "%Y-%m-%d"), end_time) playa_event.add_occurrences(event_start,event_end) elif(data['all_day']): start_time = datetime.strptime("1/1/01 00:00", "%d/%m/%y %H:%M").time() end_time = datetime.strptime("1/1/01 23:59", "%d/%m/%y %H:%M").time() event_start = datetime.combine(data['start_time'].date(), start_time) event_end = datetime.combine(data['end_time'].date(), end_time) playa_event.add_occurrences(event_start, event_end) else: playa_event.add_occurrences(data['start_time'], data['end_time']) return playa_event
def _create_or_update(self, request, year_year=None, playa_event_id=None): user = request.user if not user.get_profile().api_allowed: return rc_response(request, rc.BAD_REQUEST, 'User not permitted to use the API') method = request.method if method == "PUT": data = request.PUT.copy() elif method == "POST": data = request.POST.copy() else: return rc_response(request, rc.BAD_REQUEST, 'Bad request method: %s' % method) if year_year and 'year' not in data: data['year'] = year_year if not (data and user): log.debug('Bad request: data=%s, user=%s', data, user) return rc_response(request, rc.BAD_REQUEST, 'Missing critical information') log.debug('data for create_or_update: %s', data) if playa_event_id: try: obj = PlayaEvent.objects.get(pk=playa_event_id) log.debug('got playaevent #%i for update', obj.id) except PlayaEvent.DoesNotExist: return rc_response(request, rc.NOT_HERE, 'Event not found #%s' % playa_event_id) # no updating the year! if 'year' in data: del data['year'] else: log.debug('creating new event') obj = PlayaEvent() obj.creator = user if not 'event_type' in data: event_type = EventType.objects.get(pk=1) else: try: event_type = EventType.objects.get(abbr=data['event_type']) except EventType.DoesNotExist: return rc_response(request, rc.NOT_HERE, 'No such EventType: %s' % data['event_type']) obj.event_type = event_type # get rid of illegal-to-update attributes for key in ('id','pk'): if key in data: del data[key] # now loop through the data, updating as needed if 'year' in data: try: year = Year.objects.get(year=data['year']) except Year.DoesNotExist: return rc_response(request, rc.NOT_HERE, 'No such year: %s' % data['year']) obj.year = year # text fields for key in ('print_description', 'url', 'contact_email', 'other_location', 'slug'): if key in data: val = data[key] log.debug('setting %s=%s', key, val) setattr(obj, key, val) # moderation if 'moderation' in data: modkey = data['moderation'].upper() if modkey in ('U','A','R'): log.debug('setting moderation=%s', modkey) obj.moderation = modkey if 'hosted_by_camp' in data: key = data['hosted_by_camp'] try: camp = ThemeCamp.objects.get(pk=key) log.debug('located at camp: %s', camp) obj.hosted_by_camp = camp except ThemeCamp.DoesNotExist: return rc_response(request, rc.NOT_HERE, 'No such camp: %s' % key) if 'located_at_art' in data: key = data['located_at_art'] try: art = ArtInstallation.objects.get(pk=key) log.debug('located at art: %s', art) obj.located_at_art = art except ArtInstallation.DoesNotExist: return rc_response(request, rc.NOT_HERE, 'No such art: %s' % key) # booleans for key in ('check_location', 'all_day', 'list_online', 'list_contact_online'): if key in data: val = data[key].upper() val = val in ('1', 'T', 'Y', 'YES', 'TRUE', 'ON') log.debug('setting %s=%s', key, val) setattr(obj, key, val) obj.save() if method == 'PUT': response = rc.ALL_OK else: response = rc.CREATED response.content = json.dumps({'pk' : obj.id}) return response