Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 55b0448

Browse files
committed
Initial Commit (Honours Project)
This commit adds the code associated with the honours project submission. It is very messy, needs a lot of love and should *not* be used for anything other than entertainment yet.
0 parents  commit 55b0448

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed

app.py

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#!/usr/bin/python
2+
from flask import Flask, render_template, request, jsonify
3+
from flask_cors import CORS, cross_origin
4+
from flask_sqlalchemy import SQLAlchemy
5+
from json import dumps
6+
from sqlalchemy import ForeignKey
7+
from sqlalchemy.orm import class_mapper
8+
from sqlalchemy import update
9+
10+
import dateutil.parser
11+
12+
DB_URI = 'localhost'
13+
DB_NAME = 'carpooling'
14+
DB_UN = 'jade'
15+
DB_PW = ''
16+
17+
app = Flask(__name__)
18+
cors = CORS(app)
19+
app.config['CORS_HEADERS'] = 'Content-Type'
20+
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://' + DB_UN + ':' + DB_PW + '@' + DB_URI + '/' + DB_NAME
21+
db = SQLAlchemy(app)
22+
23+
class User(db.Model):
24+
__tablename__ = "users"
25+
id = db.Column(db.Integer, primary_key=True)
26+
email = db.Column(db.String())
27+
forename = db.Column(db.String())
28+
surname = db.Column(db.String())
29+
department = db.Column(db.String())
30+
31+
def __init__(self, email, forename, surname, department):
32+
self.email = email
33+
self.forename = forename
34+
self.surname = surname
35+
self.department = department
36+
37+
@property
38+
def serialize(self):
39+
return {
40+
'id':self.id,
41+
'forename':self.forename,
42+
'surname':self.surname,
43+
'department':self.department,
44+
'email':self.email
45+
}
46+
47+
class Carpool(db.Model):
48+
__tablename__ = "carpools"
49+
id = db.Column(db.Integer, primary_key=True)
50+
capacity = db.Column(db.Integer)
51+
origin = db.Column(db.Integer)
52+
destination = db.Column(db.Integer)
53+
date = db.Column(db.Date)
54+
tdepart = db.Column(db.Time)
55+
tarrive = db.Column(db.Time)
56+
organiser = db.Column(db.Integer, ForeignKey("users.id"), nullable=False)
57+
state = db.Column(db.Integer)
58+
dbgmemcount = db.Column(db.Integer)
59+
roundtrip = db.Column(db.Boolean)
60+
61+
def __init__(self, capacity, origin, destination, date, tdepart, tarrive, organiser, state, dbgmemcount, roundtrip):
62+
self.capacity = capacity
63+
self.origin = origin
64+
self.destination = destination
65+
self.date = date
66+
self.tdepart = tdepart
67+
self.tarrive = tarrive
68+
self.organiser = organiser
69+
self.state = state
70+
self.dbgmemcount = dbgmemcount
71+
self.roundtrip = roundtrip
72+
73+
@property
74+
def serialize(self):
75+
import calendar, datetime
76+
77+
return {
78+
'id':self.id,
79+
'capacity':self.capacity,
80+
'origin':self.origin,
81+
'destination':self.destination,
82+
'date':self.date.isoformat(),
83+
'tdepart':self.tdepart.isoformat(),
84+
'tarrive':self.tarrive.isoformat(),
85+
'organiser':User.query.filter_by(id=self.organiser).first().forename + " " + User.query.filter_by(id=self.organiser).first().surname,
86+
'state':self.state,
87+
'capacity':str(self.dbgmemcount) + "/" + str(self.capacity),
88+
'roundtrip':str(self.roundtrip)
89+
}
90+
91+
class Intermediary(db.Model):
92+
__tablename__ = "ucintermediary"
93+
id = db.Column(db.Integer, primary_key=True)
94+
uid = db.Column(db.Integer, ForeignKey("users.id"))
95+
cid = db.Column(db.Integer, ForeignKey("carpools.id"))
96+
97+
def __init__(self, uid, cid):
98+
self.uid = uid
99+
self.cid = cid
100+
101+
@property
102+
def serialize(self):
103+
return {
104+
'id':self.id,
105+
'uid':self.uid,
106+
'cid':self.cid
107+
}
108+
109+
class Proposals(db.Model):
110+
__tablename__ = "proposals"
111+
id = db.Column(db.Integer, primary_key=True)
112+
uid = db.Column(db.Integer, ForeignKey("users.id"))
113+
cid = db.Column(db.Integer, ForeignKey("carpools.id"))
114+
accepted = db.Column(db.Integer)
115+
cost = db.Column(db.Float)
116+
separation = db.Column(db.Integer)
117+
118+
def __init__(self, uid, cid, accepted, cost, separation):
119+
self.uid = uid
120+
self.cid = cid
121+
self.accepted = accepted
122+
self.cost = cost
123+
self.separation = separation
124+
125+
@property
126+
def serialize(self):
127+
return {
128+
'id':self.id,
129+
'uid':self.uid,
130+
'cid':self.cid,
131+
'accepted':self.accepted,
132+
'cost':self.cost,
133+
'separation':self.separation
134+
}
135+
136+
@app.route('/')
137+
def index():
138+
return render_template('index.html')
139+
140+
@app.route('/register', methods=['POST'])
141+
def register():
142+
email = None;
143+
forename = None;
144+
surname = None;
145+
department = None;
146+
if request.method == 'POST':
147+
email = request.form['email']
148+
forename = request.form['forename']
149+
surname = request.form['surname']
150+
department = request.form['department']
151+
if not db.session.query(User).filter(User.email == email).count():
152+
user = User(email, forename, surname, department)
153+
db.session.add(user)
154+
db.session.commit()
155+
return render_template('success.html')
156+
return render_template('index.html')
157+
158+
@app.route('/users', methods=['GET'])
159+
def get_users():
160+
users = db.session.query(User)
161+
return jsonify(users = [item.serialize for item in users.all()])
162+
163+
@app.route('/users/<int:user_id>', methods=['GET'])
164+
def get_user(user_id):
165+
user = db.session.query(User).filter(User.id == user_id)
166+
return jsonify(user.all()[0].serialize)
167+
168+
@app.route('/carpools', methods=['GET'])
169+
def get_carpools():
170+
carpools = db.session.query(Carpool)
171+
return jsonify(carpools = [item.serialize for item in carpools.all()])
172+
173+
@app.route('/carpools/<int:user_id>', methods=['GET'])
174+
def get_carpool(user_id):
175+
intermediaries = db.session.query(Intermediary).filter(Intermediary.uid == user_id)
176+
cids = []
177+
for item in intermediaries:
178+
cids.append(item.cid)
179+
if len(cids) > 0:
180+
carpools = db.session.query(Carpool).filter(Carpool.id.in_(cids))
181+
return jsonify(carpools = [item.serialize for item in carpools.all()])
182+
return "0"
183+
184+
@app.route('/carpools', methods=['POST'])
185+
def create_carpool():
186+
capacity = None
187+
origin = None
188+
destination = None
189+
date = None
190+
tdepart = None
191+
tarrive = None
192+
organiser = None
193+
state = None
194+
dbgmemcount = None;
195+
roundtrip = None;
196+
197+
if request.method == 'POST':
198+
capacity = request.form['capacity']
199+
origin = request.form['origin']
200+
destination = request.form['destination']
201+
date = dateutil.parser.parse(request.form['date']).date()
202+
tdepart = dateutil.parser.parse(request.form['tdepart']).time()
203+
tarrive = dateutil.parser.parse(request.form['tarrive']).time()
204+
organiser = request.form['organiser']
205+
state = request.form['state']
206+
dbgmemcount = 0;
207+
roundtrip = request.form['roundtrip']
208+
carpool = Carpool(capacity, origin, destination, date, tdepart, tarrive, organiser, state, dbgmemcount, roundtrip)
209+
db.session.add(carpool)
210+
db.session.flush()
211+
print(carpool.id)
212+
intermediary = Intermediary(organiser, carpool.id)
213+
db.session.add(intermediary)
214+
db.session.commit()
215+
return "OK"
216+
217+
@app.route('/intermediaries', methods=['GET'])
218+
def get_intermediaries():
219+
intermediaries = db.session.query(Intermediary)
220+
return jsonify(intermediaries = [item.serialize for item in intermediaries.all()])
221+
222+
@app.route('/proposals', methods=['POST'])
223+
def accept_carpool():
224+
225+
uid = None
226+
cid = None
227+
accepted = None
228+
229+
if request.method == 'POST':
230+
uid = request.form['uid']
231+
cid = request.form['cid']
232+
proposal = db.session.query(Proposals).filter(Proposals.uid == uid).filter(Proposals.cid == cid).first()
233+
proposal.accepted = 1;
234+
db.session.commit();
235+
return "OK"
236+
237+
@app.route('/proposals/<int:uid>', methods=['GET'])
238+
def list_proposals(uid):
239+
240+
if request.method == 'GET':
241+
proposals = db.session.query(Proposals).filter(Proposals.uid == uid).order_by(Proposals.cost)
242+
return jsonify(proposals = [item.serialize for item in proposals.all()])
243+
244+
if __name__ == '__main__':
245+
app.run(host='0.0.0.0',debug=True)
246+

requirements.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Flask==0.12
2+
Flask_Cors==3.0.2
3+
Flask_SQLAlchemy==2.2
4+
SQLAlchemy==1.1.6
5+
psycopg2==2.7
6+
python_dateutil==2.6.0

0 commit comments

Comments
 (0)