示例#1
0
def create_new_order():
    init_dict = dict()
    iter = 0
    error_list = []
    for i in fields_list:
        if i not in request.args:
            error_list.append(error_messages[iter])
            return jsonify({"status": 401, "message": "missing some data"})
        else:
            init_dict[i] = request.args[i]
        iter += 1
    new_order = Order(init_dict)
    if not collection.get_by_id(new_order.id):
        if new_order.id == 0:
            error_message = ''
            for error in error_list:
                error_message += error + "\n"
            return jsonify({"status": 400, "message": error_message})
        else:
            collection.add_order(new_order)
            collection.rewriting_to_file(file_name)
            return jsonify({
                "status": 201,
                "message": "order has been successfully added"
            })
    else:
        return jsonify({
            "status": 400,
            "message": "Order with such id already exist"
        })
示例#2
0
 def read_from_file(self, file_name):
     if Validators.file_not_empty(file_name):
         f = open(file_name)
         lines = f.readlines()
         # print(to_add.make_str())
         for i in range(len(lines)):
             to_add = Order
             line = lines[i]
             if i != len(lines) - 1:
                 line = line[:-1]
             # print("line is:", line)
             if Order.str_to_order(to_add, line) == 1:
                 print("Data error in line " + str(i + 1))
             else:
                 to_add = Order.str_to_order(to_add, line)
                 self.list_of_orders.append(to_add)
def add_elem_to_collection(collection, file_name):
    print(
        "Enter a LINE which is similar to (with a ', ' as a delimiter or enter 0 to skip)\n"
        "ID, order_status (paid, refunded, not paid), amount, discount (percentage), "
        "order_date, shipped_date, customer_email")
    read = input()
    if represents_int(read) and int(read) == 0:
        pass
    else:
        to_add = Order
        to_add = Order.str_to_order(to_add, read)
        # print(to_add.make_str_for_print())
        collection.add_order(to_add)
        collection.rewriting_to_file(file_name)
    collection.print()
def main():
    menu_choice = 0
    collection = Collection()
    read = input("Enter file name (eg. Text.txt):")
    while not Validators.validate_filename(read):
        # валідувати змінні неможливо, тому я не знаю як це замінити декоратором
        read = input("Reenter file name (eg. Text.txt):")
    file_name = read
    if Validators.file_not_empty(file_name):
        collection.read_from_file(file_name)
    collection.print()
    while menu_choice != 6:
        print(
            "You are in main menu. Possible options:\n"
            "1 - search some data through all info\n"
            "2 - sort collection by some argument\n"
            "3 - delete any order(by its number in list) and rewrite collection in file\n"
            "4 - add an order and rewrite collection in file\n"
            "5 - edit an order and rewrite collection in file\n"
            "6 - exit program\n"
            "Choose your option: ")
        read = input()
        while not Validators.menu_choice_and_sort_and_changer_validation(read):
            read = input()
        menu_choice = int(read)

        if menu_choice == 1:
            read = input("Enter data to be searched:")
            collection.search(read)

        elif menu_choice == 2:
            sort_choice = sort_user_choice()
            collection.sort(sort_choice)
            collection.print()

        elif menu_choice == 3:
            read = input(
                "Enter a number of order to be deleted(enter 0 not to delete anything)"
            )
            if represents_int(read):
                while collection.get_len() < int(read) or 0 > int(read):
                    read = input(
                        "Reenter a number of order to be deleted(enter 0 not to delete anything)"
                    )
                if int(read) == 0:
                    break
                else:
                    collection.deleter(int(read), file_name)
            collection.print()

        elif menu_choice == 4:
            print(
                "Enter a LINE which is similar to (with a ', ' as a delimiter or enter 0 to skip)\n"
                "ID, order_status (paid, refunded, not paid), amount, discount (percentage), "
                "order_date, shipped_date, customer_email")
            read = input()
            if represents_int(read) and int(read) == 0:
                pass
            else:
                to_add = Order
                to_add = Order.str_to_order(to_add, read)
                collection.add_order(to_add)
                collection.rewriting_to_file(file_name)
            collection.print()

        elif menu_choice == 5:
            editing_the_collection(collection, file_name)
            collection.print()

        elif menu_choice == 6:
            print("Have a nice day! Goodbye!")