from typing import Unpack
from q2_sdk.core.exceptions import HqResponseError
from q2_sdk.core.install_steps.base import (
InstallStep,
InstallStepAttribute,
InstallStepArguments,
)
from q2_sdk.hq.db.user_property_data_element import (
UserPropertyDataElement as UserPropertyDataElementDbObj,
)
from q2_sdk.hq.db.user_property_data import UserPropertyData
[docs]
class UserPropertyDataElement(InstallStep):
"""Interacts with the Q2_UserPropertyDataElements table"""
def __init__(
self,
property_name,
property_long_name,
data_type="STR",
category="Custom",
allow_user_edit=False,
allow_user_view=False,
allow_cust_view=False,
**kwargs: Unpack[InstallStepArguments],
):
super().__init__(**kwargs)
self.property_name = InstallStepAttribute(property_name)
self.property_long_name = InstallStepAttribute(property_long_name)
self.data_type = InstallStepAttribute(data_type)
self.category = InstallStepAttribute(category)
self.allow_user_edit = InstallStepAttribute(allow_user_edit, is_bool=True)
self.allow_user_view = InstallStepAttribute(allow_user_view, is_bool=True)
self.allow_cust_view = InstallStepAttribute(allow_cust_view, is_bool=True)
[docs]
async def install(self):
await super().install()
await UserPropertyDataElementDbObj(
self.logger, hq_credentials=self.hq_credentials
).create(
self.property_name.value,
self.property_long_name.value,
self.data_type.value,
self.category.value,
self.allow_user_edit.value,
self.allow_user_view.value,
self.allow_cust_view.value,
)
[docs]
async def uninstall(self):
try:
await UserPropertyData(
self.logger, hq_credentials=self.hq_credentials
).delete_all_property_data(self.property_name.value)
await UserPropertyDataElementDbObj(
self.logger, hq_credentials=self.hq_credentials
).delete(self.property_name.value)
except HqResponseError as err:
if (
"DELETE statement conflicted with the REFERENCE constraint "
'"FK_Q2_UserPropertyData_Q2_PropertyDataElements"'
) in str(err):
self.logger.info(
"Skipping UserPropertyDataElement delete because there was a "
"foreign key constraint preventing the deletion"
)
else:
raise