def post(self): data = request.get_json() rammetid_to_application = RammetidToApplication(data.get('umbrella_organisation').get('uri')) ensure(POST, rammetid_to_application) resource = data.get("resource", None) if not resource: abort(403, __error__=[u'Ressurs er ikke angitt.']) resource = get_resource_for_uri(resource['uri']) if not resource: abort(404, __error__=[u'Ressursen finnes ikke.']) if not resource.repeating_booking_allowed: abort(403, __error__=[u'Gjentakende lån ikke tillatt']) user = get_user(request.cookies) person_uri = '/persons/{}'.format(user['person_id']) person = get_person_for_uri(person_uri) if not person: abort(404, __error__=[u'Personen finnes ikke.']) # get umbrella member orgs from organisations backend umbrella_member_organisations = get_umbrella_organisation_from_web(data.get('umbrella_organisation').get('uri')).get('organisations', []) # get local umbrella organisation object umbrella_organisation = get_umbrella_organisation_for_uri(data.get('umbrella_organisation').get('uri')) text = "Rammetid fordeling" applications = [] organisations = data.get('organisations', None) for organisation in organisations: organisation_data = organisations[organisation] # have to be superuser if the organisation is not public and not copied to booking already organisation = get_organisation_for_uri(organisation_data['uri'], cookies=make_superuser_auth_cookie()) if not organisation: abort(404, __error__=[u'Organisasjonen finnes ikke.']) if not any(org.get('uri') == organisation_data['uri'] for org in umbrella_member_organisations): abort(403, __error__=[u'Organisasjonen hører ikke til paraplyorganisasjonen']) application = Application(person, organisation, text, None, resource) application.status = 'Granted' slots = organisation_data["slots"] for slot_data in slots: slot = self.parse_slot(slot_data, application) slot_request = self.parse_slot_request(slot_data) start_date = slot.start_date end_date = slot.end_date week_day = slot.week_day start_time = slot.start_time end_time = slot.end_time self.validate_start_and_end_times(start_date, end_date, start_time, end_time) if not self.has_matching_rammetid(umbrella_organisation.id, start_date, end_date, week_day, start_time, end_time): abort(400, __error__=[u'Ingen rammetid passer']) if self.is_conflict_slot_time(resource, start_date, end_date, week_day, start_time, end_time): abort(400, __error__=[u'Tiden du har søkt på er ikke tilgjengelig']) if self.is_conflict_blocked_time(resource, start_date, end_date, week_day, start_time, end_time): abort(400, __error__=[u'Tiden du har søkt på er blokkert']) application.request_repeating_slot(slot_request) application.add_repeating_slot(slot) applications.append(application) for application in applications: current_app.db_session.add(application) current_app.db_session.commit() current_app.db_session.refresh(application) return applications, 201
def post(self): data = request.get_json() person = data.get('person', None) if not person or not person.get('uri', None): abort(400) person_uri = person["uri"] text = data["text"] facilitation = data['facilitation'] resource_uri = data["resource"]["uri"] resource = get_resource_for_uri(resource_uri) settings = SettingsResource().get() user = get_user(request.cookies) # Check that the resource allows the type of application if (resource.repeating_booking_allowed and not settings["repeating_booking_allowed"] and not has_role(user, 'flod_saksbehandlere')) or \ (not settings["repeating_booking_allowed"] and not (has_role(user, 'flod_brukere') or has_role(user, 'flod_saksbehandlere'))) or \ not resource.repeating_booking_allowed: abort(403, __error__=[u'Gjentakende lån ikke tillatt']) organisation_data = data.get("organisation", None) if organisation_data: organisation_uri = organisation_data["uri"] organisation = get_organisation_for_uri(organisation_uri) else: organisation = None if not organisation: abort( 403, __error__=[u'Du kan ikke søke gjentakende lån som privatperson'] ) person = get_person_for_uri(person_uri) application = Application( person, organisation, text, facilitation, resource, amenities=data.get('amenities'), accessibility=data.get('accessibility'), equipment=data.get('equipment'), suitability=data.get('suitability'), facilitators=data.get('facilitators') ) slots = None try: slots = [self.parse_slot_request(d) for d in data["slots"]] except BookingDomainException as e: abort(400, __error__=[e.message]) if not slots: abort(400, __error__=[u'Tidspunkt mangler']) for slot in slots: start_date = slot.start_date end_date = slot.end_date week_day = slot.week_day start_time = slot.start_time end_time = slot.end_time self.validate_end_date_of_slot(settings["repeating_booking_enddate"], end_date.isoformat(), u'fast lån') self.validate_start_and_end_times(start_date, end_date, start_time, end_time) if self.is_conflict_slot_time(resource, start_date, end_date, week_day, start_time, end_time) or \ self.is_conflict_rammetid(resource, start_date, end_date, week_day, start_time, end_time): abort( 400, __error__=[u'Tiden du har søkt på er ikke tilgjengelig'] ) if self.is_conflict_blocked_time(resource, start_date, end_date, week_day, start_time, end_time): abort( 400, __error__=[u'Tiden du har søkt på er blokkert'] ) application.request_repeating_slot(slot) current_app.db_session.add(application) current_app.db_session.commit() current_app.db_session.refresh(application) send_email_to_resource(application) return application, 201