Beispiel #1
0
def menu(user, logger):
    continuation = True
    while continuation is True:
        print(f'\33[97m{"-" * 100}\33[m')
        print('''\33[34m
        1.my tasks
        2.change password
        3.log out\33[m''')

        if user.shared_with_me:
            print('\33[34m\t\t4.shared with me\33[m')

        choice = None
        while choice is None:
            try:
                choice = int(
                    input(
                        '\33[35m\n\t\t>> Please select one of the above items: \33[m'
                    ))
                assert choice in range(1, 5)
            except ValueError:
                print('Invalid input. Please try again.')
            except AssertionError:
                print('Invalid input. Please try again.')
                choice = None

        if choice == 1:  # my tasks
            task_menu(user, logger)

        elif choice == 2:  # change password
            user.change_password()
            write_to_file(User.user_list)

        elif choice == 3:  # log out
            continuation = False

        elif choice == 4:  # shared with me
            if not user.shared_with_me:  # user.shared_with_me = []
                print('\t\tNothing shared with you...')
                input("\t\tPress Enter to Continue...")
            else:
                save = None
                for task in user.shared_with_me:
                    print(task)
                    while save is None:
                        try:
                            save = int(
                                input(
                                    '\33[35m\t\tDo you wanna save it?   1)Yes  2)No  : \33[m'
                                ))
                            assert save in [1, 2]
                        except ValueError:
                            print('Invalid input. Please try again.')
                        except AssertionError:
                            print('Enter 1 or 2.')
                            save = None
                    if save == 1:
                        user.tasks.append(task)
                user.shared_with_me = []
                write_to_file(User.user_list)
    def register(cls):
        """
            This method takes the new user information and registers her/him
            :return: the new user object
        """
        username = input(
            "\33[35m\t\tPlease enter your username, 0 >> back: \33[m")
        if username == '0':
            return 'back'
        try:
            if User.user_list:
                assert username not in [
                    user.username for user in User.user_list
                ]
        except AssertionError:
            return False

        while True:
            password = input('\33[35m\t\tPlease enter a password: \33[m')
            repeated_pass = input(
                '\33[35m\t\tPlease enter the password again: \33[m')
            if password == repeated_pass:
                break
            else:
                print('\33[35m\t\tpasswords are not the same.\33[m')

        hashed_password = sha256(password.encode('utf-8')).hexdigest()

        user = User(username, hashed_password)

        User.user_list.append(user)
        write_to_file(User.user_list)
        return user
    def login(cls, logger):
        """
        This method takes the user information for login
        if she/he enters a wrong input more than three times,
        the account will be suspended for one day
        :return: a boolean or string that indicates whether the login was successful or not
        """
        counter = 1
        while counter <= 3:
            username = None
            while username is None:
                username = input(
                    "\33[35m\t\tPlease enter your username, 0 >> back: \33[m")
                if username == '0':
                    return 'back'
                try:
                    assert User.user_list and username in [
                        user.username for user in User.user_list
                    ]
                except AssertionError:
                    print('\33[35m\n\t\tThe username is invalid.')
                    username = None

            password = input("\33[35m\t\tPlease enter your password: \33[m")
            hash_pass = sha256(str(password).encode('utf-8')).hexdigest()
            user = None
            for user in User.user_list:
                if user.username == username and user.password == hash_pass:
                    if user.block is False:
                        return user
                    elif user.block + timedelta(days=1) < datetime.now():
                        user.block = False
                        return user
                    else:
                        total_seconds = (datetime.now() -
                                         user.block).total_seconds()
                        hours = int(24 - total_seconds / (60 * 60))
                        minutes = int(60 - (total_seconds % (60 * 60)) / 60)
                        print(f'\33[35m\n\t\tYour account is block for '
                              f'{hours} hours and {minutes} minutes.\33[m')
                        input("\n\t\tPress Enter to Continue...")
                        return False

            print('\33[35m\n\t\tWrong password.')
            logger.warning(f'{username} entered a wrong password')
            if counter == 3:
                print(
                    '\33[35m\n\t\tYour account has been blocked for one day.\33[m'
                )
                logger.warning(f'{username} has been blocked for one day')
                user.block = datetime.now()
                write_to_file(User.user_list)
                input("\n\t\tPress Enter to Continue...")
            counter += 1
Beispiel #4
0
def sign_in(logger):
    continuation = True
    while continuation is True:
        print(f'\33[97m{"-" * 100}\33[m')
        print('''\33[34m
        1. Register
        2. Login
        3. Quit\33[m''')
        choice = None
        while choice is None:
            try:
                choice = int(
                    input(
                        '\33[35m\t\t>> Please select one of the items: \33[m'))
                assert choice in range(1, 4)
            except ValueError:
                print('Invalid input. Please try again.')
            except AssertionError:
                print('Enter a number between one and four.')
                choice = None

        if choice == 3:  # Quit
            continuation = False

        elif choice == 1:  # Register
            user = User.register()
            if user == 'back':
                continue
            try:
                assert user
                logger.info(f'{user.username} logged in')
                menu(user, logger)
                write_to_file(User.user_list)
            except AssertionError:
                print()
                print('\33[35m\t\tUsername available, please login.\33[m')
                input("\t\tPress Enter to Continue...")
                continue

        elif choice == 2:  # Login
            user = User.login(logger)
            try:
                assert user and user != 'back'
                print()
                logger.info(f'{user.username} logged in')
                Task.reminder(user)
                menu(user, logger)
            except AssertionError:
                continue
Beispiel #5
0
def task_menu(user, logger):
    continuation = True
    while continuation is True:
        print(f'\33[97m{"-" * 100}\33[m')
        print('''\33[34m
        1.add a task
        2.show my tasks
        3.edit a task
        4.share a task
        5.back\33[m''')
        choice = None
        while choice is None:
            try:
                choice = int(
                    input(
                        '\33[35m\t\t>> Please select one of the above items: \33[m'
                    ))
                assert choice in range(1, 6)
            except ValueError:
                print('\33[34m\t\tInvalid input. Please try again.\33[m')
            except AssertionError:
                print('\33[34m\t\tEnter a number between 1 and 5.\33[m')
                choice = None

        if choice == 1:  # add a task
            Task.add(user)
            logger.info('a new task added')
            write_to_file(User.user_list)

        elif choice == 2:  # show my tasks
            print(f'\33[97m{"-" * 100}\33[m')
            print('''\33[34m
        1.show all of the tasks
        2.daily report
        3.weekly report
        4.monthly report
        5.back\33[m''')
            item = None
            while item is None:
                try:
                    item = int(
                        input(
                            '\33[35m\t\t>> Please select one of the above items: \33[m'
                        ))
                    assert item in range(1, 6)
                except ValueError:
                    print('\33[35m\t\tInvalid input. Please try again.\33[m')
                except AssertionError:
                    print('\33[35m\t\tEnter a number between 1 and 5.\33[m')
                    item = None

            if item == 1:
                user.show_tasks()
            elif item == 2:
                Task.daily_report(user)
            elif item == 3:
                Task.weekly_report(user)
            elif item == 4:
                Task.monthly_report(user)
            elif item == 5:
                continue

        elif choice == 3:  # edit a task
            if not user.tasks:
                print('\33[35m\t\tNo task defined yet...\33[m')
                input("\n\t\tPress Enter to Continue...")
                continue

            user.show_tasks()
            code = None
            while code is None:
                try:
                    code = int(
                        input(
                            '\33[35m\t\tPlease enter the task code which you wanna edit: \33[m'
                        ))
                    assert code in range(
                        1,
                        len(user.tasks) +
                        1) and user.tasks[code - 1].activation is True
                except ValueError:
                    print('\33[35m\t\tInvalid input. Please try again.\33[m')
                except AssertionError:
                    print('\33[35m\t\tInvalid number. Please try again.\33[m')
                    code = None

            print('''\33[34m
        1.done              2.postpone
        3.category          4.description
        5.location          6.link
        7.importance        8.urgency
                   9.delete\33[m''')
            item = None
            while item is None:
                try:
                    item = int(
                        input(
                            '\33[35m\t\t>> Which item do you wanna edit? \33[m'
                        ))
                    assert item in range(1, 10)
                except ValueError:
                    print('\33[35\t\tmInvalid input. Please try again.\33[m')
                except AssertionError:
                    print('\33[35m\t\tEnter a number between 1 and 9.\33[m')
                    item = None
            edit_task(user.tasks[code - 1], user, item, logger)
            write_to_file(User.user_list)

        elif choice == 4:  # share a task
            try:
                assert user.show_tasks()
                code = None
                while code is None:
                    try:
                        code = int(
                            input(
                                '\33[35m\t\tPlease enter the task code which you wanna share: \33[m'
                            ))
                        assert code in range(
                            1,
                            len(user.tasks) +
                            1) and user.tasks[code - 1].activation is True
                    except ValueError:
                        print(
                            '\33[35m\t\tInvalid input. Please try again.\33[m')
                    except AssertionError:
                        print(
                            '\33[35m\t\tInvalid number. Please try again.\33[m'
                        )
                        code = None

                usernames = None
                while usernames is None:
                    usernames = input(
                        '\33[35m\t\tEnter the usernames of the users you wanna share with.\n\t\t'
                        ' (separated with comma) : \33[m').split(',')
                    usernames = [username.strip() for username in usernames]
                    for username in usernames:
                        try:
                            assert username in [
                                user.username for user in User.user_list
                            ]
                        except AssertionError:
                            print(
                                f'\33[35m\t\t{username} does not exist.\33[m')
                            usernames = None
                            break
                user.tasks[code - 1].share(usernames)
                input("\n\t\tDone.\n\t\tPress Enter to Continue...")
            except AssertionError:
                print(
                    '\33[35m\t\tDefine a task first, then try to share it.\33[m'
                )
                input("\t\tPress Enter to Continue...")

        elif choice == 5:  # back
            continuation = False
"""
    This program helps you to schedule your tasks,
    and reminds you to do it

    released on 4/23/2021

    author: Ala Golshani
"""

from menu import sign_in
from user import User
from fileModule import read_from_file, write_to_file
import logging

if __name__ == '__main__':
    logger = logging.getLogger('logger')
    f_handler = logging.FileHandler(filename='reminder.log')
    f_handler.setLevel(logging.INFO)
    f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                                 datefmt='%m-%d-%y %H:%M:%S')
    f_handler.setFormatter(f_format)
    logger.addHandler(f_handler)

    User.user_list = read_from_file()
    sign_in(logger)
    write_to_file(User.user_list)