summaryrefslogtreecommitdiffstats
path: root/combo/apps/assets/api_views.py
diff options
context:
space:
mode:
Diffstat (limited to 'combo/apps/assets/api_views.py')
-rw-r--r--combo/apps/assets/api_views.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/combo/apps/assets/api_views.py b/combo/apps/assets/api_views.py
new file mode 100644
index 0000000..07f2da2
--- /dev/null
+++ b/combo/apps/assets/api_views.py
@@ -0,0 +1,58 @@
+# combo - content management system
+# Copyright (C) 2019 Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import base64
+
+from django.core.files import File
+from django.utils.six import BytesIO
+
+from rest_framework import serializers, permissions, status
+from rest_framework.generics import GenericAPIView
+from rest_framework.response import Response
+
+from .models import Asset
+
+
+class FileSerializer(serializers.Serializer):
+ content = serializers.CharField(required=True, allow_blank=False)
+ content_type = serializers.CharField(required=False, allow_null=True)
+ filename = serializers.CharField(required=False, allow_null=True)
+
+
+class AssetSerializer(serializers.Serializer):
+ asset = FileSerializer(required=True)
+
+
+class Set(GenericAPIView):
+ permission_classes = (permissions.IsAuthenticated,)
+ serializer_class = AssetSerializer
+
+ def post(self, request, key, *args, **kwargs):
+ serializer = self.get_serializer(data=request.data)
+ if not serializer.is_valid():
+ response = {'err': 1, 'err_desc': serializer.errors}
+ return Response(response, status.HTTP_400_BAD_REQUEST)
+ data = serializer.validated_data
+
+ asset, created = Asset.objects.get_or_create(key=key)
+ asset.asset = File(
+ BytesIO(base64.decodestring(data['asset']['content'].encode('ascii'))),
+ name=data['asset'].get('filename'))
+ asset.save()
+ response = {'err': 0}
+ return Response(response)
+
+view_set = Set.as_view()