From 86915f6ec540c1a30f13517f461cd1fa2afe0336 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 20:21:30 +0200 Subject: [PATCH 1/9] Added list display of Places and PhotoAlbums admin pages. --- django_lostplaces/lostplaces/admin.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/django_lostplaces/lostplaces/admin.py b/django_lostplaces/lostplaces/admin.py index 338bf8a..ae08b2b 100644 --- a/django_lostplaces/lostplaces/admin.py +++ b/django_lostplaces/lostplaces/admin.py @@ -16,8 +16,14 @@ class VoucherAdmin(admin.ModelAdmin): fields = ['code', 'expires_when', 'created_when'] readonly_fields = ['created_when'] +class PhotoAlbumsAdmin(admin.ModelAdmin): + list_display = ('label', 'place', 'url' ) + +class PlacesAdmin(admin.ModelAdmin): + list_display = ('name', 'submitted_by', 'submitted_when') + admin.site.register(Explorer) admin.site.register(Voucher, VoucherAdmin) -admin.site.register(Place) +admin.site.register(Place, PlacesAdmin) admin.site.register(PlaceImage) -admin.site.register(PhotoAlbum) +admin.site.register(PhotoAlbum, PhotoAlbumsAdmin) From 715e953182a2b1cee3674c0fe0da3d9fefda7400 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 20:30:25 +0200 Subject: [PATCH 2/9] Changed __str__ repr of Vouchers. --- django_lostplaces/lostplaces/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/models.py b/django_lostplaces/lostplaces/models.py index 2fe6127..f35eefa 100644 --- a/django_lostplaces/lostplaces/models.py +++ b/django_lostplaces/lostplaces/models.py @@ -107,7 +107,7 @@ class Voucher(models.Model): expires_when = models.DateTimeField() def __str__(self): - return "Voucher " + str(self.code) + return "Voucher " + str(self.pk) class Place(Submittable, Taggable, Mapable): From c66baaa7654cb7ee4988db3ca76fd4cdddfa46d8 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 20:37:54 +0200 Subject: [PATCH 3/9] Simplified PlaceImage __str__ repr. --- django_lostplaces/lostplaces/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/models.py b/django_lostplaces/lostplaces/models.py index f35eefa..c1b09f8 100644 --- a/django_lostplaces/lostplaces/models.py +++ b/django_lostplaces/lostplaces/models.py @@ -176,7 +176,7 @@ class PlaceImage (Submittable): of this image as textual represntation of this instance """ - return ' '.join([self.place.name, str(self.pk)]) + return 'Image ' + str(self.pk) # These two auto-delete files from filesystem when they are unneeded: From e698f3b2245136c552daafde920aca9bcc0dad1b Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 20:40:58 +0200 Subject: [PATCH 4/9] Added VouchersAdmin and PlaceImagesAdmin. --- django_lostplaces/lostplaces/admin.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/django_lostplaces/lostplaces/admin.py b/django_lostplaces/lostplaces/admin.py index ae08b2b..be58dd5 100644 --- a/django_lostplaces/lostplaces/admin.py +++ b/django_lostplaces/lostplaces/admin.py @@ -15,6 +15,8 @@ from lostplaces.forms import ExplorerCreationForm, ExplorerChangeForm class VoucherAdmin(admin.ModelAdmin): fields = ['code', 'expires_when', 'created_when'] readonly_fields = ['created_when'] + list_display = ('__str__', 'code', 'created_when', 'expires_when') + # todo: is_valid (Wurstwasser) class PhotoAlbumsAdmin(admin.ModelAdmin): list_display = ('label', 'place', 'url' ) @@ -22,8 +24,11 @@ class PhotoAlbumsAdmin(admin.ModelAdmin): class PlacesAdmin(admin.ModelAdmin): list_display = ('name', 'submitted_by', 'submitted_when') +class PlaceImagesAdmin(admin.ModelAdmin): + list_display = ('__str__', 'place', 'submitted_by') + admin.site.register(Explorer) admin.site.register(Voucher, VoucherAdmin) admin.site.register(Place, PlacesAdmin) -admin.site.register(PlaceImage) +admin.site.register(PlaceImage, PlaceImagesAdmin) admin.site.register(PhotoAlbum, PhotoAlbumsAdmin) From a3b123a207fdd58d5107eb31bca6f124355a8fe5 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 21:02:07 +0200 Subject: [PATCH 5/9] Added valid property to VoucherModel. --- django_lostplaces/lostplaces/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/django_lostplaces/lostplaces/models.py b/django_lostplaces/lostplaces/models.py index c1b09f8..2d23007 100644 --- a/django_lostplaces/lostplaces/models.py +++ b/django_lostplaces/lostplaces/models.py @@ -15,6 +15,7 @@ from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver from django.core.validators import MaxValueValidator, MinValueValidator +from django.utils import timezone from easy_thumbnails.fields import ThumbnailerImageField from easy_thumbnails.files import get_thumbnailer from taggit.managers import TaggableManager @@ -106,6 +107,10 @@ class Voucher(models.Model): created_when = models.DateTimeField(auto_now_add=True) expires_when = models.DateTimeField() + @property + def valid(self): + return timezone.now() <= self.expires_when + def __str__(self): return "Voucher " + str(self.pk) From 2bff4db1d7a0b500c0fd80800085a510ebc7757d Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 21:02:45 +0200 Subject: [PATCH 6/9] Added Voucher expiration check in form using model property. --- django_lostplaces/lostplaces/forms.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/django_lostplaces/lostplaces/forms.py b/django_lostplaces/lostplaces/forms.py index 86f291c..17e51cc 100644 --- a/django_lostplaces/lostplaces/forms.py +++ b/django_lostplaces/lostplaces/forms.py @@ -26,6 +26,10 @@ class ExplorerCreationForm(UserCreationForm): self.add_error('voucher', 'Invalid voucher') return False + if not submitted_voucher.valid: + self.add_error('voucher', 'Expired voucher') + return False + fetched_voucher.delete() return True From 6a1229a4baed1d262a89e5af1e8c88ed7ad05de0 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 21:03:05 +0200 Subject: [PATCH 7/9] Added valid column to VouchersAdmin. --- django_lostplaces/lostplaces/admin.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/django_lostplaces/lostplaces/admin.py b/django_lostplaces/lostplaces/admin.py index be58dd5..d56db16 100644 --- a/django_lostplaces/lostplaces/admin.py +++ b/django_lostplaces/lostplaces/admin.py @@ -15,8 +15,7 @@ from lostplaces.forms import ExplorerCreationForm, ExplorerChangeForm class VoucherAdmin(admin.ModelAdmin): fields = ['code', 'expires_when', 'created_when'] readonly_fields = ['created_when'] - list_display = ('__str__', 'code', 'created_when', 'expires_when') - # todo: is_valid (Wurstwasser) + list_display = ('__str__', 'code', 'created_when', 'expires_when', 'valid') class PhotoAlbumsAdmin(admin.ModelAdmin): list_display = ('label', 'place', 'url' ) From 222c97d63c34ab56c3bcbb299c6cf70a0eee7360 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 21:19:31 +0200 Subject: [PATCH 8/9] Added voucher validity boolean icon display. --- django_lostplaces/lostplaces/admin.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/django_lostplaces/lostplaces/admin.py b/django_lostplaces/lostplaces/admin.py index d56db16..977bc23 100644 --- a/django_lostplaces/lostplaces/admin.py +++ b/django_lostplaces/lostplaces/admin.py @@ -17,6 +17,11 @@ class VoucherAdmin(admin.ModelAdmin): readonly_fields = ['created_when'] list_display = ('__str__', 'code', 'created_when', 'expires_when', 'valid') + def valid(self, instance): + return timezone.now() <= instance.expires_when + + valid.boolean = True + class PhotoAlbumsAdmin(admin.ModelAdmin): list_display = ('label', 'place', 'url' ) From 17b531e181e691055797ec95a030c399f4dbcb8f Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Fri, 18 Sep 2020 21:26:05 +0200 Subject: [PATCH 9/9] Removed __str__ repr of Voucher and PlaceImage. --- .../lostplaces/tests/models/test_place_image_model.py | 7 ------- .../lostplaces/tests/models/test_voucher_model.py | 8 -------- 2 files changed, 15 deletions(-) diff --git a/django_lostplaces/lostplaces/tests/models/test_place_image_model.py b/django_lostplaces/lostplaces/tests/models/test_place_image_model.py index d351b66..c1249a7 100644 --- a/django_lostplaces/lostplaces/tests/models/test_place_image_model.py +++ b/django_lostplaces/lostplaces/tests/models/test_place_image_model.py @@ -83,13 +83,6 @@ class PlaceImageTestCase(ModelTestCase): ) ) - def test_str(self): - self.assertTrue(self.place_image.place.name.lower() in str(self.place_image).lower(), - msg='Expecting %s.__str__ to contain the name of the place' % ( - self.model.__name__ - ) - ) - def test_change_filename(self): path = self.place_image.filename.path self.place_image.filename = os.path.join(settings.MEDIA_ROOT, 'im_a_image_changed.jpeg') diff --git a/django_lostplaces/lostplaces/tests/models/test_voucher_model.py b/django_lostplaces/lostplaces/tests/models/test_voucher_model.py index 06964d8..56133a9 100644 --- a/django_lostplaces/lostplaces/tests/models/test_voucher_model.py +++ b/django_lostplaces/lostplaces/tests/models/test_voucher_model.py @@ -42,11 +42,3 @@ class VoucheTestCase(ModelTestCase): field_class=models.DateTimeField, must_not_have={'auto_now_add': True} ) - - def test_str(self): - self.assertTrue( - self.voucher.code.lower() in str(self.voucher).lower(), - msg='Expecting %s.__str__ to contain the voucher code' % ( - self.model.__name__ - ) - )