Add URL to the vulnerability and package details view in the API serializers - #1423
Conversation
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
tdruez
left a comment
There was a problem hiding this comment.
- Use a common naming convention instead of
package_urlandvulnerability_url - The URL build logic should live on the model for a global usage, not on the API, see https://docs.djangoproject.com/en/5.0/ref/models/instances/#get-absolute-url
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
|
@tdruez we already have |
tdruez
left a comment
There was a problem hiding this comment.
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.
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
|
@TG1999 LGTM but the documentation check is failing. Ready to merge otherwise. |
… API serializers (aboutcode-org#1423) * Add vulnerability_url in API Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Add package_url in API Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Fix tests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Address review comments Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Address review comments Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> * Fix tests Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com> --------- Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
Reference: #1419