Ejemplo n.º 1
0
def new_queue(request):
    user_storage = ORMUserStorage()

    user = user_storage.get_user_by_nick(request.user.username)
    if request.method != 'POST':
        form = QueueForm(user)
    else:
        form = QueueForm(user, data=request.POST)
        if form.is_valid():
            user_storage = ORMUserStorage()
            queue_storage = ORMQueueStorage()
            queue_controller = QueueController(queue_storage)
            user_controller = UserController(user_storage)

            user = user_storage.get_user_by_nick(request.user.username)
            key = os.urandom(6).hex()
            queue = queue_controller.add_queue(
                name=form.cleaned_data['name'].upper(),
                key=key,
                owner=user)
            user_controller.link_user_with_queue(user, queue)
            return HttpResponseRedirect(
                reverse('web_tracker:show_queue', args=(key,)))
    context = {'form': form}
    return render(request, 'web_tracker/new_queue.html', context)
Ejemplo n.º 2
0
def notify(request):
    user_storage = ORMUserStorage()
    user = user_storage.get_user_by_nick(request.user.username)
    user_controller = UserController(user_storage)
    context = {
        'messages': user.new_messages.split(','),
        'notifications': user.notifications.split(',')
    }
    user_controller.clear_new_messages(user)
    return render(request, 'web_tracker/notify.html', context)
Ejemplo n.º 3
0
def register(request):
    if request.method != 'POST':
        form = UserCreationFormBoot()

    else:
        form = UserCreationFormBoot(data=request.POST)

        if form.is_valid():
            user_storage = ORMUserStorage()
            user_controller = UserController(user_storage)
            queue_storage = ORMQueueStorage()
            queue_controller = QueueController(queue_storage)

            new_user = form.save()
            authenticated_user = authenticate(
                username=new_user.username,
                password=request.POST['password1']
            )
            user_controller.add_user(
                nick=new_user.username,
                uid=new_user.id
            )
            user = user_storage.get_user_by_nick(nick=new_user.username)
            queue = queue_controller.add_queue(name='DEFAULT',
                                               key=os.urandom(6).hex(),
                                               owner=user)
            user_controller.link_user_with_queue(user, queue)
            login(request, authenticated_user)
            return HttpResponseRedirect(reverse('web_tracker:index'))
    context = {'form': form}

    return render(request, 'users/register.html', context)
Ejemplo n.º 4
0
def delete_queue(request, queue_key):
    queue_storage = ORMQueueStorage()
    queue = queue_storage.get_queue_by_key(queue_key)
    user_storage = ORMUserStorage()
    user = user_storage.get_user_by_uid(request.user.id)

    if queue.owner != user.uid:
        context = {'message': 'Access denied. You cannot delete this queue!'}
        return render(request, 'web_tracker/404.html', context)

    if queue.name == 'DEFAULT':
        context = {'message': 'Cannot delete default queue!'}
        return render(request, 'web_tracker/404.html', context)

    if queue.opened_tasks or queue.solved_tasks or queue.failed_tasks:
        task_storage = ORMTaskStorage()
        task_storage.delete_queue_tasks(queue)
    user_controller = UserController(user_storage)
    user_controller.unlink_user_and_queue(user, queue)
    queue_storage.remove_queue(queue)

    return HttpResponseRedirect(reverse('web_tracker:index'))
Ejemplo n.º 5
0
    def wrapped_func(*args, **kwargs):
        task_storage = ORMTaskStorage()
        user_storage = ORMUserStorage()
        plan_storage = ORMPlanStorage()
        queue_storage = ORMQueueStorage()

        task_controller = TaskController(task_storage)
        user_controller = UserController(user_storage)
        plan_controller = PlanController(plan_storage)
        queue_controller = QueueController(queue_storage)
        interface = Interface('', queue_controller, user_controller,
                              task_controller, plan_controller)
        interface.update_all()
        return func(*args, **kwargs)
Ejemplo n.º 6
0
def show_task(request, task_key):
    user_storage = ORMUserStorage()
    task_storage = ORMTaskStorage()
    user_controller = UserController(user_storage)
    user = user_storage.get_user_by_nick(request.user.username)
    task = task_storage.get_task_by_key(key=task_key)
    task.priority = TaskForm.priority_choices[task.priority]
    try:
        task.start = Time.get_date(task.start)
    except Exception:
        pass
    try:
        task.deadline = Time.get_date(task.deadline)
    except Exception:
        pass

    if not user_controller.can_show(user, task):
        context = {'message': 'Cannot edit see task: "{}", key {}!'.format(
            task.name, task_key)
        }
        return render(request, 'web_tracker/404.html', context)

    sub_tasks = task_storage.get_sub_tasks(task)
    parent_task = task_storage.get_task_by_key(task.parent)
    queue_storage = ORMQueueStorage()
    queue = queue_storage.get_queue_by_key(task.queue)

    can_edit = user_controller.can_edit(user, task)
    can_show_parent = user_controller.can_show_parent(user, parent_task)
    context = {'task': task,
               'sub_tasks': sub_tasks,
               'parent_task': parent_task,
               'queue': queue,
               'can_edit': can_edit,
               'can_show_parent': can_show_parent
               }
    return render(request, 'web_tracker/show_task.html', context)
Ejemplo n.º 7
0
def run() -> int:
    """
    Start program
    :return: int - exit code
    """
    # check settings
    Configuration.apply_settings()
    set_logger_enabled(Configuration.is_logger_enabled())

    # check for files and create it if they missed
    check_program_data_files(Files.FOLDER, Files.FILES)

    # set logging configuration
    if os.path.exists(Files.LOG_CONFIG):
        set_logger_config_file(Files.LOG_CONFIG)
    else:
        set_logger_config_file(Files.DEFAULT_LOG_CONFIG_FILE)

    # create loggers and
    cli_logger = get_logger()
    cli_logger.info('Start program.')

    # load data from storage and create entities controllers
    users_wrapper_storage = UserWrapperStorage(Files.AUTH_FILE, Files.ONLINE)

    queue_storage = JsonQueueStorage(Files.QUEUES_FILE)
    queue_controller = QueueController(queue_storage)

    task_storage = JsonTaskStorage(Files.TASKS_FILE)
    task_controller = TaskController(task_storage)

    user_storage = JsonUserStorage(Files.USERS_FILE)
    user_controller = UserController(user_storage)

    plan_storage = JsonPlanStorage(Files.PLANS_FILE)
    plan_controller = PlanController(plan_storage)

    # init library interface
    library = Interface(users_wrapper_storage.online_user, queue_controller,
                        user_controller, task_controller, plan_controller)

    # update reminders deadlines queue and other
    library.update_all()
    _show_new_messages(library)

    parser = get_parsers()
    args = vars(parser.parse_args())
    cli_logger.debug('Console args: {}'.format(args))

    # check that target is defined
    target = args.pop(ParserArgs.TARGET.name)
    if target is None:
        parser.error('target is required', need_to_exit=False)
        return ERROR_CODE

    if target == ParserArgs.UPDATE.name:
        return 0

    # check that action is defined
    action = args.pop(ParserArgs.ACTION)
    if action is None:
        FormattedParser.active_sub_parser.error('action is required',
                                                need_to_exit=False)
        return ERROR_CODE

    # check that target is config and do action with it
    if target == ParserArgs.CONFIG.name:
        if action == ParserArgs.SET:
            return _set_configuration(args)

        if action == ParserArgs.RESET:
            conf_type_obj = args.pop(ParserArgs.CONFIG_TYPE.name)
            return _reset_configuration(conf_type_obj)

    # check that target is user and do action with it
    if target == ParserArgs.USER.name:
        if action == ParserArgs.ADD:
            return _add_user(nick=args.pop(ParserArgs.NICKNAME.name),
                             password=args.pop(ParserArgs.PASSWORD.name),
                             users_storage=users_wrapper_storage,
                             library=library)

        if action == ParserArgs.LOGIN.name:
            return _login(nick=args.pop(ParserArgs.NICKNAME.name),
                          password=args.pop(ParserArgs.PASSWORD.name),
                          users_storage=users_wrapper_storage,
                          library=library)

        if action == ParserArgs.LOGOUT.name:
            return _logout(users_wrapper_storage)

        if action == ParserArgs.SHOW:
            long = args.pop(ParserArgs.LONG.dest)
            sortby = args.pop(ParserArgs.SORT_BY.dest)
            return _show_user_tasks(library, long, sortby)

    # check that target is queue and do action with it
    if target == ParserArgs.QUEUE.name:
        if action == ParserArgs.ADD:
            return _add_queue(name=args.pop(
                ParserArgs.QUEUE_NAME.name).strip(' '),
                              library=library)

        if action == ParserArgs.DELETE:
            return _del_queue(key=args.pop(
                ParserArgs.QUEUE_NAME.name).strip(' '),
                              recursive=args.pop(ParserArgs.RECURSIVE.dest),
                              library=library)

        if action == ParserArgs.SET:
            key = args.pop(ParserArgs.KEY.name)
            new_name = args.pop(ParserArgs.NEW_NAME.dest)
            if new_name is None:
                parser.active_sub_parser.help()
                return 0

            return _edit_queue(key=key, new_name=new_name, library=library)

        if action == ParserArgs.SHOW:
            return _show_queue_tasks(
                key=args.pop(ParserArgs.KEY.name),
                opened=args.pop(ParserArgs.OPEN_TASKS.dest),
                archive=args.pop(ParserArgs.SOLVED_TASKS.dest),
                failed=args.pop(ParserArgs.FAILED_TASKS.dest),
                long=args.pop(ParserArgs.LONG.dest),
                library=library,
                sortby=args.pop(ParserArgs.SORT_BY.dest))

        if action == ParserArgs.FIND:
            name = args.pop(ParserArgs.QUEUE_NAME.name)
            return _find_queues(name, library)

    # check that target is task and do action with it
    if target == ParserArgs.TASK.name:
        if action == ParserArgs.ADD:
            return _add_task(args, library)

        if action == ParserArgs.SET:
            return _edit_task(args, library)

        if action == ParserArgs.DELETE:
            return _del_task(args, library)

        if action == ParserArgs.SHOW:
            return _show_task(args.pop(ParserArgs.KEY.name), library,
                              args.pop(ParserArgs.LONG.dest))

        if action == ParserArgs.FIND:
            return _find_task(args, library)

        if action == ParserArgs.ACTIVATE:
            key = args.pop(ParserArgs.KEY.name)
            return _activate_task(key, library)

    # check that target is plan and do action with it
    if target == ParserArgs.PLAN.name:
        if action == ParserArgs.ADD:
            name = args.pop(ParserArgs.PLAN_NAME.name)
            period = args.pop(ParserArgs.PLAN_PERIOD.name)
            activation_time = args.pop(ParserArgs.PLAN_ACTIVATION_TIME.name)
            reminder = args.pop(ParserArgs.TASK_REMINDER.dest)
            return _add_plan(name, activation_time, period, reminder, library)

        if action == ParserArgs.SET:
            key = args.pop(ParserArgs.KEY.name)
            new_name = args.pop(ParserArgs.PLAN_NAME_OPTIONAL.dest)
            period = args.pop(ParserArgs.PLAN_PERIOD_OPTIONAL.dest)
            activation_time = args.pop(
                ParserArgs.PLAN_ACTIVATION_TIME_OPTIONAL.dest)

            reminder = args.pop(ParserArgs.TASK_REMINDER.dest)
            return _edit_plan(key, new_name, period, activation_time, reminder,
                              library)

        if action == ParserArgs.SHOW:
            return _show_plans(library)

        if action == ParserArgs.DELETE:
            key = args.pop(ParserArgs.KEY.name)
            return _delete_plan(key, library)

    if target == ParserArgs.NOTIFICATIONS.name:
        if action == ParserArgs.SHOW:
            notifications = library.online_user.notifications
            print('Notifications for user "{}":'.format(
                library.online_user.nick))
            if _show_messages(notifications):
                print('Notifications not found!')
            library.clear_new_messages()

        if action == ParserArgs.DELETE:
            _del_notifications(library,
                               _all=args.pop(ParserArgs.ALL.dest),
                               old=args.pop(ParserArgs.OLD.dest))
Ejemplo n.º 8
0
 def setUp(self):
     self.storage = TestUserStorage([])
     self.controller = UserController(self.storage)
Ejemplo n.º 9
0
class UserTests(TestCase):
    def setUp(self):
        self.storage = TestUserStorage([])
        self.controller = UserController(self.storage)

    def test_add_user(self):
        user = self.controller.add_user(nick='tester', uid=0)
        self.assertIn(user, self.storage.users)

    def test_find_user_by_nick(self):
        user_1 = self.controller.add_user(nick='user 1', uid=0)
        igor = self.controller.add_user(nick='igor', uid=1)
        vanya = self.controller.add_user(nick='vanya', uid=2)
        found = self.controller.find_user(nick='igor')
        self.assertEqual(igor, found)

    def test_find_user_by_uid(self):
        user_1 = self.controller.add_user(nick='user 1', uid=0)
        igor = self.controller.add_user(nick='igor', uid=1)
        vanya = self.controller.add_user(nick='vanya', uid=2)
        found = self.controller.find_user(uid=2)
        self.assertEqual(vanya, found)

    def test_clear_user_notifications(self):
        user = self.controller.add_user(nick='tester', uid=0)
        user.notifications.append('Hello')
        user.notifications.append('I need to work')
        user.notifications.append('Go home')
        self.controller.clear_user_notifications(user)
        self.assertListEqual(user.notifications, [])

    def test_clear_user_2_old_notifications(self):
        user = self.controller.add_user(nick='tester', uid=0)
        user.notifications.append('Hello')
        user.notifications.append('I need to work')
        user.notifications.append('Go home')
        self.controller.clear_user_notifications(user, 2)
        self.assertListEqual(user.notifications, ['Go home'])

    def test_clear_new_messages(self):
        user = self.controller.add_user(nick='tester', uid=0)
        user.notifications.append('other notification')
        user.new_messages.append('1 new notify')
        user.new_messages.append('2 new notify')
        self.controller.clear_new_messages(user)
        self.assertListEqual(user.notifications, ['other notification',
                                                  '1 new notify',
                                                  '2 new notify'])
        self.assertListEqual(user.new_messages, [])

    def test_notify_user(self):
        user = self.controller.add_user(nick='tester', uid=0)
        self.controller.notify_user(user, 'privet', show_time=False)

        self.controller.notify_user(user, 'with time')
        expected_str = ''.join([dt.now().strftime(Time.EXTENDED_TIME_FORMAT),
                                ': ', 'with time'])
        self.assertListEqual(user.new_messages, ['privet', expected_str])

    def test_link_user_with_queue(self):
        user = self.controller.add_user(nick='tester', uid=0)
        queue = Queue('test queue', key='0', owner=user.nick)
        self.controller.link_user_with_queue(user, queue)
        self.assertListEqual(user.queues, ['0'])

    def test_unlink_user_with_queue(self):
        user = self.controller.add_user(nick='tester', uid=0)
        queue = Queue('test queue', key='0', owner=user.nick)
        self.controller.link_user_with_queue(user, queue)
        self.controller.unlink_user_and_queue(user, queue)
        self.assertListEqual(user.queues, [])

    def test_link_responsible_with_task(self):
        owner = self.controller.add_user(nick='owner', uid=0)
        responsible = self.controller.add_user(nick='responsible', uid=1)
        task = Task(name='Test task', key='3453', author=owner.nick)
        self.controller.link_responsible_with_task(responsible, task)
        self.assertListEqual(responsible.tasks_responsible, ['3453'])

    def test_unlink_responsible_with_task(self):
        owner = self.controller.add_user(nick='owner', uid=0)
        responsible = self.controller.add_user(nick='responsible', uid=1)
        task = Task(name='Test task', key='3453', author=owner.nick)
        self.controller.link_responsible_with_task(responsible, task)
        self.controller.unlink_responsible_and_task(responsible, task)
        self.assertListEqual(responsible.tasks_responsible, [])

    def test_link_author_with_task(self):
        owner = self.controller.add_user(nick='owner', uid=0)
        task = Task(name='Test task', key='3453', author=owner.nick)
        self.controller.link_author_with_task(owner, task)
        self.assertListEqual(owner.tasks_author, ['3453'])

    def test_unlink_author_with_task(self):
        owner = self.controller.add_user(nick='owner', uid=0)
        task = Task(name='Test task', key='3453', author=owner.nick)
        self.controller.link_author_with_task(owner, task)
        self.controller.unlink_author_and_task(owner, task)
        self.assertListEqual(owner.tasks_author, [])
Ejemplo n.º 10
0
def new_task(request):
    user_storage = ORMUserStorage()
    user = user_storage.get_user_by_nick(nick=request.user.username)
    if request.method != 'POST':
        form = TaskForm(user)

    else:
        form = TaskForm(user, data=request.POST)
        if form.is_valid():
            creating_time = dt.strftime(dt.now(), Time.EXTENDED_TIME_FORMAT)
            task_storage = ORMTaskStorage()
            task_controller = TaskController(task_storage)
            user_controller = UserController(user_storage)
            queue_storage = ORMQueueStorage()
            queue_controller = QueueController(queue_storage)
            queue = queue_storage.find_queue(key=form.cleaned_data['queue'],
                                             owner=user)

            resp = form.cleaned_data['responsible']
            responsible_users = ','.join(resp)
            start = form.cleaned_data['start']
            deadline = form.cleaned_data['deadline']
            try:
                Time.get_date(start)
            except Exception:
                start = ''
            try:
                Time.get_date(deadline)
            except Exception:
                deadline = ''

            key = os.urandom(8).hex()
            try:
                task = task_controller.add_task(
                    name=form.cleaned_data['name'],
                    description=form.cleaned_data['description'],
                    queue=queue,
                    parent=form.cleaned_data['parent'],
                    related=form.cleaned_data['related'],
                    responsible=responsible_users,
                    priority=form.cleaned_data['priority'],
                    progress=form.cleaned_data['progress'],
                    start=start,
                    deadline=deadline,
                    tags=form.cleaned_data['tags'],
                    reminder=form.cleaned_data['reminder'],
                    key=key,
                    author=user,
                    creating_time=creating_time
                )
            except AppError as e:
                form.add_error('name', e.message)
                context = {'form': form}
                return render(request, 'web_tracker/new_task.html', context)
            user_controller.link_author_with_task(user, task)
            for user_nick in resp:
                user = user_storage.get_user_by_nick(user_nick)
                user_controller.link_responsible_with_task(user, task)
            queue_controller.link_queue_with_task(queue, task)

            return HttpResponseRedirect(
                reverse('web_tracker:show_task', args=(key,)))
    context = {'form': form}
    return render(request, 'web_tracker/new_task.html', context)