generated from nyu-software-engineering/web-app-exercise
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
115 lines (88 loc) · 2.96 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import pymongo
from flask import Flask, request, render_template, redirect
import os
app = Flask(__name__)
# Connecting to local host
# connection = pymongo.MongoClient("mongodb://localhost:27017")
# db = connection["test_database"]
# connect to the database
password = os.getenv("MONGO_INITDB_ROOT_PASSWORD")
connection = pymongo.MongoClient(
os.getenv(
"mongodb+srv://last-team:"
+ str(password)
+ "@cluster0.m5t5gvu.mongodb.net/?retryWrites=true&w=majority"
),
serverSelectionTimeoutMS=5000,
)
try:
db = connection["music_app"]
except Exception as e:
# the ping command failed, so the connection is not available.
print(
" *",
"Failed to connect to MongoDB at",
os.getenv(
"mongodb+srv://last-team:"
+ str(password)
+ "@cluster0.m5t5gvu.mongodb.net/?retryWrites=true&w=majority"
),
)
print("Database connection error:", e) # debug
# Views
@app.route("/")
@app.route("/<user_name>")
def display_songs(user_name=None):
song_list = None
if user_name:
song_list = db.songs.find({"user_name": user_name})
else:
song_list = db.songs.find({})
return render_template("home.html", song_list=song_list)
@app.route("/add-songs")
def add_songs():
return render_template("addSongs.html")
@app.route("/delete-songs")
def delete_songs():
return render_template("deleteSongs.html")
@app.route("/search-songs")
def search_songs():
return render_template("searchSongs.html")
@app.route("/edit-songs")
def edit_songs():
return render_template("editSongs.html")
# Form methods
@app.route("/api/add-songs", methods=["POST"])
def addSongs():
user_name = request.form["user_name"]
song_name = request.form["song_name"]
db.songs.insert_one({"user_name": user_name, "song_name": song_name})
return redirect("/")
@app.route("/api/delete-songs", methods=["POST"])
def deleteSongs():
user_name = request.form["user_name"]
song_name = request.form["song_name"]
db.songs.delete_one({"user_name": user_name, "song_name": song_name})
return redirect("/")
@app.route("/api/search-songs", methods=["POST"])
def searchSongs():
user_name = request.form["user_name"]
return redirect("/" + user_name)
@app.route("/api/edit-songs", methods=["POST"])
def editSongs():
user_name = request.form["user_name"]
song_name = request.form["song_name"]
updated_user_name = request.form["updated_user_name"]
updated_song_name = request.form["updated_song_name"]
db.songs.update_one(
{"user_name": user_name, "song_name": song_name},
{"$set": {"user_name": updated_user_name, "song_name": updated_song_name}},
)
return redirect("/")
if __name__ == "__main__":
PORT = os.getenv(
"PORT", 5000
) # use the PORT environment variable, or default to 5000
# import logging
# logging.basicConfig(filename='/home/ak8257/error.log',level=logging.DEBUG)
app.run(port=PORT)