-
Notifications
You must be signed in to change notification settings - Fork 0
/
atSea.py
49 lines (34 loc) · 1.25 KB
/
atSea.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
#CS496 Mobile and Web development HW3
# Cord Meados 2017
#-----------------------------------
#at_sea handler for main.py
#-----------------------------------
# [START imports]
from google.appengine.ext import ndb
import webapp2
import json
from boat import Boat
from slip import Slip
class AtSeaHandler(webapp2.RequestHandler):
def post(self, id=None):
if id:
depart_date = json.loads(self.request.body) #get departure date from json body for departing boat
b = ndb.Key(urlsafe=id).get() #get the boat object from the database
if b.at_sea == True:
webapp2.abort(400,"Boat is not in a slip.")
for slip in Slip.query().fetch(): #fetch all slips from the database
if slip.current_boat == str(id):
s = slip
#check data is correct type
if isinstance(depart_date['depart'], basestring) == False:
webapp2.abort(400,"Bad user input. Give json string with 'depart' and 'date' where date is a string")
#add departure history to slip
departure = "departed boat: " + str(s.current_boat) + ", departure date: " + str(depart_date["depart"])
s.departure_history.append(departure)
#update data
s.current_boat = None
s.arrival_date = None
b.at_sea = True
#update server
s.put()
b.put()