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 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 detail(action, id): vet = vet_repository.select_id(id) if request.method == 'GET': if action == "show": return render_template("vets/show.html.j2", vet=vet) elif action == "edit": return render_template("vets/edit.html.j2", vet=vet) else: message = "Error Malformed URL" return redirect(url_for("vets.index", message=message)) if request.method == 'POST': if action == "delete": vet_repository.delete_id(request.form['id']) message = f"Vet: {vet.name} (id:{vet.id}) deleted" return redirect(url_for("vets.index", message=message)) elif action == "edit": name = request.form['name'] try: deactivated = request.form['deactivated'] if len(animal_repository.select_all_by_vet_id(id)) > 0: message = f"Vet: {vet.name} (id:{vet.id}) has animals assigned" return redirect(url_for("vets.index", message=message)) except: deactivated = False vet = Vet(name, deactivated, id) vet_repository.update(vet) message = f"Vet: {vet.name} (id:{vet.id}) updated" return redirect(url_for("vets.index", message=message)) elif action == "deactivate": if len(animal_repository.select_all_by_vet_id(id)) == 0: vet = vet_repository.select_id(id) vet.deactivated = True vet_repository.update(vet) message = f"Vet: {vet.name} (id:{vet.id}) has been deactivated" else: message = f"Vet: {vet.name} (id:{vet.id}) has animals assigned, reassign these first" return redirect(url_for("vets.index", message=message)) # elif action == "move_animals": #code to move all animals from one vet to another# else: message = "Error Malformed URL" return redirect(url_for("vets.index", 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