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 27, 2023
1 parent a1ee575 commit b8753de
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 162 deletions.
19 changes: 5 additions & 14 deletions ajapaik/ajapaik/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,15 @@ 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 = ('images', 'description', 'uploader_is_author', 'author', 'licence')
fields = ('image', 'description', 'uploader_is_author', 'author', 'licence')
labels = {
'images': _('Picture files'),
'image': _('Picture file'),
'description': _('Description'),
'uploader_is_author': _('I am the author'),
'author': _('Author'),
Expand All @@ -450,17 +448,10 @@ 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.'))

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'):
Expand Down Expand Up @@ -674,4 +665,4 @@ class ApiWikidocsAlbumSearchForm(forms.Form):

class CsvImportForm(forms.Form):
zip_file = forms.FileField(required=False)
csv_file = forms.FileField()
csv_file = forms.FileField()
13 changes: 6 additions & 7 deletions ajapaik/ajapaik/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,7 @@ class PhotoModelSuggestionResult(Suggestion):
viewpoint_elevation = PositiveSmallIntegerField(_('Viewpoint elevation'), choices=VIEWPOINT_ELEVATION_CHOICES, blank=True, null=True)
scene = PositiveSmallIntegerField(_('Scene'), choices=SCENE_CHOICES, blank=True, null=True)


class PhotoModelSuggestionAlternativeCategory(Suggestion):
INTERIOR, EXTERIOR = range(2)
GROUND_LEVEL, RAISED, AERIAL = range(3)
Expand All @@ -2102,10 +2103,13 @@ class PhotoModelSuggestionAlternativeCategory(Suggestion):
(RAISED, _('Raised')),
(AERIAL, _('Aerial'))
)
viewpoint_elevation_alternation = PositiveSmallIntegerField(_('Viewpoint elevation'), choices=VIEWPOINT_ELEVATION_CHOICES, blank=True, null=True)
viewpoint_elevation_alternation = PositiveSmallIntegerField(_('Viewpoint elevation'),
choices=VIEWPOINT_ELEVATION_CHOICES, blank=True,
null=True)
scene_alternation = PositiveSmallIntegerField(_('Scene'), choices=SCENE_CHOICES, blank=True, null=True)

proposer = ForeignKey('Profile', blank=True, null=True, related_name='photo_scene_suggestions_alternation', on_delete=CASCADE)
proposer = ForeignKey('Profile', blank=True, null=True, related_name='photo_scene_suggestions_alternation',
on_delete=CASCADE)

def validate_unique(self, exclude=None):
super().validate_unique(exclude)
Expand All @@ -2116,11 +2120,6 @@ def validate_unique(self, exclude=None):
photo_id=self.photo_id
).exclude(pk=self.pk)

print(f"QUERY SET IS {queryset}")
print(type(self.scene_alternation))
print(self.scene_alternation in ['0', '1'])
print(queryset.exists())

if self.scene_alternation in ['0', '1'] and queryset.exists():
raise ValidationError('Only one record with scene_alternation 0 or 1 is allowed.')

Expand Down
6 changes: 0 additions & 6 deletions ajapaik/ajapaik/static/css/ajp-geotagger-plugin.css
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,6 @@
border-radius: 0;
}

.ajp-button {
bottom: 3% !important;
right: 3% !important;
min-width: 150px;
border-radius: 0;
}

#ajp-geotagging-container div.gm-style-mtc > div:nth-of-type(2) {
bottom: auto;
Expand Down
2 changes: 1 addition & 1 deletion ajapaik/ajapaik/static/js/ajp-picture-category-retrival.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ function sendCategoryFeedback(photoId, category, categoryValue) {
constants.translations.queries.POST_CATEGORY_CONFIRMATION_FAILED,
onSuccess
);
}
}
121 changes: 65 additions & 56 deletions ajapaik/ajapaik/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
from ajapaik.utils import get_etag, calculate_thumbnail_size, convert_to_degrees, calculate_thumbnail_size_max_height, \
distance_in_meters, angle_diff, last_modified, suggest_photo_edit
from .fotis_utils import parse_fotis_timestamp_data
from .utils import get_comment_replies, get_pagination_parameters
from .utils import get_comment_replies, get_pagination_parameters, ImportBlacklistService

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -1531,10 +1531,10 @@ def photoslug(request, photo_id=None, pseudo_slug=None):
next_similar_photo = next_photo
compare_photos_url = request.build_absolute_uri(
reverse('compare-photos', args=(photo_obj.id, next_similar_photo.id)))
imageSimilarities = ImageSimilarity.objects.filter(from_photo_id=photo_obj.id).exclude(similarity_type=0)
if imageSimilarities.exists():
image_similarities = ImageSimilarity.objects.filter(from_photo_id=photo_obj.id).exclude(similarity_type=0)
if image_similarities.exists():
compare_photos_url = request.build_absolute_uri(
reverse('compare-photos', args=(photo_obj.id, imageSimilarities.first().to_photo_id)))
reverse('compare-photos', args=(photo_obj.id, image_similarities.first().to_photo_id)))

people = [x.name for x in photo_obj.people]
similar_photos = ImageSimilarity.objects.filter(from_photo=photo_obj.id).exclude(similarity_type=0)
Expand Down Expand Up @@ -2336,11 +2336,22 @@ def curator_photo_upload_handler(request):
# 15 => unknown copyright
unknown_licence = Licence.objects.get(pk=15)
flickr_licence = Licence.objects.filter(url='https://www.flickr.com/commons/usage/').first()
import_blacklist_service = ImportBlacklistService()

for k, v in selection.items():
upload_form = CuratorPhotoUploadForm(v)
created_album_photo_links = []
awarded_curator_points = []
if upload_form.is_valid():
source_key = upload_form.cleaned_data['identifyingNumber']

if source_key and import_blacklist_service.is_blacklisted(source_key):
context['photos'][k] = {
'error': _(
f'Could not import picture, as it is blacklisted from being imported: {upload_form.cleaned_data["imageUrl"]}')}
context['photos'][k]['success'] = False
continue

if not upload_form.cleaned_data['institution']:
licence = unknown_licence
source = Source.objects.get(name='AJP')
Expand Down Expand Up @@ -2378,7 +2389,7 @@ def curator_photo_upload_handler(request):
existing_photo = None
if upload_form.cleaned_data['id'] and upload_form.cleaned_data['id'] != '':
if upload_form.cleaned_data['collections'] == 'DIGAR':
incoming_muis_id = upload_form.cleaned_data['identifyingNumber']
incoming_muis_id = source_key
else:
incoming_muis_id = upload_form.cleaned_data['id']
if 'ETERA' in upload_form.cleaned_data['institution']:
Expand All @@ -2392,7 +2403,7 @@ def curator_photo_upload_handler(request):
muis_id = incoming_muis_id
muis_media_id = None
if upload_form.cleaned_data['collections'] == 'DIGAR':
upload_form.cleaned_data['identifyingNumber'] = \
source_key = \
f'nlib-digar:{upload_form.cleaned_data["identifyingNumber"]}'
muis_media_id = 1
try:
Expand Down Expand Up @@ -2422,7 +2433,7 @@ def curator_photo_upload_handler(request):
licence=licence,
external_id=muis_id,
external_sub_id=muis_media_id,
source_key=upload_form.cleaned_data['identifyingNumber'],
source_key=source_key,
source_url=upload_form.cleaned_data['urlToRecord'],
flip=upload_form.cleaned_data['flip'],
invert=upload_form.cleaned_data['invert'],
Expand Down Expand Up @@ -2605,9 +2616,8 @@ def curator_photo_upload_handler(request):
ap.delete()
for cp in awarded_curator_points:
cp.delete()
context['photos'][k] = {}
context['photos'][k]['error'] = _('Error uploading file: %s (%s)' %
(e, upload_form.cleaned_data['imageUrl']))
context['photos'][k] = {'error': _('Error uploading file: %s (%s)' %
(e, upload_form.cleaned_data['imageUrl']))}
else:
if general_albums.exists():
for a in general_albums:
Expand Down Expand Up @@ -2745,6 +2755,7 @@ def csv_import(request):
not_found_list = []
profile = request.get_user().profile
skipped_list = []
blacklisted_list = []
success = None
unique_album_list = []
upload_folder = f'{settings.MEDIA_ROOT}/uploads/'
Expand Down Expand Up @@ -2785,6 +2796,8 @@ def del_evenReadonly(action, name, exc):

existing_photos = Photo.objects.filter(image__in=file_list).values_list('image', flat=True)

import_blacklist_service = ImportBlacklistService()

# TODO: map over row fields instead to directly set attributes of photo with setattr
# before doing so remove any exceptions like album, source, licence or start using only ids
for row in csv.DictReader(decoded_file, delimiter=',', quotechar='"'):
Expand Down Expand Up @@ -2832,6 +2845,11 @@ def del_evenReadonly(action, name, exc):
source = Source.objects.filter(id=row['source']).first()
if 'source_key' in row.keys():
source_key = row['source_key']

if source_key and import_blacklist_service.is_blacklisted(source_key):
blacklisted_list.append(row['file'])
continue

if 'source_url' in row.keys():
source_url = row['source_url']
if 'date_text' in row.keys():
Expand Down Expand Up @@ -2984,6 +3002,9 @@ def del_evenReadonly(action, name, exc):
if len(skipped_list) > 0:
already_exists_error = "Some imports were skipped since they already exist on Ajapaik:"
errors.append({'message': already_exists_error, 'list': list(set(skipped_list))})
if len(blacklisted_list) > 0:
blacklisted_error = "Some images are blacklisted from Ajapaik, they were not added"
errors.append({'message': blacklisted_error, 'list': list(set(blacklisted_list))})
if len(errors) < 1:
success = 'OK'

Expand Down Expand Up @@ -3306,67 +3327,55 @@ def compare_photos_generic(request, photo_id=None, photo_id_2=None, view='compar
}
return render(request, 'compare_photos/compare_photos.html', context)

@csrf_exempt

def user_upload(request):
context = {
'ajapaik_facebook_link': settings.AJAPAIK_FACEBOOK_LINK,
'is_user_upload': True,
'show_albums_error': False
}

if request.method == 'POST':
form = UserPhotoUploadForm(request.POST, request.FILES)
albums = request.POST.getlist('albums')
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 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()
if request.POST.get('geotag') == 'true':
return redirect(f'{reverse("frontpage_photos")}?photo={str(photo.id)}&locationToolsOpen=1')
else:
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
context['message'] = _('Photo uploaded')
if albums is None or len(albums) < 1:
context['show_albums_error'] = True
else:
form = UserPhotoUploadForm()

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 Expand Up @@ -3902,4 +3911,4 @@ class MyConnectionsView(LoginRequiredMixin, ConnectionsView):


class MyEmailView(LoginRequiredMixin, EmailView):
success_url = reverse_lazy('user_settings')
success_url = reverse_lazy('user_settings')
2 changes: 1 addition & 1 deletion ajapaik/ajapaik_object_categorization/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
urlpatterns += [
url(r'^api/v1/annotation/(?P<annotation_id>\d+)/$', api.Category.as_view(),
name='object_categorization_api_category'),
]
]
Binary file added ajapaik/settings/.DS_Store
Binary file not shown.
Loading

0 comments on commit b8753de

Please sign in to comment.