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

Add URL to the vulnerability and package details view in the API serializers #1423

Merged
merged 7 commits into from
Feb 23, 2024

Conversation

TG1999
Copy link
Member

@TG1999 TG1999 commented Feb 19, 2024

Reference: #1419

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@TG1999 TG1999 changed the title Fix issue 1419 Fix issue #1419 Feb 19, 2024
@TG1999 TG1999 changed the title Fix issue #1419 Add URL to the vulnerability and package details view in the API serializers Feb 19, 2024
Copy link
Member

@tdruez tdruez left a comment

Choose a reason for hiding this comment

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

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@TG1999
Copy link
Member Author

TG1999 commented Feb 19, 2024

@tdruez we already have get_absolute_url method on both Package and Vulnerability models, but it returns /packages/pkg:foo/bar and not public.vulnerablecode.io/packages/pkg:foo/bar, I have added a new method get_details_url for the needed purpose. Please review, thanks!

Copy link
Member

@tdruez tdruez left a comment

Choose a reason for hiding this comment

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

Your implementation has too much duplication.
When copy/pasting things around in Python, it's usually time for refactoring.

You can create a generic mixin including:

    def get_resource_url(self, instance):
        """
        Return the instance fully qualified URL including the schema and domain.

        Usage:
            resource_url = serializers.SerializerMethodField()
        """
        resource_url = obj.get_absolute_url()

        if request := self.context.get("request", None):
            return request.build_absolute_uri(location=resource_url)

        return resource_url

Then simply define a clean field resource_url = serializers.SerializerMethodField() on your serializers instead of overriding the to_representation

Also, the tests are failing.

TG1999 and others added 3 commits February 23, 2024 17:45
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
@tdruez
Copy link
Member

tdruez commented Feb 23, 2024

@TG1999 LGTM but the documentation check is failing. Ready to merge otherwise.

@TG1999 TG1999 merged commit 4e5ef60 into nexB:main Feb 23, 2024
6 of 7 checks passed
@TG1999 TG1999 deleted the fix_issue_1419 branch February 23, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants