Exemplo n.º 1
0
    def update_custom_export(self):
        """
        Updates custom_export object from the request
        and saves to the db
        """

        post_data = self.post_data

        custom_export_json = post_data['custom_export']

        if post_data['presave'] and not self.allow_daily_saved:
            raise BadExportConfiguration(_("This user does not have permission to create Daily Saved Exports"))
        if custom_export_json['default_format'] == "html" and not self.allow_excel_dashboard:
            raise BadExportConfiguration(_("This user does not have permission to create an excel dashboard"))
        if custom_export_json["is_safe"] and not self.allow_deid:
            raise BadExportConfiguration(_("This user does not have permission to create a de-identified export"))

        SAFE_KEYS = ('default_format', 'is_safe', 'name', 'schema_id', 'transform_dates')
        for key in SAFE_KEYS:
            self.custom_export[key] = custom_export_json[key]

        # update the custom export index (to stay in sync)
        schema_id = self.custom_export.schema_id
        schema = ExportSchema.get(schema_id)
        self.custom_export.index = schema.index
        self.presave = post_data['presave']
        self.export_stock = post_data['export_stock']

        self.custom_export.tables = [
            ExportTable.wrap(table)
            for table in custom_export_json['tables']
        ]

        table_dict = dict((t.index, t) for t in self.custom_export.tables)
        for table in self.custom_export.tables:
            if table.index in table_dict:
                table_dict[table.index].columns = table.columns
            else:
                self.custom_export.tables.append(
                    ExportTable(
                        index=table.index,
                        display=self.custom_export.name,
                        columns=table.columns
                    )
                )

        self.update_custom_params()
        self.custom_export.custom_validate()
        self.custom_export.save()
        touch_exports(self.domain)

        if self.presave:
            HQGroupExportConfiguration.add_custom_export(self.domain, self.custom_export.get_id)
        else:
            HQGroupExportConfiguration.remove_custom_export(self.domain, self.custom_export.get_id)
        return self.custom_export.get_id
Exemplo n.º 2
0
    def __init__(self, request, domain, export_id=None, minimal=False):
        self.request = request
        self.domain = domain
        self.presave = False
        self.transform_dates = False
        self.creating_new_export = not bool(export_id)
        self.minimal = minimal

        if export_id:
            self.custom_export = self.ExportSchemaClass.get(export_id)
            # also update the schema to include potential new stuff
            self.custom_export.update_schema()

            # enable configuring saved exports from this page
            saved_group = HQGroupExportConfiguration.get_for_domain(
                self.domain)
            self.presave = export_id in saved_group.custom_export_ids

            self.export_stock = self.has_stock_column()

            try:
                assert self.custom_export.doc_type == 'SavedExportSchema', 'bad export doc type'
                assert self.custom_export.type == self.export_type, 'wrong export type specified'
                assert self.custom_export.index[
                    0] == domain, 'bad export doc domain'
            except AssertionError as e:
                raise BadExportConfiguration(str(e))
        else:
            self.custom_export = self.ExportSchemaClass(type=self.export_type)
            self.export_stock = False
Exemplo n.º 3
0
    def commit(self, request):
        export = self.export_instance_cls.wrap(
            json.loads(request.body.decode('utf-8')))
        if (self.domain != export.domain
                or (export.export_format == "html"
                    and not domain_has_privilege(self.domain, EXCEL_DASHBOARD))
                or
            (export.is_daily_saved_export
             and not domain_has_privilege(self.domain, DAILY_SAVED_EXPORT))):
            raise BadExportConfiguration()

        if not export._rev:
            if toggles.EXPORT_OWNERSHIP.enabled(request.domain):
                export.owner_id = request.couch_user.user_id
            if getattr(settings, "ENTERPRISE_MODE"):
                # default auto rebuild to False for enterprise clusters
                # only do this on first save to prevent disabling on every edit
                export.auto_rebuild_enabled = False

        if export.is_odata_config:
            for table_id, table in enumerate(export.tables):
                labels = []
                for column in table.columns:
                    is_reserved_number = (column.label == 'number'
                                          and table_id > 0 and table.selected)
                    if ((column.label in ['formid', 'caseid']
                         and PROPERTY_TAG_INFO in column.tags)
                            or is_reserved_number):
                        column.selected = True
                    elif (column.label in ['formid', 'caseid']
                          and PROPERTY_TAG_INFO not in column.tags):
                        # make sure hidden (eg deleted) labels that are
                        # formid/caseid are never selected
                        column.selected = False

                    if column.label not in labels and column.selected:
                        labels.append(column.label)
                    elif column.selected:
                        raise ExportODataDuplicateLabelException(
                            _("Column labels must be unique. "
                              "'{}' appears more than once.").format(
                                  column.label))
            num_nodes = sum(
                [1 for table in export.tables[1:] if table.selected])
            if hasattr(self, 'is_copy') and self.is_copy:
                event_title = "[BI Integration] Clicked Save button for feed copy"
            else:
                event_title = "[BI Integration] Clicked Save button for feed creation"
            track_workflow(
                request.user.username, event_title, {
                    "Feed Type": export.type,
                    "Number of additional nodes": num_nodes,
                })

        export.save()
        messages.success(
            request,
            mark_safe(
                _("Export <strong>{}</strong> saved.").format(export.name)))
        return export._id
Exemplo n.º 4
0
    def commit(self, request):
        export = self.export_instance_cls.wrap(json.loads(request.body))
        if (self.domain != export.domain
                or (export.export_format == "html"
                    and not domain_has_privilege(self.domain, EXCEL_DASHBOARD))
                or
            (export.is_daily_saved_export
             and not domain_has_privilege(self.domain, DAILY_SAVED_EXPORT))):
            raise BadExportConfiguration()

        if not export._rev:
            if toggles.EXPORT_OWNERSHIP.enabled(request.domain):
                export.owner_id = request.couch_user.user_id
            if getattr(settings, "ENTERPRISE_MODE"):
                # default auto rebuild to False for enterprise clusters
                # only do this on first save to prevent disabling on every edit
                export.auto_rebuild_enabled = False
        export.save()
        messages.success(
            request,
            mark_safe(
                _("Export <strong>{}</strong> saved.").format(export.name)))
        return export._id