-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
202 lines (155 loc) · 7.41 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#Packages that are imported
from flask import (Flask, render_template, session, redirect, jsonify)
from flask_bootstrap import Bootstrap
from datetime import (date, datetime)
from form import (LoginForm, SignupForm, DogSignupForm, MealForm, MoodForm, ActivityForm, TrainingForm, GroomingForm, NoteForm, MedForm)
from flask_uploads import (configure_uploads, IMAGES, UploadSet)
from werkzeug.security import (generate_password_hash, check_password_hash)
from model import connect_to_db
import pytz
import crud
# -------------------------------------- #
"""ROUTES & SERVER"""
# Creating an instance of the flask server along with other instances that are needed for the app
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = 'ilovedogs'
# Configuring a folder for images
app.config['UPLOADS_DEFAULT_DEST'] = 'static/imgs/user_uploads'
images = UploadSet('images', IMAGES)
configure_uploads(app, (images,))
# Index page with about information and a link to log in or sign up
@app.route('/')
def index():
return render_template('index.html')
# Log in page
@app.route("/login", methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
owner = crud.get_owner_by_email(form.email.data)
if owner:
if check_password_hash(owner.password, form.password.data):
owners_dog = crud.get_dog_by_owner(owner.email)
session['owner_email'] = owner.email
session['dog_id'] = owners_dog.dog_id
return redirect('/dashboard')
return '<p>Invalid Email or Password</p>'
return render_template("login.html", form=form)
# sign up page for owner
@app.route("/signup", methods=['GET', 'POST'])
def signup():
form= SignupForm()
if form.validate_on_submit():
hashed_password = generate_password_hash(form.password.data, method='sha256')
new_owner = crud.create_owner(form.email.data, form.first_name.data, form.last_name.data, hashed_password)
session['owner_email'] = new_owner.email
return redirect('/pupsignup')
return render_template("signup.html", form=form)
# Puppy sign up form
@app.route("/pupsignup", methods=['GET', 'POST'])
def pupsignup():
form= DogSignupForm()
if form.validate_on_submit():
owner_email = session['owner_email']
new_dog = crud.create_dog(owner_email, form.dog_name.data, form.dog_bio.data, form.dog_img.data.filename)
image_save = images.save(form.dog_img.data)
session['dog_id'] = new_dog.dog_id
return redirect('/dashboard')
return render_template("pupsignup.html", form=form)
# Puppy dashboard page
@app.route("/dashboard", methods=['GET', 'POST'])
def dashboard():
# Create variables for all forms
meal_form = MealForm()
mood_form = MoodForm()
activity_form = ActivityForm()
training_form = TrainingForm()
grooming_form = GroomingForm()
note_form = NoteForm()
med_form = MedForm()
# Variables for dog_id, current date, and current time
dog_id = session['dog_id']
# Current date and Month to use for filtering Database
current_date = date.today().strftime("%m/%d/%y")
current_month = date.today().strftime("%m")
# Not Ideal but datetime does not offer taking time directly from device being used
central_time = pytz.timezone('America/Chicago')
current_time = datetime.now(central_time).strftime("%I:%M")
# Getting all data to render on dashboard
dog_info = crud.get_dog_by_dog_id(dog_id)
all_meals = crud.get_all_meals(dog_id, current_date)
all_moods = crud.get_all_moods(dog_id, current_date)
all_activities = crud.get_all_activities(dog_id, current_date)
all_trainings = crud.get_all_training_sessions(dog_id, current_date)
all_groomings = crud.get_all_grooming_sessions(dog_id, current_date)
all_notes = crud.get_all_notes(dog_id, current_date)
all_meds = crud.get_all_meds(dog_id, current_month)
# Creating a new meal from form
if meal_form.validate_on_submit():
new_meal = crud.create_meal(dog_id, meal_form.meal_type.data, current_date, current_month, current_time)
return redirect('/dashboard')
# Creating a new mood from form
if mood_form.validate_on_submit():
new_mood = crud.create_mood(dog_id, mood_form.mood_type.data, current_date, current_month, mood_form.mood_note.data)
return redirect('/dashboard')
# Creating a new activity from form
if activity_form.validate_on_submit():
new_activity = crud.create_activity(dog_id, activity_form.activity_type.data, current_date, current_month, current_time, activity_form.activity_duration.data, activity_form.activity_note.data)
return redirect('/dashboard')
# Creating a new training session from form
if training_form.validate_on_submit():
new_training = crud.create_training(dog_id, training_form.training_type.data, current_date, current_month, current_time, training_form.training_duration.data, training_form.training_note.data)
return redirect('/dashboard')
# Creating a new grooming session from form
if grooming_form.validate_on_submit():
new_grooming = crud.create_grooming(dog_id, grooming_form.grooming_type.data, current_date, current_month)
return redirect('/dashboard')
# Creating a new note from form
if note_form.validate_on_submit():
new_note = crud.create_note(dog_id, current_date, note_form.note.data)
return redirect('/dashboard')
# Create a new medication from form
if med_form.validate_on_submit():
new_med = crud.create_med(dog_id, med_form.med_type.data , current_month, med_form.med_note.data )
return redirect('/dashboard')
return render_template("dashboard.html", meal_form=meal_form, mood_form=mood_form, activity_form=activity_form, training_form=training_form, grooming_form=grooming_form, note_form=note_form, med_form=med_form, all_meals=all_meals, all_moods=all_moods, all_activities=all_activities, all_trainings=all_trainings, all_groomings=all_groomings, all_notes=all_notes, all_meds=all_meds, dog_info=dog_info)
# Data for Meal Chart
@app.route("/mealdata")
def mealdata():
dog_id = session['dog_id']
current_month = date.today().strftime("%m")
meal_amounts = crud.get_month_meals(dog_id, current_month)
return jsonify(meal_amounts)
# Data for Mood Chart
@app.route("/mooddata")
def mooddata():
dog_id = session['dog_id']
current_month = date.today().strftime("%m")
mood_amounts = crud.get_month_moods(dog_id, current_month)
return jsonify(mood_amounts)
# Data for Grooming Chart
@app.route("/groomdata")
def groomdata():
dog_id = session['dog_id']
current_month = date.today().strftime("%m")
groom_amounts = crud.get_month_grooms(dog_id, current_month)
return jsonify(groom_amounts)
# Data for Training Chart
@app.route("/trainingdata")
def trainingdata():
dog_id = session['dog_id']
current_month = date.today().strftime("%m")
training_amounts = crud.get_month_training(dog_id, current_month)
return jsonify(training_amounts)
# Data for Activity Chart
@app.route("/activitydata")
def activitydata():
dog_id = session['dog_id']
current_month = date.today().strftime("%m")
activity_amounts = crud.get_month_activity(dog_id, current_month)
return jsonify(activity_amounts)
#Running the flask app when name = main
if __name__ == "__main__":
connect_to_db(app)
app.run(debug=True, host="0.0.0.0")