Пример #1
0
class ExperimentHire(App):
    action_label = StringProperty()

    def __init__(self, **kwargs):
        super(ExperimentHire, self).__init__(**kwargs)

    # Builds the GUI Kivy
    def build(self):
        self.mode = List_Items
        self.list_item = ItemList()
        temp_list = load_items()
        self.item_listing = []
        self.list_item.save_items(temp_list)
        self.action_label = "Select a mode on the left side, then select items on the right"
        self.title = 'Experiment Hire'
        self.root = Builder.load_file('GUI A2.kv')
        self.create_entry_buttons()
        return self.root

    # This function controls the status display of the items
    def listing_items(self):
        self.mode = List_Items
        self.item_listing = []
        statusLable = "Select a mode on the left side, then select items on the right"
        self.action_label = str(statusLable)
        self.root.ids.listItems.state = 'down'
        self.root.ids.hireItem.state = 'normal'
        self.root.ids.addButton.state = 'normal'
        self.root.ids.itemConfirm.state = 'normal'
        self.root.ids.returnItem.state = 'normal'

    # This function controls when the user wants to hire out an item, it passes it through the confirm button andthen deselects it from the GUI making it unavailable. It can only hire out items that are available
    def hiring_item(self):
        statusLable = 'Select Available Items to Hire'
        self.root.ids.action_label.text = str(statusLable)
        self.mode = Hire_Items
        self.item_listing = []
        self.root.ids.listItems.state = 'normal'
        self.root.ids.hireItem.state = 'down'
        self.root.ids.addButton.state = 'normal'
        self.root.ids.itemConfirm.state = 'normal'
        self.root.ids.returnItem.state = 'normal'



    # This function controls when the user wants to return out an item, it passes it through the confirm button and then you are able to select unavailable items it from the GUI making them available
    def return_item(self):
        statuslabel = 'Select available items to return'
        self.root.ids.action_label.text = str(statuslabel)
        self.mode = Return_Items
        self.item_listing = []
        self.root.ids.listItems.state = 'normal'
        self.root.ids.hireItem.state = 'normal'
        self.root.ids.addButton.state = 'normal'
        self.root.ids.itemConfirm.state = 'normal'
        self.root.ids.returnItem.state = 'down'


    # This function controls the hiring and return fucntions, if items are 'out' you can return them, if items are 'in' you can hire them
    def confirm_item(self):
        if self.mode == Hire_Items:
            for item in self.item_listing:
                item.stock = "out"
                self.set_button_to_normal()
                self.listing_items()

        elif self.mode == Return_Items:
            for item in self.item_listing:
                item.stock = "in"
                self.set_button_to_normal()
                # self.item_listing.state = 'normal'
            self.listing_items()

        for self.item in self.item_listing:
            if self.item.stock == "in":
                self.item.state = 'down'
                for button in self.root.ids.newItem.children:
                    button.state = 'down'
                    self.item_listing = []
            elif self.item.stock == "in":
                self.item.state = 'normal'
                for button in self.root.ids.newItem.children:
                    button.state = 'normal'
                    self.item_listing = []



    # Creates the entry button and then adds it to the GUI Kivy
    def create_entry_buttons(self):
        # creates a button for each new item entry being added
        for item in self.list_item.items:
            temp_button = Button(text=item.name)
            temp_button.bind(on_release=self.press_entry)
            # adds the button to "newItem" using add.widget
            self.root.ids.newItem.add_widget(temp_button)

    # updates the button status reloading if the button selected is in or out and passes them
    def button_status(self, status):
        while status != 'in' or 'out':
            if status == 'in':
                pass
            elif status == 'out':
                pass

    # Handles the entry buttons pressing and updates the status text
    def press_entry(self, instance):
        item = self.list_item.get_item(instance.text)
        if self.mode == List_Items:
            self.action_label = str(item)
        elif self.mode == Hire_Items:
            if item.stock == 'in':
                if item not in self.item_listing:
                    self.item_listing.append(item)
                    instance.state = 'down'
                    running_total = 0
                    names = []
                    for item in self.item_listing:
                        running_total += item.price
                        names.append(item.name)
                    self.action_label = "Hiring: {} for ${}".format(', '.join(names), running_total)
        elif self.mode == Return_Items:
            if item.stock == 'out':
                if item not in self.item_listing:
                    self.item_listing.append(item)
                    instance.state = 'down'
                    names = []
                    for item in self.item_listing:
                        names.append(item.name)
                    self.action_label = "Returning: {}".format(', '.join(names))
        elif self.mode == Add_Items:
            pass

    # Sets the button status back to normal after being selected
    def set_button_to_normal(self):
        # uses the .children attribute to access all widgets that are located in another widget
        for button in self.root.ids.newItem.children:
            button.state = 'normal'
            self.item_listing = []




    # Handles the add button, and opens up the pop up window
    def press_add(self):
        self.mode = Add_Items
        self.item_listing = []
        self.action_label = ''
        self.root.ids.listItems.state = 'normal'
        self.root.ids.hireItem.state = 'normal'
        self.root.ids.addButton.state = 'down'
        self.root.ids.itemConfirm.state = 'normal'
        self.root.ids.returnItem.state = 'normal'
        self.action_label = "Enter Details For New Item"
        self.root.ids.popup.open()
        pass

        # clears any button that has been selected by user and reset the status text

    def clear_fields(self):
        # if this function is not completed, the popup will continue to have text in the fields after submission
        self.root.ids.addedName.text = ""
        self.root.ids.addedDescription.text = ""
        self.root.ids.addedPrice.text = ""

    # handles the cancel button in the popup window
    def press_cancel(self):
        self.root.ids.popup.dismiss()
        self.clear_fields()
        self.action_label = ""

    # handles the save item button in the popup window, saves a new entry to the memory
    def press_save(self, added_name, added_description, added_price):
        # changes the number of columns based on the number of entries
        try:
            if added_name == "" or added_description == "" or added_price == "":
                self.root.ids.popup_label.text = "All Fields Must Be Completed"
            if float(added_price) < 0:
                self.root.ids.popup_label.text = "Must Be a Valid Number"
            self.list_item.add_item(added_name, added_description, float(added_price), 'in')
            # number of entries
            self.root.ids.newItem.cols = len(self.list_item.items) // 5 + 1
            # add button for new entry
            new_button = Button(text=added_name)
            new_button.bind(on_release=self.press_entry)
            self.root.ids.newItem.add_widget(new_button)
            self.root.ids.popup.dismiss()
            self.clear_fields()
        except ValueError:
            self.root.ids.addedPrice.text = ''
        # creates an error check if the fields are empty






    # after the GUI is closed this saves the created items to the list
    def on_stop(self):
        save_items(self.list_item.get_items_as_list())
Пример #2
0
class LibraryController(object):
    """Responsible for all access to objects in library

    Including all items (books, DVDs and journals), and users."""
    MAX_LOANS = 5
    MAX_FINE = 50

    def _log(self, message):
        """Poor man's 'logging'!"""
        if self._verbose:
            print(message)

    def __init__(self, verbose=False):
        """Initialize library, optionally setting verbose mode."""
        self._verbose = verbose
        self._log('Initializing {}'.format(self))
        # Initialize internal lists.
        self._item_list = ItemList()
        self._user_list = UserList()

    def add_item(self, item):
        """Add an item to the library."""
        self._log('Adding item: {}'.format(item))
        self._item_list.add_item(item)

    def add_user(self, user):
        """Add an user to the library."""
        #self._log('Adding user: {}'.format(user))
        self._user_list.add_user(user)

    def is_on_loan(self, item_title):
        """Find out if an item is on load."""
        return self._item_list.is_on_loan(item_title)

    def pay_fine(self, user_id, amount):
        """Pay a find for a user."""
        self._log('Paying fine: {} - {}'.format(user_id, amount))
        self._user_list.pay_fine(user_id, amount)

    def user_checkout(self, user_id, item_title, date=dt.now()):
        """Checkout an item for a user."""
        self._log('User checkout: {} - {}'.format(user_id, item_title))
        if self._user_list.able_to_borrow(user_id, self.MAX_LOANS,
                                          self.MAX_FINE):
            self._log('User {} able to borrow.'.format(user_id))
            item = self._item_list.checkout_item(item_title, date)
            self._user_list.checkout_item(user_id, item, date)
            return True
        else:
            self._log('User {} not able to borrow'.format(user_id))
            return False

    def get_user_fine(self, user_id):
        """Get fines that apply to a user."""
        return self._user_list.get_fine_total(user_id)

    def user_return(self, user_id, item_title):
        """Return an item for a user."""
        self._log('User returning: {} - {}'.format(user_id, item_title))
        self._user_list.return_item(user_id, item_title)
        self._item_list.return_item(item_title)
Пример #3
0
class ItemsHiring(App):
    """ Main application class """

    status_text = StringProperty()

    def on_start(self):
        """ Load items from the CSV file on start """
        source_items = load_items()
        self.item_list = ItemList(source_items)
        self.create_item_buttons()
        self.set_mode(Mode.listing)

    def on_stop(self):
        """ Save items to the CSV file on exit """
        save_items(self.item_list.export_items())

    def build(self):
        """ Build Kivy app from the kv file """
        self.title = "Items Hiring"
        self.root = Builder.load_file('app.kv')
        return self.root

    def create_item_button(self, item, item_index):
        """ Create an item button for a certain item """
        button = ItemButton(text=item.name, hired=item.hired, item_index=item_index)
        button.bind(on_release=self.press_item)
        self.root.ids.itemsGrid.add_widget(button)

    def create_item_buttons(self):
        """ Create the item buttons and add them to the GUI """
        for i, item in enumerate(self.item_list.items):
            self.create_item_button(item, i)

    def clear_selection(self):
        """ Clear any buttons that have been selected """
        for instance in self.root.ids.itemsGrid.children:
            instance.set_selected(False)

    def update_item_buttons(self):
        """ Update colours of item buttons """
        for instance in self.root.ids.itemsGrid.children:
            item = self.item_list.items[instance.item_index]
            instance.hired = item.hired

    def set_mode(self, mode):
        """ Switch between 'listing', 'hiring' and 'returning' modes """
        ids = self.root.ids
        for button in [ids.buttonList, ids.buttonHire, ids.buttonReturn]:
            button.state = 'normal'
        if mode == Mode.listing:
            self.status_text = "Choose action from the left menu, then select items on the right"
            ids.buttonList.state = 'down'
        elif mode == Mode.hiring:
            if self.item_list.count(hired=False) > 0:
                self.status_text = "Select available items to hire"
            else:
                self.status_text = "No items available for hire"
            ids.buttonHire.state = 'down'
        elif mode == Mode.returning:
            if self.item_list.count(hired=True) > 0:
                self.status_text = "Select available items to return"
            else:
                self.status_text = "No items are currently on hire"
            ids.buttonReturn.state = 'down'
        self.clear_selection()
        self.mode = mode

    def show_details(self, item_index):
        """ Display item details in the status bar """
        item = self.item_list.items[item_index]
        status = "out" if item.hired else "in"
        self.status_text = "{}({}), ${:.2f} is {}".format(item.name, item.description, item.price, status)

    def selecting_allowed(self, hired):
        """ Check if item is selectable depending on hired status """
        if self.mode == Mode.hiring and not hired:
            return True
        elif self.mode == Mode.returning and hired:
            return True
        return False

    def show_selection_status(self):
        """ Display selected items in the status bar """
        names = []
        total_price = 0
        for i, button in enumerate(self.root.ids.itemsGrid.children):
            item = self.item_list.items[button.item_index]
            if button.selected:
                names.append(item.name)
                total_price += item.price
        if len(names) == 0:
            names = "no items"
        else:
            names = ", ".join(names)
        if self.mode == Mode.hiring:
            self.status_text = "Hiring: {} for ${:.2f}".format(names, total_price)
        else:
            self.status_text = "Returning: {}".format(names)

    def press_item(self, instance):
        """ Handler for pressing an item button """
        if self.mode == Mode.listing:
            self.show_details(instance.item_index)
            return
        if self.selecting_allowed(instance.hired):
            instance.set_selected(not instance.selected)
        self.show_selection_status()

    def press_list(self):
        """ Handler for pressing the 'List Items' button """
        self.set_mode(Mode.listing)

    def press_hire(self):
        """ Handler for pressing the 'Hire Items' button """
        self.set_mode(Mode.hiring)

    def press_return(self):
        """ Handler for pressing the 'Return Items' button """
        self.set_mode(Mode.returning)

    def press_confirm(self):
        """ Handler for pressing the 'Conrirm' button """
        for button in self.root.ids.itemsGrid.children:
            if not button.selected:
                continue
            item = self.item_list.items[button.item_index]
            if self.mode == Mode.hiring:
                self.item_list.hire_item(button.item_index)
            elif self.mode == Mode.returning:
                self.item_list.return_item(button.item_index)
        self.update_item_buttons()
        self.set_mode(Mode.listing)

    def press_add(self):
        """ Handler for pressing the 'Add New Item' button """
        self.status_text = "Enter details for new item"
        self.root.ids.popup.open()

    def press_save(self, name, description, price):
        """ Handler for pressing the save button in the popup """
        if not name or not description or not price:
            self.status_text = "All fields must be completed"
            return
        try:
            price = float(price)
        except:
            self.status_text = "Price must be a valid number"
            return
        if price <= 0:
            self.status_text = "Price must not be negative or zero"
            return
        self.item_list.add_item(name, description, price, hired=False)
        last_index = len(self.item_list.items) - 1
        item = self.item_list.items[last_index]
        self.create_item_button(item, last_index)
        self.close_popup()

    def close_popup(self):
        """ Close the popup and clear input fields """
        self.root.ids.popup.dismiss()
        self.clear_fields()

    def clear_fields(self):
        """ Clear input fields in the popup """
        self.root.ids.itemName.text = ""
        self.root.ids.itemDescription.text = ""
        self.root.ids.itemPrice.text = ""
Пример #4
0
class ItemHire(App):  # creating class ItemHire
    status_text = StringProperty()  # for showing the status of each button and item

    def __init__(self, **kwargs):  # initializer function
        super().__init__(**kwargs)

        temp_item_list = load_items()
        self.items = ItemList()
        self.mode = LIST_MODE
        self.mode1 = HIRE_MODE
        self.mode2 = RETURN_MODE

        self.items.generate_list(temp_item_list)
        self.pressed_items = []

    def build(self):  # creating function for connecting GUI
        self.title = "Item hiring Application"  # creating title
        self.root = Builder.load_file('app.kv')  # loading .kv file
        self.create_buttons()
        return self.root  # reference to root the widget

    def create_buttons(self):  # creating the layout and buttons in GUI
        self.root.ids.statusLabel.text = "Choose action from the left Menu,then select item on the right"
        for item in self.items.items:
            temp_button = Button(text=item.name)
            if item.status == "in":
                temp_button.background_color = (0, 1, 1, 1)
                temp_button.bind(on_press=self.press_entry)
                self.root.ids.entriesBox.add_widget(temp_button)

            if item.status == "out":
                temp_button.background_color = (1, 0, 1, 1)
                temp_button.bind(on_press=self.press_entry)
                self.root.ids.entriesBox.add_widget(temp_button)

    def press_button(self):
        self.mode = LIST_MODE
        self.root.ids.statusLabel.text = "Choose action from the left Menu,then select item on the right"
        self.root.ids.ListItem.state = 'down'
        self.root.ids.HireItem.state = 'normal'
        self.root.ids.ReturnItem.state = 'normal'

    def press_entry(self, instance):  # function to handle the pressing of items in GridLayout
        item = self.items.get_item(instance)
        if self.mode == LIST_MODE:
            name = instance.text
            self.status_text = name
            for item in self.items.items:
                if name == item.name and item.status == "out":
                    self.root.ids.statusLabel.text = item.name + '(' + item.description + ')' + '$' + str(
                        item.price) + 'is out'
                if name == item.name and item.status == "in":
                    self.root.ids.statusLabel.text = item.name + '(' + item.description + ')' \
                                                     + 'is available for hire' + '$' + str(item.price)
        elif self.mode == HIRE_MODE:
            pass
        instance.state = 'down'

    def press_hire(self):  # function to hire items
        self.mode1 = HIRE_MODE
        self.root.ids.HireItem.state = "down"
        self.root.ids.ListItem.state = "normal"
        self.root.ids.ReturnItem.state = "normal"
        self.root.ids.statusLabel.text = "Select available item to hire"

    def press_entry_hire(self, instance1):  # function to handle the pressing of items in GridLayout under press_hire
        if self.mode1 == HIRE_MODE:
            name = instance1.text
            self.status_text = name
            for item in self.items.items:
                if name == item.name and item.status == "out":
                    self.root.ids.statusLabel.text = item.name + '(' + item.description + ')' + 'is out'
                elif name == item.name and item.status == "in":
                    self.root.ids.statusLabel.text = 'Hiring' + item.name + '(' + item.description + ')' \
                                                     + 'for' + '$' + str(item.price)
        elif self.mode1 == LIST_MODE:
            pass
        instance1.state = 'down'

    def press_return(self):  # function to return items
        self.root.ids.statusLabel.text = "Select item to return"
        self.root.ids.ReturnItem.state = "down"
        self.root.ids.ListItem.state = "normal"
        self.root.ids.HireItem.state = "normal"
        self.mode2 = RETURN_MODE

    def press_entry_return(self,
                           instance2):  # function to handle the pressing of items in GridLayout under press_return
        if self.mode2 == RETURN_MODE:
            name = instance2.text
            self.status_text = name
            for item in self.items.items:
                if name == item.name and item.status == "in":
                    self.root.ids.statusLabel.text = item.name + 'is not hired'
                elif name == item.name and item.status == "out":
                    self.root.ids.statusLabel.text = 'Returning:' + item.name + '(' + item.description + ')' \
                                                     + 'for' + '$' + str(item.price)
        elif self.mode1 == LIST_MODE:
            pass
        instance2.state = 'down'

    def press_confirm(self):  # function to confirm the changes like hiring and returning
        self.root.ids.Confirm.state = "down"
        self.root.ids.HireItem.state = "normal"
        self.root.ids.ListItem.state = "normal"
        self.root.ids.ReturnItem.state = "normal"

        if self.mode1 == HIRE_MODE:
            for item in self.pressed_items:
                if item.status == "in":
                    item.status = "out"

        if self.mode2 == RETURN_MODE:
            for item in self.pressed_items:
                if item.status == "out":
                    item.status = "in "

    def on_stop(self):  # function to save all the changes when the window is closed(stopped)
        save_list(self.items.make_list())

    def press_add(self):
        self.status_text = "Enter details for new item"  # status line for add_item popup
        self.root.ids.popup.open()

    def press_save(self, added_name, added_description, added_price):  # function for saving new item to the list
        self.items.add_item(added_name, added_description, str(added_price), "in")

        self.root.ids.entriesBox.cols = 2  # setting the column format in GridLayout

        temp_button = Button(text=added_name)
        temp_button.bind(on_release=self.press_entry)
        temp_button.background_color = (0, 1, 1, 1)
        self.root.ids.entriesBox.add_widget(temp_button)

        self.root.ids.popup.dismiss()
        self.clear_fields()

    def press_cancel(self):  # function to cancel the adding of new item
        self.root.ids.popup.dismiss()  # dismissing the popup
        self.clear_fields()
        self.status_text = ""

    def clear_fields(self):  # function for clearing the fields in add_item popup
        self.root.ids.AddName.text = ""
        self.root.ids.AddDescription.text = ""
        self.root.ids.AddPrice.text = ""