-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
237 lines (199 loc) · 8.77 KB
/
database.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from flask_cors import CORS
import os
import hashlib
import json
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
CORS(app)
#app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:p@localhost:5432/tigernest"
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://localhost/tigernest"
db = SQLAlchemy(app)
ma = Marshmallow(app)
#------------------------------------------------------------------------------------------
class EventOrganizer(db.Model):
event_organizer_id = db.Column(db.Integer, primary_key = True)
netid = db.Column(db.Unicode, unique = True)
name = db.Column(db.Unicode, unique = False)
email = db.Column(db.Unicode, unique = False)
campus_organizations = db.Column(db.JSON, unique = False)
def __init__(self, netid, name, email, campus_organizations):
self.netid = netid
self.name = name
self.email = email
self.campus_organizations = campus_organizations
class EventOrganizerSchema(ma.Schema):
class Meta:
fields = ('event_organizer_id', 'netid', 'name', 'email', 'campus_organizations')
event_organizer_schema = EventOrganizerSchema()
event_organizers_schema = EventOrganizerSchema(many = True)
# Endpoint to add new event organizer
@app.route("/event_organizer", methods=["POST"])
def event_organizer_add():
netid = request.json['netid']
name = request.json['name']
email = request.json['email']
campus_organizations = request.json['campus_organizations']
new_event_organizer = EventOrganizer(netid, name, email, campus_organizations)
db.session.add(new_event_organizer)
db.session.commit()
return event_organizer_schema.jsonify(new_event_organizer)
# Endpoint to retrieve existing event organizer from the database
@app.route("/event_organizer/<event_organizer_id>", methods=["GET"])
def event_organizer_get(event_organizer_id):
event_organizer = EventOrganizer.query.get(event_organizer_id)
return event_organizer_schema.jsonify(event_organizer)
#------------------------------------------------------------------------------------------
class Event(db.Model):
event_id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.Unicode, unique = False)
start_date = db.Column(db.Unicode, unique = False)
start_time = db.Column(db.Unicode, unique = False)
end_date = db.Column(db.Unicode, unique = False)
end_time = db.Column(db.Unicode, unique = False)
description = db.Column(db.Unicode, unique = False)
location = db.Column(db.Unicode, unique = False)
time = db.Column(db.Unicode, unique = False)
expected_number_visitors = db.Column(db.Integer, unique = False)
number_of_hosts = db.Column(db.Integer, unique = False)
hosts = db.Column(db.JSON, unique = False)
hosting_organization = db.Column(db.Unicode, unique=False)
def __init__(self, name, start_date, end_date, location, time, expected_number_visitors, number_of_hosts, hosts, start_time, end_time, description, hosting_organization):
self.name = name
self.start_date = start_date
self.end_date = end_date
self.location = location
self.time = time
self.expected_number_visitors = expected_number_visitors
self.number_of_hosts = number_of_hosts
self.hosts = hosts
self.start_time = start_time
self.end_time = end_time
self.description = description
self.hosting_organization = hosting_organization
class EventSchema(ma.Schema):
class Meta:
fields = ('event_id', 'name', 'start_date', 'end_date', 'location', 'time', 'expected_number_visitors', 'number_of_hosts', 'hosts', 'start_time', 'end_time', 'description', 'hosting_organization')
event_schema = EventSchema()
events_schema = EventSchema(many = True)
@app.route("/event", methods=["POST"])
def event_add():
name = request.json['name']
start_date = request.json['start_date']
end_date = request.json['end_date']
location = request.json['location']
time = request.json['time']
expected_number_visitors = request.json['expected_number_visitors']
number_of_hosts = request.json['number_of_hosts']
hosts = request.json['hosts']
start_time = request.json['start_time']
end_time = request.json['end_time']
description = request.json['description']
hosting_organization = request.json['hosting_organization']
new_event = Event(name, start_date, end_date, location, time, expected_number_visitors, number_of_hosts, hosts, start_time, end_time, description, hosting_organization)
db.session.add(new_event)
db.session.commit()
return event_schema.jsonify(new_event)
@app.route("/event/<event_id>", methods=["GET"])
def event_get(event_id):
event = Event.query.get(event_id)
return event_schema.jsonify(event)
@app.route("/event/<event_id>", methods=["POST"])
def event_add_host(event_id):
event = Event.query.get(event_id)
event.number_of_hosts = event.number_of_hosts + 1
return event_schema.jsonify(event)
@app.route("/event/sort_date", methods=["GET"])
def event_get_all():
events = Event.query
result = events.order_by(Event.start_date).all()
return events_schema.jsonify(result)
#------------------------------------------------------------------------------------------
class Host(db.Model):
host_id = db.Column(db.Integer, primary_key = True)
netid = db.Column(db.Unicode, unique = True)
name = db.Column(db.Unicode, unique = False)
email = db.Column(db.Unicode, unique = False)
campus_organizations = db.Column(db.Unicode, unique = False)
hosting_address = db.Column(db.Unicode, unique = False)
max_visitors = db.Column(db.Integer, unique = False)
gender = db.Column(db.Unicode, unique = False)
same_gender = db.Column(db.Boolean, unique = False)
expandable = db.Column(db.Boolean, unique = False)
additional_visitors = db.Column(db.Integer, unique = False)
def __init__(self, netid, name, email, campus_organizations, hosting_address, max_visitors, gender, same_gender, expandable, additional_visitors):
self.netid = netid
self.name = name
self.email = email
self.campus_organizations = campus_organizations
self.hosting_address = hosting_address
self.max_visitors = max_visitors
self.gender = gender
self.same_gender = same_gender
self.expandable = expandable
self.additional_visitors = additional_visitors
class HostSchema(ma.Schema):
class Meta:
fields = ('host_id', 'netid', 'name', 'email', 'campus_organizations', 'hosting_address', 'max_visitors', 'gender', 'same_gender', 'expandable', 'additional_visitors')
host_schema = HostSchema()
hosts_schema = HostSchema(many = True)
@app.route("/host", methods=["POST"])
def host_add():
netid = request.json['netid']
name = request.json['name']
email = request.json['email']
campus_organizations = request.json['campus_organizations']
hosting_address = request.json['hosting_address']
max_visitors = request.json['max_visitors']
gender = request.json['gender']
same_gender = request.json['same_gender']
expandable = request.json['expandable']
additional_visitors = request.json['additional_visitors']
new_host = Host(netid, name, email, campus_organizations, hosting_address, max_visitors, gender, same_gender, expandable, additional_visitors)
db.session.add(new_host)
db.session.commit()
return event_schema.jsonify(new_host)
@app.route("/host/<host_id>", methods=["GET"])
def host_get(host_id):
host = Host.query.get(host_id)
return host_schema.jsonify(host)
#------------------------------------------------------------------------------------------
class Visitor(db.Model):
visitor_id = db.Column(db.Integer, primary_key = True)
gender = db.Column(db.Unicode, unique = False)
name = db.Column(db.Unicode, unique = False)
same_gender = db.Column(db.Boolean, unique = False)
university = db.Column(db.Unicode, unique = False)
email = db.Column(db.Unicode, unique = False)
def __init__(self, gender, name, same_gender, university, email):
self.gender = gender
self.name = name
self.same_gender = same_gender
self.university = university
self.email = email
class VisitorSchema(ma.Schema):
class Meta:
fields = ('visitor_id', 'gender', 'name', 'same_gender', 'university', 'email')
visitor_schema = VisitorSchema()
visitors_schema = VisitorSchema(many = True)
@app.route("/visitor", methods=["POST"])
def visitor_add():
gender = request.json['gender']
name = request.json['name']
same_gender = request.json['same_gender']
university = request.json['university']
email = request.json['email']
new_visitor = Visitor(gender, name, same_gender, university, email)
db.session.add(new_visitor)
db.session.commit()
return event_schema.jsonify(new_visitor)
@app.route("/visitor/<visitor_id>", methods=["GET"])
def visitor_get(visitor_id):
visitor = Visitor.query.get(visitor_id)
return visitor_schema.jsonify(host)
db.create_all()
#---------------------------------------------------------------------------------------------------
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=os.environ.get("PORT", 5000))