def put(self, id): current_user_id = get_jwt_identity() if current_user_id: data = Client.parser.parse_args() client = ClientModel.find_by_id(id) if client and client.user_id == current_user_id: denomination = data['denomination'] if not denomination: return {'message': "Veuillez entrer la denomination"} type_client = data['type_client'] if not type_client: return {'message': "Veuillez entrer le type de client"} ad_postale = data['ad_postale'] if not ad_postale: return { 'message': "Veuillez entrer l'adresse postale du client" } tel = data['tel'] if not tel: return {"message": "Veuillez entrez le numero du client"} name_interloc = data['name_interloc'] if not name_interloc: return { "message": "Veuillez entrez le nom de l'interlocuteur" } contact_interloc = data['contact_interloc'] if not contact_interloc: return { "message": "Veuillez entrez le contact de l'interlocuteur" } tel = data['tel'] if not tel: return { "message": "Veuillez entrer votre numero de telephone" } if ClientModel.find_by_tel(tel): return {"message": "Ce numero existe deja"} client.denomination = denomination client.type_client = type_client client.ad_postale = ad_postale client.tel = tel client.name_interloc = name_interloc client.contact_interloc = contact_interloc client.save_to_db() return client.json() return {"message": "Client not found"}, 400 return {"message": "Veuillez vous connectez"}
def put(self): data = Client.parser.parse_args() client = ClientModel(**data) try: client.update_to_db() except: return {"message": "An error occurred updating the operation"}, 500 return client.json(), 200
def post(self): data = Client.parser.parse_args() client = ClientModel(**data) try: client.save_to_db() except: return {"message": "An error occurred creating the client"}, 500 return client.json(), 201
def post(self): data = ClientRegister.args.parse_args() if ClientModel.find_by_email( data['email'] ): #quando usuario é criado, ele ainda não sabe seu user_id return { 'message': "Email {} already been used.".format(data['email']) }, 400 client = ClientModel(productList, **data) client.insertClient() return {'message': 'Client created successfully'}, 201
def delete(cls, client_id: int): client = ClientModel.find_by_id(client_id) if client: client.delete_from_db() return {"message": gettext("client_deleted")}, 200 return {"message": gettext("client_not_found")}, 404
def get(self, email): client = ClientModel.find_by_email(email) if client: return client.json() return { 'message': 'client not found' }, 404 #status code do http para dizer que não achou
def put(cls, client_id: int): client_json = request.get_json() client = ClientModel.find_by_id(client_id) if client is None: client = ClientModel() client.name = client_json["name"] client.email = client_json["email"] client.age = client_json["age"] client.phone_number = client_json["phone_number"] client.user_id = client_json["user_id"] client.save_to_db() return client_schema.dump(client), 200
def put(self, id): item = request.get_json() if request.get_json() else request.form try: if item: model = ClientModel.get_by_id(id) if 'first_name' in item: model.first_name = item['first_name'] if 'last_name' in item: model.last_name = item['last_name'] if 'cpf' in item: model.cpf = item['cpf'] if 'rg_number' in item: model.rg_number = item['rg_number'] if 'rg_uf' in item: model.rg_uf = item['rg_uf'] if 'email' in item: model.email = item['email'] if 'phone' in item: model.phone = item['phone'] if 'active' in item: model.active = item['active'] if 'active' in item else True model.save() return 'edited', 204 else: return 'unedited, invalid payload', 400 except Exception as e: return f"{e}", 500
def put(self, id): client_data = ClientModel.find_by_id(id) client_json = request.get_json() client_data.name = client_json['name'] client_data.save_to_db() return client_schema.dump(client_data), 200
def delete(self, id): try: client = ClientModel.get_by_id(id) client.delete() return 'No Content', 204 except Exception as e: return f"{e}", 500
def post(cls): data = ClientLogin.args.parse_args() client = ClientModel.find_by_email(data['email']) if client: accessToken = create_access_token(identity=client.email) return {'accessToken': accessToken}, 200 return {'message': 'Client doesnt exists or email is incorrect'}, 401
def _list_client(self): clients = ClientModel.list_all() return list(map(lambda client: { 'id': client.id, 'name': client.first_name, 'email': client.email, 'active': client.active }, clients))
def delete(self, id): current_user_id = get_jwt_identity() if current_user_id: client = ClientModel.find_by_id(id) if client and client.user_id == current_user_id: client.delete_from_db() return {'message': 'Client deleted'} return {'message': 'Client not found.'}, 404 return {"message": "Veuillez vous connectez"}
def get(self, id): current_user_id = get_jwt_identity() client = ClientModel.find_by_id(id) if client: if client.user_id == current_user_id: return client.json() return {"message": "Veuillez vous connectez"} return {'message': 'Client not found'}, 404
def get(self, name): """Get endpoint for client name. Headers: {Authorization: JWT jwt_token, ContentType: application/json} If client name in database not found returns json message 404 error. Returns json object of client and all requests with that client name and 200 code.""" client = ClientModel.select(name) if client: return client.json(), 200 return {'message': 'Client not found'}, 404
def post(self): data = Clients.parser.parse_args() try: initials = data['initials'] user_id = data['user_id'] item = ClientModel( None, initials, user_id) item.save() return data except Exception as err: return str(err)
def delete(self, email): client = ClientModel.find_by_email(email) if client: try: client.deleteClient() except: return { 'message': 'Internal error trying to delete client.' }, 500 #status code para internal server error return {'message': 'Client deleted'} return {'message': 'Client not found.'}, 404
class ClientController: model = ClientModel() @classmethod def choose(cls, parent=None): from views.client_dialog import ClientDialog dlg = ClientDialog(parent) result = dlg.exec() item_id = dlg.getResult() item = cls.model.getItemById(item_id) return result, item @classmethod def create(cls, parent=None): from views.client_form import ClientForm dlg = ClientForm(parent) result = dlg.exec() if result == ClientForm.Accepted: client = dlg.getClient() if not Options.get().autonomy_mode: while not cls.model.saveItem(client): p = QMessageBox().warning( parent, 'Ошибка', 'Потеряно соединение с сервером. Повторить?\nПри отмене программа будет ' 'закрыта, а несохраненные изменения потеряны.', QMessageBox.Retry | QMessageBox.Cancel) if p != QMessageBox.Retry: die() cls.model.addItem(client) return True return False @classmethod def edit(cls, client, parent=None): from views.client_form import ClientForm dlg = ClientForm(parent) dlg.setClient(client) result = dlg.exec() if result == ClientForm.Accepted: client = dlg.getClient() if not Options.get().autonomy_mode: while not cls.model.saveItem(client): p = QMessageBox().warning( parent, 'Ошибка', 'Потеряно соединение с сервером. Повторить?\nПри отмене программа будет ' 'закрыта, а несохраненные изменения потеряны.', QMessageBox.Retry | QMessageBox.Cancel) if p != QMessageBox.Retry: die() return True return False
def put(self, email): client = ClientModel.find_by_email(email) productData = addProduct.args.parse_args() url = 'http://challenge-api.luizalabs.com/api/product/' + productData.get( 'id') magalu = requests.request('GET', url) if magalu.status_code == 200: client.updateProductList(**productData) try: client.insertClient() except: return {'message': 'Error trying to update list'}, 500 return client.json(), 200 return {'message': 'Product does not exist.'}, 400
def delete(self, name): """Delete endpoint for client name. Headers: {Authorization: JWT jwt_token, ContentType: application/json} If client name is not found in database returns json message and 404 error. Deletes all requests of client and then the client from the database and returns json message and 201 code.""" client = ClientModel.select(name) if client: for request in client.requests: request.delete_from_db() client.delete_from_db() return {'message': 'Client deleted'}, 200 return {'message': 'Client not found'}, 404
def post(self): data = Client.parser.parse_args() if ClientModel.find_by_email(data['email']): return {'message': "A Client with that email already exists."}, 400 if ClientModel.find_by_tel(data['tel']): return { 'message': "A Client with that number already exists." }, 400 denomination = data['denomination'] if not denomination: return { 'message': "Veuilez entrer la denomination de l'entreprise" } type_client = data['type_client'] if not type_client: return {'message': "Veuilez entrer le type de client"} email = data['email'] if not email: return {'message': "Veuilez entrer le mail du client"} match = re.search(r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$', email) if not match: return {"message": "Movaise adresse mail"} ad_postale = data['ad_postale'] if not ad_postale: return {'message': "Veuilez entrer l'adresse postale du client"} tel = data['tel'] if not tel: return { 'message': "Veuilez entrer le numero de telephone du client" } name_interloc = data['name_interloc'] if not name_interloc: return {'message': "Veuilez entrer le nom de l'interlocuteur"} contact_interloc = data['contact_interloc'] if not contact_interloc: return {'message': "Veuilez entrer le contact de l'interlocuteur"} client = ClientModel(**data) try: client.save_to_db() except: return {"message": "An error occurred creating the client."}, 500 return client.json(), 201
def post(cls): client_json = request.get_json() client = ClientModel.find_by_email(client_json["email"]) if client: return {"message": gettext("client_name_exists")}, 400 client = client_schema.load(client_json) try: client.save_to_db() except: return {"message": gettext("client_error_inserting")}, 500 return client_schema.dump(client), 201
def _get_client(self, id_client): client = ClientModel.get_by_id(id_client) if client is None: return {'message': 'Client not found'}, 404 return { 'id': client.id, 'first_name': client.first_name, 'last_name': client.last_name, 'email': client.email, 'cpf': client.cpf, 'phone': client.phone, 'rg_number': client.rg_number, 'rg_uf': client.rg_uf, 'active': client.active }
def put(self, name): client = ClientModel.find_by_name(name) parser = reqparse.RequestParser() parser.add_argument('credit_score', type=int, help='This is required') data = parser.parse_args() if client: client.credit_score = data['credit_score'] else: client = ClientModel(name, data['credit_score']) client.save_to_db() return client.json()
def get(self, email, id): client = ClientModel.find_by_email(email) try: produto = client.productList[id] if produto['reviewScore']: return { "title": produto['title'], "image": produto['image'], "price": produto['price'], "reviewScore": produto['reviewScore'] } return { "title": produto['title'], "image": produto['image'], "price": produto['price'] } except: return {'message': 'Product is not in clients list.'}, 400
def _list_by_client(self, client_id): reports = ReportModel.get_by_client(client_id) res = [] for report in reports: client = ClientModel.get_by_id(report.client_id) res.append({ 'id': report.id, 'status': report.status, 'client_id': report.client_id, 'vehicle_id': report.vehicle_id, 'description': report.description, 'client': { 'id': client.id, 'first_name': client.first_name } }) return res
def fromDict(cls, data): obj = cls() obj.id = data['id'] obj.application_id = data['application'] obj.number = data['number'] obj.date = QDate().fromString(data['date'], 'yyyy-MM-dd') obj.terms = data['terms'] obj.price = data['price'] obj.advance = data['advance'] obj.client = ClientModel.getItemById(data['client']) obj.poa = data['poa_client'] obj.poa_number = data['poa_client_number'] obj.poa_date = QDate().fromString(data['poa_client_date'], 'yyyy-MM-dd') obj.poa = data['poa_contractor'] obj.poa_number = data['poa_contractor_number'] obj.poa_date = QDate().fromString(data['poa_contractor_date'], 'yyyy-MM-dd') return obj
def post(self, name): """Post endpoint for client name. Headers: {Authorization: JWT jwt_token, ContentType: application/json} If client name already in database returns json message and 400 error. If error occurs while adding to database returns 500 error. Returns json object of client and empty requests field and 201 code.""" if ClientModel.select(name): return {'message': 'Client already exists'}, 400 client = ClientModel(name) try: client.save_to_db() except BaseException: return {'message': 'Something went wrong'}, 500 return client.json(), 201
def post(self): """Post endpoint for adding requests into the database. Headers: {Authorization: JWT jwt_token, ContentType: application/json} Body must be json with priority, target_date, product_area, client_name, title, description fields. Database must have matching client_name or will return json message 400 error. If error occurs while inserting into database will return json message and 500 error. On successful insert into database returns json of request and 201 code.""" parser = Parser() parser.required_fields( 'priority', 'target_date', 'product_area', 'client_name', 'title', 'description') data = parser.parse_args() if not ClientModel.select(data['client_name']): return { 'message': 'Client does not exist'}, 400 request = RequestModel(**data) client_name = data['client_name'] priority = data['priority'] update_list = [] try: while RequestModel.select_same_priority(client_name, priority): update_list.append( RequestModel.select_same_priority( client_name, priority)) priority += 1 request.save_to_db() for req in update_list: req.priority += 1 req.save_to_db() except BaseException: return {'message': 'Something went wrong'}, 500 return request.json(), 201
def _get_report(self, id_report): report = ReportModel.get_by_id(id_report) if report is None: return {'message': 'report not found'}, 404 client = ClientModel.get_by_id(report.client_id) vehicle = VehicleModel.get_by_id(report.vehicle_id) return { 'id': report.id, 'status': report.status, 'client_id': report.client_id, 'vehicle_id': report.vehicle_id, 'client': { 'id': client.id, 'first_name': client.first_name }, 'vehicle': { 'model': vehicle.model, 'board': vehicle.board }, 'description': report.description }