def register_user(login, password, mail, name) -> User: session = db_session.create_session() user = User() auth = Auth() user.is_approved = False user.name = name user.role = 'user' user.key = geherate_key() auth.login = login auth.password = password auth.email = mail user.auth.append(auth) session.add(user) session.flush() application = Applications() print(user.id) application.user_id = user.id application.login = login session.add(application) session.commit() session.close() return user
def update_team(team_id): session = current_app.db.session owner_id = get_jwt_identity() body: dict = request.get_json() found_team: TeamModel = TeamModel.query.filter_by(id=team_id).first() if not found_team: return { "error": "Not found any team that matches the requested ID." }, HTTPStatus.NOT_FOUND if found_team.owner_id != owner_id: return {"error": "You are not the owner of this Team."}, HTTPStatus.FORBIDDEN for key, value in body.items(): setattr(found_team, key, value) session.add(found_team) session.commit() return { "team": { "team_name": found_team.team_name, "team_description": found_team.team_description, "owner_id": found_team.owner_id, "team_create_date": found_team.team_created_date, } }, HTTPStatus.OK
def patch(self, user_id): parse = reqparse.RequestParser() parse.add_argument("name", type=str) parse.add_argument("nickname", type=str) parse.add_argument("email", type=str) parse.add_argument("password", type=str) parse.add_argument("phone", type=str) parse.add_argument("link_profile_picture", type=str) parse.add_argument("bio", type=str) kwargs = parse.parse_args() user = UserModel.query.get_or_404(user_id) for key, value in kwargs.items(): if value: if key != "password": setattr(user, key, value) if kwargs.password: setattr(user, "password", kwargs.password) session = current_app.db.session session.add(user) session.commit() serializer = user_schema.dump(user) return {"data": serializer}, HTTPStatus.OK
def post(self): parse = reqparse.RequestParser() parse.add_argument("name", type=str, required=True) parse.add_argument("nickname", type=str, required=True) parse.add_argument("email", type=str, required=True) parse.add_argument("password", type=str, required=True) parse.add_argument("phone", type=str, required=True) parse.add_argument("link_profile_picture", type=str, required=True) parse.add_argument("bio", type=str, required=True) kwargs = parse.parse_args() new_user = UserModel( name=kwargs.name, nickname=kwargs.nickname, email=kwargs.email, phone=kwargs.phone, link_profile_picture=kwargs.link_profile_picture, bio=kwargs.bio, ) new_user.password = kwargs.password session = current_app.db.session session.add(new_user) session.commit() serializer = user_schema.dump(new_user) return {"data": serializer}, HTTPStatus.OK
def hw_add_cadastro(request): session = SessionLocal() senha = request["senha"] hash_obj = hashlib.md5(f"{senha}".encode()) md5_value = hash_obj.hexdigest() cadastro = Cadastro(nome=request["nome"], email=request["email"], senha=md5_value) cadastro_json = populate_cadastro([cadastro]) session.add(cadastro) session.commit() session.close() return cadastro_json
def create_category(): session = current_app.db.session body_category = request.get_json() name = body_category.get('name') new_category = CategoryModel(name=name) session.add(new_category) session.commit() return { 'category': { 'id': new_category.id, 'name': new_category.name } }, HTTPStatus.CREATED
def register_admin(login, password, mail, name) -> User: session = db_session.create_session() user = User() auth = Auth() user.is_approved = True user.name = name user.role = 'admin' user.key = geherate_key() auth.login = login auth.password = password auth.email = mail user.auth.append(auth) session.add(user) session.commit() session.close()
def create_user(): session = Session() data = request.get_json() try: user = User(**data) except: return jsonify({"Message": "Invalid input"}), 405 user.hash_password() session.add(user) session.commit() return jsonify({"Success": "User has been created"}), 200
def create_audience(): session = Session() data = request.get_json() try: audience = Audience(**data) except Exception: abort(405, description="Invalid input") if not data['size'] > 0 and data['capacity'] > 0: abort(405, description="Invalid input") session.add(audience) session.commit() return jsonify({"Success": "Audience has been created"}), 200
def create_reservation(): session = Session() data = request.get_json() try: user = session.query(User).filter_by(id=int(data.pop('user_id'))).one() except Exception: abort(404, description="User not found") try: audience = session.query(Audience).filter_by( id=int(data.pop('audience_id'))).one() except: abort(404, description="Audience not found") d = datetime.strptime(data['from_date'], '%Y-%m-%d') data['from_date'] = d.date() d = datetime.strptime(data['to_date'], '%Y-%m-%d') data['to_date'] = d.date() compare_dates(data['from_date'], data['to_date']) reservations = session.query(Reservation).filter_by( audience_id=int(audience.id)).all() for reserv_other in reservations: check_dates(data['from_date'], reserv_other.from_date, reserv_other.to_date) check_dates(data['to_date'], reserv_other.from_date, reserv_other.to_date) reservation = Reservation(**data, user_r=user, audience_r=audience) session.add(reservation) session.commit() return jsonify({"Success": "Reservation has been created"}), 200
def register_team(): session = current_app.db.session res = request.get_json() team_name = res.get("team_name") team_description = res.get("team_description") owner_id = get_jwt_identity() new_team = TeamModel( team_name=team_name, team_description=team_description, owner_id=owner_id ) session.add(new_team) session.commit() return { "team": { "team_name": new_team.team_name, "team_description": new_team.team_description, "owner_id": new_team.owner_id, "team_created_date": new_team.team_created_date, } }, HTTPStatus.CREATED