def put(self, request, pk, format=None):
        gallery = self.get_object(pk)
        serializer = GallerySerializer(gallery, data=request.DATA)
        if serializer.is_valid():
            serializer.save()

            sort_by = 0

            current_element_ids = GalleryElement.objects.filter(gallery=gallery).values_list("element__id",flat=True)
            delete_element_ids = []
            if "element_id" in request.DATA and len(request.DATA.getlist("element_id"))>0:
                element_ids = request.DATA.getlist("element_id")
                credits = request.DATA.getlist("element_credit")
                descriptions = request.DATA.getlist("element_description")

                for element_id in current_element_ids:
                    if str(element_id) not in element_ids:
                        delete_element_ids.append(element_id)

                #Delete elements
                for element_id in delete_element_ids:
                    element = Element.objects.get(id=element_id)
                    galleryelement = GalleryElement.objects.get(gallery=gallery,element=element)
                    galleryelement.delete()

                #Add elements
                gallery = Gallery.objects.get(id=serializer.data["id"])
                count = 0
                for element_id in element_ids:
                    if not element_id:
                        count += 1
                        continue
                    if Element.objects.filter(id=element_id).exists():
                        element = Element.objects.get(id=element_id)
                        if GalleryElement.objects.filter(gallery=gallery,element=element).exists():
                            galleryelement = GalleryElement.objects.get(gallery=gallery,element=element)
                        else:
                            galleryelement = GalleryElement()
                            galleryelement.gallery = gallery
                            galleryelement.element = element
                        galleryelement.credit = credits[count]
                        galleryelement.description = descriptions[count]
                        galleryelement.sort_by = sort_by
                        galleryelement.save()
                        sort_by += 1
                    count += 1

                #Save the gallery again so the thumbnail_image_url is set
                gallery.save()

                serializer = GallerySerializer(gallery)

            return response.Response(serializer.data)

        return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def put(self, request, pk, format=None):
        gallery = self.get_object(pk)
        serializer = GallerySerializer(gallery, data=request.DATA)
        if serializer.is_valid():
            serializer.save()

            sort_by = 0

            current_element_ids = GalleryElement.objects.filter(gallery=gallery).values_list("element__id",flat=True)
            delete_element_ids = []
            if "element_id" in request.DATA and len(request.DATA.getlist("element_id"))>0:
                element_ids = request.DATA.getlist("element_id")
                credits = request.DATA.getlist("element_credit")
                descriptions = request.DATA.getlist("element_description")

                for element_id in current_element_ids:
                    if str(element_id) not in element_ids:
                        delete_element_ids.append(element_id)

                #Delete elements
                for element_id in delete_element_ids:
                    element = Element.objects.get(id=element_id)
                    galleryelement = GalleryElement.objects.get(gallery=gallery,element=element)
                    galleryelement.delete()

                #Add elements
                gallery = Gallery.objects.get(id=serializer.data["id"])
                count = 0
                for element_id in element_ids:
                    if not element_id:
                        count += 1
                        continue
                    if Element.objects.filter(id=element_id).exists():
                        element = Element.objects.get(id=element_id)
                        if GalleryElement.objects.filter(gallery=gallery,element=element).exists():
                            galleryelement = GalleryElement.objects.get(gallery=gallery,element=element)
                        else:
                            galleryelement = GalleryElement()
                            galleryelement.gallery = gallery
                            galleryelement.element = element
                        galleryelement.credit = credits[count]
                        galleryelement.description = descriptions[count]
                        galleryelement.sort_by = sort_by
                        galleryelement.save()
                        sort_by += 1
                    count += 1

                #Save the gallery again so the thumbnail_image_url is set
                gallery.save()

                serializer = GallerySerializer(gallery)

            return response.Response(serializer.data)

        return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def post(self, request, format=None):
        serializer = GallerySerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            #Add the Gallery elements if element_id is present
            sort_by = 0
            if "element_id" in request.DATA and len(request.DATA.getlist("element_id"))>0:
                element_ids = request.DATA.getlist("element_id")
                credits = request.DATA.getlist("element_credit")
                descriptions = request.DATA.getlist("element_description")
                gallery = Gallery.objects.get(id=serializer.data["id"])
                count = 0
                for element_id in element_ids:
                    if not element_id:
                        count += 1
                        continue
                    if Element.objects.filter(id=element_id).exists():
                        element = Element.objects.get(id=element_id)
                        if GalleryElement.objects.filter(gallery=gallery,element=element).exists():
                            galleryelement = GalleryElement.objects.get(gallery=gallery,element=element)
                        else:
                            galleryelement = GalleryElement()
                            galleryelement.gallery = gallery
                            galleryelement.element = element

                        try:
                            galleryelement.credit = credits[count]
                        except Exception as e:
                            pass
                        try:
                            galleryelement.description = descriptions[count]
                        except Exception as e:
                            pass

                        galleryelement.sort_by = sort_by
                        galleryelement.save()
                        sort_by += 1
                    count += 1

                #Save the gallery again so the thumbnail_image_url is set
                gallery.save()

                serializer = GallerySerializer(self.get_queryset(gallery.id), many=True)

            return response.Response(serializer.data)

        return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    def post(self, request, format=None):
        serializer = GallerySerializer(data=request.DATA)
        if serializer.is_valid():
            serializer.save()
            # Add the Gallery elements if element_id is present
            sort_by = 0
            if "element_id" in request.DATA and len(request.DATA.getlist("element_id")) > 0:
                element_ids = request.DATA.getlist("element_id")
                credits = request.DATA.getlist("element_credit")
                descriptions = request.DATA.getlist("element_description")
                gallery = Gallery.objects.get(id=serializer.data["id"])
                count = 0
                for element_id in element_ids:
                    if not element_id:
                        count += 1
                        continue
                    if Element.objects.filter(id=element_id).exists():
                        element = Element.objects.get(id=element_id)
                        if GalleryElement.objects.filter(gallery=gallery, element=element).exists():
                            galleryelement = GalleryElement.objects.get(gallery=gallery, element=element)
                        else:
                            galleryelement = GalleryElement()
                            galleryelement.gallery = gallery
                            galleryelement.element = element

                        try:
                            galleryelement.credit = credits[count]
                        except Exception as e:
                            pass
                        try:
                            galleryelement.description = descriptions[count]
                        except Exception as e:
                            pass

                        galleryelement.sort_by = sort_by
                        galleryelement.save()
                        sort_by += 1
                    count += 1

                # Save the gallery again so the thumbnail_image_url is set
                gallery.save()

                serializer = GallerySerializer(self.get_queryset(gallery.id), many=True)

            return response.Response(serializer.data)

        return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)