def people(): p = PeopleModel.query.all() # POST method is used when you want to tell the server to accept # blocks of data, usually to create something. if request.method == 'POST': first_name = request.form['fname'] last_name = request.form['lname'] gender = request.form['gender'] email = request.form['email'] phone = request.form['phone'] # to confirm whether we are actually getting user's input from # the html form, we print the variable name eg print(first_name) print(last_name) print(gender) print(email) print(phone) # create an instance of the PeopleModel class and pass in the data you want to send to the db person = PeopleModel(first_name=first_name, last_name=last_name, gender=gender, email=email, phone_number=phone) # call the create record method to insert the record in the database. person.create_record() return redirect(url_for('people')) #to confirm that we have actually inserted the record we are going to print out a string. print('Imeingia') return render_template('people.html', x=p)
def post(self): data = self.parser.parse_args() if data["form_type"] == "add": # since put it will update regardless # TODO delete if PeopleModel.find_person(data['user']): return {'message': "This user already exists"}, 400 person = PeopleModel(data["user"]) try: person.save_to_db() except Exception as e: return { "message": "An error occurred while saving the item.", "error": "{}".format(e) }, 500 return redirect(url_for("home")) elif data["form_type"] == "del": person = PeopleModel.find_person(data['user']) if person: try: person.delete_from_db() except Exception as e: return { "message": "An error occurred while deleting", "error": "{}".format(e) }, 500 else: return {'message': "Doesn't exist."}, 400 return redirect(url_for("home"))
def __init__(self, drawing, recipient, sender): # check for people if not PeopleModel.find_person(recipient): raise Exception("Recipient does not exist") if not PeopleModel.find_person(sender): raise Exception("Sender does not exist") hashed_string = self.hash_drawing(drawing) # make sure no duplicate # if not DrawingModel.find_by_hash(hashed_string): self.drawing = drawing self.date_time = datetime.now(tz=pytz.utc) self.hash_value = hashed_string self.recipient = recipient self.sender = sender
def __init__(self, recipient, message): if PeopleModel.find_person(recipient): self.recipient = recipient self.message = message self.date_time = datetime.now(tz=pytz.utc) else: raise Exception("Person does not exist")
def get(self, name): character = PeopleModel.find_by_name(name) if character: return character.json() return { 'message': 'El personaje buscado no existe, consultar con Sandwich Tardy si hay registros suyos en la Citadel' }
def delete(self, name): #Para borrar, necesitamos una lista, esta lista son todos los personajes, menos el que buscamos borrar if PeopleModel.find_by_name(name): connection = sqlite3.connect('people.db') cursor = connection.cursor() query = "DELETE FROM people WHERE name=?" cursor.execute(query, (name, )) connection.commit() connection.close() return {'message': str(name) + ' fue eliminado.'} else: return {'message': str(name) + ' no existe. ¿Está ya muerto?'}
def get(self): people = PeopleModel.find_all() people = [person.person for person in people] messages = MessageModel.find_all() headers = {'Content-Type': 'text/html'} matrices = DrawingModel.find_all() for i in matrices: print(i.json()) return make_response(render_template('home.html', people=people, messages=messages, title="DistancePi", matrices=DrawingModel.find_all() ), 200, headers )
def put(self, name, **kwargs): data = People.parser.parse_args() person = PeopleModel.find_by_name(name) if person: person.role = data['role'] person.email = data['email'] else: person = PeopleModel(name, **data) person.save_to_db() return person.json(), 201
def put(self, name): data = Character.parser.parse_args() character = PeopleModel.find_by_name(name) updated_character = PeopleModel(name, data['Id'], data['isAlive'], data['placeId'], data['isKing']) if character is None: try: updated_character.insert() except: return {"message": "Error al añadir el personaje"}, 500 else: try: updated_character.update() except: return {"message": "Error al actualizar el personaje"}, 500 return character #Return el character actualizado/creado para reflejar el cambio
def post(self, name, **kwargs): if PeopleModel.find_by_name(name): return { 'message': f"A person with name '{name}' already exists" }, 400 data = People.parser.parse_args() person = PeopleModel(name, **data) try: person.save_to_db() except Exception as e: return {"message": f"An error occurred: {e}"}, 500 return person.json(), 201
def post(self, name): #Si el usuario intenta crear un personaje ya existente en la base de datos, la solicitud del usuario es incorrecta. Hay que avisar al usuario, y devolver el codigo 400 (bad-request) if PeopleModel.find_by_name(name): return { 'message': 'El personaje ' + str(name) + ' ya existe.' }, 400 data = Character.parser.parse_args() character = PeopleModel(name, data['Id'], data['isAlive'], data['isKing'], data['placeId']) try: character.insert() return character.json() except: return { 'message': 'Error al intentar añadir el personaje a la base de datos' }, 500 #Http: Devolvemos un internal-server-error
#!/usr/bin/python import rospy from models.people import PeopleModel if __name__ == "__main__": try: rospy.init_node('model_people', anonymous=True) node = PeopleModel() rospy.spin() except rospy.ROSInterruptException: pass
def get(self): return {'people': [x.json() for x in PeopleModel.find_all()]}, 200
def delete(self, name): person = PeopleModel.find_by_name(name) if person: person.delete_from_db() return {'message': 'Person deleted.'} return {'message': 'Person not found.'}, 404
def get(self, name): person = PeopleModel.find_by_name(name) if person: return person.json() return {'message': 'Person not found'}, 404
def __init__(self, reader: AbstractReader): self.planet = PlanetsModel(reader) self.people = PeopleModel(reader) self.vehicle = VehicleModel(reader) self.specie = SpecieModel(reader) self.starship = StarshipModel(reader)