Exemple #1
0
def _export_assessments(export, AssessmentModel, excel_sheet_data_generator):
    user = export.exported_by
    project = export.project
    export_type = export.export_type
    is_preview = export.is_preview

    arys = AssessmentModel.objects.filter(
        project=project).select_related('project').distinct()
    if AssessmentModel == Assessment:  # Filter is only available for Assessments (not PlannedAssessment)
        user_project_permissions = PP.get_permissions(project, user)
        filters = copy.deepcopy(
            export.filters)  # Avoid mutating database values
        # Lead filtered queryset
        leads_qs = Lead.objects.filter(project=export.project)
        if PP.Permission.VIEW_ALL_LEAD not in user_project_permissions:
            leads_qs = leads_qs.filter(
                confidentiality=Lead.Confidentiality.UNPROTECTED)
        dummy_request = LeadGQFilterSet.get_dummy_request(project)
        leads_qs = LeadGQFilterSet(data=filters,
                                   queryset=leads_qs,
                                   request=dummy_request).qs
        arys = arys.filter(lead__in=leads_qs)
    iterable_arys = arys[:Export.
                         PREVIEW_ASSESSMENT_SIZE] if is_preview else arys

    if export_type == Export.ExportType.JSON:
        exporter = JsonExporter()
        exporter.data = {
            ary.project.title: ary.to_exportable_json()
            for ary in iterable_arys
        }
        export_data = exporter.export()
    elif export_type == Export.ExportType.EXCEL:
        sheets_data = excel_sheet_data_generator(iterable_arys)
        export_data = NewExcelExporter(sheets_data)\
            .export()
    else:
        raise Exception(
            f'(Assessments Export) Unkown Export Type Provided: {export_type} for Export: {export.id}'
        )

    return export_data
Exemple #2
0
    def resolve_public_lead(_, info, **kwargs):
        def _return(lead, project, has_access):
            _project = project
            if (project and project.is_private) and not has_access:
                _project = None
            if lead:
                lead.has_project_access = has_access
            return {
                'project': _project,
                'lead': lead,
            }

        def _get_lead_from_qs(qs):
            return qs\
                .select_related(
                    'project',
                    'created_by',
                    'source',
                    'source__parent',
                ).filter(uuid=kwargs['uuid']).first()

        user = info.context.user
        if user is None or user.is_anonymous:
            lead = _get_lead_from_qs(get_public_lead_qs())
            return _return(lead, None, False)

        lead = _get_lead_from_qs(Lead.objects.all())
        if lead is None:
            return _return(None, None, False)
        user_permissions = PP.get_permissions(lead.project, user)
        has_access = len(user_permissions) > 0
        if (PP.Permission.VIEW_ALL_LEAD in user_permissions or
            (PP.Permission.VIEW_ONLY_UNPROTECTED_LEAD in user_permissions
             and lead.confidentiality != Lead.Confidentiality.CONFIDENTIAL) or
            (lead.confidentiality == Lead.Confidentiality.UNPROTECTED
             and  # IS public
             lead.project.
             has_publicly_viewable_leads  # Project allows to share pubilc leads
             )):
            return _return(lead, lead.project, has_access)
        return _return(None, lead.project, has_access)
Exemple #3
0
def export_entries(export):
    user = export.exported_by
    project = export.project
    export_type = export.export_type
    is_preview = export.is_preview

    user_project_permissions = PP.get_permissions(project, user)

    # Avoid mutating database values
    filters = copy.deepcopy(export.filters)
    extra_options = copy.deepcopy(export.extra_options)

    # Lead/Entry filtered queryset
    leads_qs = Lead.objects.filter(project=export.project)
    if PP.Permission.VIEW_ALL_LEAD not in user_project_permissions:
        leads_qs = leads_qs.filter(
            confidentiality=Lead.Confidentiality.UNPROTECTED)
    # Lead and Entry FilterSet needs request to work with active_project
    dummy_request = LeadGQFilterSet.get_dummy_request(project)
    leads_qs = LeadGQFilterSet(data=filters,
                               queryset=leads_qs,
                               request=dummy_request).qs
    entries_qs = EntryGQFilterSet(
        data=filters.get('entries_filter_data'),
        request=dummy_request,
        queryset=Entry.objects.filter(
            project=export.project,
            analysis_framework=export.project.analysis_framework_id,
            lead__in=leads_qs,
        )).qs

    # Prefetches
    entries_qs = entries_qs.prefetch_related(
        'entrygrouplabel_set',
        'lead__authors',
        'lead__authors__organization_type',
        # Also organization parents
        'lead__authors__parent',
        'lead__authors__parent__organization_type',
    )

    exportables = Exportable.objects.filter(
        analysis_framework__project=project,
        exportdata__isnull=False,
    ).distinct()
    regions = Region.objects.filter(project=project).distinct()

    if export_type == Export.ExportType.EXCEL:
        decoupled = extra_options.get('excel_decoupled', False)
        export_data = ExcelExporter(entries_qs, decoupled, project.id, is_preview=is_preview)\
            .load_exportables(exportables, regions)\
            .add_entries(entries_qs)\
            .export()

    elif export_type == Export.ExportType.REPORT:
        # which widget data needs to be exported along with
        exporting_widgets = extra_options.get('report_exporting_widgets', [])
        report_show_attributes = dict(
            show_lead_entry_id=extra_options.get('report_show_lead_entry_id',
                                                 True),
            show_assessment_data=extra_options.get(
                'report_show_assessment_data', True),
            show_entry_widget_data=extra_options.get(
                'report_show_entry_widget_data', True),
        )

        report_structure = extra_options.get('report_structure')
        report_levels = extra_options.get('report_levels')
        text_widget_ids = extra_options.get('report_text_widget_ids') or []
        show_groups = extra_options.get('report_show_groups')
        export_data = (ReportExporter(
            exporting_widgets=exporting_widgets,
            is_preview=is_preview,
            **report_show_attributes,
        ).load_exportables(exportables, regions).load_levels(
            report_levels).load_structure(report_structure).load_group_lables(
                entries_qs, show_groups).load_text_from_text_widgets(
                    entries_qs,
                    text_widget_ids).add_entries(entries_qs).export(
                        pdf=export.format == Export.Format.PDF))

    elif export_type == Export.ExportType.JSON:
        export_data = JsonExporter(is_preview=is_preview)\
            .load_exportables(exportables)\
            .add_entries(entries_qs)\
            .export()

    else:
        raise Exception(
            '(Entries Export) Unkown Export Type Provided: {export_type} for Export: {export.id}'
        )

    return export_data
Exemple #4
0
 def set_active_project(self, project):
     self.active_project = self.request.active_project = project
     self.project_permissions = self.request.project_permissions = PP.get_permissions(
         project, self.request.user)
Exemple #5
0
 def resolve_allowed_permissions(root, info) -> List[PP.Permission]:
     return PP.get_permissions(root, info.context.request.user)