Skip to content

Commit

Permalink
Don't save vulnerability and security scan history. Fix network ip
Browse files Browse the repository at this point in the history
address save
  • Loading branch information
hipek8 committed Oct 10, 2024
1 parent 88c51a6 commit fa7940f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
23 changes: 14 additions & 9 deletions src/ralph/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,27 @@ class NestedSerializer(RalphAPISerializer):
return field_class, field_kwargs


class ReversionHistoryAPISerializerMixin(object):
class ReversionHistoryAPISerializerMixin(serializers.ModelSerializer):
@property
def _save_history(self) -> bool:
try:
return self.Meta.save_history
except AttributeError:
return True

def save(self):
instance = None
with transaction.atomic(), reversion.create_revision():
reversion.set_comment('API Save')
reversion.set_user(self.context['request'].user)
instance = super().save()

return instance
if self._save_history:
with transaction.atomic(), reversion.create_revision():
reversion.set_comment('API Save')
reversion.set_user(self.context['request'].user)
return super().save()
else:
return super().save()


class RalphAPISaveSerializer(
ReversionHistoryAPISerializerMixin,
TaggitSerializer,
serializers.ModelSerializer,
metaclass=DeclaredFieldsMetaclass
):
serializer_choice_field = ReversedChoiceField
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/lib/mixins/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class MemorizeBeforeStateMixin(object):

def save_model(self, request, obj, form, change):
if change and obj.pk:
obj._before_state = obj._default_manager.get(pk=obj.pk)
obj._before_state = obj._meta.default_manager.get(pk=obj.pk)
super().save_model(request, obj, form, change)


Expand Down
3 changes: 3 additions & 0 deletions src/ralph/security/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Meta:
model = Vulnerability
depth = 1
fields = "__all__"
save_history = False



class VulnerabilityViewSet(RalphAPIViewSet):
Expand All @@ -40,6 +42,7 @@ class SaveSecurityScanSerializer(RalphAPISaveSerializer):
class Meta:
model = SecurityScan
fields = "__all__"
save_history = False

def to_internal_value(self, data):
errors = OrderedDict()
Expand Down
2 changes: 1 addition & 1 deletion src/ralph/security/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class SecurityScan(
vulnerabilities = models.ManyToManyField(Vulnerability, blank=True)
# this is a quirk field, it is updated manually (for now it's in API)
# this is because it's hard to handling it automatically
# (its value is computated depending on M2M field and M2M signals are
# (its value is computed depending on M2M field and M2M signals are
# complicated)
is_patched = models.BooleanField(default=False)

Expand Down

0 comments on commit fa7940f

Please sign in to comment.