Example #1
0
 def group(self, list):
     return group_by(list, lambda t: t)
Example #2
0
 def test_tasks(self):
     t = Task(description = 'T1')
     list = group_by([t], lambda t: t.importance())
     self.assertEqual(list[0][0], t.importance())
     self.assertEqual(list[0][1], [t])
     self.assertEqual([t], flatten(list))
Example #3
0
File: views.py Project: nodet/yata
def index(request):

    def build_context_menu(chosen):
        l = []
        l.append(('All', reverse('yata.views.select_context_all')))
        l.append(('None', reverse('yata.views.select_context_none')))
        for c in Context.objects.filter(user = request.user):
            l.append((c.title, reverse('yata.views.select_context', args=[c.id,])))
        return [ 'Context to display', chosen, l ]

    def context_menu_displayed(contexts_to_display):
        # Specify which context is actually displayed
        if len(contexts_to_display) == 0:
            return 'All'
        elif contexts_to_display[0] == '':
            return 'None'
        else:
            return contexts_to_display[0]

    contexts_to_display = request.session.get('contexts_to_display', [])
    # Need to ensure something is put in the session so that it's saved.
    request.session['contexts_to_display'] = contexts_to_display
    
    def build_future_menu(chosen):
        return [ 'Future tasks', chosen, [
                    ('Hide', reverse('hide-future-tasks')),
                    ('Show', reverse('show-future-tasks')),
        ]]

    def future_tasks_menu_displayed(b):
        return 'Show' if b else 'Hide'

    show_future_tasks = request.session.get('show_future_tasks', False)
    # Need to ensure something is put in the session so that it's saved.
    request.session['show_future_tasks'] = show_future_tasks


    def build_done_menu(chosen):
        return [ 'Tasks done', chosen, [
                    ('Active', reverse('show-active-tasks')),
                    ('Done', reverse('show-done-tasks')),
                    ('All', reverse('show-all-tasks')),
        ]]

    def show_task(t, show_tasks_done):
        if show_tasks_done == 'All':
            return True
        return t.done == (show_tasks_done == 'Done')

    show_tasks_done = request.session.get('show_tasks_done', 'Active')
    request.session['show_tasks_done'] = show_tasks_done

    tasks = [t for t in Task.objects.for_user(request.user)
                if show_task(t, show_tasks_done)
                if t.matches_contexts(contexts_to_display)
                if show_future_tasks or t.can_start_now()]
    tasks.sort(Task.compare)
    tasks = group_by(tasks, lambda t:
        'Importance %s' % t.importance()
    )

    the_context_menu = build_context_menu(context_menu_displayed(contexts_to_display))
    the_future_menu  = build_future_menu(future_tasks_menu_displayed(show_future_tasks))
    the_done_menu    = build_done_menu(show_tasks_done)

    footers = [ [
        (reverse('add-task'),    'Add task'),
        (reverse('add-context'), 'Add context'),
    ], [
        ('/admin/yata/task',  'Admin'),
        (reverse('yata.views.xml_import'), 'Import tasks...'),
        (reverse('yata.views.xml_export'), 'Export tasks...'),
        ('/yata/accounts/logout/', 'Logout'),
    ] ]
    
    return render_to_response('yata/index.html', {
        'tasks': tasks,
        'menus': [the_context_menu, the_future_menu, the_done_menu],
        'footers': footers,
    })