Skip to content

Commit

Permalink
[AJP-2]
Browse files Browse the repository at this point in the history
  • Loading branch information
Anna Grund authored and Anna Grund committed Sep 17, 2023
1 parent 4f02068 commit 5fdaeba
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
25 changes: 20 additions & 5 deletions ajapaik/ajapaik/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ class FrontpagePagingForm(forms.Form):


class ApiAlbumNearestPhotosForm(forms.Form):
print("HERE")
id = forms.ModelChoiceField(queryset=Album.objects.filter(is_public=True), required=False)
latitude = forms.FloatField(min_value=-85.05115, max_value=85)
longitude = forms.FloatField(min_value=-180, max_value=180)
Expand All @@ -324,6 +325,7 @@ class ApiAlbumNearestPhotosForm(forms.Form):


class ApiFinnaNearestPhotosForm(forms.Form):
print("HERE")
id = forms.ModelChoiceField(queryset=Album.objects.filter(is_public=True), required=False)
latitude = forms.FloatField(min_value=-85.05115, max_value=85)
longitude = forms.FloatField(min_value=-180, max_value=180)
Expand All @@ -335,19 +337,22 @@ class ApiFinnaNearestPhotosForm(forms.Form):


class ApiAlbumStateForm(forms.Form):
print("HERE")
id = forms.ModelChoiceField(queryset=Album.objects.filter(is_public=True))
state = forms.CharField(max_length=255, required=False)
start = forms.IntegerField(required=False)
limit = forms.IntegerField(required=False)


class ApiAlbumSourceForm(forms.Form):
print("HERE")
query = forms.CharField(max_length=255, required=True)
start = forms.IntegerField(required=False)
limit = forms.IntegerField(required=False)


class ApiPhotoUploadForm(forms.Form):
print("HERE")
id = forms.CharField(max_length=255)
latitude = forms.FloatField(min_value=-85.05115, max_value=85, required=False)
longitude = forms.FloatField(min_value=-180, max_value=180, required=False)
Expand Down Expand Up @@ -417,15 +422,17 @@ class VideoStillCaptureForm(forms.Form):


class UserPhotoUploadForm(forms.ModelForm):
images = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True}))

def __init__(self, *args, **kwargs):
super(UserPhotoUploadForm, self).__init__(*args, **kwargs)
self.fields['licence'].queryset = Licence.objects.filter(is_public=True)

class Meta:
model = Photo
fields = ('image', 'description', 'uploader_is_author', 'author', 'licence')
fields = ('images', 'description', 'uploader_is_author', 'author', 'licence')
labels = {
'image': _('Picture file'),
'images': _('Picture files'),
'description': _('Description'),
'uploader_is_author': _('I am the author'),
'author': _('Author'),
Expand All @@ -448,16 +455,24 @@ class Meta:
'author': forms.TextInput(attrs={'placeholder': _('Author name')}),
}

def clean_images(self):
images = self.cleaned_data.get('images')

if not images:
raise forms.ValidationError(_('At least one image file is required.'))

# Additional validation for image files can be added here if needed.

return images

def clean(self):
super(UserPhotoUploadForm, self).clean()
if not self.cleaned_data.get('image'):
self.errors['image'] = [_('Missing image')]

if not self.cleaned_data.get('description'):
self.errors['description'] = [_('Missing description')]
if not self.cleaned_data.get('uploader_is_author') and not self.cleaned_data.get('licence'):
self.errors['licence'] = [_('Missing licence')]


class UserPhotoUploadAddAlbumForm(forms.ModelForm):
location = forms.CharField(max_length=255, required=False)

Expand Down
76 changes: 44 additions & 32 deletions ajapaik/ajapaik/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3313,48 +3313,60 @@ def user_upload(request):
'is_user_upload': True,
'show_albums_error': False
}

if request.method == 'POST':
form = UserPhotoUploadForm(request.POST, request.FILES)
albums = request.POST.getlist('albums')
if form.is_valid() and albums is not None and len(albums) > 0:
photo = form.save(commit=False)
photo.user = request.user.profile
if photo.uploader_is_author:
photo.author = request.user.profile.get_display_name
photo.licence = Licence.objects.get(id=17) # CC BY 4.0
photo.save()
photo.set_aspect_ratio()
photo.find_similar()
albums = request.POST.getlist('albums')
album_photos = []
for each in albums:
album_photos.append(
AlbumPhoto(photo=photo,
album=Album.objects.filter(id=each).first(),
type=AlbumPhoto.UPLOADED,
profile=request.user.profile
))
AlbumPhoto.objects.bulk_create(album_photos)
for a in albums:
album = Album.objects.filter(id=a).first()
if album is not None:
album.set_calculated_fields()
album.light_save()
form = UserPhotoUploadForm()
photo.add_to_source_album()
uploaded_images = request.FILES.getlist('images')

if uploaded_images:
for uploaded_file in uploaded_images:
form = UserPhotoUploadForm(request.POST, request.FILES)
if form.is_valid():
photo = form.save(commit=False)
photo.user = request.user.profile
if photo.uploader_is_author:
photo.author = request.user.profile.get_display_name()
photo.licence = Licence.objects.get(id=17) # CC BY 4.0
photo.image = uploaded_file
photo.save()
photo.set_aspect_ratio()
photo.find_similar()

album_photos = []
for album_id in albums:
album = Album.objects.filter(id=album_id).first()
if album:
album_photos.append(
AlbumPhoto(
photo=photo,
album=album,
type=AlbumPhoto.UPLOADED,
profile=request.user.profile
)
)
AlbumPhoto.objects.bulk_create(album_photos)

for album_id in albums:
album = Album.objects.filter(id=album_id).first()
if album:
album.set_calculated_fields()
album.light_save()

photo.add_to_source_album()

if request.POST.get('geotag') == 'true':
return redirect(f'{reverse("frontpage_photos")}?photo={str(photo.id)}&locationToolsOpen=1')
else:
context['message'] = _('Photo uploaded')
if albums is None or len(albums) < 1:
context['show_albums_error'] = True
context['message'] = _('Photos uploaded')
form = UserPhotoUploadForm() # Clear the form for a new upload
else:
context['show_albums_error'] = True # Display error if no images are uploaded
else:
form = UserPhotoUploadForm()
context['form'] = form

context['form'] = form
return render(request, 'user_upload/user_upload.html', context)


def user_upload_add_album(request):
context = {
'ajapaik_facebook_link': settings.AJAPAIK_FACEBOOK_LINK
Expand Down

0 comments on commit 5fdaeba

Please sign in to comment.