Comments and Codeformatting

This commit is contained in:
Leonhard Strohmidel 2020-07-30 10:51:05 +02:00
parent 066bc6011a
commit 73a6a4f8d8
4 changed files with 54 additions and 18 deletions

View File

@ -15,6 +15,5 @@ class ExplorerAdmin(UserAdmin):
list_display = ['email', 'username',]
admin.site.register(Explorer, ExplorerAdmin)
admin.site.register(Place)
admin.site.register(PlaceImage)

View File

@ -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)

View File

@ -7,5 +7,5 @@ urlpatterns = [
path('place/<int:pk>/', place_detail_view, name='place_detail'),
path('place/create/', PlaceEditView.as_view(), name='place_create'),
path('place/edit/<int:pk>/', PlaceEditView.as_view(), name='place_edit'),
path('place/', place_list_view)
path('place/', place_list_view, name='place_list')
]

View File

@ -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)