core: improve API validation for Application's set_icon_url (fix JSON Syntax Error)
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
06b7f62a40
commit
ed3859800c
|
@ -1,6 +1,4 @@
|
||||||
"""Application API Views"""
|
"""Application API Views"""
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from django.core.cache import cache
|
from django.core.cache import cache
|
||||||
from django.db.models import QuerySet
|
from django.db.models import QuerySet
|
||||||
from django.http.response import HttpResponseBadRequest
|
from django.http.response import HttpResponseBadRequest
|
||||||
|
@ -13,12 +11,7 @@ from drf_spectacular.utils import (
|
||||||
inline_serializer,
|
inline_serializer,
|
||||||
)
|
)
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from rest_framework.fields import (
|
from rest_framework.fields import CharField, FileField, IntegerField, ReadOnlyField
|
||||||
CharField,
|
|
||||||
FileField,
|
|
||||||
IntegerField,
|
|
||||||
SerializerMethodField,
|
|
||||||
)
|
|
||||||
from rest_framework.parsers import MultiPartParser
|
from rest_framework.parsers import MultiPartParser
|
||||||
from rest_framework.request import Request
|
from rest_framework.request import Request
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
@ -48,22 +41,10 @@ def user_app_cache_key(user_pk: str) -> str:
|
||||||
class ApplicationSerializer(ModelSerializer):
|
class ApplicationSerializer(ModelSerializer):
|
||||||
"""Application Serializer"""
|
"""Application Serializer"""
|
||||||
|
|
||||||
launch_url = SerializerMethodField()
|
launch_url = ReadOnlyField(source="get_launch_url")
|
||||||
provider_obj = ProviderSerializer(source="get_provider", required=False)
|
provider_obj = ProviderSerializer(source="get_provider", required=False)
|
||||||
|
|
||||||
meta_icon = SerializerMethodField()
|
meta_icon = ReadOnlyField(source="get_meta_icon")
|
||||||
|
|
||||||
def get_meta_icon(self, instance: Application) -> Optional[str]:
|
|
||||||
"""When meta_icon was set to a URL, return the name as-is"""
|
|
||||||
if not instance.meta_icon:
|
|
||||||
return None
|
|
||||||
if instance.meta_icon.name.startswith("http"):
|
|
||||||
return instance.meta_icon.name
|
|
||||||
return instance.meta_icon.url
|
|
||||||
|
|
||||||
def get_launch_url(self, instance: Application) -> Optional[str]:
|
|
||||||
"""Get generated launch URL"""
|
|
||||||
return instance.get_launch_url()
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
|
@ -237,9 +218,9 @@ class ApplicationViewSet(ModelViewSet):
|
||||||
"""Set application icon (as URL)"""
|
"""Set application icon (as URL)"""
|
||||||
app: Application = self.get_object()
|
app: Application = self.get_object()
|
||||||
url = request.data.get("url", None)
|
url = request.data.get("url", None)
|
||||||
if not url:
|
if url is None:
|
||||||
return HttpResponseBadRequest()
|
return HttpResponseBadRequest()
|
||||||
app.meta_icon = url
|
app.meta_icon.name = url
|
||||||
app.save()
|
app.save()
|
||||||
return Response({})
|
return Response({})
|
||||||
|
|
||||||
|
|
|
@ -224,6 +224,18 @@ class Application(PolicyBindingModel):
|
||||||
meta_description = models.TextField(default="", blank=True)
|
meta_description = models.TextField(default="", blank=True)
|
||||||
meta_publisher = models.TextField(default="", blank=True)
|
meta_publisher = models.TextField(default="", blank=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def get_meta_icon(self) -> Optional[str]:
|
||||||
|
"""Get the URL to the App Icon image. If the name is /static or starts with http
|
||||||
|
it is returned as-is"""
|
||||||
|
if not self.meta_icon:
|
||||||
|
return None
|
||||||
|
if self.meta_icon.name.startswith("http") or self.meta_icon.name.startswith(
|
||||||
|
"/static"
|
||||||
|
):
|
||||||
|
return self.meta_icon.name
|
||||||
|
return self.meta_icon.url
|
||||||
|
|
||||||
def get_launch_url(self) -> Optional[str]:
|
def get_launch_url(self) -> Optional[str]:
|
||||||
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
|
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
|
||||||
if self.meta_launch_url:
|
if self.meta_launch_url:
|
||||||
|
|
|
@ -15481,7 +15481,6 @@ components:
|
||||||
meta_launch_url:
|
meta_launch_url:
|
||||||
type: string
|
type: string
|
||||||
format: uri
|
format: uri
|
||||||
maxLength: 200
|
|
||||||
meta_icon:
|
meta_icon:
|
||||||
type: string
|
type: string
|
||||||
nullable: true
|
nullable: true
|
||||||
|
@ -15518,7 +15517,6 @@ components:
|
||||||
meta_launch_url:
|
meta_launch_url:
|
||||||
type: string
|
type: string
|
||||||
format: uri
|
format: uri
|
||||||
maxLength: 200
|
|
||||||
meta_description:
|
meta_description:
|
||||||
type: string
|
type: string
|
||||||
meta_publisher:
|
meta_publisher:
|
||||||
|
@ -21891,7 +21889,6 @@ components:
|
||||||
meta_launch_url:
|
meta_launch_url:
|
||||||
type: string
|
type: string
|
||||||
format: uri
|
format: uri
|
||||||
maxLength: 200
|
|
||||||
meta_description:
|
meta_description:
|
||||||
type: string
|
type: string
|
||||||
meta_publisher:
|
meta_publisher:
|
||||||
|
|
Reference in New Issue