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

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

## 101 statements

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

1 import json

2 import os

3

4 from flask import Blueprint

5 from flask import jsonify

6 from flask import redirect

7 from flask import render_template

8 from flask import request

9 from flask_login import login_required

10 from init import db

11 from init import ma

12

13 from .models import Appointments

14

15 dirpath = os.path.dirname(os.path.abspath(__file__))

16 module_info = {}

17

18 with open(dirpath + "/info.json") as f:

19 module_info = json.load(f)

20

21 appointment_blueprint = Blueprint(

22 "appointment",

23 __name__,

24 template_folder="templates",

25 url_prefix=module_info["url_prefix"],

26)

27

28

29 class AppointmentSchema(ma.Schema):

30 class Meta:

31 # Fields to expose

32 fields = ("id", "name", "date", "time", "active")

33

34

35 appointment_schema = AppointmentSchema()

36 appointment_schema = AppointmentSchema(many=True)

37

38

39@appointment_blueprint.route("/")

40@login_required

41 def index():

42 context = {}

43

44 context["appointments"] = Appointments.query.all()

45 return render_template("appointment/index.html", **context)

46

47

48@appointment_blueprint.route("/add", methods=["GET", "POST"])

49@login_required

50 def add():

51 context = {}

52

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)

63

64

65@appointment_blueprint.route("/delete/<ids>", methods=["GET", "POST"])

66@login_required

67 def appointment_delete(ids):

68 Appointments.query.filter(Appointments.id == ids).delete()

69 db.session.commit()

70 return redirect("/appointment")

71

72

73@appointment_blueprint.route("/edit/<ids>", methods=["GET", "POST"])

74@login_required

75 def appointment_edit(ids):

76 context = {}

77

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)

85

86

87@appointment_blueprint.route("/update", methods=["GET", "POST"])

88@login_required

89 def 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")

102

103

104@appointment_blueprint.route("/active/<ids>", methods=["GET", "POST"])

105@login_required

106 def active(ids):

107 s = Appointments.query.get(ids)

108 s.active = "active"

109 db.session.commit()

110 return redirect("/appointment")

111

112

113@appointment_blueprint.route("/inactive/<ids>", methods=["GET", "POST"])

114@login_required

115 def deactive(ids):

116 s = Appointments.query.get(ids)

117 s.active = "inactive"

118 db.session.commit()

119 return redirect("/appointment")

120

121

122@appointment_blueprint.route("/lookup", methods=["GET", "POST"])

123@login_required

124 def lookup():

125 context = {}

126 context["appointments"] = Appointments.query.all()

127 return render_template("appointment/lookup.html", **context)

128

129

130# api

131@appointment_blueprint.route("/search/name/<name>", methods=["GET", "POST"])

132@login_required

133 def 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)