def call_direct_with_transaction(self, sp_name, params, db_type='W'): """ :description: This function facilitates to call the sp directly (For add/update/delete) Function requires sp_name as string and params as list It does not provide the transaction and it does not closes the cursor Returns the data object to the data layer :param sp_name: Procedure name to call :param params: list of parameters to call the sp :param db_type: Type of database (Read or write) :return: List of dictionary (Whatever comes from the database directly) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, sp_name, params) try: # transaction.set_autocommit(autocommit=False) self.get_cursor(db_type).callproc(sp_name, params) self.data_as_dict_transactional() # transaction.commit() except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, ex.error_object) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data) return self.data
def execute_raw_sql(self, sql_query): """ :description: This executes the raw sql and returns the data object to the business layer This should be used only for single result set :param sql_query: raw sql query :return: list of dictionary (Whatever coming from the database as is) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", sql_query) try: self.get_cursor(db_type="W").excecute(sql_query) self.data_as_dict() except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, self.data) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data) return self.data
def call_direct_multiple(self, sp_name, params, db_type='R'): """ :description: This function facilitates to call the sp directly Function requires sp_name as string and params as list It does not provide the transaction THis should be used only for multiple result set :param sp_name: Procedure name to call :param params: list of parameters to call the sp :param db_type: Type of database (Read or write) :return: List of dictionary (Whatever coming from the database directly) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, sp_name, params) LoggingHelper().log_debug(self.__class__, sp_name, params) try: self.get_cursor(db_type).callproc(sp_name, params) self.data_as_dict_multiple_rs() except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, self.data) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data) return self.data
def get_data_list_object_paginated(self, params): """ :description: To get the list of data from the system for any context for the transactional object with pagination :param object: ParamsObject :return: List of Actual object (Model) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", params) try: self.pre_get_data_list_object_paginated(params) self.get_cursor(db_type="R").callproc(self.sp_name, self.params_list) list_cursor_object = self.data_as_cursor_list_multiple_rs() return self.post_get_data_list_object_paginated(list_cursor_object) except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, self.data) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data)
def get_data_list_object_any_paginated(self, sp_name, params: ParamsObject): """ :description: This method allows user to get the data list object for any procedure which always returns the model object with pagination :param sp_name: Procedure name to call :param params: list of parameters :return: List of model object """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", params) try: self.get_cursor(db_type="R").callproc(sp_name, params.get_params_list()) list_cursor_object = self.data_as_cursor_list_multiple_rs() return self.post_get_data_list_object_paginated(list_cursor_object) except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, self.data) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data)
def get_data(self, params): """ :description: To get the data from the system using id :param params: Actual Object (Model) :return: Actual object (Model) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", params) try: self.pre_get(params) self.get_cursor(db_type="R").callproc(self.sp_name, self.params_list) cursor_object = self.get_data_from_cursor() return self.post_get(cursor_object) except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, self.data) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data)
def delete_data(self, object): """ :description: To delete the data from the system using the object :param object: Actual Object (Model) :return: Actual object (Model) """ if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", object) try: self.pre_delete(object) self.get_cursor(db_type="W").callproc(self.sp_name, self.params_list) returned_data = self.data_as_dict_transactional() return self.post_delete(object, returned_data[0]) except Exception as ex: if base.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, "", str(ex)) if isinstance(ex, DatabaseException) or isinstance( ex, AppointmentException): raise DatabaseException(self.db_error_code, ex.error_message, ex.error_object) else: raise DatabaseException(self.db_error_code, str(ex), self.data) finally: self.close_cursor() if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", self.data)
def check_session(self, request: HttpRequest): try: if request.method != "OPTIONS": if 'HTTP_AUTHORIZATION' in request.META: self.token_payload = JWTManager.decode_token( request.META['HTTP_AUTHORIZATION'].replace("Bearer ", '')) # Check the checksum cp_token_val = dict(self.token_payload) cp_token_val.pop("cs") cs = JWTManager.get_checksum(cp_token_val) if self.token_payload.get("cs") != cs: raise PermissionDeniedException(ErrorCodes.MALFUNCTIONED_TOKEN, 'Malfunctioned Token', None) # Check whether the token is expired expire_str = self.token_payload.get("expires_on") cur = datetime.now(tz=timezone.utc).strftime(AppConstants.TOKEN_EXPIRY_DATETIME_FORMAT) datetime_obj = datetime.strptime(expire_str, AppConstants.TOKEN_EXPIRY_DATETIME_FORMAT) if datetime.strptime(cur, AppConstants.TOKEN_EXPIRY_DATETIME_FORMAT) > datetime_obj: raise PermissionDeniedException(ErrorCodes.TOKEN_EXPIRED, 'Token Expired', None) else: raise PermissionDeniedException(ErrorCodes.PERMISSION_DENIED, 'Permission Denied', None) except PermissionDeniedException as ex: if settings.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, str(ex), request.META.keys()) raise PermissionDeniedException(ex.error_code, ex.error_message, None) except Exception as ex: if settings.DEBUG_ENABLED: LoggingHelper().log_error(self.__class__, str(ex), request.META.keys()) raise PermissionDeniedException(ErrorCodes.PERMISSION_DENIED, 'Permission Denied', None)
def __init__(self): if base.DEBUG_ENABLED: LoggingHelper().log_debug(self.__class__, "", object) try: self.config = pdfkit.configuration(wkhtmltopdf=settings.WKHTMLTOPDF_PATH) LoggingHelper().log_error(self.__class__, "", 'worked') except Exception as ex: LoggingHelper().log_error(self.__class__, "", "exception - constr")
def convert_from_string_to_pdf(self, html_string, output_file_path=None): dispay = Display(visible=False, size=(1440, 900)) try: if output_file_path is None: LoggingHelper().log_error(self.__class__, "", "before") dispay.start() return pdfkit.from_string(html_string, False, configuration=self.config) else: pdfkit.from_string(html_string, output_file_path, configuration=self.config) return output_file_path except Exception as e: LoggingHelper().log_error(self.__class__, "", "convert_from_string_to_pdf - ex") LoggingHelper().log_error(self.__class__, "", str(e)) return None finally: dispay.stop()