def post(self): if request.method == 'POST': #json_input = request.get_json(force=True) parser.add_argument('name',type=str,location='form') parser.add_argument('born',type=str,location='form') #request.get_json(force=True) args = parser.parse_args() #_name = json_input['name'] #_born = json_input['born'] _name = str(args['name']) _born = str(args['born']) try: if isinstance(hd.validateDate(_born),str) == False: if PersonModel.find_by_name(hd.getName(_name)['firstname'],hd.getName(_name)['lastname']): resp = jsonify(message='Persona ya existe!') resp.status_code = 400 else: PersonModel.insert_into_table(hd.getName(_name)['firstname'],hd.getName(_name)['lastname'],hd.changeFormatDate(_born),hd.calculate_age(_born),hd.daysMissing(_born)) resp = jsonify( firstname=hd.getName(_name)['firstname'], lastname=hd.getName(_name)['lastname'], date=hd.changeFormatDate(_born), age=hd.calculate_age(_born), text=hd.daysMissing(_born), message='Persona agregada a la base de datos!') resp.status_code = 200 else: resp = jsonify(message='Error fecha incorrecta, la fecha deberia ser DD-MM-YYYY o una valida.') resp.status_code = 500 except Exception as e: resp = jsonify(error='Error al consumir servicio de cumpleaños, {}'.format(str(e))) resp.status_code = 400 return resp
def put(cls, id): data = PersonModel.get_person(id) if data.estado == 'DESACTIVO': PersonModel.deactive_and_active_person(id, 'ACTIVO') return {'message': 'person actived'}, 202 else: return {'message': 'person already actived'}, 500
def get(self, name=None, id=None): if id: person = PersonModel.find_by_id(id) else: person = PersonModel.find_by_name(name) if person: return person.json() return {'message': 'Person not found'}, 404
def delete(self, name=None, id=None): if id: person = PersonModel.find_by_id(id) else: person = PersonModel.find_by_name(name) if person: person.delete_from_db() return {'message': 'Person deleted'}, 200 else: return {'message': 'Person not found'}, 400
def put(cls, id): data = _person_parse.parse_args() person = PersonModel(**data) try: person.update_person(id, person) return {'message': 'user updated'}, 202 except: return { "message": "Ocurrio un error al ingresar un dato a la base de datos." }, 500
def patch(self, id): """Update a single person using their id""" try: valid_id = is_uuid(id) if valid_id: person_data = PersonModel.find_by_id(id) if person_data: original_data = person_data.json() request_data = request.get_json() schema = PersonRequestSchema() proposed_modification = schema.load(request_data, partial=True) if proposed_modification and is_different( proposed_modification, original_data, USER_INPUT_FIELDS): updated_person = PersonModel( id=person_data.id, first_name=proposed_modification.get('first_name') or original_data.get('first_name'), middle_name=proposed_modification.get( 'middle_name') or original_data.get('middle_name'), last_name=proposed_modification.get('last_name') or original_data.get('last_name'), age=proposed_modification.get('age') or original_data.get('age'), email=proposed_modification.get('email') or original_data.get('email'), version=original_data.get('version') + 1, latest=True) updated_person.save_to_db() # The original person object is no longer the latest person_data.latest = False person_data.save_to_db() return { 'message': f"Successfully updated person with id: {id}" } else: return { 'message': f"No changes detected from user input versus database record" } else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except ValidationError as error: return error.messages, 400 except Exception as error: print(error) return "Internal Server Error", 500
def read(): key = request.args.keys()[0] value = request.arg[key] if key is not None and value is not None: reponse = PersonColl.find_one({key: value}) model = PersonModel.parse_json(reponse) jsonify(model.to_json()) else: reponse = PersonColl.find() list = [] for result in reponse: model = PersonModel.parse_json(result, hasid=True) list.append(model.to_json(include_id=True)) return jsonify(list)
def post(self): user = users.get_current_user() if user: person = PersonModel( personaddedby = users.get_current_user(), personname = self.request.get('companyname'), personpname = self.request.get('prenom'), organisme = CompanyModel.getCompanyByName(self.request.get('organisme').strip()), fonction = self.request.get('fonction'), personwebsite = self.request.get('companywebsite'), personaddress = self.request.get('companyaddress'), personwilaya = self.request.get('companywilaya'), persondescription = self.request.get('companydescription')) person.put(); emails = self.request.get_all('companymail') for email in emails: PersonEmailsModel(email = email , person = person).put() tels = self.request.get_all('companytel') for tel in tels: PersonTelModel(persontel = tel, person = person).put() self.redirect('/')
def post(self): """Create a new person""" try: request_data = request.get_json() schema = PersonRequestSchema() new_person_request = schema.load(request_data) unique_id = uuid.uuid4() first_name = new_person_request.get('first_name') middle_name = new_person_request.get('middle_name') last_name = new_person_request.get('last_name') print(new_person_request) is_unique_name = PersonModel.is_unique(first_name, middle_name, last_name) if is_unique_name: person = PersonModel( id=unique_id, first_name=new_person_request.get('first_name'), middle_name=new_person_request.get('middle_name'), last_name=new_person_request.get('last_name'), age=new_person_request.get('age'), email=new_person_request.get('email'), version=1, latest=True) person.save_to_db() return { "message": f"Person successfully created!", "id": str(person.id) }, 201 else: return { "message": "Resource with the specified name identifier already exists" }, 409 except ValidationError as error: return error.messages, 400 except Exception as error: print(error) return "Internal Server Error", 500
def get(self, id): """Fetch the latest version of a single person using their id; if version is provided, fetch a single person using their id and the specified version""" try: valid_id = is_uuid(id) version = request.args.get('version', '') person_schema = PersonSchema(only=OUTPUT_FIELDS) if valid_id and version: person_data = PersonModel.find_by_id_and_version(id, version) if person_data: return person_schema.dump(person_data) else: return {'message': f"Resource not found"}, 404 elif valid_id: person_data = PersonModel.find_by_id(id) if person_data: return person_schema.dump(person_data) else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except Exception as error: print(error) return "Internal Server Error", 500
def post(self): data = _person_parse.parse_args() person = PersonModel(**data) person.estado = 'ACTIVO' person.save_to_db() try: person.save_to_db() return {'message': 'user created'}, 201 except: return { "message": "Ocurrio un error al ingresar un dato a la base de datos." }, 500
def get(self,id): if request.method == 'GET': try: user = PersonModel.find_by_id(id) if user: resp = jsonify(person=user.json()) resp.status_code = 200 else: resp = jsonify(message='No se encontró persona con ese id!') resp.status_code = 404 except ValueError as e: resp = jsonify(message='Error, {}'.format(e)) resp.status_code = 500 return resp
def get(self): if request.method == 'GET': try: users = PersonModel.find_all() if users: resp = jsonify(people=[user.json() for user in users]) resp.status_code = 200 else: resp = jsonify(message='No se han registrado personas!') resp.status_code = 404 except ValueError as e: resp = jsonify(message='Error, {}'.format(e)) resp.status_code = 500 return resp
def put(self, id): data = Person.parser.parse_args() person = PersonModel.find_by_id(id) current_user = current_identity.id if person: person.name = data['name'] person.admin = current_user else: person = PersonModel(name=data['name'], admin=current_user) person.save_to_db() return person.json(), 200
def post(self): current_user = current_identity.id data = CreatePerson.parser.parse_args() person = PersonModel(name=data['name'], admin=current_user) if PersonModel.find_by_name(data['name']): return { 'message': "A person with name '{}' already exists.".format(data['name']) }, 400 person.save_to_db() return person.json(), 201
def delete(self, id): """Delete a single person using their id""" try: valid_id = is_uuid(id) if valid_id: person = PersonModel.find_by_id(id) if person: person.delete_from_db() return { 'message': f"Successfully deleted person with id: {person.id} and version: {person.version}" } else: return {'message': f"Resource not found"}, 404 else: return {'message': f"Resource not found"}, 404 except Exception as error: print(error) return "Internal Server Error", 500
def post(self, name): if PersonModel.find_by_name(name): return { 'message': "A person with name '{}' already exists.".format(name) }, 400 current_user = current_identity.id person = PersonModel(name=name, admin=current_user) try: person.save_to_db() except: return {"message": "An error occurred creating the site."}, 500 return person.json(), 201
def get(self): user = users.get_current_user() url = users.create_login_url(self.request.uri) url_linktext = 'Login' if user: url = users.create_logout_url(self.request.uri) url_linktext = 'Logout' raw_id = self.request.get('id') id = int(raw_id) lentreprise = CompanyModel.get_by_id(id) emails = CompanyEmailsModel.getAllEmailsByCompanyID(id) notes = NoteCompanyModel.all().order('creedate').filter('company', CompanyModel.get_by_id(id)) tels = CompanyTelModel.getAllTelsByCompanyID(id) persons = PersonModel.all().filter('organisme =', lentreprise) sujets = SujetModel.all().filter('organismeref =', lentreprise) tasks = TasksAboutModel.all().filter('organisme', lentreprise).filter('profile', UserProfileModel.getCurrent()) else: self.redirect(users.create_login_url(self.request.uri)) values = { 'emails' : emails, 'idcompany' : id, 'sujets' : sujets, 'tasks' : tasks, 'tels' : tels, 'persons' : persons, 'company': lentreprise, 'notes' : notes, 'user': user, 'url': url, 'url_linktext': url_linktext, } self.response.out.write(template.render('templates/fiche_entreprise.html', values))
def insert(): result = request.get_json() model = PersonModel.parse_json(result) PersonColl.insert_one(model.to_json()) return Response(status=200)
def get(self): person = PersonModel.get_all_person() persons = [x.json() for x in person if x.estado != 'DESACTIVO'] return {'values': persons}
def get(self): person = PersonModel.get_all_person() persons = [x.json() for x in person] return {'values:': persons}
def get(cls, id): person = PersonModel.get_person(id) if not person: return {'message': 'person whit id: {} not exist'.format(id)} return person.json()
def get(self): """Fetch a list of all persons (latest version)""" persons_list_schema = PersonSchema(many=True, only=OUTPUT_FIELDS) return persons_list_schema.dump(PersonModel.find_all())