示例#1
0
 def case_types(self):
     """
     :return: Set of all case types for the domain excluding the CallCenter case type.
     """
     case_types = set(get_case_types_for_domain(self.domain))
     case_types.remove(self.cc_case_type)
     return case_types
示例#2
0
    def _get_cases_for_apps(self, apps_by_type, as_dict=True):
        used_case_types = set()
        case_types_by_app = collections.defaultdict(list)
        for app_type, apps in apps_by_type.items():
            if not app_type == self.APP_TYPE_NONE:
                for app_choice in apps:
                    app = get_app(self.domain, app_choice.id)
                    case_types = []
                    if hasattr(app, "modules"):
                        case_types = set([module.case_type for module in app.modules if module.case_type])
                        used_case_types = used_case_types.union(case_types)
                        case_types = map(lambda c: RMIDataChoice(id=c, text=c, data=app_choice.data), case_types)
                        if as_dict:
                            case_types = map(lambda c: c._asdict(), case_types)
                    case_types_by_app[app_choice.id] = case_types

        if apps_by_type[self.APP_TYPE_NONE]:
            all_case_types = set(get_case_types_for_domain(self.domain))
            unknown_case_types = all_case_types.difference(used_case_types)
            unknown_case_types = map(lambda c: RMIDataChoice(id=c, text=c, data={"unknown": True}), unknown_case_types)
            if as_dict:
                unknown_case_types = map(lambda c: c._asdict(), unknown_case_types)
            case_types_by_app[self.UNKNOWN_SOURCE] = unknown_case_types

        return case_types_by_app
示例#3
0
    def get_number_of_cases_matching_filter(self, domain, user_id, status=None,
                                            date_range=None):

        owner_ids = [user_id]
        groups = Group.by_user(user_id, wrap=False)
        owner_ids.extend(groups)

        if self.filter_option == CaseFilter.include:
            include_case_types = self.case_types
        elif self.filter_option == CaseFilter.no_filter:
            include_case_types = None
        elif self.filter_option == CaseFilter.exclude:
            all_case_types = get_case_types_for_domain(self.domain)
            include_case_types = list(
                set(all_case_types) - set(self.case_types))
        else:
            raise ValueError('filter_option must be one of {}'
                             .format(CaseFilter.options))

        number_of_cases = 0
        for case_type in include_case_types or [None]:
            for owner in owner_ids:
                number_of_cases += get_number_of_cases_by_filters(
                    domain, owner, case_type, status=status,
                    date_range=date_range)
        return number_of_cases
示例#4
0
 def set_case_type_choices(self, initial):
     case_types = [''] + get_case_types_for_domain(self.domain)
     if initial and initial not in case_types:
         # Include the deleted case type in the list of choices so that
         # we always allow proper display and edit of rules
         case_types.append(initial)
     case_types.sort()
     self.fields['case_type'].choices = (
         (case_type, case_type) for case_type in case_types
     )
示例#5
0
 def report_context(self):
     context = super(CaseExportReport, self).report_context
     case_types = get_case_types_for_domain(self.domain)
     groups = HQGroupExportConfiguration.by_domain(self.domain)
     context.update(
         case_types=case_types,
         group_exports=[group.case_exports for group in groups if group.case_exports],
         report_slug=self.slug,
     )
     context["case_format"] = self.request.GET.get("case_format") or "csv"
     return context
示例#6
0
 def report_context(self):
     context = super(CaseExportReport, self).report_context
     case_types = get_case_types_for_domain(self.domain)
     groups = HQGroupExportConfiguration.by_domain(self.domain)
     context.update(
         case_types=case_types,
         group_exports=[group.case_exports for group in groups
                        if group.case_exports],
         report_slug=self.slug,
     )
     context['case_format'] = self.request.GET.get('case_format') or 'csv'
     return context
示例#7
0
def excel_config(request, domain):
    """
    Step one of three.

    This is the initial post when the user uploads the excel file

    named_columns:
        Whether or not the first row of the excel sheet contains
        header strings for the columns. This defaults to True and
        should potentially not be an option as it is always used
        due to how important it is to see column headers
        in the rest of the importer.
    """
    if request.method != 'POST':
        return HttpResponseRedirect(base.ImportCases.get_url(domain=domain))

    if not request.FILES:
        return render_error(request, domain, 'Please choose an Excel file to import.')

    named_columns = request.POST.get('named_columns') == "on"
    uploaded_file_handle = request.FILES['file']

    extension = os.path.splitext(uploaded_file_handle.name)[1][1:].strip().lower()

    # NOTE: We may not always be able to reference files from subsequent
    # views if your worker changes, so we have to store it elsewhere
    # using the soil framework.

    if extension not in importer_util.ExcelFile.ALLOWED_EXTENSIONS:
        return render_error(request, domain,
                            'The Excel file you chose could not be processed. '
                            'Please check that it is saved as a Microsoft '
                            'Excel 97/2000 .xls file.')

    # stash content in the default storage for subsequent views
    file_ref = expose_cached_download(
        uploaded_file_handle.read(),
        expiry=1*60*60,
        file_extension=file_extention_from_filename(uploaded_file_handle.name),
    )
    request.session[EXCEL_SESSION_ID] = file_ref.download_id
    spreadsheet = importer_util.get_spreadsheet(file_ref, named_columns)

    if not spreadsheet:
        return _spreadsheet_expired(request, domain)

    columns = spreadsheet.get_header_columns()
    row_count = spreadsheet.get_num_rows()

    if row_count == 0:
        return render_error(request, domain,
                            'Your spreadsheet is empty. '
                            'Please try again with a different spreadsheet.')

    case_types_from_apps = []
    # load types from all modules
    for row in ApplicationBase.view(
        'app_manager/types_by_module',
        reduce=True,
        group=True,
        startkey=[domain],
        endkey=[domain, {}]
    ).all():
        if not row['key'][1] in case_types_from_apps:
            case_types_from_apps.append(row['key'][1])

    case_types_from_cases = get_case_types_for_domain(domain)
    # for this we just want cases that have data but aren't being used anymore
    case_types_from_cases = filter(lambda x: x not in case_types_from_apps, case_types_from_cases)

    if len(case_types_from_apps) == 0 and len(case_types_from_cases) == 0:
        return render_error(
            request,
            domain,
            'No cases have been submitted to this domain and there are no '
            'applications yet. You cannot import case details from an Excel '
            'file until you have existing cases or applications.'
        )

    return render(
        request,
        "importer/excel_config.html", {
            'named_columns': named_columns,
            'columns': columns,
            'case_types_from_cases': case_types_from_cases,
            'case_types_from_apps': case_types_from_apps,
            'domain': domain,
            'report': {
                'name': 'Import: Configuration'
            },
            'slug': base.ImportCases.slug
        }
    )
示例#8
0
 def test_get_case_types_for_domain(self):
     self.assertEqual(
         set(get_case_types_for_domain(self.domain)),
         {case.type
          for case in self.cases if case.domain == self.domain})
示例#9
0
 def options(self):
     case_types = get_case_types_for_domain(self.domain)
     return [(case, "%s" % case) for case in case_types]
 def test_get_case_types_for_domain(self):
     self.assertEqual(
         set(get_case_types_for_domain(self.domain)),
         {case.type for case in self.cases if case.domain == self.domain}
     )
示例#11
0
 def options(self):
     case_types = get_case_types_for_domain(self.domain)
     return [(case, "%s" % case) for case in case_types
             if case != USER_LOCATION_OWNER_MAP_TYPE]
示例#12
0
 def get_case_types_for_domain(domain):
     return get_case_types_for_domain(domain)
示例#13
0
 def get_case_types_for_domain(domain):
     return get_case_types_for_domain(domain)
示例#14
0
 def options(self):
     case_types = get_case_types_for_domain(self.domain)
     return [(case, "%s" % case) for case in case_types]
示例#15
0
 def case_type_choices(self):
     return [(t, t) for t in get_case_types_for_domain(self.domain)]
示例#16
0
def excel_config(request, domain):
    """
    Step one of three.

    This is the initial post when the user uploads the excel file

    named_columns:
        Whether or not the first row of the excel sheet contains
        header strings for the columns. This defaults to True and
        should potentially not be an option as it is always used
        due to how important it is to see column headers
        in the rest of the importer.
    """
    if request.method != 'POST':
        return HttpResponseRedirect(base.ImportCases.get_url(domain=domain))

    if not request.FILES:
        return render_error(request, domain, 'Please choose an Excel file to import.')

    named_columns = request.POST.get('named_columns') == "on"
    uploaded_file_handle = request.FILES['file']

    extension = os.path.splitext(uploaded_file_handle.name)[1][1:].strip().lower()

    # NOTE: We may not always be able to reference files from subsequent
    # views if your worker changes, so we have to store it elsewhere
    # using the soil framework.

    if extension not in importer_util.ExcelFile.ALLOWED_EXTENSIONS:
        return render_error(request, domain,
                            'The Excel file you chose could not be processed. '
                            'Please check that it is saved as a Microsoft '
                            'Excel 97/2000 .xls file.')

    # stash content in the default storage for subsequent views
    file_ref = expose_cached_download(uploaded_file_handle.read(), expiry=1*60*60)
    request.session[EXCEL_SESSION_ID] = file_ref.download_id
    spreadsheet = importer_util.get_spreadsheet(file_ref, named_columns)

    if not spreadsheet:
        return _spreadsheet_expired(request, domain)

    columns = spreadsheet.get_header_columns()
    row_count = spreadsheet.get_num_rows()

    if row_count == 0:
        return render_error(request, domain,
                            'Your spreadsheet is empty. '
                            'Please try again with a different spreadsheet.')

    case_types_from_apps = []
    # load types from all modules
    for row in ApplicationBase.view(
        'app_manager/types_by_module',
        reduce=True,
        group=True,
        startkey=[domain],
        endkey=[domain, {}]
    ).all():
        if not row['key'][1] in case_types_from_apps:
            case_types_from_apps.append(row['key'][1])

    case_types_from_cases = get_case_types_for_domain(domain)
    # for this we just want cases that have data but aren't being used anymore
    case_types_from_cases = filter(lambda x: x not in case_types_from_apps, case_types_from_cases)

    if len(case_types_from_apps) == 0 and len(case_types_from_cases) == 0:
        return render_error(
            request,
            domain,
            'No cases have been submitted to this domain and there are no '
            'applications yet. You cannot import case details from an Excel '
            'file until you have existing cases or applications.'
        )

    return render(
        request,
        "importer/excel_config.html", {
            'named_columns': named_columns,
            'columns': columns,
            'case_types_from_cases': case_types_from_cases,
            'case_types_from_apps': case_types_from_apps,
            'domain': domain,
            'report': {
                'name': 'Import: Configuration'
            },
            'slug': base.ImportCases.slug
        }
    )