Exemplo n.º 1
0
def get_option(id):
    response = variant_pb2.VariantOption(result="failed", status=status.STATUS_404_NOT_FOUND, message="Variant Option not found!")
    try:
        with session_scope() as session:
            try:
                option = session.query(VariantOption).filter(VariantOption.id==id, VariantOption.is_deleted==False).first()
            except DataError:
                response = variant_pb2.VariantOption(result="failed", status=status.STATUS_400_BAD_REQUEST, message="Variant Option not found!")
                session.commit
                return response
            if option:
                response = variant_pb2.VariantOption(id=str(option.id), name=option.name, description=option.description,
                                                   variant_id=str(option.variant_id), variant_name=option.variant.name)
            session.commit()
    except Exception as e:
        print(e)
        pass
    return response
Exemplo n.º 2
0
 def create_option(self, user_id, option_data):
     option = pb2.VariantOption(
         name=option_data['name'].strip(),
         variant_id=option_data['variantId'],
         user_id=user_id,
         description=option_data['description'].strip())
     option = self.stub.Create(option)
     response = MessageToDict(option)
     return response
Exemplo n.º 3
0
def update_option(id, name, description, variant_id, user_id):
    name = name.title()
    response = variant_pb2.VariantOption(result="failed", status=status.STATUS_404_NOT_FOUND, message="Variant Option not found!")
    try:
        with session_scope() as session:
            try:
                option = session.query(VariantOption).filter(VariantOption.id == id, VariantOption.is_deleted==False).first()
            except DataError:
                response = variant_pb2.VariantOption(result="failed", status=status.STATUS_400_BAD_REQUEST, message="Variant Option not found!")
                session.commit
                return response
            if option:
                if variant_id:
                    try:
                        variant = session.query(Variant).filter(Variant.id == variant_id, Variant.is_deleted==False).first()
                    except DataError:
                        response = variant_pb2.VariantOption(result="failed", status=status.STATUS_400_BAD_REQUEST, message="Variant not found!")
                        session.commit
                        return response
                    if variant:
                        option.variant_id = variant.id
                    else:
                       response = variant_pb2.VariantOption(result="failed", status=status.STATUS_404_NOT_FOUND, message="Variant not found!")
                       session.commit
                       return response
                option.name = name
                option.description = description
                option.updated_at = datetime.now()
                option.updated_by = user_id
                response = variant_pb2.VariantOption(result="success", status=status.STATUS_200_OK, message="Variant Option updated successfully!")
            session.commit()
    except IntegrityError:
        response.message = "Variant 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
Exemplo n.º 4
0
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(VariantOption).filter(VariantOption.is_deleted==False).all()
        for item in options:
            option = variant_pb2.VariantOption(id=str(item.id), name=item.name, description=item.description,
                                                      variant_id=str(item.variant_id), variant_name=item.variant.name)
            options_list.append(option)
    return options_list
Exemplo n.º 5
0
def delete_option(id, user_id):
    response = variant_pb2.VariantOption(result="failed", status=status.STATUS_404_NOT_FOUND, message="Variant Option not found!")
    try:
        with session_scope() as session:
            try:
                option = session.query(VariantOption).filter(VariantOption.id == id, VariantOption.is_deleted==False).first()
            except DataError:
                response = variant_pb2.VariantOption(result="failed", status=status.STATUS_400_BAD_REQUEST, message="Variant not found!")
                session.commit
                return response
            if option:
                option.deleted_at = datetime.now()
                option.deleted_by = user_id
                option.is_deleted = True
                response.message = "Variant 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
Exemplo n.º 6
0
 def Destroy(self, request, context):
     response = variant_pb2.VariantOption()
     response = crud.delete_option(request.id, request.user_id)
     return response
Exemplo n.º 7
0
 def Update(self, request, context):
     response = variant_pb2.VariantOption()
     response = crud.update_option(request.id, request.name, request.description, request.variant_id, request.user_id)
     return response
Exemplo n.º 8
0
 def Retrieve(self, request, context):
     response = variant_pb2.VariantOption()
     response = crud.get_option(request.id)
     return response