Ejemplo n.º 1
0
 def reset_order_counter(self):
     if REFS.MAIN_STATION:
         if OrdersService.truncate_table():
             self.update_view_and_database_content()
     else:
         OrderMessagingService.request_table_deletion(only_inactive=False)
Ejemplo n.º 2
0
    def process_message(self, message: str):
        """ Gets called, whenever the network handler receives a message,
        that is for this specific service.

        message: contains only the main body; no service- and msg-id
        """
        print("Message to process:", message)

        # Message says: DB content has changed
        if message.startswith(REFS.DB_CHANGED_PREFIX):
            if not message[1:].startswith(REFS.SILENT_PREFIX):
                order_id = message[2:]

                if order_id == "0":
                    if OrderMessagingService.AUTO_REFRESH_ENABLED:
                        OrderMessagingService.on_database_changed_event()
                    return

                toast_title = "DB CHANGED"
                toast_text = "<text>"

                # More precise: a new order has been created
                if message[1:].startswith(REFS.ORDER_CREATED_PREFIX):
                    order_timestamp = OrdersService.get_orders(
                        row_filter=f"{REFS.ORDERS_TABLE_ID}={order_id}",
                        columns=[f"{REFS.ORDERS_TABLE_TIMESTAMP}"])[0][0]
                    order_timestamp = OrdersService.convert_timestamp(
                        order_timestamp)

                    toast_title = REFS.ORDER_CREATED_TOAST[0]
                    toast_text = REFS.ORDER_CREATED_TOAST[1].format(
                        order_id, order_timestamp)
                # More precise: a new order has been changed
                elif message[1:].startswith(REFS.ORDER_CHANGED_PREFIX):
                    # First: get the order's current data
                    result = OrdersService.get_orders(
                        row_filter=f"{REFS.ORDERS_TABLE_ID}={order_id}")

                    if result == None or len(result) == 0:
                        raise RuntimeError(
                            "The given order can not be changed because it's not in the database."
                        )

                    changed_order = OrdersService.convert_to_order_object(
                        result[0])

                    # order_details = OrdersService.get_orders(
                    #     row_filter=f"{REFS.ORDERS_TABLE_ID}={order_id}",
                    #     columns=[f"{REFS.ORDERS_TABLE_TIMESTAMP}", f"{REFS.ORDERS_TABLE_STATE}"]
                    # )[0]
                    order_timestamp = OrdersService.convert_timestamp(
                        changed_order.timestamp)

                    # order_state = int(order_details[1])
                    order_change = f"Status > {REFS.ORDER_STATES[changed_order.state]}"

                    # OrdersService.handle_timer(changed_order)

                    toast_title = REFS.ORDER_CHANGED_TOAST[0]
                    toast_text = REFS.ORDER_CHANGED_TOAST[1].format(
                        order_id, order_timestamp, order_change)

                #NotificationService.show_toast(
                #    title=toast_title,
                #    text=toast_text,
                #    keep_alive=False
                #)
            else:  # SILENT prefix
                if message[2:].startswith(REFS.DELETING_NOT_CONFIRMED):
                    messagebox.showwarning(
                        title="Delete response",
                        message="Deleting from table has been denied.")
                elif message[2:].startswith(REFS.DELETING_CONFIRMED):
                    print("Deleting worked")

            # Fire event to inform subscribed classes, like views
            OrderMessagingService.on_database_changed_event()
        # Message says: Request to change given order in DB
        elif message.startswith(
                REFS.ORDER_CHANGE_REQUEST_PREFIX) and REFS.MAIN_STATION:
            order_id = message[2:-1]
            change = message[-1:]

            print("Order id:", order_id)
            print("Change to:", change)

            # First: get the order's current data
            result = OrdersService.get_orders(
                row_filter=f"{REFS.ORDERS_TABLE_ID}={order_id}")

            if result == None or len(result) == 0:
                raise RuntimeError(
                    "The given order can not be changed because it's not in the database."
                )

            old_order = OrdersService.convert_to_order_object(result[0])

            if message[1:].startswith(REFS.ORDER_STATUS_CHANGED_PREFIX):
                old_order.state = int(change)
            elif message[1:].startswith(REFS.ORDER_TYPE_CHANGED_PREFIX):
                old_order.form = int(change)

            OrdersService.update_order(old_order, active=True)

            ## Send Message to other station about order creation
            #OrderMessagingService.notify_of_changes(
            #    changed_order=old_order,
            #    prefix=REFS.ORDER_CHANGED_PREFIX
            #)

            # Fire event to inform subscribed classes, like views
            OrderMessagingService.on_database_changed_event()
        # Message says: Request to delete rows in orders table
        elif message.startswith(
                REFS.CLEAR_TABLE_REQUEST_PREFIX) and REFS.MAIN_STATION:
            clear_type = message[1:]
            result = False

            if clear_type == REFS.DELETE_INACTIVE_PREFIX:
                result = OrdersService.delete_from_table(
                    condition=
                    f"{REFS.ORDERS_TABLE_ACTIVE}='{REFS.ORDERS_TABLE_ACTIVE_FALSE}'",
                    confirm=True)
            elif clear_type == REFS.DELETE_ALL_PREFIX:
                result = OrdersService.truncate_table()

            addinfo = REFS.DELETING_NOT_CONFIRMED

            if result:
                # Fire event to inform subscribed classes, like views
                OrderMessagingService.on_database_changed_event()
                addinfo = REFS.DELETING_CONFIRMED

            OrderMessagingService.notify_of_changes(changed_order=None,
                                                    prefix=REFS.SILENT_PREFIX,
                                                    additional_prefix=addinfo)