-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
223 lines (160 loc) · 7.89 KB
/
main.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import json
import os
import requests
from flask import Flask, render_template, request, redirect, url_for, make_response
from models import Contact, db
from requests_oauthlib import OAuth2Session
try:
import secrets
except ImportError as e:
pass
app = Flask(__name__)
db.create_all()
@app.route("/")
def index():
return render_template("index.html")
# LOGIN
@app.route("/github/login")
def github_login():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"))
authorization_url, state = github.authorization_url("https://github.com/login/oauth/authorize")
response = make_response(redirect(authorization_url))
response.set_cookie("oauth_state", state, httponly=True)
return response
@app.route("/github/callback")
def github_callback():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), state=request.cookies.get("oauth_state"))
token = github.fetch_token("https://github.com/login/oauth/access_token",
client_secret=os.environ.get("GITHUB_CLIENT_SECRET"),
authorization_response=request.url)
response = make_response(redirect(url_for("my_contacts")))
response.set_cookie("oauth_token", json.dumps(token), httponly=True)
return response
# LOGOUT
@app.route("/github/logout")
def logout():
response = make_response(redirect(url_for("index")))
response.set_cookie("oauth_token", expires=0)
return response
# VIEW USER'S CONTACT LIST
@app.route("/my_contacts", methods=["GET"])
def my_contacts():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
user_name = github_profile_data.get("login")
# show all contacts belonging to that user based on their username
contacts = db.query(Contact).filter_by(contact_user_name=user_name).all()
return render_template("my_contacts.html", github_profile_data=github_profile_data, user_name=user_name, contacts=contacts)
# INPUT NEW CONTACT DATA
@app.route("/add_contact", methods=["GET", "POST"])
def add_contact():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
return render_template("add_contact.html", github_profile_data=github_profile_data)
# SAVE NEW CONTACT INTO DATABASE
@app.route("/store_contact", methods=["GET", "POST"])
def store_contact():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
user_name = github_profile_data.get("login")
# get user and new contact data
contact_user_name = user_name
contact_first_name = request.form.get("contact_first_name")
contact_last_name = request.form.get("contact_last_name")
contact_email = request.form.get("contact_email")
contact_telephone = request.form.get("contact_telephone")
contact_address = request.form.get("contact_address")
contact_city = request.form.get("contact_city")
contact_zipcode = request.form.get("contact_zipcode")
contact_country = request.form.get("contact_country")
# create a contact object
contact = Contact(contact_user_name=contact_user_name,
contact_first_name=contact_first_name,
contact_last_name=contact_last_name,
contact_email=contact_email,
contact_telephone=contact_telephone,
contact_address=contact_address,
contact_city=contact_city,
contact_zipcode=contact_zipcode,
contact_country=contact_country)
# save the contact object into a database
db.add(contact)
db.commit()
return redirect(url_for("success"))
# VIEW DETAILS FOR INDIVIDUAL CONTACT & API SETUP
@app.route("/contact/<contact_id>", methods=["GET"])
def contact_details(contact_id):
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
contact = db.query(Contact).get(int(contact_id)) # query by id
# OPENWEATHER.ORG API SETUP
query = contact.contact_city
unit = "metric"
api_key = os.environ.get("API_KEY_OPENWEATHER")
url = "https://api.openweathermap.org/data/2.5/weather?q={0}&units={1}&appid={2}".format(query, unit, api_key)
data = requests.get(url=url)
return render_template("contact_details.html", contact=contact, github_profile_data=github_profile_data, data=data.json())
# EDIT CONTACT DETAILS
@app.route("/contact/edit/<contact_id>", methods=["GET", "POST"])
def contact_edit(contact_id):
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
contact = db.query(Contact).get(int(contact_id)) # query by id
if request.method == "GET":
if contact:
return render_template("contact_edit.html", contact=contact, github_profile_data=github_profile_data)
else:
return redirect(url_for("my_contacts"))
elif request.method == "POST":
contact_first_name = request.form.get("contact_first_name")
contact_last_name = request.form.get("contact_last_name")
contact_email = request.form.get("contact_email")
contact_telephone = request.form.get("contact_telephone")
contact_address = request.form.get("contact_address")
contact_city = request.form.get("contact_city")
contact_zipcode = request.form.get("contact_zipcode")
contact_country = request.form.get("contact_country")
# update the contact object
contact.contact_first_name = contact_first_name
contact.contact_last_name = contact_last_name
contact.contact_email = contact_email
contact.contact_telephone = contact_telephone
contact.contact_address = contact_address
contact.contact_city = contact_city
contact.contact_zipcode = contact_zipcode
contact.contact_country = contact_country
# store changes into the database
db.add(contact)
db.commit()
return redirect(url_for("success"))
# DELETE ENTIRE CONTACT
@app.route("/contact/delete/<contact_id>", methods=["GET", "POST"])
def contact_delete(contact_id):
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
contact = db.query(Contact).get(int(contact_id)) # query by id
if request.method == "GET":
if contact: # if contact is found
return render_template("contact_delete.html", contact=contact, github_profile_data=github_profile_data)
else:
return redirect(url_for("my_contacts"))
elif request.method == "POST":
# delete the contact in the database
db.delete(contact)
db.commit()
return redirect(url_for("success"))
# SUCCESS MESSAGE FOR ADDING, EDITING AND DELETING CONTACTS
@app.route("/success")
def success():
return render_template("success.html")
# SOCIAL DISTANCING CORNER (NASA API SETUP)
@app.route("/faraway")
def faraway():
github = OAuth2Session(os.environ.get("GITHUB_CLIENT_ID"), token=json.loads(request.cookies.get("oauth_token")))
github_profile_data = github.get("https://api.github.com/user").json()
api_key = os.environ.get("API_KEY_NASA_APOD")
url = "https://api.nasa.gov/planetary/apod?api_key={0}".format(api_key)
data = requests.get(url=url)
return render_template("faraway.html", github_profile_data=github_profile_data, data=data.json())
if __name__ == '__main__':
app.run()