Exemple #1
0
class ChangeLogMixin(object):
    def __init__(self, request, *args, **kwargs):
        self.request = request
        super().__init__(*args, **kwargs)
        if self.instance.id:
            self.old_serialized_vals = ChangeLogSerializer(self.instance).data

    def get_changelog_message(self):
        return ""

    def save(self, *args, **kwargs):

        experiment = super().save(*args, **kwargs)

        old_values = {}
        new_values = {}
        old_status = None

        self.new_serialized_vals = ChangeLogSerializer(self.instance).data
        latest_change = experiment.changes.latest()

        # account for changes in variant values
        if latest_change:
            old_status = latest_change.new_status
            if (self.old_serialized_vals["variants"] !=
                    self.new_serialized_vals["variants"]):
                old_values["variants"] = self.old_serialized_vals["variants"]
                new_values["variants"] = self.new_serialized_vals["variants"]

        elif self.new_serialized_vals.get("variants"):
            old_values["variants"] = None
            new_values["variants"] = self.new_serialized_vals["variants"]

        if self.changed_data:
            if latest_change:
                old_status = latest_change.new_status

                for field in self.changed_data:
                    if field in self.old_serialized_vals:
                        old_values[field] = self.old_serialized_vals[field]

            else:
                prev_values = {field: None for field in self.changed_data}
                old_values.update(prev_values)

            for field in self.changed_data:
                if field in self.new_serialized_vals:
                    new_values[field] = self.new_serialized_vals[field]

        ExperimentChangeLog.objects.create(
            experiment=experiment,
            changed_by=self.request.user,
            old_status=old_status,
            new_status=experiment.status,
            old_values=old_values,
            new_values=new_values,
            message=self.get_changelog_message(),
        )

        return experiment
Exemple #2
0
class ChangeLogMixin(object):
    def __init__(self, request, *args, **kwargs):
        self.request = request
        super().__init__(*args, **kwargs)
        if self.instance.id:
            self.old_serialized_vals = ChangeLogSerializer(self.instance).data

    def get_changelog_message(self):
        return ""

    def save(self, *args, **kwargs):

        experiment = super().save(*args, **kwargs)

        changed_values = {}
        old_status = None

        self.new_serialized_vals = ChangeLogSerializer(self.instance).data
        latest_change = experiment.changes.latest()

        # account for changes in variant values
        if latest_change:
            old_status = latest_change.new_status
            if (self.old_serialized_vals["variants"] !=
                    self.new_serialized_vals["variants"]):
                old_value = self.old_serialized_vals["variants"]
                new_value = self.new_serialized_vals["variants"]
                display_name = "Branches"
                changed_values["variants"] = {
                    "old_value": old_value,
                    "new_value": new_value,
                    "display_name": display_name,
                }

        elif self.new_serialized_vals.get("variants"):
            old_value = None
            new_value = self.new_serialized_vals["variants"]
            display_name = "Branches"
            changed_values["variants"] = {
                "old_value": old_value,
                "new_value": new_value,
                "display_name": display_name,
            }

        if self.changed_data:
            if latest_change:
                old_status = latest_change.new_status

                for field in self.changed_data:
                    old_val = None
                    new_val = None

                    if field in self.old_serialized_vals:
                        if field in ("countries", "locales"):
                            old_field_values = self.old_serialized_vals[field]
                            codes = [obj["code"] for obj in old_field_values]
                            old_val = codes
                        else:
                            old_val = self.old_serialized_vals[field]
                    if field in self.new_serialized_vals:
                        if field in ("countries", "locales"):
                            new_field_values = self.new_serialized_vals[field]
                            codes = [obj["code"] for obj in new_field_values]
                            new_val = codes
                        else:
                            new_val = self.new_serialized_vals[field]

                    display_name = self._get_display_name(field)

                    if new_val or old_val:
                        changed_values[field] = {
                            "old_value": old_val,
                            "new_value": new_val,
                            "display_name": display_name,
                        }

            else:
                for field in self.changed_data:
                    old_val = None
                    new_val = None
                    if field in self.new_serialized_vals:
                        if field in ("countries", "locales"):
                            new_field_values = self.new_serialized_vals[field]
                            codes = [obj["code"] for obj in new_field_values]
                            new_val = codes
                        else:
                            new_val = self.new_serialized_vals[field]
                        display_name = self._get_display_name(field)
                        changed_values[field] = {
                            "old_value": old_val,
                            "new_value": new_val,
                            "display_name": display_name,
                        }

        ExperimentChangeLog.objects.create(
            experiment=experiment,
            changed_by=self.request.user,
            old_status=old_status,
            new_status=experiment.status,
            changed_values=changed_values,
            message=self.get_changelog_message(),
        )

        return experiment

    def _get_display_name(self, field):
        if self.fields[field].label:
            return self.fields[field].label
        return field.replace("_", " ").title()