def detail(action, id): if request.method == 'GET': animals = animal_repository.select_all_by_owner_id(id) owner = owner_repository.select_id(id) len_animals = len(animal_repository.select_all_by_owner_id(id)) if action == "show": return render_template("owners/show.html.j2", owner=owner, len_animals=len_animals) elif action == "edit": return render_template("owners/edit.html.j2", owner=owner) elif action == "animals": return render_template("owners/detail.html.j2", owner=owner, len_animals=len_animals, animals=animals) elif action == "orphaned": animals = animal_repository.select_all_by_owner_id(1) active_owners = owner_repository.select_all_active() return render_template("owners/orphaned.html.j2", active_owners=active_owners, owner=owner, len_animals=len_animals, animals=animals) else: message = "Malformed URL" return redirect(url_for("owners.index", message=message)) if request.method == 'POST': if action == "delete": owner_repository.delete_id(request.form['id']) message = f"Owner: {owner.name} (id:{owner.id}) deleted" return redirect(url_for("owners.index", message=message)) elif action == "edit": name = request.form['name'] address = request.form['address'] try: deactivated = request.form['deactivated'] except: deactivated = False id = request.form['id'] owner = Owner(name, address, deactivated, id) owner_repository.update(owner) message = f"Owner: {owner.name} (id:{owner.id}) updated" return redirect(url_for("owners.index", message=message)) elif action == "deactivate": owner = owner_repository.select_id(id) animals = animal_repository.select_all_by_owner_id(id) for animal in animals: animal.owner = owner_repository.select_id(1) animal_repository.update(animal) owner.deactivated = True owner_repository.update(owner) message = f"All animals moved to No Owner, Owner {owner.name} deactivated, please reassign" return redirect(url_for("owners.index", message=message)) else: message = "Malformed URL" return redirect(url_for("owners.index", message=message))
def detail(action, id): animal = animal_repository.select_id(id) len_treatments = len(treatment_repository.select_all_by_animal_id(id)) if request.method == 'GET': if action == "show": return render_template("animals/show.html.j2", animal=animal, len_treatments=len_treatments) elif action == "edit": active_vets = vet_repository.select_all_active() return render_template("animals/edit.html.j2", animal=animal, active_vets=active_vets) elif action == "treatments": treatments = treatment_repository.select_all_by_animal_id(id) return render_template("animals/detail.html.j2", animal=animal, len_treatments=len_treatments, treatments=treatments) else: message = "Malformed URL" return redirect(url_for("animals.index", message=message)) if request.method == 'POST': if action == "delete": animal_repository.delete_id(request.form['id']) message = f"Animal: {animal.name} (id:{animal.id}) deleted" return redirect(url_for("animals.index", message=message)) elif action == "edit": name = request.form['name'] dob = request.form['dob'] species = request.form['species'] owner_id = request.form['owner_id'] vet_id = request.form['vet_id'] try: deactivated = request.form['deactivated'] except: deactivated = False id = request.form['id'] vet = vet_repository.select_id(vet_id) owner = owner_repository.select_id(owner_id) animal = Animal(name, dob, species, owner, vet, deactivated, id) animal_repository.update(animal) message = f"Animal: {animal.name} (id:{animal.id}) updated" return redirect(url_for("animals.index", message=message)) elif action == "deactivate": animal = animal_repository.select_id(id) animal.owner = owner_repository.select_id(1) animal.deactivated = True animal_repository.update(animal) message = f"{animal.name} moved to No Owner, please reassign" # I want an if statment to make thsi work too: return redirect(url_for("owners.index", message=message)) return redirect(url_for("animals.index", message=message)) else: message = "Malformed URL" return redirect(url_for("animals.index", message=message))
def new(): active_vets = vet_repository.select_all_active() if request.method == 'GET': return render_template("animals/new.html.j2", active_vets=active_vets) if request.method == 'POST': name = request.form['name'] dob = request.form['dob'] species = request.form['species'] owner = owner_repository.select_id(1) vet_id = request.form['vet_id'] deactivated = False vet = vet_repository.select_id(vet_id) animal = Animal(name, dob, species, owner, vet, deactivated) saved_animal = animal_repository.save(animal) if saved_animal.id != None: message = f"Success {saved_animal.name} added" else: message = "Failure" if request.form['action'] == "finish": return redirect(url_for("animals.index", message=message)) elif request.form['action'] == "continue": return render_template("animals/new.html.j2", message=message, active_vets=active_vets) else: # POST Error 405 Method Not Allowed print("POST Error 405 Method Not Allowed")
def settings(): if request.method == 'GET': return render_template('settings.html.j2') if request.method == 'POST': if request.form['action'] == "delete_id": id = request.form['id'] if request.form['table'] == "vet": vet = vet_repository.select_id(id) vet_repository.delete_id(id) message = f"Vet {vet.name} {vet.id} deleted, and cascaded" elif request.form['table'] == "owner": owner = owner_repository.select_id(id) owner_repository.delete_id(id) message = f"Owner {owner.name} {owner.id} deleted, and cascaded" elif request.form['table'] == "animal": animal = animal_repository.select_id(id) animal_repository.delete_id(id) message = f"Vet {animal.name} {animal.id} deleted, and cascaded" elif request.form['action'] == "delete_all": if request.form['table'] == "vet": vet_repository.delete_all() message = "All Vets deleted, and cascaded" elif request.form['table'] == "owner": owner_repository.delete_all() message = f"All Owners deleted, and cascaded" elif request.form['table'] == "animal": animal_repository.delete_all() message = f"All Animals deleted, and cascaded" return render_template('settings.html.j2', message=message)
def select_id(id): animal = None sql = "SELECT * FROM animals WHERE id = %s" values = [id] result = run_sql(sql, values)[0] if result is not None: vet = vet_repository.select_id(result['vet_id']) owner = owner_repository.select_id(result['owner']) animal = Animal(result['name'], result['dob'], result['species'], owner, vet, result['deactivated'], result['id']) return animal
def select_all(): animals = [] sql = "SELECT * FROM animals" results = run_sql(sql) for row in results: vet = vet_repository.select_id(row['vet_id']) owner = owner_repository.select_id(row['owner']) animal = Animal(row['name'], row['dob'], row['species'], owner, vet, row['deactivated'], row['id']) animals.append(animal) return animals
def select_all_by_vet_id(id): animals = [] sql = "SELECT * FROM animals WHERE vet_id = %s" values = [id] results = run_sql(sql, values) for row in results: vet = vet_repository.select_id(row['vet_id']) owner = owner_repository.select_id(row['owner']) animal = Animal(row['name'], row['dob'], row['species'], owner, vet, row['deactivated'], row['id']) animals.append(animal) return animals