# Coverage for modules/box__bizhelp/people/view.py : 29%

## 140 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 datetime

2 import json

3 import os

4

5 from flask import Blueprint

6 from flask import jsonify

7 from flask import redirect

8 from flask import render_template

9 from flask import request

10 from flask_login import login_required

11 from init import db

12 from init import ma

13

14 from .models import People

15

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

17 module_info = {}

18

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

20 module_info = json.load(f)

21

22 people_blueprint = Blueprint(

23 "people",

24 __name__,

25 template_folder="templates",

26 url_prefix=module_info["url_prefix"],

27)

28

29

30 class PeopleSchema(ma.Schema):

31 class Meta:

32 # Fields to expose

33 fields = (

34 "id",

35 "name",

36 "phone",

37 "mobile",

38 "email",

39 "facebook",

40 "twitter",

41 "linkedin",

42 "age",

43 "birthday",

44 "notes",

45 "is_manufacturer",

46 "manufacturer_name",

47 "manufacturer_phone",

48 "manufacturer_address",

49 )

50

51

52 people_schema = PeopleSchema()

53 people_schema = PeopleSchema(many=True)

54

55

56@people_blueprint.route("/")

57@login_required

58 def index():

59 context = {}

60

61 context["people"] = People.query.all()

62 return render_template("people/index.html", **context)

63

64

65@people_blueprint.route("/add", methods=["GET", "POST"])

66@login_required

67 def people_add():

68 context = {}

69

70 if request.method == "POST":

71 name = request.form["name"]

72 phone = request.form["phone"]

73 mobile = request.form["mobile"]

74 email = request.form["email"]

75 linkedin = request.form["linkedin"]

76 facebook = request.form["facebook"]

77 twitter = request.form["twitter"]

78 birthday = request.form["birthday"]

79 notes = request.form["notes"]

80 is_manufacturer = request.form.get("is_manufacturer", False)

81 manufacturer_name = request.form.get("manufacturer_name", "")

82 manufacturer_phone = request.form.get("manufacturer_phone", None)

83 manufacturer_address = request.form.get("manufacturer_address", "")

84

85 # check if is_manufacturer is true

86 if is_manufacturer == "on":

87 is_manufacturer = True

88

89 # calculate age

90 today_date = datetime.date.today()

91 date_format = "%Y-%m-%d"

92 b_day = datetime.datetime.strptime(birthday, date_format)

93 age = (

94 today_date.year

95 - b_day.year

96 - ((today_date.month, today_date.day) < (b_day.month, b_day.day))

97 )

98

99 # insert data into DB

100 person = People(

101 name=name,

102 phone=phone,

103 mobile=mobile,

104 email=email,

105 linkedin=linkedin,

106 facebook=facebook,

107 twitter=twitter,

108 age=age,

109 birthday=birthday,

110 notes=notes,

111 is_manufacturer=is_manufacturer,

112 manufacturer_name=manufacturer_name,

113 manufacturer_phone=manufacturer_phone,

114 manufacturer_address=manufacturer_address,

115 )

116 db.session.add(person)

117 db.session.commit()

118 return redirect("/people/add")

119 context["message"] = ""

120 return render_template("people/add.html", **context)

121

122

123@people_blueprint.route("/delete/<id>", methods=["GET", "POST"])

124@login_required

125 def people_delete(id):

126 People.query.filter(People.id == id).delete()

127 db.session.commit()

128 return redirect("/people")

129

130

131@people_blueprint.route("/edit/<id>", methods=["GET", "POST"])

132@login_required

133 def people_edit(id):

134 context = {}

135

136 a = People.query.get(id)

137

138 context["id"] = a.id

139 context["name"] = a.name

140 context["phone"] = a.phone

141 context["mobile"] = a.mobile

142 context["email"] = a.email

143 context["linkedin"] = a.linkedin

144 context["facebook"] = a.facebook

145 context["twitter"] = a.twitter

146 context["age"] = a.age

147 context["birthday"] = a.birthday

148 context["notes"] = a.notes

149 context["is_manufacturer"] = a.is_manufacturer

150 context["manufacturer_name"] = a.manufacturer_name

151 context["manufacturer_phone"] = a.manufacturer_phone

152 context["manufacturer_address"] = a.manufacturer_address

153 return render_template("people/edit.html", **context)

154

155

156@people_blueprint.route("/update", methods=["GET", "POST"])

157@login_required

158 def people_update():

159 if request.method == "POST":

160 people_id = request.form["id"]

161 people_name = request.form["name"]

162 people_phone = request.form["phone"]

163 people_mobile = request.form["mobile"]

164 people_email = request.form["email"]

165 people_linkedin = request.form["linkedin"]

166 people_facebook = request.form["facebook"]

167 people_twitter = request.form["twitter"]

168 people_birthday = request.form["birthday"]

169 people_notes = request.form["notes"]

170 people_is_manufacturer = request.form.get("is_manufacturer", False)

171 people_manufacturer_name = request.form.get("manufacturer_name", "")

172 people_manufacturer_phone = request.form.get

173 ("manufacturer_phone", None)

174 people_manufacturer_address = request.form.get

175 ("manufacturer_address", "")

176

177 # check if is_manufacturer is true

178 if people_is_manufacturer == "on":

179 people_is_manufacturer = True

180

181 # calculate age

182 today_date = datetime.datetime.now()

183 time_format = "%Y-%m-%d"

184 b_day = datetime.datetime.strptime(people_birthday, time_format)

185 people_age = str(today_date - b_day)

186 # retrive record from db with id

187 s = People.query.get(people_id)

188 s.name = people_name

189 s.phone = people_phone

190 s.mobile = people_mobile

191 s.email = people_email

192 s.facebook = people_facebook

193 s.linkedin = people_linkedin

194 s.twitter = people_twitter

195 s.birthday = people_birthday

196 s.notes = people_notes

197 s.age = people_age

198 s.is_manufacturer = people_is_manufacturer

199 s.manufacturer_name = people_manufacturer_name

200 s.manufacturer_phone = people_manufacturer_phone

201 s.manufacturer_address = people_manufacturer_address

202 db.session.commit()

203

204 return redirect("/people")

205

206

207@people_blueprint.route("/lookup", methods=["GET", "POST"])

208@login_required

209 def lookup():

210 context = {}

211 context["people"] = People.query.all()

212 return render_template("people/lookup.html", **context)

213

214

215# api

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

217@login_required

218 def search_name(name):

219 if name == "searchValueIsEmpty":

220 all_a = People.query.all()

221 else:

222 all_a = People.query.filter(People.name.like("%" + name + "%")).all()

223 result = people_schema.dump(all_a)

224 return jsonify(result)