From 7aeea66a79ce35c7b949c5e8f6ad514f3817310f Mon Sep 17 00:00:00 2001
From: Andy Summers <andrew.summers@wisc.edu>
Date: Thu, 29 Nov 2018 14:22:48 -0600
Subject: [PATCH] Fix failing tests with POSTs and PUTs

---
 .gitignore                           | 20 +++++++++++++++++---
 spotseeker_server/models.py          |  3 +++
 spotseeker_server/urls.py            |  5 ++++-
 spotseeker_server/views/spot_item.py | 18 ++++++++++++++----
 4 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/.gitignore b/.gitignore
index 1443a8e..11f8422 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,7 +1,21 @@
 .vscode/
 *.py[cod]
 *.egg-info/
-space_images/
-item_images/
 dist/
-build/
\ No newline at end of file
+build/
+*.gif
+*.jpg
+*.png
+*.bmp
+spotseeker_server/test/resources/**/image*
+spotseeker_server/test/resources/space_images
+spotseeker_server/test/resources/item_images
+!fake_jpeg.jpg
+!test_bmp.bmp
+!test_bmp2.bmp
+!test_gif.gif
+!test_gif2.gif
+!test_jpeg.jpg
+!test_jpep2.jpg
+!test_png.png
+!test_png2.png
diff --git a/spotseeker_server/models.py b/spotseeker_server/models.py
index ed39fb0..cb2e10f 100644
--- a/spotseeker_server/models.py
+++ b/spotseeker_server/models.py
@@ -504,6 +504,9 @@ class ItemExtendedInfo(models.Model):
         verbose_name_plural = "Item extended info"
         unique_together = ('item', 'key')
 
+    def __str__(self):
+        return f"({self.pk}) {self.key}: {self.value} item: {self.item.id}"
+
 
 class ItemImage(models.Model):
     """ An image of a Item. Multiple images can be associated with a Item,
diff --git a/spotseeker_server/urls.py b/spotseeker_server/urls.py
index 1c4ef59..e951d9e 100644
--- a/spotseeker_server/urls.py
+++ b/spotseeker_server/urls.py
@@ -65,7 +65,7 @@ urlpatterns = [
     path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>',
          csrf_exempt(ThumbnailView().run),
          {'constrain': True}),
-    path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/',
+    path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb',
          csrf_exempt(ThumbnailView().run),
          name='spot-image-thumb'),
     path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/<str:thumb_dimensions>',
@@ -76,6 +76,9 @@ urlpatterns = [
     path('v1/item/<int:item_id>/image/<int:image_id>',
          csrf_exempt(ItemImageView().run),
          name='item-image'),
+    path('v1/item/<int:item_id>/image/<int:image_id>/thumb',
+        csrf_exempt(ItemThumbnailView().run),
+        name='item-image-thumb'),
     path('v1/item/<int:item_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>',
         csrf_exempt(ItemThumbnailView().run),
         {'constrain': True}),
diff --git a/spotseeker_server/views/spot_item.py b/spotseeker_server/views/spot_item.py
index c177793..02541b6 100644
--- a/spotseeker_server/views/spot_item.py
+++ b/spotseeker_server/views/spot_item.py
@@ -86,6 +86,9 @@ class ItemStash(object):
     def get_instance(self):
         return self.instance
 
+    def __str__(self):
+        return str(self.json)
+
 
 @django.dispatch.receiver(
     spot_pre_build,
@@ -200,9 +203,9 @@ def clean_ei(old_ei_list, new_ei_forms):
         found = False
 
         for ei_form in new_ei_forms:
-            if ei_form.fields['key'] == old_ei.key:
+            if ei_form.cleaned_data['key'] == old_ei.key:
                 found = True
-                if ei_form.fields['value'] == old_ei.value:
+                if ei_form.cleaned_data['value'] == old_ei.value:
                     forms_to_remove.append(ei_form)
                 else:
                     ei_form.instance = old_ei
@@ -250,8 +253,15 @@ def _save_items(sender, **kwargs):
         ei_forms = item.get_ei_forms()
 
         for item_ei in ei_forms:
-            # save the new EI
-            item_ei_model = item_ei.save(commit=False)
+            # Check if an ItemExtendedInfo with this key and Item already exists,
+            # create if it doesn't
+            item_ei_model, created = ItemExtendedInfo.objects.get_or_create(
+                key=item_ei.cleaned_data['key'],
+                item_id=item_model.id,
+                defaults=item_ei.cleaned_data
+            )
+            if not created and item_ei_model.value != item_ei.cleaned_data['value']:
+                item_ei_model.value = item_ei.cleaned_data['value']
             item_ei_model.item = item_model
             item_ei_model.save()
 
-- 
GitLab