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"""
|
||||
from typing import Optional
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.db.models import QuerySet
|
||||
from django.http.response import HttpResponseBadRequest
|
||||
|
@ -13,12 +11,7 @@ from drf_spectacular.utils import (
|
|||
inline_serializer,
|
||||
)
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.fields import (
|
||||
CharField,
|
||||
FileField,
|
||||
IntegerField,
|
||||
SerializerMethodField,
|
||||
)
|
||||
from rest_framework.fields import CharField, FileField, IntegerField, ReadOnlyField
|
||||
from rest_framework.parsers import MultiPartParser
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.response import Response
|
||||
|
@ -48,22 +41,10 @@ def user_app_cache_key(user_pk: str) -> str:
|
|||
class ApplicationSerializer(ModelSerializer):
|
||||
"""Application Serializer"""
|
||||
|
||||
launch_url = SerializerMethodField()
|
||||
launch_url = ReadOnlyField(source="get_launch_url")
|
||||
provider_obj = ProviderSerializer(source="get_provider", required=False)
|
||||
|
||||
meta_icon = SerializerMethodField()
|
||||
|
||||
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()
|
||||
meta_icon = ReadOnlyField(source="get_meta_icon")
|
||||
|
||||
class Meta:
|
||||
|
||||
|
@ -237,9 +218,9 @@ class ApplicationViewSet(ModelViewSet):
|
|||
"""Set application icon (as URL)"""
|
||||
app: Application = self.get_object()
|
||||
url = request.data.get("url", None)
|
||||
if not url:
|
||||
if url is None:
|
||||
return HttpResponseBadRequest()
|
||||
app.meta_icon = url
|
||||
app.meta_icon.name = url
|
||||
app.save()
|
||||
return Response({})
|
||||
|
||||
|
|
|
@ -224,6 +224,18 @@ class Application(PolicyBindingModel):
|
|||
meta_description = 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]:
|
||||
"""Get launch URL if set, otherwise attempt to get launch URL based on provider."""
|
||||
if self.meta_launch_url:
|
||||
|
|
|
@ -15481,7 +15481,6 @@ components:
|
|||
meta_launch_url:
|
||||
type: string
|
||||
format: uri
|
||||
maxLength: 200
|
||||
meta_icon:
|
||||
type: string
|
||||
nullable: true
|
||||
|
@ -15518,7 +15517,6 @@ components:
|
|||
meta_launch_url:
|
||||
type: string
|
||||
format: uri
|
||||
maxLength: 200
|
||||
meta_description:
|
||||
type: string
|
||||
meta_publisher:
|
||||
|
@ -21891,7 +21889,6 @@ components:
|
|||
meta_launch_url:
|
||||
type: string
|
||||
format: uri
|
||||
maxLength: 200
|
||||
meta_description:
|
||||
type: string
|
||||
meta_publisher:
|
||||
|
|
Reference in New Issue