def getAvailableOperators(ot_id): ots = Ot.query.filter(Ot.start != None, Ot.end == None, Ot.id != ot_id).all() search = request.args.get('search') operators_not_available = [] operators_schema = UserSchema( many=True, exclude=['email', 'role', 'section', 'stade', 'photo']) for ot in ots: operators = User.query\ .join(OtOperator, OtOperator.operator_id == User.id)\ .filter(OtOperator.ot_id == ot.id, OtOperator.team_lead_id == ot.teamLeadId)\ .all() operators_not_available.extend(operators) all_operators = User.query.filter(User.role_id == 4).all() operators_available = [] for op in all_operators: if op not in operators_not_available: operators_available.append(op) if search is not None: operators_available = [ op for op in operators_available if search.casefold() in op.lastname.casefold() or search in op.firstname.casefold() ] return operators_schema.jsonify(operators_available)
def put(self, id): try: user = Users.query.get(id) if request.json.get('name'): user.name = request.json.get('name') if request.json.get('email') and Users.isMail( request.json.get('email')): user.email = request.json.get('email') if request.json.get('password'): user.password = Users.setPassword(request.json.get('password')) db.session.add(user) db.session.commit() db.session.remove() userschema = UserSchema(only=['id', 'name', 'email', 'password']) get_users = Users.query.filter(Users.status == 1).order_by( Users.id.desc()).all() users_schema = UserSchema(many=True) users = users_schema.dump(get_users) db.session.remove() request.close() return {'response': 'Registro Actualizado', 'users': users}, 200 except: request.close() return {'response': ' El registro no existe'}, 400
def create_user(): try: data = request.get_json() if (User.find_by_email(data['email']) is not None or User.find_by_username( data['username']) is not None): # already exist user return response_with(resp.INVALID_INPUT_422) data['password'] = User.generate_hash( data['password']) # password to hash user_schmea = UserSchema() user = user_schmea.load(data).data token = generate_verification_token( data['email']) # generate token by email verification_email = url_for('user_routes.verify_email', token=token, _external=True) html = render_template_string( "<p>Welcome! Thanks for signing up. Please follow this link to activate your account:</p> <p><a href='{{ verification_email }}'>{{ verification_email }}</a></p> <br> <p>Thanks!</p>", verification_email=verification_email) subject = "Please Verify your email" send_email(user.email, subject, html) result = user_schmea.dump(user.create()) return response_with(resp.SUCCESS_201) except Exception as e: print(e) return response_with(resp.INVALID_INPUT_422)
def get(self): get_users = Users.query.filter(Users.status == 1).order_by( Users.id.desc()).all() users_schema = UserSchema(many=True) users = users_schema.dump(get_users) db.session.remove() request.close() return make_response(jsonify(users))
def get(self, search): get_users = Users.query.filter( Users.status == 1).filter((Users.name.ilike(f'%{search}%')) | ( Users.email.ilike(f'%{search}%'))).order_by( Users.id.desc()).all() users_schema = UserSchema(many=True) users = users_schema.dump(get_users) db.session.remove() request.close() return make_response(jsonify(users))
def create_user(): try: data = request.get_json() data['password'] = User.generate_hash(data['password']) user_schmea = UserSchema() user = user_schmea.load(data) result = user_schmea.dump(user.data.create()) return response_with(resp.SUCCESS_201) except Exception as e: print(e) return response_with(resp.INVALID_INPUT_422)
def post(self): try: user_id = get_jwt_identity() try: user = Users.query.get(user_id) if request.json.get('name'): user.name = request.json.get('name') if request.json.get('email') and Users.isMail( request.json.get('email')): user.email = request.json.get('email') if request.json.get('password'): user.password = Users.setPassword( request.json.get('password')) db.session.add(user) db.session.commit() db.session.remove() userschema = UserSchema( only=['id', 'name', 'email', 'password']) db.session.remove() request.close() return {'response': 'Sus datos han sido actualizados'}, 200 except: request.close() return {'response': ' El registro no existe'}, 401 except: request.close() return {'response': ' El registro no existe'}, 401
def delete(self, id): try: user = Users.query.get(id) user.status = 0 db.session.add(user) db.session.commit() get_users = Users.query.filter(Users.status == 1).order_by( Users.id.desc()).all() users_schema = UserSchema(many=True) users = users_schema.dump(get_users) db.session.remove() request.close() return {'response': 'Registro Eliminado', 'users': users}, 200 except: request.close() return {'error': ' El registro no existe'}, 401
return 0 @staticmethod def convert_status(obj): if obj.status is None: return 'provisional' return OtSchema.statusMap.get(obj.status, None) @staticmethod def read_status(value): return OtSchema.statusMapReverse.get(value, None) @staticmethod def get_documents(obj): return docs_schema.dump(obj.action.documents) @staticmethod def get_operators(obj): operators = User.query\ .join(OtOperator, OtOperator.operator_id == User.id)\ .filter(OtOperator.ot_id == obj.id, OtOperator.team_lead_id == obj.teamLeadId)\ .all() return operators_schema.dump(operators).data ot_schema = OtSchema() ots_schema = OtSchema(many=True) operators_schema = UserSchema( many=True, exclude=['email', 'role', 'section', 'stade', 'photo']) #operators_schema = UserSchema(many=True)
def post(self): name = request.json.get('name') email = request.json.get('email') password = request.json.get('password') if name and email and password: if Users.isMail(email): user = Users.query.filter(Users.email == email).first() if user: return {'error': 'El correo electrónico ya existe'}, 401 data = { 'name': name, 'email': email, 'code': '', 'password': Users.setPassword(password), 'status': 1, 'images': 0 } users_schema = UserSchema() user = users_schema.load(data) users_schema.dump(user.create()) db.session.commit() get_sampling = Sampling.query.filter( Sampling.status == 0).limit(400).all() sampling_schema = SamplingSchema(many=True) sampling = sampling_schema.dump(get_sampling) for row in sampling: data = { 'user_id': user.id, 'sampling_id': row['id'], 'tag_id': 0, 'status': '0' } training_shema = TrainingSchema() training = training_shema.load(data) training_shema.dump(training.create()) db.session.commit() samp = Sampling.query.get(row['id']) samp.status = 1 db.session.add(samp) db.session.commit() get_users = Users.query.filter(Users.status == 1).order_by( Users.id.desc()).all() users_schema = UserSchema(many=True) users = users_schema.dump(get_users) db.session.remove() request.close() return {'response': 'Registro Exitoso', 'users': users}, 200 else: request.close() return {'error': 'Ingrese un correo valido'}, 401 else: request.close() return {'error': 'Faltan datos'}, 401
"title": fields.String(required=True, description="The title of the agenda"), "description": fields.String(required=True, description="The description for the agenda title") }) login = api.model('login', { "email": fields.String(required=True, description="User's email for login"), "password": fields.String(required=True, description="User's password for login") }) #importing the models to be used from models.agenda import AgendaModel, AgendaSchema from models.users import UserModel, UserSchema #instanciating the schema classes we have user_schema = UserSchema() users_schema = UserSchema(many = True) agenda_schema = AgendaSchema() agendas_schema = AgendaSchema(many = True) #before any request is handled, we need to have the tables existing in our db @app.before_first_request def createTables(): db.create_all() @ns_users.route('') class User(Resource): @api.expect(user)