[chronojump-server] Fixed no image player creation or update



commit ca7f5549d361e4afaf22ee90777715a91b03d1f3
Author: Max Ros i Morejon <mros33 gmail com>
Date:   Fri Apr 5 11:20:42 2019 +0200

    Fixed no image player creation or update

 .../chronojump_networks/organizations/api/views.py | 38 +++++++++++++++-------
 1 file changed, 27 insertions(+), 11 deletions(-)
---
diff --git a/chronojumpserver-django/chronojump_networks/organizations/api/views.py 
b/chronojumpserver-django/chronojump_networks/organizations/api/views.py
index 73c09c8..29f410c 100644
--- a/chronojumpserver-django/chronojump_networks/organizations/api/views.py
+++ b/chronojumpserver-django/chronojump_networks/organizations/api/views.py
@@ -122,38 +122,54 @@ class PlayerListView(ListCreateAPIView):
         print("crea player")
         data = dict(request.data)
         # TODO: Sure there is a better way to do this
-        photo = data['image'][0]
-        new_photo = photo.name + '_' + str(int(time()))
-        print(new_photo)
+        try:
+            photo = data['image'][0]
+        except KeyError:
+            print("no photo")
+            newimage = '../static/images/no_image.png'
+        else:
+            print("yes photo")
+            newimage = 'players/' + new_photo
+            photo = data['image'][0]
+            new_photo = photo.name + '_' + str(int(time()))
+            full_path = os.path.join(settings.MEDIA_ROOT, 'players', new_photo)
+            path = default_storage.save(full_path, ContentFile(photo.read()))
+            print(new_photo)
         o = Player.objects.create(
             name = str(data['name'][0]),
-            image = 'players/' + new_photo,
+            image = newimage,
             number = int(data['number'][0]),
             height = float(data['height'][0]),
             weight = float(data['weight'][0]),
             rfid = str(data['rfid'][0]),
             organization_id = int(self.kwargs['organization_id'])
         )
-        full_path = os.path.join(settings.MEDIA_ROOT, 'players', new_photo)
-        path = default_storage.save(full_path, ContentFile(photo.read()))        
         s = PlayerSerializer(o)
         return Response(s.data)
     
     def put(self, request, *args, **kwars):
         print("update player view")
+        #PROBLEMES AMB ACCENTS AL NOM DEL PLAYER
         data = dict(request.data)
-        photo = data['image'][0]
-        new_photo = photo.name + '_' + str(int(time()))
         o = Player.objects.get(id=int(data['id'][0]))
         o.name = str(data['name'][0])
-        o.image = 'players/' + new_photo
         o.number = int(data['number'][0])
         o.height = float(data['height'][0])
         o.weight = float(data['weight'][0])
         o.rfid = str(data['rfid'][0])
         o.organization_id = int(self.kwargs['organization_id'])
-        full_path = os.path.join(settings.MEDIA_ROOT, 'players', new_photo)
-        path = default_storage.save(full_path, ContentFile(photo.read()))  
+        try:
+            photo = data['image'][0]
+        except KeyError:
+            print("no photo")
+            o.image = '../static/images/no_image.png'
+        else:
+            print("yes photo")
+            photo = data['image'][0]
+            new_photo = photo.name + '_' + str(int(time()))
+            o.image = 'players/' + new_photo
+            full_path = os.path.join(settings.MEDIA_ROOT, 'players', new_photo)
+            path = default_storage.save(full_path, ContentFile(photo.read()))  
         o.save()
         return JsonResponse({}, status=200, safe=False)
     


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]