diff --git a/lostplaces/lostplaces_app/forms.py b/lostplaces/lostplaces_app/forms.py index 734e77d..b0306c9 100644 --- a/lostplaces/lostplaces_app/forms.py +++ b/lostplaces/lostplaces_app/forms.py @@ -1,6 +1,6 @@ from django import forms from django.contrib.auth.forms import UserCreationForm, UserChangeForm -from .models import Explorer, Place +from .models import Explorer, Place, PlaceImage class ExplorerCreationForm(UserCreationForm): class Meta: @@ -18,3 +18,12 @@ class PlaceForm(forms.ModelForm): model = Place fields = '__all__' exclude = ['submitted_by'] + +class PlaceImageCreateForm(forms.ModelForm): + class Meta: + model = PlaceImage + fields = '__all__' + exclude = ['submitted_by', 'place', 'description'] + widgets = { + 'filename': forms.ClearableFileInput(attrs={'multiple': True}) + } diff --git a/lostplaces/lostplaces_app/migrations/0004_placeimage_submitted_by.py b/lostplaces/lostplaces_app/migrations/0004_placeimage_submitted_by.py new file mode 100644 index 0000000..34bb878 --- /dev/null +++ b/lostplaces/lostplaces_app/migrations/0004_placeimage_submitted_by.py @@ -0,0 +1,20 @@ +# Generated by Django 3.0.8 on 2020-07-30 09:18 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('lostplaces_app', '0003_auto_20200729_1822'), + ] + + operations = [ + migrations.AddField( + model_name='placeimage', + name='submitted_by', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='images', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/lostplaces/lostplaces_app/models.py b/lostplaces/lostplaces_app/models.py index ceb6c36..96adb92 100644 --- a/lostplaces/lostplaces_app/models.py +++ b/lostplaces/lostplaces_app/models.py @@ -38,6 +38,12 @@ class Place (models.Model): return self.name +def generate_image_upload_path(instance, filename): + """ + Callback for generating path for uploaded images + """ + return 'places/' + str(uuid.uuid4())+'.'+filename.split('.')[-1] + class PlaceImage (models.Model): SIZES=( @@ -46,17 +52,24 @@ class PlaceImage (models.Model): {'code': 'large', 'wxh': '1920x1920'} ) + description = models.TextField(blank=True) filename = ImageThumbsField( upload_to=generate_image_upload_path, max_length=50, - sizes=self.SIZES + sizes=SIZES ) place = models.ForeignKey( Place, on_delete=models.CASCADE, related_name='images' ) - description = models.TextField(blank=True) + submitted_by = models.ForeignKey( + Explorer, + on_delete=models.SET_NULL, + null=True, + blank=True, + related_name='images' + ) def __str__(self): """ @@ -65,11 +78,6 @@ class PlaceImage (models.Model): """ 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: diff --git a/lostplaces/lostplaces_app/templates/create_place.html b/lostplaces/lostplaces_app/templates/create_place.html index dab22be..a9a3ae3 100644 --- a/lostplaces/lostplaces_app/templates/create_place.html +++ b/lostplaces/lostplaces_app/templates/create_place.html @@ -8,7 +8,8 @@

Place erstellen

{% csrf_token %} - {{ form.as_p }} + {{ place_form.as_p }} + {{ place_image_form.as_p }}
diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index b04181f..e29147b 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -3,7 +3,7 @@ from django.urls import reverse_lazy from django.views.generic.edit import CreateView from django.views import View -from .forms import ExplorerCreationForm, PlaceForm +from .forms import ExplorerCreationForm, PlaceForm, PlaceImageCreateForm from .models import Place # Create your views here. @@ -25,19 +25,21 @@ def hello_world(request): class PlaceEditView(View): def get(self, request, *args, **kwargs): + place_image_form = PlaceImageCreateForm() if 'pk' in kwargs: place = get_object_or_404(Place,pk=kwargs['pk']) place_form = PlaceForm(instance=place) else: place_form = PlaceForm() context = { - 'form': place_form + 'place_form': place_form, + 'place_image_form': place_image_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"