def decorated_function(*args, **kwargs): if permission == 'admin': if not g.current_user.admin: return error_response( 403, "missing permission {}".format(permission)) elif permission == 'reviewer': if not g.current_user.reviewer: return error_response( 403, "missing permission {}".format(permission)) return f(*args, **kwargs)
def get_submission_upload_detail(comp_id, cat_id, sub_id): """Retrives information about uploads for this submission""" uploads = SubmissionUploads.query.filter_by(submission_id=sub_id).first() if uploads is None: return jsonify([]) # Returns nothing json = [uploads.to_json()] submission = Submission.query.filter_by(id=sub_id).filter_by( comp_id=comp_id).first_or_404() if not submission.check_category(cat_id): return error_response(404, 'submission is not in this category') return jsonify(json)
def delete_user(user_id): """Deletes selected user""" user = User.query.filter_by(id=user_id).first() submissions = Submission.query.filter_by(user_id=user_id) if user is not None: for _, submission in enumerate(submissions): db.session.delete(submission) db.session.delete(user) db.session.commit() return '', 200 return error_response(404, "user id doesn't exist")
def get_submission_detail(comp_id, cat_id, sub_id): """Retrives information about the review and category""" json = [ Category.query.filter_by(id=cat_id).filter_by( comp_id=comp_id).first_or_404().to_json() ] submission = Submission.query.filter_by(id=sub_id).filter_by( comp_id=comp_id).first_or_404() if not submission.check_category(cat_id): return error_response(404, 'submission is not in this category') json.append(submission.to_json()) return jsonify(json)
def org(): # Get request parameters and check if 'name' is presented data = request.args if 'name' not in data: return bad_request('Parameter name must be in request') org_found = db.session.query(Org).filter( or_(Org.name_eng == data['name'], Org.name == data['name'])).first() if org_found: result = [org_found.as_json()] return jsonify(result) else: return error_response(404, 'Organization not found')
def edit_user(user_id): """Changes user details""" user = User.query.filter_by(id=user_id).first() if user is not None: data = request.get_json() or {} if 'username' in data and data['username'] != user.username \ and User.query.filter_by(username=data['username']).first(): return bad_request('please use a different username') if 'email' in data and data['email'] != user.email \ and User.query.filter_by(email=data['email']).first(): return bad_request('please use a different email') user.from_json(data, admin=True) db.session.commit() return jsonify(user.to_json(admin=True)) return error_response(404, "user id doesn't exist")
def terrorist(): data = request.args if 'iin' in data: ter = db.session.query(Terrorist).filter( Terrorist.iin == data['iin'].strip()).first() elif 'lname' in data and 'fname' in data: ter = db.session.query(Terrorist).filter( Terrorist.lname == data['lname'].strip().upper(), Terrorist.fname == data['fname'].strip().upper()).first() else: return bad_request( 'parameters iin or lname and fname must be in request') if ter: response = jsonify([ter.as_json()]) return response return error_response(404)
def get_user(user_id): """Returns user from id""" user = User.query.filter_by(id=user_id).first() if user is not None: return jsonify(user.to_json()) return error_response(404, "user id doesn't exist")
def token_auth_error(): """Error if unauthorized""" return error_response(401)
def basic_auth_error(): """Error if unauthorized""" return error_response(401)
def create_flower(): """ Create flower if it doesn't exists """ logging.info("Called creating flower endpoint ...") headers = request.headers or {} # Check request if 'Authorization' not in headers: return bad_request("Missing 'Authorization' header in request") # Parse auth try: login, password = parse_authorization_header( headers.get('Authorization')) except Exception as e: return server_error( f"Exception occurred during parsing user credentials: {str(e)}") try: user = User.query.filter_by(login=login).first() except Exception as e: return server_error( f"Exception occurred during loading user: {str(e)}") if user and authorize(login, password, user): data = request.get_json() or {} logging.info(data) if 'name' not in data or 'type' not in data or 'sensor' not in data: return bad_request( f"Request must includes name, sensor and type fields. Request: " f"{data}") if Flower.query.filter_by(name=data.get('name')).first(): return error_response( 500, f"Flower with name {data.get('name')} already exists for " f"user {login}") sensor = Sensor.query.filter_by( serial_number=data.get('sensor')).first() if not sensor or sensor.token != user.token: bad_request("Incorrect sensor serial number") flower = Flower() flower.name = data.get('name') flower.flower_type = data.get('type') flower.user = user.id flower.sensor = sensor.id flower.last_transplantation_year = data.get('last_transplantation_year') or \ datetime.datetime.now().year # Commit changes to db db.session.add(flower) db.session.commit() for recommendation_class in recommendation_classes(): recom = RecommendationItem() recom.r_class = recommendation_class.__name__ recom.flower = flower.id db.session.add(recom) db.session.commit() RecommendationBackGroundTask( recommendation_class.create_from_db(t_id=recom.id, flower=flower)) resp_data = flower.to_dict() resp_data['recommendations'] = _get_alarms_for_flower(user, id, 2) resp_data['warnings'] = _get_alarms_for_flower(user, id, 1) resp_data['problems'] = _get_alarms_for_flower(user, id, 0) return create_response_from_data_with_code(resp_data, 201) else: return unauthorized(login)