<commit_before>from urllib import urlencode
from django.core.urlresolvers import reverse
from django.http import Http404
from django.utils.safestring import mark_safe, mark_for_escaping
from django.utils.translation import ugettext_noop, ugettext as _, ugettext_lazy
from corehq import privileges, toggles
from corehq.apps.accounting.dispatcher import AccountingAdminInterfaceDispatcher
from corehq.apps.accounting.models import BillingAccount, Invoice
from corehq.apps.accounting.utils import domain_has_privilege, is_accounting_admin
from corehq.apps.app_manager.dbaccessors import domain_has_apps, get_brief_apps_in_domain
from corehq.apps.domain.models import Domain
from corehq.apps.domain.utils import user_has_custom_top_menu
from corehq.apps.hqadmin.reports import RealProjectSpacesReport, \
    CommConnectProjectSpacesReport, CommTrackProjectSpacesReport, \
    DeviceLogSoftAssertReport
from corehq.apps.hqwebapp.models import GaTracker
from corehq.apps.hqwebapp.view_permissions import user_can_view_reports
from corehq.apps.indicators.dispatcher import IndicatorAdminInterfaceDispatcher
from corehq.apps.indicators.utils import get_indicator_domains
from corehq.apps.locations.analytics import users_have_locations
from corehq.apps.reports.dispatcher import ProjectReportDispatcher, \
    CustomProjectReportDispatcher
from corehq.apps.reports.models import ReportConfig
from corehq.apps.smsbillables.dispatcher import SMSAdminInterfaceDispatcher
from corehq.apps.userreports.util import has_report_builder_access
from corehq.apps.users.decorators import get_permission_name
from corehq.apps.users.models import Permissions
from corehq.apps.users.permissions import FORM_EXPORT_PERMISSION
from corehq.tabs.uitab import UITab
from corehq.tabs.utils import dropdown_dict, sidebar_to_dropdown
from corehq.toggles import OPENLMIS
from dimagi.utils.decorators.memoized import memoized
from django_prbac.utils import has_privilege


class ProjectReportsTab(UITab):
    title = ugettext_noop("Reports")
    view = "corehq.apps.reports.views.default"

    url_prefix_formats = ('/a/{domain}/reports/',)

    @property
    def _is_viewable(self):
        return user_can_view_reports(self.project, self.couch_user)

    @property
    def view(self):
        module = Domain.get_module_by_name(self.domain)
        if hasattr(module, 'DEFAULT_REPORT_CLASS'):
            return "corehq.apps.reports.views.default"
        from corehq.apps.reports.views import MySavedReportsView
        return MySavedReportsView.urlname

    @property
    def sidebar_items(self):

        from corehq.apps.reports.views import MySavedReportsView

        tools = [(_("Tools"), [
            {'title': MySavedReportsView.page_title,
             'url': reverse(MySavedReportsView.urlname, args=[self.domain]),
             'icon': 'icon-tasks fa fa-tasks',
             'show_in_dropdown': True}
        ])]

        if has_report_builder_access(self._request):
            create_report_url = reverse("report_builder_select_type", args=[self.domain])
        else:
            from corehq.apps.userreports.views import paywall_home
            create_report_url = paywall_home(self.domain)

        user_reports = [(
            _("Create Reports"),
            [{
                "title": _('Create new report'),
                "url": create_report_url,
                "icon": "icon-plus fa fa-plus"
            }]
        )]

        project_reports = ProjectReportDispatcher.navigation_sections(
            request=self._request, domain=self.domain)
        custom_reports = CustomProjectReportDispatcher.navigation_sections(
            request=self._request, domain=self.domain)
        return tools + user_reports + project_reports + custom_reports

    @property
    def dropdown_items(self):
        saved_report_header = dropdown_dict(_('My Saved Reports'), is_header=True)
        saved_reports_list = list(ReportConfig.by_domain_and_owner(
                                  self.domain,
                                  self.couch_user._id))

        MAX_DISPLAYABLE_SAVED_REPORTS = 5
        first_five_items = [
            dropdown_dict(saved_report.name, url=saved_report.url)
            for counter, saved_report in enumerate(saved_reports_list)
            if counter < MAX_DISPLAYABLE_SAVED_REPORTS
        ]
        rest_as_second_level_items = [
            dropdown_dict("More Saved Reports", "#", second_level_dropdowns=[
                dropdown_dict(saved_report.name, url=saved_report.url)
                for counter, saved_report in enumerate(saved_reports_list)
                if counter >= MAX_DISPLAYABLE_SAVED_REPORTS
            ])
        ] if len(saved_reports_list) > MAX_DISPLAYABLE_SAVED_REPORTS else []

        if first_five_items:
            saved_reports_dropdown = ([saved_report_header] + first_five_items + rest_as_second_level_items)<commit_msg>Don't show "create new report" button to users without edit perms<commit_after>from urllib import urlencode
from django.core.urlresolvers import reverse
from django.http import Http404
from django.utils.safestring import mark_safe, mark_for_escaping
from django.utils.translation import ugettext_noop, ugettext as _, ugettext_lazy
from corehq import privileges, toggles
from corehq.apps.accounting.dispatcher import AccountingAdminInterfaceDispatcher
from corehq.apps.accounting.models import BillingAccount, Invoice
from corehq.apps.accounting.utils import domain_has_privilege, is_accounting_admin
from corehq.apps.app_manager.dbaccessors import domain_has_apps, get_brief_apps_in_domain
from corehq.apps.domain.models import Domain
from corehq.apps.domain.utils import user_has_custom_top_menu
from corehq.apps.hqadmin.reports import RealProjectSpacesReport, \
    CommConnectProjectSpacesReport, CommTrackProjectSpacesReport, \
    DeviceLogSoftAssertReport
from corehq.apps.hqwebapp.models import GaTracker
from corehq.apps.hqwebapp.view_permissions import user_can_view_reports
from corehq.apps.indicators.dispatcher import IndicatorAdminInterfaceDispatcher
from corehq.apps.indicators.utils import get_indicator_domains
from corehq.apps.locations.analytics import users_have_locations
from corehq.apps.reports.dispatcher import ProjectReportDispatcher, \
    CustomProjectReportDispatcher
from corehq.apps.reports.models import ReportConfig
from corehq.apps.smsbillables.dispatcher import SMSAdminInterfaceDispatcher
from corehq.apps.userreports.util import has_report_builder_access
from corehq.apps.users.decorators import get_permission_name
from corehq.apps.users.models import Permissions
from corehq.apps.users.permissions import FORM_EXPORT_PERMISSION
from corehq.tabs.uitab import UITab
from corehq.tabs.utils import dropdown_dict, sidebar_to_dropdown
from corehq.toggles import OPENLMIS
from dimagi.utils.decorators.memoized import memoized
from django_prbac.utils import has_privilege


class ProjectReportsTab(UITab):
    title = ugettext_noop("Reports")
    view = "corehq.apps.reports.views.default"

    url_prefix_formats = ('/a/{domain}/reports/',)

    @property
    def _is_viewable(self):
        return user_can_view_reports(self.project, self.couch_user)

    @property
    def view(self):
        module = Domain.get_module_by_name(self.domain)
        if hasattr(module, 'DEFAULT_REPORT_CLASS'):
            return "corehq.apps.reports.views.default"
        from corehq.apps.reports.views import MySavedReportsView
        return MySavedReportsView.urlname

    @property
    def sidebar_items(self):

        from corehq.apps.reports.views import MySavedReportsView

        tools = [(_("Tools"), [
            {'title': MySavedReportsView.page_title,
             'url': reverse(MySavedReportsView.urlname, args=[self.domain]),
             'icon': 'icon-tasks fa fa-tasks',
             'show_in_dropdown': True}
        ])]

        user_reports = []
        if self.couch_user.can_edit_data():
            if has_report_builder_access(self._request):
                create_report_url = reverse("report_builder_select_type", args=[self.domain])
            else:
                from corehq.apps.userreports.views import paywall_home
                create_report_url = paywall_home(self.domain)

            user_reports = [(
                _("Create Reports"),
                [{
                    "title": _('Create new report'),
                    "url": create_report_url,
                    "icon": "icon-plus fa fa-plus"
                }]
            )]

        project_reports = ProjectReportDispatcher.navigation_sections(
            request=self._request, domain=self.domain)
        custom_reports = CustomProjectReportDispatcher.navigation_sections(
            request=self._request, domain=self.domain)
        return tools + user_reports + project_reports + custom_reports

    @property
    def dropdown_items(self):
        saved_report_header = dropdown_dict(_('My Saved Reports'), is_header=True)
        saved_reports_list = list(ReportConfig.by_domain_and_owner(
                                  self.domain,
                                  self.couch_user._id))

        MAX_DISPLAYABLE_SAVED_REPORTS = 5
        first_five_items = [
            dropdown_dict(saved_report.name, url=saved_report.url)
            for counter, saved_report in enumerate(saved_reports_list)
            if counter < MAX_DISPLAYABLE_SAVED_REPORTS
        ]
        rest_as_second_level_items = [
            dropdown_dict("More Saved Reports", "#", second_level_dropdowns=[
                dropdown_dict(saved_report.name, url=saved_report.url)
                for counter, saved_report in enumerate(saved_reports_list)
                if counter >= MAX_DISPLAYABLE_SAVED_REPORTS
            ])
        ] if len(saved_reports_list) > MAX_DISPLAYABLE_SAVED_REPORTS else []

        if first_five_items:
            saved_reports_dropdown = ([saved_report_header] + first_five_items + rest_as_second_level_items)
{'fraction_non_alphanumeric': 0.07210378117457764, 'fraction_numerical': 0.0008045052292839903, 'mean_word_length': 3.404340124003543, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2100241', 'n_tokens_mistral': 3016, 'n_tokens_neox': 2842, 'n_words': 554}