Ejemplo n.º 1
0
def domains_for_user(request, selected_domain=None):
    """
    Generate pulldown menu for domains.
    Cache the entire string alongside the couch_user's doc_id that can get invalidated when
    the user doc updates via save.
    """
    domain_list = []
    if selected_domain != 'public':
        cached_domains = cache_core.get_cached_prop(request.couch_user.get_id, 'domain_list')
        if cached_domains:
            domain_list = [Domain.wrap(x) for x in cached_domains]
        else:
            try:
                domain_list = Domain.active_for_user(request.couch_user)
                cache_core.cache_doc_prop(request.couch_user.get_id, 'domain_list', [x.to_json() for x in domain_list])
            except Exception:
                if settings.DEBUG:
                    raise
                else:
                    domain_list = Domain.active_for_user(request.user)
                    notify_exception(request)
    domain_list = [dict(
        url=reverse('domain_homepage', args=[d.name]),
        name=d.long_display_name()
    ) for d in domain_list]
    context = {
        'is_public': selected_domain == 'public',
        'domain_list': domain_list,
        'current_domain': selected_domain,
    }
    template = {
        style_utils.BOOTSTRAP_2: 'hqwebapp/partials/domain_list_dropdown.html',
        style_utils.BOOTSTRAP_3: 'style/includes/domain_list_dropdown.html',
    }[style_utils.bootstrap_version(request)]
    return mark_safe(render_to_string(template, context))
Ejemplo n.º 2
0
def format_sidebar(context):
    current_url_name = context['current_url_name']
    active_tab = context.get('active_tab', None)
    request = context['request']

    sections = None

    if active_tab and active_tab.subtabs:
        # if active_tab is active then at least one of its subtabs should have
        # is_active == True, but we guard against the possibility of this not
        # being the case by setting sections = None above
        for s in active_tab.subtabs:
            if s.is_active:
                sections = s.sidebar_items
                break
        if sections is None:
            for s in active_tab.subtabs:
                if s.url and request.get_full_path().startswith(s.url):
                    sections = s.sidebar_items
                    break
    else:
        sections = active_tab.sidebar_items if active_tab else None

    if sections:
        # set is_active on active sidebar item by modifying nav by reference
        # and see if the nav needs a subnav for the current contextual item
        for section_title, navs in sections:
            for nav in navs:
                if (request.get_full_path().startswith(nav['url']) or
                   request.build_absolute_uri().startswith(nav['url'])):
                    nav['is_active'] = True
                else:
                    nav['is_active'] = False

                if 'subpages' in nav:
                    for subpage in nav['subpages']:
                        if subpage['urlname'] == current_url_name:
                            if callable(subpage['title']):
                                actual_context = {}
                                for d in context.dicts:
                                    actual_context.update(d)
                                subpage['title'] = subpage['title'](**actual_context)
                            nav['subpage'] = subpage
                            break

    template = {
        style_utils.BOOTSTRAP_2: 'hqwebapp/partials/sidebar.html',
        style_utils.BOOTSTRAP_3: 'style/includes/navigation_left_sidebar.html',
    }[style_utils.bootstrap_version(request)]
    return mark_safe(render_to_string(template, {
        'sections': sections
    }))
Ejemplo n.º 3
0
def format_sidebar(context):
    current_url_name = context['current_url_name']
    active_tab = context.get('active_tab', None)
    request = context['request']

    sections = None

    if active_tab and active_tab.subtabs:
        # if active_tab is active then at least one of its subtabs should have
        # is_active == True, but we guard against the possibility of this not
        # being the case by setting sections = None above
        for s in active_tab.subtabs:
            if s.is_active:
                sections = s.sidebar_items
                break
        if sections is None:
            for s in active_tab.subtabs:
                if s.url and request.get_full_path().startswith(s.url):
                    sections = s.sidebar_items
                    break
    else:
        sections = active_tab.sidebar_items if active_tab else None

    if sections:
        # set is_active on active sidebar item by modifying nav by reference
        # and see if the nav needs a subnav for the current contextual item
        for section_title, navs in sections:
            for nav in navs:
                if request.get_full_path().startswith(nav['url']):
                    nav['is_active'] = True
                else:
                    nav['is_active'] = False

                if 'subpages' in nav:
                    for subpage in nav['subpages']:
                        if subpage['urlname'] == current_url_name:
                            if callable(subpage['title']):
                                actual_context = {}
                                for d in context.dicts:
                                    actual_context.update(d)
                                subpage['title'] = subpage['title'](
                                    **actual_context)
                            nav['subpage'] = subpage
                            break

    template = {
        style_utils.BOOTSTRAP_2: 'hqwebapp/partials/sidebar.html',
        style_utils.BOOTSTRAP_3: 'style/includes/navigation_left_sidebar.html',
    }[style_utils.bootstrap_version(request)]
    return mark_safe(render_to_string(template, {'sections': sections}))
Ejemplo n.º 4
0
    def render(self, context):
        request = context['request']
        current_url_name = context['current_url_name']
        couch_user = getattr(request, 'couch_user', None)
        project = getattr(request, 'project', None)
        domain = context.get('domain')
        org = context.get('org')

        try:
            module = Domain.get_module_by_name(domain)
        except (ValueError, AttributeError):
            module = None

        tabs = corehq.TABS + getattr(module, 'TABS', ())
        visible_tabs = []
        all_tabs = []

        active_tab = None

        for tab_class in tabs:
            t = tab_class(
                    request, current_url_name, domain=domain,
                    couch_user=couch_user, project=project, org=org)

            t.is_active_tab = False
            all_tabs.append(t)
            if t.real_is_viewable:
                visible_tabs.append(t)

        # only highlight the first tab considered active.  This allows
        # multiple tabs to contain the same sidebar item, but in all but
        # the first it will effectively be a link to the other tabs.
        for t in all_tabs:
            if t.is_active_fast:
                t.is_active_tab = True
                active_tab = t
                break

        if active_tab is None:
            for t in all_tabs:
                if t.is_active:
                    t.is_active_tab = True
                    active_tab = t
                    break

        if active_tab is None:
            for t in visible_tabs:
                if t.url and request.get_full_path().startswith(t.url):
                    active_tab = t
                    break

        # set the context variable in the highest scope so it can be used in
        # other blocks
        context.dicts[0]['active_tab'] = active_tab

        template = {
            style_utils.BOOTSTRAP_2: 'hqwebapp/partials/main_menu.html',
            style_utils.BOOTSTRAP_3: 'style/includes/menu_main.html',
        }[style_utils.bootstrap_version(request)]
        return mark_safe(render_to_string(template, {
            'tabs': visible_tabs,
        }))
Ejemplo n.º 5
0
    def render(self, context):
        request = context['request']
        current_url_name = context['current_url_name']
        couch_user = getattr(request, 'couch_user', None)
        project = getattr(request, 'project', None)
        domain = context.get('domain')
        org = context.get('org')

        try:
            module = Domain.get_module_by_name(domain)
        except (ValueError, AttributeError):
            module = None

        tabs = corehq.TABS + getattr(module, 'TABS', ())
        visible_tabs = []
        all_tabs = []

        active_tab = None

        for tab_class in tabs:
            t = tab_class(request,
                          current_url_name,
                          domain=domain,
                          couch_user=couch_user,
                          project=project,
                          org=org)

            t.is_active_tab = False
            all_tabs.append(t)
            if t.real_is_viewable:
                visible_tabs.append(t)

        # only highlight the first tab considered active.  This allows
        # multiple tabs to contain the same sidebar item, but in all but
        # the first it will effectively be a link to the other tabs.
        for t in all_tabs:
            if t.is_active_fast:
                t.is_active_tab = True
                active_tab = t
                break

        if active_tab is None:
            for t in all_tabs:
                if t.is_active:
                    t.is_active_tab = True
                    active_tab = t
                    break

        if active_tab is None:
            for t in visible_tabs:
                if t.url and request.get_full_path().startswith(t.url):
                    active_tab = t
                    break

        # set the context variable in the highest scope so it can be used in
        # other blocks
        context.dicts[0]['active_tab'] = active_tab

        template = {
            style_utils.BOOTSTRAP_2: 'hqwebapp/partials/main_menu.html',
            style_utils.BOOTSTRAP_3: 'style/includes/menu_main.html',
        }[style_utils.bootstrap_version(request)]
        return mark_safe(render_to_string(template, {
            'tabs': visible_tabs,
        }))
Ejemplo n.º 6
0
 def template_name(self):
     template = {
         style_utils.BOOTSTRAP_2: 'settings/edit_my_account.b2.html',
         style_utils.BOOTSTRAP_3: 'settings/edit_my_account.html',
     }[style_utils.bootstrap_version(self.request)]
     return template
Ejemplo n.º 7
0
 def template_name(self):
     template = {
         style_utils.BOOTSTRAP_2: 'settings/edit_my_account.b2.html',
         style_utils.BOOTSTRAP_3: 'settings/edit_my_account.html',
     }[style_utils.bootstrap_version(self.request)]
     return template