Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move common functionality to destination base class #936

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

hmpf
Copy link
Contributor

@hmpf hmpf commented Nov 11, 2024

Best reviewed per file.

@hmpf hmpf requested review from johannaengland, stveit and a team November 11, 2024 14:24
@hmpf hmpf changed the title move common functionality to base class Move common functionality to destination base class Nov 11, 2024
@hmpf hmpf self-assigned this Nov 12, 2024
@hmpf hmpf added the refactor label Nov 12, 2024
@hmpf hmpf force-pushed the cleanup-destination-plugins branch from c39d564 to 55a2ff0 Compare November 13, 2024 09:23
@hmpf hmpf force-pushed the cleanup-destination-plugins branch from 55a2ff0 to 5989b0a Compare December 2, 2024 12:39
@hmpf hmpf marked this pull request as ready for review December 3, 2024 08:53
@hmpf hmpf requested a review from lunkwill42 December 3, 2024 08:53
@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 76.10619% with 27 lines in your changes missing coverage. Please review.

Project coverage is 82.45%. Comparing base (c83f348) to head (3131826).

Files with missing lines Patch % Lines
src/argus/notificationprofile/media/base.py 75.86% 14 Missing ⚠️
src/argus/notificationprofile/media/email.py 69.69% 10 Missing ⚠️
src/argus/notificationprofile/serializers.py 87.50% 2 Missing ⚠️
...rc/argus/notificationprofile/media/sms_as_email.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #936   +/-   ##
=======================================
  Coverage   82.44%   82.45%           
=======================================
  Files         132      132           
  Lines        4866     4884   +18     
=======================================
+ Hits         4012     4027   +15     
- Misses        854      857    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hmpf hmpf requested a review from stveit December 3, 2024 12:25
src/argus/notificationprofile/media/base.py Show resolved Hide resolved
src/argus/notificationprofile/media/base.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/media/base.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/media/email.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/media/base.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/media/base.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/media/base.py Outdated Show resolved Hide resolved
src/argus/notificationprofile/serializers.py Show resolved Hide resolved
@hmpf hmpf force-pushed the cleanup-destination-plugins branch from 3b78212 to 7b8826c Compare December 4, 2024 06:51
@hmpf hmpf requested a review from johannaengland December 4, 2024 10:44
@hmpf hmpf force-pushed the cleanup-destination-plugins branch from 0e23752 to 3131826 Compare December 4, 2024 13:52
Copy link

sonarcloud bot commented Dec 4, 2024

Copy link
Contributor

@johannaengland johannaengland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and we don't have a test for trying to PATCH update with medium being empty, that should be added, I think then it would be clearer if the changes work or not

We do have tests for when trying to change the medium that that will lead to an error

if there is only one piece of data you need to send the notification. Among
other things, it is used to check for duplicate entries, so in a way it acts
as the primary key for your plugin. For that reason, it must be required in
the json schema.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
the json schema.
the json schema. For example for email it is ``"email_address"`` or for SMS
it is ``"phone_number"``.

cls, data: dict, user: User, instance: DestinationConfig = None, exception_class=ValidationError
) -> dict:
if instance:
if data.get("media", "") != instance.media.slug:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in the case of updating an instance (PATCH) when no media is given then this will raise this error

Suggested change
if data.get("media", "") != instance.media.slug:
if "media" in data.keys() and data["media"] != instance.media.slug:

if self.instance:
medium = api_safely_get_medium_object(self.instance.media.slug)
# A PATCH need not contain the media key
if attrs.get("media", None) != self.instance.media:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also here: If we have PATCH and no media in the attrs, then this will fail since None != EmailMedium for example

Suggested change
if attrs.get("media", None) != self.instance.media:
if "media" in attrs.keys() and attrs["media"] != self.instance.media:

@hmpf
Copy link
Contributor Author

hmpf commented Dec 6, 2024

I just checked and we don't have a test for trying to PATCH update with medium being empty, that should be added, I think then it would be clearer if the changes work or not

We do have tests for when trying to change the medium that that will lead to an error

We are now way out of scope for a refactor.

I guess the underlying problem is that we cannot lookup and change destinations of a specific media type directly, as part of the url of the endpoint. If we could, we would never need bother with media in the update-serializer.

@johannaengland
Copy link
Contributor

I just checked and we don't have a test for trying to PATCH update with medium being empty, that should be added, I think then it would be clearer if the changes work or not
We do have tests for when trying to change the medium that that will lead to an error

We are now way out of scope for a refactor.

Yes, I agree, I can do this in another PR, but that would have to merged before this then, because it would show if the functionality is staying the same

@hmpf
Copy link
Contributor Author

hmpf commented Dec 6, 2024

I don't think it is wise to stress with finishing this PR this week anymore, so start on the patch-PR next week.

def validate(cls, instance: RequestDestinationConfigSerializer, dict: dict, user: User) -> dict:
def validate(
cls, data: dict, user: User, instance: DestinationConfig = None, exception_class=ValidationError
) -> dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return type should be CommonDestinationConfigForm?

if not form.is_valid():
raise exception_class(form.errors)

settings_form = cls.validate_settings(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
settings_form = cls.validate_settings(data)
settings_form = cls.validate_settings(data, user)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Changes requested
Development

Successfully merging this pull request may close these issues.

4 participants