Coverage for modules/box__bizhelp/appointment/view.py : 46%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import json
2import os
4from flask import Blueprint
5from flask import jsonify
6from flask import redirect
7from flask import render_template
8from flask import request
9from flask_login import login_required
10from init import db
11from init import ma
13from .models import Appointments
15dirpath = os.path.dirname(os.path.abspath(__file__))
16module_info = {}
18with open(dirpath + "/info.json") as f:
19 module_info = json.load(f)
21appointment_blueprint = Blueprint(
22 "appointment",
23 __name__,
24 template_folder="templates",
25 url_prefix=module_info["url_prefix"],
26)
29class AppointmentSchema(ma.Schema):
30 class Meta:
31 # Fields to expose
32 fields = ("id", "name", "date", "time", "active")
35appointment_schema = AppointmentSchema()
36appointment_schema = AppointmentSchema(many=True)
39@appointment_blueprint.route("/")
40@login_required
41def index():
42 context = {}
44 context["appointments"] = Appointments.query.all()
45 return render_template("appointment/index.html", **context)
48@appointment_blueprint.route("/add", methods=["GET", "POST"])
49@login_required
50def add():
51 context = {}
53 if request.method == "POST":
54 name = request.form["name"]
55 date = request.form["date"]
56 active = request.form["active"]
57 time = request.form["time"]
58 m = Appointments(name=name, date=date, time=time, active=active)
59 db.session.add(m)
60 db.session.commit()
61 return redirect("/appointment/add")
62 return render_template("appointment/add.html", **context)
65@appointment_blueprint.route("/delete/<ids>", methods=["GET", "POST"])
66@login_required
67def appointment_delete(ids):
68 Appointments.query.filter(Appointments.id == ids).delete()
69 db.session.commit()
70 return redirect("/appointment")
73@appointment_blueprint.route("/edit/<ids>", methods=["GET", "POST"])
74@login_required
75def appointment_edit(ids):
76 context = {}
78 a = Appointments.query.get(ids)
79 context["id"] = a.id
80 context["name"] = a.name
81 context["date"] = a.date
82 context["time"] = a.time
83 context["active"] = a.active
84 return render_template("appointment/edit.html", **context)
87@appointment_blueprint.route("/update", methods=["GET", "POST"])
88@login_required
89def appointment_update():
90 appointment_name = request.form["appointment_name"]
91 appointment_date = request.form["appointment_date"]
92 appointment_time = request.form["appointment_time"]
93 appointment_id = request.form["appointment_id"]
94 appointment_active = request.form["appointment_active"]
95 s = Appointments.query.get(appointment_id)
96 s.name = appointment_name
97 s.date = appointment_date
98 s.time = appointment_time
99 s.active = appointment_active
100 db.session.commit()
101 return redirect("/appointment")
104@appointment_blueprint.route("/active/<ids>", methods=["GET", "POST"])
105@login_required
106def active(ids):
107 s = Appointments.query.get(ids)
108 s.active = "active"
109 db.session.commit()
110 return redirect("/appointment")
113@appointment_blueprint.route("/inactive/<ids>", methods=["GET", "POST"])
114@login_required
115def deactive(ids):
116 s = Appointments.query.get(ids)
117 s.active = "inactive"
118 db.session.commit()
119 return redirect("/appointment")
122@appointment_blueprint.route("/lookup", methods=["GET", "POST"])
123@login_required
124def lookup():
125 context = {}
126 context["appointments"] = Appointments.query.all()
127 return render_template("appointment/lookup.html", **context)
130# api
131@appointment_blueprint.route("/search/name/<name>", methods=["GET", "POST"])
132@login_required
133def search_name(name):
134 if name == "searchValueIsEmpty":
135 all_a = Appointments.query.all()
136 else:
137 all_a = Appointments.query.filter(
138 Appointments.name.like("%" + name + "%")
139 ).all()
140 result = appointment_schema.dump(all_a)
141 return jsonify(result)