def post(self): request_data = request.get_json() try: load_result = self.eventSchema.load(request_data).data address_dict = { 'street': load_result.street_address1, 'city': load_result.city, 'state': load_result.state, 'zip': load_result.zip } geocode = Geocode.get_geocode(address_dict=address_dict) load_result.latitude = geocode['lat'] load_result.longitude = geocode['lng'] db.session.add(load_result) db.session.commit() elastic_index.add_document(load_result, 'Event', latitude=load_result.latitude, longitude=load_result.longitude) self.log_update(event_id=load_result.id, event_title=load_result.title, change_type='create') return self.eventSchema.dump(load_result) except ValidationError as err: raise RestException(RestException.INVALID_OBJECT, details=load_result.errors)
def put(self, id): request_data = request.get_json() instance = db.session.query(Event).filter_by(id=id).first() if instance.zip != request_data['zip'] \ or instance.street_address1 != request_data['street_address1']\ or instance.latitude is None: address_dict = { 'street': request_data['street_address1'], 'city': request_data['city'], 'state': request_data['state'], 'zip': request_data['zip'] } geocode = Geocode.get_geocode(address_dict=address_dict) request_data['latitude'] = geocode['lat'] request_data['longitude'] = geocode['lng'] updated, errors = self.schema.load(request_data, instance=instance) if errors: raise RestException(RestException.INVALID_OBJECT, details=errors) updated.last_updated = datetime.datetime.utcnow() db.session.add(updated) db.session.commit() elastic_index.update_document(updated, 'Event', latitude=updated.latitude, longitude=updated.longitude) self.log_update(event_id=updated.id, event_title=updated.title, change_type='edit') return self.schema.dump(updated)