forked from reneenoble/code-to-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (114 loc) · 5.35 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
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
from flask import Flask, request, render_template
import json, os
from azure.storage.blob import BlobServiceClient
from azure.core.exceptions import ResourceExistsError, ResourceNotFoundError
# Create the Flask app
app = Flask('app')
############################################################################################################
# Use dotenv when you want to run this locally and get your environement variables out of the .env file
############################################################################################################
from dotenv import load_dotenv
load_dotenv(".env")
############################################################################################################
# Make Containter
connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
# Create a local directory to hold RSVP files
local_path = "./data"
os.makedirs("data", exist_ok=True)
# Create a unique name for the container
container_name = "rsvps-live"
# Create the container in Azure blob storage
try:
container_client = blob_service_client.create_container(container_name)
except ResourceExistsError:
container_client = blob_service_client.get_container_client(container_name)
################################################################################
#### Routes ####################################################################
# The route where you see the form to create an invite
@app.route("/")
def form():
return render_template("form.html")
# The route where you see the invite
@app.route("/view")
def view_invite():
# to = "Sarah"
# event = "Artemis' birthday party"
# date = "February 10th"
# time = "4am"
# sender = "Jack"
to = request.args.get("to")
event = request.args.get("event")
date = request.args.get("date")
time = request.args.get("time")
sender = request.args.get("sender")
style = request.args.get('style')
eventId = sender + "|" + event
template = "invite-" + style + ".html"
return render_template(template, to=to, event_name=event, date=date, time=time, sender=sender, eventId=eventId)
# The route that lists all the files that exist in the storage account (all the events that have RSVPs)
@app.route("/events")
def events():
blob_list = container_client.list_blobs()
blob_list = [b.name for b in blob_list]
return render_template("events.html", event_list=blob_list)
# The route that lists all the RSVPs for a specific event
@app.route("/event-rsvps/<event_file>")
def event_rsvps(event_file):
blob_client = blob_service_client.get_blob_client(container=container_name, blob=event_file)
attendees = str(blob_client.download_blob().readall(), "utf-8")
attendees = attendees.split("\n")
print(attendees)
return render_template("event-rsvps.html", attendees=attendees)
# The route that handles the RSVP button from the invitaiton page
@app.route("/rsvp", methods=('GET', 'POST'))
def rsvp():
data = request.json
event_data = data["event-rsvp"].split(",")
attendee = event_data[0]
event_ID = event_data[1]
print("RSVP, conect str", connect_str)
# Attempt to sync the blob
try:
sync_blob(event_ID, attendee)
print("Blob sync succeeded")
return json.dumps({'success':True}), 200, {'ContentType':'application/json'}
except:
print("Blob sync failed")
return json.dumps({'success':True}), 400, {'ContentType':'application/json'}
################################################################################
################################################################################
# A helper function that handles updating or creating a blob in Azure blob storage
def sync_blob(event_ID, attendee):
filename = event_ID + ".txt"
local_file = os.path.join(local_path, filename)
print(local_file)
blob_client = blob_service_client.get_blob_client(container=container_name, blob=filename)
# Try to download the blob, if it exists. Then update the file with the new attendee
try:
print("\nDownloading blob to \n\t" + local_file)
# Get the data out of the file, split it into a list of people who have already RSVPed
attendees = str(blob_client.download_blob().readall(), "utf-8")
attendees = attendees.split("\n")
if attendee not in attendees:
attendees.append(attendee)
# Make the local file
with open(local_file, "w") as download_file:
download_file.writelines("\n".join(attendees))
# Push the local file to Azure blob
with open(local_file, "rb") as data:
blob_client.delete_blob()
blob_client.upload_blob(data)
print(f"File ({filename}) updated on blob.")
# The blob wasn't there, oh no! Let's make a new one, this must be the first RSVP for this event
except ResourceNotFoundError:
# If the file doesn't exist, make a local file, conatining the single attendee from the request
with open(local_file, "w") as f:
f.write(attendee)
# Write the file to Azure blob
with open(local_file, "rb") as data:
blob_client.upload_blob(data)
print("New file uploaded to blob.")
# Run the app
if __name__ == '__main__':
app.run(debug=True, use_reloader=True, host='0.0.0.0', port=8000)