示例#1
0
    def get(self, id=None):
        try:
            key = ndb.Key(urlsafe=id)
            house = House.get_by_id(key.id())

        except:
            self.response.status = 404
            return

        house_json = ndb_json.dumps(house)
        house_dict = ndb_json.loads(house_json)
        house_dict['id'] = id
        self.response.write(ndb_json.dumps(house_dict))
        self.response.headers['Content-Type'] = 'application/json'
示例#2
0
 def post(self):
     """ Post end point, receives json """
     params = self.req_params
     prime_key = Config.get('users', 'key_arg')
     if prime_key in params.keys():
         email = params[prime_key].lower()
     else:
         details = 'no {0} provided'.format(prime_key)
         error_msg = ErrorReturn(self.response,
                                 error_code='',
                                 details=details)
         error_msg.handle_response(status=400)
         return
     password = params['password']
     json_dict = {"success": "1"}
     try:
         usv = UserValidator(self.auth)
         user = usv.check_login(email, password)
         self._handle_other_validators(user, params)
         self._handle_other_recorders(user, params)
         token = pbi.token.generate(user)
         json_dict["jwt"] = token
     except CustomException as cex:
         logging.info("Custom exception: " + cex.details)
         cex.respond(response=self.response, status='401')
         return
     self.response.write(ndb_json.dumps(json_dict))
示例#3
0
    def get(self, boatId=None):
        try:
            if boatId:
                slip = Slip.query(Slip.current_boat == boatId).get()

                if slip != None:
                    slip_json = ndb_json.dumps(slip)
                    slip_dict = ndb_json.loads(slip_json)

                    slipKey = slip.key
                    slip_dict['id'] = slipKey.urlsafe()

                    self.response.write(json.dumps(slip_dict))
                    self.response.headers['Content-Type'] = 'application/json'

                else:
                    boatKey = ndb.Key(urlsafe=boatId)
                    boat = Boat.get_by_id(boatKey.id())
                    if boat:
                        self.response.status = 404
                    else:
                        self.response.status = 400

            else:
                self.response.status = 400

        except:
            self.response.status = 400
示例#4
0
def getUserHouses(googleId):
	houses = House.query(House.trackedBy == googleId).fetch()
	house_json = ndb_json.dumps(houses)
	house_dict = ndb_json.loads(house_json)
	
	for i in range(0, House.query(House.trackedBy == googleId).count()):
		house_dict[i]['id'] = houses[i].key.urlsafe()
	
	return house_json
示例#5
0
    def patch(self, id=None):
        # Check if the house id exists
        try:
            key = ndb.Key(urlsafe=id)
            house = House.get_by_id(key.id())

        except:
            self.response.status = 404
            return

        try:
            # Check if this is a valid user
            try:
                userToken = self.request.headers['Authorization']
            except:
                self.response.status = 403
                return

            googleId = getGoogleId(userToken)

            # If googleId is nothing, then the authorization token was invalid
            if (not googleId):
                self.response.status = 403
                self.response.write("Invalid authorization header")
                return

            user = User.query(User.gid == googleId).get()
            if (user):
                userId = user.key.urlsafe()
                house_data = json.loads(self.request.body)

                # Check that the replaced house belongs to this user
                key = ndb.Key(urlsafe=id)
                house = House.get_by_id(key.id())
                house_json = ndb_json.dumps(house)
                house_dict = ndb_json.loads(house_json)
                self.response.write(house_dict['trackedBy'])
                self.response.write(userId)
                if (house_dict['trackedBy'] == userId):

                    # Edit the house
                    if ('price' in house_data):

                        self.response.status = 204

                    else:
                        self.response.status = 401

                else:
                    self.response.status = 403

            else:
                self.response.status = 402

        except:
            self.response.status = 400
示例#6
0
def getAllHouses():
	query = House.query()
	allEntities = query.fetch()
	query_json = ndb_json.dumps(query)
	query_dict = ndb_json.loads(query_json)
	
	for i in range(0, House.query().count()):
		query_dict[i]['id'] = allEntities[i].key.urlsafe()
		
	return query_dict
示例#7
0
def getUserHouses(googleId):
    query = House.query(House.trackedBy == googleId)
    allEntities = query.fetch()
    query_json = ndb_json.dumps(query)
    query_dict = ndb_json.loads(query_json)

    for i in range(0, House.query().count()):
        query_dict[i]['id'] = allEntities[i].key.urlsafe()

    return query_dict
示例#8
0
	def get(self):
		googleId = "118105365369656868555"
		houses = House.query(House.trackedBy == googleId).fetch()
		house_json = ndb_json.dumps(houses)
		house_dict = ndb_json.loads(house_json)
		
		for i in range(0, House.query(House.trackedBy == googleId).count()):
			house_dict[i]['id'] = houses[i].key.urlsafe()
		
		self.response.write(house_json)
		self.response.headers['Content-Type'] = 'application/json'
示例#9
0
    def get(self):
        query = Slip.query()
        allEntities = query.fetch()
        query_json = ndb_json.dumps(query)
        query_dict = ndb_json.loads(query_json)

        for i in range(0, Slip.query().count()):
            query_dict[i]['id'] = allEntities[i].key.urlsafe()

        output = json.dumps(query_dict)
        self.response.write(output)
        self.response.headers['Content-Type'] = 'application/json'
示例#10
0
def userIsAdmin(handler):
	try:
		userToken = handler.request.headers['Authorization']
		userId = getGoogleId(userToken)
		user = User.query(User.gid == userId).get()
		user_json = ndb_json.dumps(user)
		user_dict = ndb_json.loads(user_json)
		
		return user_dict['isAdmin']
		
	except:
		return False
示例#11
0
 def post(self):
     """ Post end point, receives json """
     params = self.req_params
     try:
         creator = UserCreator(self.user_model)
         creator.verify_args(params)
         creator.create_by_args(params)
     except CustomException as cex:
         logging.info("aborting signup with 400, user id is duplicate")
         cex.respond(status=400, response=self.response)
         return
     json_dict = {"success": True}
     self.response.write(ndb_json.dumps(json_dict))
示例#12
0
	def get(self):
		query = User.query()
		allEntities = query.fetch()
		query_json = ndb_json.dumps(query)
		query_dict = ndb_json.loads(query_json)
		
		for i in range(0, User.query().count()):
			query_dict[i].pop('gid', None) # Remove google ID from public
			query_dict[i]['id'] = allEntities[i].key.urlsafe()
		
		output = json.dumps(query_dict)
		self.response.write(output)
		self.response.headers['Content-Type'] = 'application/json'
示例#13
0
    def delete(self, id=None):
        # Check if the house id exists
        try:
            key = ndb.Key(urlsafe=id)
            house = House.get_by_id(key.id())

        except:
            self.response.status = 404
            return

        # Check if this is a valid user
        try:
            userToken = self.request.headers['Authorization']
        except:
            self.response.status = 403
            return

        googleId = getGoogleId(userToken)

        # If googleId is nothing, then the authorization token was invalid
        if (not googleId):
            self.response.status = 403
            self.response.write("Invalid authorization header")
            return

        user = User.query(User.gid == googleId).get()
        if (user):
            userId = user.key.urlsafe()

            # Check that the replaced house belongs to this user
            key = ndb.Key(urlsafe=id)
            old_house = House.get_by_id(key.id())
            old_house_json = ndb_json.dumps(old_house)
            old_house_dict = ndb_json.loads(old_house_json)

            if (old_house_dict['trackedBy'] == userId):

                old_house.key.delete()
                self.response.status = 204

            else:
                if (userIsAdmin(self)):
                    old_house.key.delete()
                    self.response.status = 204

                else:
                    self.response.status = 403

        else:
            self.response.status = 403
示例#14
0
	def put(self, id=None):
		# Check if the house id exists
		try:
			key = ndb.Key(urlsafe=id)
			house = House.get_by_id(key.id())
		
		except:
			self.response.status = 404
			return
			
		try:
			# Check if this is a valid user
			userToken = self.request.headers['Authorization']
			googleId = getGoogleId(userToken)
			
			# If googleId is nothing, then the authorization token was invalid
			if (not googleId):
				self.response.status = 403
				self.response.write("Invalid authorization header")
				return
			
			user = User.query(User.gid == googleId).get()
			if (user):
				userId = user.key.urlsafe()
				house_data = json.loads(self.request.body)
				
				# Check that the replaced house belongs to this user
				key = ndb.Key(urlsafe=id)
				old_house = House.get_by_id(key.id())
				old_house_json = ndb_json.dumps(old_house)
				old_house_dict = ndb_json.loads(old_house_json)
				
				if (old_house_dict['trackedBy'] != userId):
					
					addressValid = addressAndZipAreValid(house_data)
					
					if (addressValid):
						# Create the house
						if ('address' in house_data and \
							'price' in house_data and \
							'zip' in house_data):
							
							new_house = House(
								address=house_data['address'],
								price=house_data['price'],
								zip=house_data['zip'],
								trackedBy=userId
							)
							
							new_house.put()
							old_house.key.delete()
							
							house_dict = new_house.to_dict()
							house_dict['id'] = new_house.key.urlsafe()
							self.response.status = 201
							self.response.write(json.dumps(house_dict))
							self.response.headers['Content-Type'] = 'application/json'
							
						else:
							self.response.status = 400
						
					else:
						self.response.status = 400
						
				else:
					self.response.status = 403
			
			else:
				self.response.status = 400
		
		except:
			self.response.status = 400