def update(self, instance, validated_data):
        displayfields_data = validated_data.pop('displayfield_set')
        filterfields_data = validated_data.pop('filterfield_set')

        with transaction.atomic():
            instance.name = validated_data.get('name', instance.name)
            instance.description = validated_data.get('description', instance.description)
            instance.distinct = validated_data.get(
                'distinct', instance.distinct)
            instance.modified = datetime.date.today()
            instance.save()

            for displayfield_data in instance.displayfield_set.all():
                 if displayfield_data.aggregate == "Filter":
                    displayfield_data.filter_field.delete()

            instance.displayfield_set.all().delete()

            for displayfield_data in displayfields_data:
                display_field = DisplayField()
                for key, value in displayfield_data.items():
                    setattr(display_field, key, value)

                display_field.save()
            instance.filterfield_set.all().delete()

            for filterfield_data in filterfields_data:
                filter_field = FilterField()
                for key, value in filterfield_data.items():
                    setattr(filter_field, key, value)
                filter_field.save()

        return instance
Esempio n. 2
0
    def update(self, instance, validated_data):
        displayfields_data = validated_data.pop('displayfield_set')
        filterfields_data = validated_data.pop('filterfield_set')

        with transaction.atomic():
            instance.name = validated_data.get('name', instance.name)
            instance.description = validated_data.get('description',
                                                      instance.description)
            instance.distinct = validated_data.get('distinct',
                                                   instance.distinct)
            instance.modified = datetime.date.today()
            instance.save()
            instance.displayfield_set.all().delete()
            for displayfield_data in displayfields_data:
                display_field = DisplayField()
                for key, value in displayfield_data.items():
                    setattr(display_field, key, value)
                display_field.save()
            instance.filterfield_set.all().delete()
            for filterfield_data in filterfields_data:
                filter_field = FilterField()
                for key, value in filterfield_data.items():
                    setattr(filter_field, key, value)
                filter_field.save()
        return instance
    def add_field_to_report(self,
                            report,
                            model,
                            field_name,
                            allow_recursion=True):
        """Adds the given field to the report.  Includes logic to follow
        foreign keys in order to display their text value properly.
        """

        field = model._meta.get_field(field_name)

        if isinstance(field, ForeignKey):
            fk_model = field.rel.to

            if fk_model.__name__ in FK_DISPLAY_FIELDS:
                report_field = FK_DISPLAY_FIELDS[fk_model.__name__]

                if fk_model == User:
                    path = '%s__' % field_name
                else:
                    path = '%s__%s__' % (model._meta.model_name, field_name)

                display_field = DisplayField(
                    report=report,
                    field=report_field,
                    field_verbose='%s [%s]' %
                    (field.verbose_name, type(field).__name__),
                    name=field.verbose_name,
                    path=path,
                    path_verbose=fk_model._meta.model_name,
                    position=self.position_counter,
                )
                display_field.save()
                return

        display_field = DisplayField(
            report=report,
            field=field_name,
            field_verbose='%s [%s]' %
            (field.verbose_name, type(field).__name__),
            name=field.verbose_name,
            position=self.position_counter)

        if model != Award:
            display_field.path = '%s__' % model._meta.model_name
            display_field.path_verbose = model._meta.model_name

        display_field.save()
    def update(self, instance, validated_data):
        displayfields_data = validated_data.pop('displayfield_set')
        filterfields_data = validated_data.pop('filterfield_set')

        with transaction.atomic():
            instance.name = validated_data.get('name', instance.name)
            instance.chart_categories = validated_data.get(
                'chart_categories', instance.chart_categories)
            instance.chart_series = validated_data.get('chart_series',
                                                       instance.chart_series)
            instance.chart_values = validated_data.get('chart_values',
                                                       instance.chart_values)
            instance.chart_type = validated_data.get('chart_type',
                                                     instance.chart_type)
            instance.chart_style = validated_data.get('chart_style',
                                                      instance.chart_style)
            instance.chart_stacked = validated_data.get(
                'chart_stacked', instance.chart_stacked)
            instance.chart_labels = validated_data.get('chart_labels',
                                                       instance.chart_labels)
            instance.chart_total = validated_data.get('chart_total',
                                                      instance.chart_total)
            instance.description = validated_data.get('description',
                                                      instance.description)
            instance.distinct = validated_data.get('distinct',
                                                   instance.distinct)
            instance.modified = datetime.date.today()
            instance.save()
            instance.displayfield_set.all().delete()
            for displayfield_data in displayfields_data:
                display_field = DisplayField()
                for key, value in displayfield_data.items():
                    setattr(display_field, key, value)
                display_field.save()
            instance.filterfield_set.all().delete()
            for filterfield_data in filterfields_data:
                filter_field = FilterField()
                for key, value in filterfield_data.items():
                    setattr(filter_field, key, value)
                filter_field.save()
        return instance