def update_option(id, name, description, attribute_id, user_id): name = name.title() response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_404_NOT_FOUND, message="Attribute Option not found!") try: with session_scope() as session: try: option = session.query(AttributeOption).filter( AttributeOption.id == id, AttributeOption.is_deleted == False).first() except DataError: response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_400_BAD_REQUEST, message="Attribute Option not found!") session.commit return response if option: if attribute_id: try: attribute = session.query(Attribute).filter( Attribute.id == attribute_id, Attribute.is_deleted == False).first() except DataError: response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_400_BAD_REQUEST, message="Attribute not found!") session.commit return response if attribute: option.attribute_id = attribute.id else: response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_404_NOT_FOUND, message="Attribute not found!") session.commit return response option.name = name option.description = description option.updated_at = datetime.now() option.updated_by = user_id response = attribute_pb2.AttributeOption( result="success", status=status.STATUS_200_OK, message="Attribute Option updated successfully!") session.commit() except IntegrityError: response.message = "Attribute Option already exists with same name!" response.status = status.STATUS_403_FORBIDDEN return response except Exception as e: print(e) response.message = "Unexpected error occurred!" response.status = status.STATUS_500_INTERNAL_SERVER_ERROR pass return response
def delete_option(id, user_id): response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_404_NOT_FOUND, message="Attribute Option not found!") try: with session_scope() as session: try: option = session.query(AttributeOption).filter( AttributeOption.id == id, AttributeOption.is_deleted == False).first() except DataError: response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_400_BAD_REQUEST, message="Attribute not found!") session.commit return response if option: option.deleted_at = datetime.now() option.deleted_by = user_id option.is_deleted = True response.message = "Attribute Option deleted successfully!" response.result = "success" response.status = status.STATUS_204_NO_CONTENT session.commit() except Exception as e: print(e) response.message = "Unexpected error occurred!" response.status = status.STATUS_500_INTERNAL_SERVER_ERROR pass return response
def get_option(id): response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_404_NOT_FOUND, message="Attribute Option not found!") try: with session_scope() as session: try: option = session.query(AttributeOption).filter( AttributeOption.id == id, AttributeOption.is_deleted == False).first() except DataError: response = attribute_pb2.AttributeOption( result="failed", status=status.STATUS_400_BAD_REQUEST, message="Attribute Option not found!") session.commit return response if option: response = attribute_pb2.AttributeOption( id=str(option.id), name=option.name, description=option.description, attribute_id=str(option.attribute_id), attribute_name=option.attribute.name) session.commit() except Exception as e: print(e) pass return response
def create_option(self, user_id, option_data): option = pb2.AttributeOption( name=option_data['name'].strip(), attribute_id=option_data['attributeId'], user_id=user_id, description=option_data['description'].strip()) option = self.stub.Create(option) response = MessageToDict(option) return response
def read_options_database(): options_list = [] """Reads the route guide database. Returns: The full contents of the route guide database as a sequence of route_guide_pb2.Features. """ with session_scope() as session: options = session.query(AttributeOption).filter( AttributeOption.is_deleted == False).all() for item in options: option = attribute_pb2.AttributeOption( id=str(item.id), name=item.name, description=item.description, attribute_id=str(item.attribute_id), attribute_name=item.attribute.name) options_list.append(option) return options_list
def Destroy(self, request, context): response = attribute_pb2.AttributeOption() response = crud.delete_option(request.id, request.user_id) return response
def Update(self, request, context): response = attribute_pb2.AttributeOption() response = crud.update_option(request.id, request.name, request.description, request.attribute_id, request.user_id) return response
def Retrieve(self, request, context): response = attribute_pb2.AttributeOption() response = crud.get_option(request.id) return response