Exemplo n.º 1
0
    def post(self):
        errors = []
        response = json.loads(self.request.body)

        location_param = param_util.GetParam(response, 'location', True)
        id = param_util.GetInt(location_param, 'id', True)
        name = param_util.GetString(location_param, 'name', True)
        address = param_util.GetParam(location_param, 'address')
        geopt = param_util.GetParam(location_param, 'geopt')
        image_url = param_util.GetString(location_param, 'image_url')
        url = param_util.GetString(location_param, 'url')
        notes = param_util.GetString(location_param, 'notes')

        if errors:
            data = {'errors': errors}
        else:
            found_location = LocationModel.get_by_id(id)

            if not found_location:
                data = {'errors': [{'message': 'id %s is not found.' % id}]}
            else:
                found_location.name = name

                if address:
                    logging.error(address)
                    found_location.address = AddressModel(
                        street=param_util.GetString(address, 'street'),
                        city=param_util.GetString(address, 'city'),
                        state=param_util.GetString(address, 'state'),
                        zip=param_util.GetString(address, 'zip'))
                else:
                    found_location.address = None

                if geopt:
                    if 'lat' in geopt:
                        lat = geopt['lat']
                    else:
                        lat = 0

                    if 'lon' in geopt:
                        lon = geopt['lon']
                    else:
                        lon = 0

                    found_location.geopt = ndb.GeoPt(lat=lat, lon=lon)
                else:
                    found_location.geopt = None

                found_location.image_url = image_url
                found_location.url = url
                found_location.notes = notes

                found_location.put()

                data = {'message': 'Location updated.'}

        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(data))
Exemplo n.º 2
0
    def get(self):
        id = int(self.request.get('id'))

        if not id:
            data = {'errors': [{'message': 'The "id" parameter is required.'}]}
        else:
            found_location = LocationModel.get_by_id(id)

            if not found_location:
                data = {'errors': [{'message': 'id %s is not found.' % id}]}
            else:
                found_location.key.delete()
                data = {'message': 'Deleted successfully.'}

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