def test_api_add_person(app, auth): # login as caregiver client = app.test_client() client.post("/login", data={ "login": CAREGIVER_LOGIN, "password": CAREGIVER_PASSWORD }) # add new child response = client.post('api/user', data=DATASET_ADD_CHILD) assert response.status_code == 302 # add new caregiver response = client.post('api/user', data=DATASET_ADD_CAREGIVER) assert response.status_code == 302 with app.app_context(): child = users.get_username_id_and_role_by_username('test_child') assert child['role'] == 'child' caregiver = users.get_username_id_and_role_by_username( 'test_caregiver') assert caregiver['role'] == 'caregiver' child_id = users.get_user_id('test_child') caregiver_id = users.get_user_id('caregiver_login') assert users.is_child_under_caregiver(child_id, caregiver_id)
def wrapped_view(*args, **kwargs): logged_user_id = g.user_data['id'] try: child_id = int(kwargs['child_id']) except KeyError: print(f"The view '{view.__name__}' did not pass 'child_id' parameter") return abort(500) if not users.is_child_under_caregiver(child_id, logged_user_id): return abort(403) return view(*args, **kwargs)
def set_caregiver_to_child(child_id): child_id = int(child_id) caregiver_username_to_child = request.form.get( 'caregiver_username_to_child') caregiver_data = users.get_username_id_and_role_by_username( caregiver_username_to_child) if caregiver_data and caregiver_data['role'] == 'caregiver': if not users.is_child_under_caregiver(child_id, caregiver_data['id']): users.associate_child_with_caregiver(caregiver_data['id'], child_id) flash('Opiekun został przydzielony do dziecka') else: flash('Podany opiekun już był przydzielony') else: flash('Wprowadź poprawny login (username) opiekuna') return redirect(url_for('views.add_caregiver_to_child', child_id=child_id))