This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
98 lines (76 loc) · 2.87 KB
/
app.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
from flask import Flask, request, json, Response, redirect, url_for, render_template, flash
import pymongo
import os
import datetime
# start Flask app
app = Flask(__name__)
app.secret_key = 'geniosity'
# connect to online MongoDB Atlas database
mongo = pymongo.MongoClient('mongodb+srv://admin:[email protected]/test?retryWrites=true', maxPoolSize=50, connect=False)
db = pymongo.database.Database(mongo, 'FBLA')
student = pymongo.collection.Collection(db, 'Users')
book = pymongo.collection.Collection(db, 'Books')
history = pymongo.collection.Collection(db, 'History')
# home page
@app.route('/')
def index():
return render_template('index.html', book=book.count(), student=student.count())
# checking out + tracking history
@app.route('/checkout', methods=['GET', 'POST'])
def checkout():
# if form is accessed
if request.method == 'POST':
print(request.form)
# access the form data
book, student = int(request.form.get('id')), int(request.form.get('student'))
# make the document for MongoDB and insert
profile = {
'book': book,
'student': student,
'checkedout': datetime.datetime.utcnow()
}
history.insert_one(profile)
# confirmation message
flash(f'{book} checked out by {student} successfully.')
# render
return render_template('checkout.html', entries=history.find(
{'checkedout': {
'$gte': datetime.datetime.utcnow() - datetime.timedelta(days=7),
'$lt': datetime.datetime.utcnow()
}}
))
# interface for managing students
@app.route('/students', methods=['GET', 'POST'])
def students():
# if form is accessed
if request.method == 'POST':
# access the form data
name, grade = request.form.get('name'), int(request.form.get('grade'))
# make the document for MongoDB and insert
profile = {
'name': name,
'grade': grade,
'added': datetime.datetime.utcnow()
}
student.insert_one(profile)
# confirmation message
flash(f'{name} in grade {grade} successfully added to database.')
# render
return render_template('students.html', entries=student.find({}))
@app.route('/books')
def books():
# if form is accessed
if request.method == 'POST':
# make the document for MongoDB and insert
profile = {
'title': request.form.get('title'),
'isbn': request.form.get('isbn'),
'code': request.form.get('code'),
'checkedout': request.form.get('checkedout'),
'last': datetime.datetime.utcnow()
}
book.insert_one(profile)
# confirmation message
flash(f'{title} (code {code}) successfully added to database.')
# render
return render_template('books.html', entries=book.find({}))