From 73a6a4f8d8d268bc74088f6392f4135285a949a5 Mon Sep 17 00:00:00 2001 From: Leonhard Strohmidel Date: Thu, 30 Jul 2020 10:51:05 +0200 Subject: [PATCH] Comments and Codeformatting --- lostplaces/lostplaces_app/admin.py | 1 - lostplaces/lostplaces_app/models.py | 51 +++++++++++++++++++++-------- lostplaces/lostplaces_app/urls.py | 2 +- lostplaces/lostplaces_app/views.py | 18 ++++++++-- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/lostplaces/lostplaces_app/admin.py b/lostplaces/lostplaces_app/admin.py index 2da6230..f944604 100644 --- a/lostplaces/lostplaces_app/admin.py +++ b/lostplaces/lostplaces_app/admin.py @@ -15,6 +15,5 @@ class ExplorerAdmin(UserAdmin): list_display = ['email', 'username',] admin.site.register(Explorer, ExplorerAdmin) - admin.site.register(Place) admin.site.register(PlaceImage) \ No newline at end of file diff --git a/lostplaces/lostplaces_app/models.py b/lostplaces/lostplaces_app/models.py index 42d3bae..ceb6c36 100644 --- a/lostplaces/lostplaces_app/models.py +++ b/lostplaces/lostplaces_app/models.py @@ -8,17 +8,27 @@ from django_thumbs.fields import ImageThumbsField # Create your models here. -# Custom user model class Explorer(AbstractUser): - # add additional fields in here + """ + Custom user model + Addtional fields wbd + """ def __str__(self): return self.username -# Place defines a lost place (location, name, description etc.). class Place (models.Model): + """ + Place defines a lost place (location, name, description etc.). + """ name = models.CharField(max_length=50) - submitted_by = models.ForeignKey(Explorer, on_delete=models.SET_NULL, null=True, blank=True, related_name='places') + submitted_by = models.ForeignKey( + Explorer, + on_delete=models.SET_NULL, + null=True, + blank=True, + related_name='places' + ) location = models.CharField(max_length=50) latitude = models.FloatField() longitude = models.FloatField() @@ -27,26 +37,41 @@ class Place (models.Model): def __str__(self): return self.name -# Define callback that generates /path/to/image.ext as filename. -def generate_image_upload_path(instance, filename): - return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1] class PlaceImage (models.Model): - filename = ImageThumbsField( - upload_to=generate_image_upload_path, - max_length=50, - sizes=( + + SIZES=( {'code': 'thumbnail', 'wxh': '390x390'}, {'code': 'hero', 'wxh': '700x700'}, {'code': 'large', 'wxh': '1920x1920'} - ) ) - place = models.ForeignKey(Place, on_delete=models.CASCADE, related_name='images') + + filename = ImageThumbsField( + upload_to=generate_image_upload_path, + max_length=50, + sizes=self.SIZES + ) + place = models.ForeignKey( + Place, + on_delete=models.CASCADE, + related_name='images' + ) description = models.TextField(blank=True) def __str__(self): + """ + Returning the name of the corresponding place + id + of this image as textual represntation of this instance + """ return ' '.join([self.place.name, str(self.pk)]) + def generate_image_upload_path(self, filename): + """ + Callback for generating path for uploaded images + """ + return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1] + + # These two auto-delete files from filesystem when they are unneeded: @receiver(models.signals.post_delete, sender=PlaceImage) diff --git a/lostplaces/lostplaces_app/urls.py b/lostplaces/lostplaces_app/urls.py index 373a30f..663a9f1 100644 --- a/lostplaces/lostplaces_app/urls.py +++ b/lostplaces/lostplaces_app/urls.py @@ -7,5 +7,5 @@ urlpatterns = [ path('place//', place_detail_view, name='place_detail'), path('place/create/', PlaceEditView.as_view(), name='place_create'), path('place/edit//', PlaceEditView.as_view(), name='place_edit'), - path('place/', place_list_view) + path('place/', place_list_view, name='place_list') ] \ No newline at end of file diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index 19b165e..b04181f 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -23,20 +23,32 @@ def hello_world(request): return render(request, 'hello_world.html', {'text':'Hello World!'}) class PlaceEditView(View): + def get(self, request, *args, **kwargs): if 'pk' in kwargs: place = get_object_or_404(Place,pk=kwargs['pk']) place_form = PlaceForm(instance=place) else: place_form = PlaceForm() - return render(request, 'create_place.html', {'form':place_form}) + context = { + 'form': place_form + } + return render(request, 'create_place.html', context) + def post(self, request, *args, **kwargs): place_form = PlaceForm(request.POST) + if place_form.is_valid() == True: instance = place_form.save(commit=False) # Save logged in user as "submitted_by" instance.submitted_by = request.user instance.save() - return redirect(reverse_lazy('place_detail', kwargs={'pk':instance.pk})) + kwargs_to_pass = { + 'pk': instance.pk + } + return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass)) else: - return render(request, 'create_place.html', {'form':place_form}) + context = { + 'form': form_place + } + return render(request, 'create_place.html', context)