def handle_generic_message(message): if str(message.chat.id) in add_feed_session: user = repo.get_user(message.chat.id) urls = get_urls(message.text) if len(urls) > 0: chat_id = str(message.chat.id) add_feed_session[chat_id] = {'link': urls[0]} response, link, detail = repo.add_feed(chat_id, add_feed_session[chat_id]['link'], 'personal') if response: message_subject.on_next( HooNewsMessage(chat_id, 'INFO', hoonewsstrings.get_string( user['language'], 'FEED_ADDED' )) ) else: print(detail) message_subject.on_next( HooNewsMessage(chat_id, 'ERROR', hoonewsstrings.get_string( user['language'], 'FEED_NOT_VALID' )) ) del (add_feed_session[chat_id]) else: message_subject.on_next( HooNewsMessage(str(message.chat.id), 'ERROR', hoonewsstrings.get_string( message.from_user.language_code, 'UNEXPECTED_MESSAGE_URL' )) ) else: message_subject.on_next( HooNewsMessage(message.chat.id, 'ERROR', hoonewsstrings.get_string( message.from_user.language_code, 'UNEXPECTED_MESSAGE' )) )
def show_list_of_countries(message): country_list = repo.get_country_list(message.from_user.language_code) user = repo.get_user(str(message.chat.id)) if country_list: message_subject.on_next( HooNewsMessage(message.chat.id, 'UPDATE_COUNTRY', (hoonewsstrings.get_string(user['language'], 'UPDATE_COUNTRY'), 'UPDATE_COUNTRY', country_list)))
def get_user_from_auth(): if verify_superuser_auth_token(request.cookies.get('auth_token')): return {'is_superuser': True} user = get_user(request.cookies) if user is None: abort(401) return user
def get_article(chat_id, article_id): art = repo.get_article(chat_id, article_id) user = repo.get_user(chat_id) if art: message_subject.on_next(HooNewsMessage(chat_id, 'ITEM', ( art, str(int(article_id) + 1, ), hoonewsstrings.get_string(user['language'], 'NEXT')))) else: message_subject.on_next( HooNewsMessage(chat_id, 'ITEM_END', hoonewsstrings.get_string(user['language'], 'READ_ALL')))
def filter_applications(applications, cookies): """Filter applications based on user roles""" is_superuser = verify_superuser_auth_token( request.cookies.get('auth_token')) if is_superuser: # No filter for superuser return applications user = get_user(request.cookies) if user is None: abort(401) if has_role(user, 'flod_saksbehandlere'): # No filter for flod_saksbehandlere return applications if has_role(user, 'flod_brukere'): # Administratosr should only see applications for resources they own remote_resource_ids = [c['resource_id'] for c in user.get('credentials', []) if c['id'].startswith('CAN_EDIT_FACILITY_')] # Need to map the ids in the credentials which uses remote ids # to the local ones used in booking. uris = ["/facilities/%s" % i for i in remote_resource_ids] res = current_app.db_session.query(Resource).filter(Resource.uri.in_(uris)).all() local_resource_ids = [r.id for r in res] applications = applications.filter( Application.resource_id.in_(local_resource_ids) ) else: # External users should only see their own applications and the applications belonging to their organisations org_ids = [] orgs = get_person_from_web('/persons/%s/organisations/' % user['person_id']) org_uris = [org.get('uri') for org in orgs] if len(org_uris) > 0: res = current_app.db_session.query(Organisation).filter(Organisation.uri.in_(org_uris)).all() org_ids = [o.id for o in res] person_uri = '/persons/{}'.format(user['person_id']) if len(org_ids) == 0: applications = applications.filter( Application.person.has(uri=person_uri) ) else: applications = applications.filter( or_( Application.person.has(uri=person_uri), Application.organisation_id.in_(org_ids) ) ) return applications
def get(self, facility_id): user = repo.get_user(request.cookies) anonymous = user is None facility = self.get_or_abort(facility_id) if anonymous or not repo.is_administrator(user): return marshal(facility, facility_fields) else: return marshal(facility, facility_fields_admin)
def make_search(chat_id, category): user = repo.get_user(chat_id) message_subject.on_next( (HooNewsMessage(chat_id, 'LOADING', hoonewsstrings.get_string(user['language'], 'GENERIC_LOADING')))) message_subject.on_next( HooNewsMessage(chat_id, 'LOADING', hoonewsstrings.get_string(user['language'], 'NEWS_LOADING'))) repo.get_articles(chat_id, category, user['language'], user.get('country')) art = repo.get_article(chat_id, "0") message_subject.on_next( HooNewsMessage(chat_id, 'ITEM', (art, '1', hoonewsstrings.get_string(user['language'], 'NEXT'))))
def GET(self, name): name = name.strip().lower() web.header('Content-Type', 'application/json') if len(name) == 0: logging.info("Listing all users.") users = repo.get_all_users( ) jsonstr = json.dumps(users) return jsonstr else: logging.info("Request to list user '" + name + "'") user = repo.get_user(name) jsonstr = json.dumps(user) return jsonstr
def GET(self, name): name = name.strip().lower() web.header('Content-Type', 'application/json') if len(name) == 0: logging.info("Listing all users.") users = repo.get_all_users() jsonstr = json.dumps(users) return jsonstr else: logging.info("Request to list user '" + name + "'") user = repo.get_user(name) jsonstr = json.dumps(user) return jsonstr
def get_display_name(application): if application.organisation: try: org = get_organisation_from_web(application.organisation.uri) return org.get('name') except werkzeug.exceptions.NotFound: return u'Ad-hoc aktør' user = get_user(request.cookies) show_person = user is not None and has_role(user, 'flod_saksbehandlere') if show_person: person = get_person_from_web(application.person.uri) return '%s, %s' % (person.get('last_name', ''), person.get('first_name', '')) return "Privatperson"
def umbrella_organisation_new(): if not (current_user.is_authenticated() and current_user.is_aktorregister_admin()): abort(401) flod_activity_types = repo.get_flod_activity_types() brreg_activity_codes = repo.get_brreg_activity_codes() try: return render_flod_template( 'umbrella_organisation_detail.html', umbrella_organisation=json.dumps(None), auth=repo.get_user(current_user.user_id, current_user.user_id), brreg_activity_codes=brreg_activity_codes, flod_activity_types=flod_activity_types ) except requests.exceptions.ConnectionError: app.logger.exception('Request failed') return "", 500
def delete(self, application_id): application = self.get_object_by_id(application_id) ensure(DELETE, application) if is_idporten_user(get_user(request.cookies)): if application.status == 'Denied': abort(400, __error__=[u'Kan ikke slette avviste søknader']) if application.status == 'Granted': # check that no slot has started slots = application.slots for slot in slots: start = slot.start_date if hasattr(slot, 'start_date') else slot.start_time.date() if start <= datetime.date.today(): abort(400, __error__=[u'Kan ikke slette godkjente søknader når perioden er påbegynt']) current_app.db_session.delete(application) current_app.db_session.commit() return "", 204
def handle_category_choose(chat_id, category): if chat_id in add_feed_session: user = repo.get_user(chat_id) add_feed_session[chat_id]['category'] = category response, link, detail = repo.add_feed(chat_id, add_feed_session[chat_id]['link'], add_feed_session[chat_id]['category']) if response: message_subject.on_next( HooNewsMessage(chat_id, 'INFO', hoonewsstrings.get_string( user['language'], 'FEED_ADDED' )) ) else: print(detail) message_subject.on_next( HooNewsMessage(chat_id, 'ERROR', hoonewsstrings.get_string( user['language'], 'FEED_NOT_VALID' )) ) del (add_feed_session[chat_id]) else: make_search(chat_id, category)
def map_internal_notes_to_users(notes): for note in notes: note["user"] = repo.get_user(note["auth_id"], note["auth_id"])
def get(self): if 'name' in request.args and request.args['name'] == '': return marshal([], facility_fields) facilities = current_app.db_session.query(Facility).order_by(Facility.name.asc()) max_capacity = None min_capacity = None if 'name' in request.args: name_filter = Facility.name.ilike('%%%s%%' % request.args['name']) short_code_filter = Facility.short_code.ilike( '%s' % request.args['name']) facilities = facilities.filter(or_(name_filter, short_code_filter)) if 'bbox' in request.args: facilities = facilities.filter( Facility.pos.contained(bbox_to_polygon(request.args['bbox']))) if 'district' in request.args: facilities = facilities.filter( Facility.district_id == request.args['district']) if 'min_capacity' in request.args: try: min_capacity = int(request.args['min_capacity']) facilities = facilities.filter( Facility.capacity >= min_capacity) except ValueError: abort(400, min_capacity='must be a valid integer') if 'max_capacity' in request.args: try: max_capacity = int(request.args['max_capacity']) facilities = facilities.filter( Facility.capacity <= max_capacity) except ValueError: abort(400, max_capacity='must be a valid integer') if max_capacity and min_capacity and min_capacity > max_capacity: abort(400, max_capacity=('max_capacity must be greater than ' 'min_capacity')) if 'short_code' in request.args: facilities = facilities.filter( Facility.short_code == request.args['short_code']) if 'type' in request.args: try: type_id = int(request.args['type']) facilities = facilities.filter( Facility.facility_type_id == type_id) except ValueError: abort(400, type='type must integer') if 'accessibility' in request.args: accessibility = request.args["accessibility"] keys = accessibility.split(",") facilities = facilities.filter(Facility.accessibility.has_all(keys)) anonymous = False user = {} superuser = repo.is_super_admin(request.cookies) if not superuser: user = repo.get_user(request.cookies) anonymous = user is None # Do not show deleted facilities by default show_deleted = request.args.get('show_deleted', False) if not show_deleted: facilities = facilities.filter(Facility.is_deleted == False) # Do not show not published facilities by default show_not_published = request.args.get('show_not_published', False) if not show_not_published: facilities = facilities.filter(Facility.is_published == True) # NOTE: Admin should see all facilities, regular AD users only to see their own if not anonymous and not superuser and repo.is_administrator(user): if 'show_only_my_facilities' in request.args and request.args['show_only_my_facilities'] == 'True': my_facilities = repo.get_facilities_edit_credentials(user['id'], request.cookies) facilities = facilities.filter(Facility.id.in_(my_facilities)) facilities = facilities.all() if anonymous or not repo.is_administrator(user): return marshal(facilities, facility_fields) return marshal(facilities, facility_fields_admin)
def get(self): year = None if "year" in request.args: year = int(request.args.get('year')) week_number = None if "week" in request.args: week_number = int(request.args.get('week')) day = None if "day" in request.args: day = datetime.strptime(request.args.get('day'), '%Y-%m-%d') resource_uri = request.args.get('resource_uri') except_application = request.args.get('except') if not (year and week_number) and not day: abort(404) if "status" in request.args: statuses = [request.args["status"]] else: statuses = ["Pending", "Processing", "Granted"] resource = self.get_resource_with_id_or_uri(None, resource_uri) except_applications = [] if except_application: except_applications.append(except_application) single = self.get_slots( resource, except_applications, statuses, year, week_number, day ) repeating = self.get_repeating_slots( resource, except_applications, statuses, year, week_number, day ) strotimer = self.get_strotime_slots( resource, year, week_number, day ) slots = single + repeating + strotimer user = get_user(request.cookies) if user is not None and has_role(user, 'flod_saksbehandlere'): for slot in slots: applicant = self.get_applicant(slot) if applicant is not None: name, email = applicant slot['applicant_email'] = email slot['applicant_name'] = name return marshal(slots, week_exploded_slot_fields_applicant), 200 return marshal(slots, week_exploded_slot_fields), 200
def get_resource_overview(facilities_ids, start_date, end_date): facilities = get_facilities_from_web(facilities_ids) facilities_uris = [facility.get('uri') for facility in facilities] statuses = ["Granted"] single_booking_query = current_app.db_session.query(Application) \ .filter(Resource.id == Application.resource_id, Resource.uri.in_(facilities_uris), Slot.application_id == Application.id, Application.status.in_(statuses), cast(Slot.start_time, Date).between(start_date, end_date), cast(Slot.end_time, Date).between(start_date, end_date), ) strotime_booking_query = current_app.db_session.query(Application) \ .filter(Resource.id == Application.resource_id, Resource.uri.in_(facilities_uris), Slot.application_id == Application.id, Application.status.in_(statuses), cast(StrotimeSlot.start_time, Date).between(start_date, end_date), cast(StrotimeSlot.end_time, Date).between(start_date, end_date), ) repeating_booking_query = current_app.db_session.query(Application) \ .filter(Resource.id == Application.resource_id, Resource.uri.in_(facilities_uris), RepeatingSlot.application_id == Application.id, Application.status.in_(statuses), # Get all slots between start and end date cast(RepeatingSlot.start_date, Date) <= end_date, cast(RepeatingSlot.end_date, Date) >= start_date ) resources = current_app.db_session.query(Resource).filter(Resource.uri.in_(facilities_uris)) blocked_times = {} for resource in resources: blocked_times[resource.uri] = BlockedTimeUtil.get_blocked_time_for_date_range(current_app.db_session, resource, start_date, end_date) persons_ids = [app.person.uri.replace('/persons/', '') for app in single_booking_query] + \ [app.person.uri.replace('/persons/', '') for app in repeating_booking_query] + \ [app.person.uri.replace('/persons/', '') for app in strotime_booking_query] organisations_ids = [app.organisation.uri.replace('/organisations/', '') for app in single_booking_query if app.organisation] + \ [app.organisation.uri.replace('/organisations/', '') for app in repeating_booking_query if app.organisation] + \ [app.organisation.uri.replace('/organisations/', '') for app in strotime_booking_query if app.organisation] current_user = get_user(request.cookies) persons = ExternalResourceHelper.get_persons_by_id(person_ids=persons_ids, auth_token_username=current_user.get('id')) organisations = ExternalResourceHelper.get_organisations_by_id(organisation_ids=organisations_ids) result = [] single_soknader = map_applications_from_query_to_pers_and_orgs_and_resources(single_booking_query, organisations, persons, facilities) add_entries_for_single_day_bookings(result=result, soknader=single_soknader) strotime_soknader = map_applications_from_query_to_pers_and_orgs_and_resources(strotime_booking_query, organisations, persons, facilities) add_entries_for_single_day_bookings(result=result, soknader=strotime_soknader) repeating_soknader = map_applications_from_query_to_pers_and_orgs_and_resources(repeating_booking_query, organisations, persons, facilities) add_entries_for_repeating_booking(result=result, repeating_bookings=repeating_soknader, start_date=start_date, end_date=end_date) add_entries_for_blocked_time(result, blocked_times, facilities) return result
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 put(self, application_id): data = request.get_json() application = self.get_object_by_id(application_id) ensure(PUT, application) status = data.get("status", None) resource_uri = data["resource"]["uri"] application_message = data.get("message", "") to_be_invoiced = data.get("to_be_invoiced", None) invoice_amount = data.get("invoice_amount") or None if invoice_amount is not None and type(invoice_amount) is not int \ and not invoice_amount.isdigit(): abort(400, __error__=[u'Fakturabeløp må være heltall']) resource = get_resource_for_uri(resource_uri) if not resource: abort(404) resource_details = get_resource_from_web(application.resource.uri) if not resource_details: abort(404) settings = SettingsResource().get() user = get_user(request.cookies) # Check that the resource allows the type of application if application.get_type() == "single" and not resource.single_booking_allowed: abort(403, __error__=[u'Det er ikke mulig å behandle en søknad om engangslån fordi type utlån er deaktivert for lokalet']) if application.get_type() == "repeating" and (not resource.repeating_booking_allowed or \ (resource.repeating_booking_allowed and not settings["repeating_booking_allowed"] and not has_role(user, 'flod_saksbehandlere'))): abort(403, __error__=[u'Det er ikke mulig å behandle en søknad om fast lån fordi type utlån er deaktivert for lokalet']) # The application might have been moved to a different resource. application.resource = resource if status == "Pending": application.amenities = application.requested_amenities application.accessibility = application.requested_accessibility application.equipment = application.requested_equipment application.suitability = application.requested_suitability application.facilitators = application.requested_facilitators else: application.amenities = data.get('amenities') application.accessibility = data.get('accessibility') application.equipment = data.get('equipment') application.suitability = data.get('suitability') application.facilitators = data.get('facilitators') slot_data = data.get("slots", None) if status == "Granted" and not slot_data: abort(403, __error__=[u'Tildelt tid mangler']) if application.get_type() == "single": slots = [parse_single_slot(slot, application) for slot in slot_data] application.single_slots = slots elif application.get_type() == "repeating": slots = [parse_repeating_slot(slot, application) for slot in slot_data] application.repeating_slots = slots # Check if there are any conflicts for slot in slots: if application.get_type() == "single": start_date = slot.start_time.date() end_date = slot.end_time.date() week_day = slot.start_time.isoweekday() start_time = slot.start_time.time() end_time = slot.end_time.time() elif application.get_type() == "repeating": 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 if application.get_type() == "single": self.validate_end_date_of_slot(settings["single_booking_enddate"], end_date.isoformat(), u'engangslån') elif application.get_type() == "repeating": 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 application.is_arrangement or status == "Denied": # NOTE: Arrangements trumph all other applications. If we reject an application, we dont nede to # check if the time is available... pass else: 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 status != "Denied": 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'] ) if status == "Granted" or status == "Denied" or (status == "Pending" and len(slots) == 0): application.status = status else: application.status = "Processing" application.message = application_message application.invoice_amount = invoice_amount application.to_be_invoiced = to_be_invoiced application.comment = data.get('comment') current_app.db_session.add(application) current_app.db_session.commit() current_app.db_session.refresh(application) document = '' if status == "Granted" or status == "Denied": document = send_email_application_processed(application) # Create erv code if invoice_amount is not None and status == "Granted": invoice_amount = int(invoice_amount) org_number = None if application.organisation is not None: organisation = get_organisation_from_web( application.organisation.uri) org_number = organisation.get('org_number', None) if org_number is None: person = get_person_from_web(application.person.uri) erv_code = erv_person(application, invoice_amount, person['national_identity_number']) else: erv_code = erv_organisation(application, invoice_amount, org_number) print 'erv: "{}"'.format(erv_code) # Skip sending mail for now # send_erv_code_mail("{},".format(erv_code)) application_dict = {} if application.get_type() == "single": application_dict = marshal(application, single_application_fields) elif application.get_type() == "repeating": # Authentication is not implemented for all resources yet, so we # skip creating sak/journalpost if no auth_token exists if status == 'Granted' and 'auth_token' in request.cookies: arkiver(application, document) application_dict = marshal(application, repeating_application_fields) # Include emails in response, if requested if data.get("include_emails", False): _, granted_message = render_email_template(application, "Granted") _, denied_message = render_email_template(application, "Denied") application_dict['emails'] = dict( granted_message=granted_message, denied_message=denied_message ) return application_dict
def update_user_county_at_start(chat_id, country): response = repo.update_user(chat_id, {'country': country.lower()}) user = repo.get_user(chat_id) if response: message_subject.on_next(HooNewsMessage(chat_id, 'UPDATE', hoonewsstrings.get_string(user['language'], 'SETTINGS_UPDATED')))
def post(self): data = request.get_json() is_arrangement = data.get("isArrangement", False) 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 not settings["single_booking_allowed"] and not (has_role(user, 'flod_brukere') or has_role(user, 'flod_saksbehandlere')) or \ not resource.single_booking_allowed: abort(403, __error__=[u'Engangslå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 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') ) application.is_arrangement = is_arrangement slots = [self.parse_slot_request(d) for d in data["slots"]] if not slots: abort(400, __error__=[u'Tidspunkt mangler']) for slot in slots: start_date = slot.start_time.date() end_date = slot.end_time.date() week_day = slot.start_time.isoweekday() start_time = slot.start_time.time() end_time = slot.end_time.time() self.validate_end_date_of_slot(settings["single_booking_enddate"], end_date.isoformat(), u'engangslån') self.validate_start_and_end_times(start_date, end_date, start_time, end_time) if not is_arrangement: 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_single_slot(slot) current_app.db_session.add(application) current_app.db_session.commit() current_app.db_session.refresh(application) send_email_to_resource(application) send_email_to_applicant(application) return application, 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