Skip to content

Commit

Permalink
Add year filter to gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
MaertHaekkinen committed Nov 23, 2024
1 parent 56aa4d2 commit c732ce1
Show file tree
Hide file tree
Showing 9 changed files with 629 additions and 476 deletions.
40 changes: 21 additions & 19 deletions ajapaik/ajapaik/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def _authenticate_with_wikimedia_commons(self, request, access_token):
adapter = WikimediaCommonsOAuth2Adapter(request)
return self._authenticate_with_oauth2_access_token(adapter, request, access_token)

def post(self, request, format=None):
def post(self, request):
form = forms.APILoginForm(request.data)
user = None

Expand Down Expand Up @@ -276,7 +276,7 @@ class Register(APIView):
permission_classes = (AllowAny,)
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)

def post(self, request, format=None):
def post(self, request):
form = forms.APIRegisterForm(request.data)
if form.is_valid():
registration_type = form.cleaned_data['type']
Expand Down Expand Up @@ -368,13 +368,13 @@ def _reset_session_cookie(self, request):
del request.session['sessionid']
return request

def post(self, request, format=None):
def post(self, request):
body = request.body
post = self._fix_latin1_query_param(request, body)
request = self._reset_session_cookie(request)
return self._handle_request(post, request.user, request)

def get(self, request, format=None):
def get(self, request):
request = self._reset_session_cookie(request)
return self._handle_request(request.GET, request.user, request)

Expand Down Expand Up @@ -679,7 +679,7 @@ def _handle_request(self, data, user, request):
photos = Photo.objects.filter(rephoto_of__isnull=True, ).annotate(
distance=GeometryDistance("geography", ref_location)).order_by("distance")[start:end]
photo_ids = photos.values_list('id', flat=True)
photos = Photo.objects.filter(id__in=photo_ids).order_by(GeometryDistance("geography", ref_location));
photos = Photo.objects.filter(id__in=photo_ids).order_by(GeometryDistance("geography", ref_location))

photos = PhotoSerializer.annotate_photos(
photos,
Expand Down Expand Up @@ -852,7 +852,7 @@ class RephotoUpload(APIView):
permission_classes = (AllowAny,)
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)

def post(self, request, format=None):
def post(self, request):
print('rephotoupload', file=sys.stderr)
form = forms.ApiPhotoUploadForm(request.POST, request.FILES)

Expand Down Expand Up @@ -1022,7 +1022,7 @@ class RephotoUploadSettings(AjapaikAPIView):
API endpoint for saving rephoto upload settings
'''

def post(self, request, format=None):
def post(self, request):
try:
profile = request.user.profile
profile.wikimedia_commons_rephoto_upload_consent = request.POST['wikimedia_commons_rephoto_upload_consent']
Expand Down Expand Up @@ -1388,14 +1388,14 @@ def _handle_request(self, data, user, request):
'albums': []
})

def post(self, request, format=None):
def post(self, request):
user = request.user or None
body = request.body
post = self._fix_latin1_query_param(request, body)

return self._handle_request(post, user, request)

def get(self, request, format=None):
def get(self, request):
user = request.user or None
return self._handle_request(request.GET, user, request)

Expand Down Expand Up @@ -1580,7 +1580,7 @@ class SubmitSimilarPhotos(AjapaikAPIView):
API endpoint for posting similar photos.
'''

def post(self, request, format=None):
def post(self, request):
points = 0
profile = request.user.profile
data = json.loads(request.body.decode('utf-8'))
Expand Down Expand Up @@ -1616,7 +1616,7 @@ class Transcriptions(AjapaikAPIView):
API endpoint for getting transcriptions for an image.
'''

def get(self, request, photo_id, format=None):
def get(self, request, photo_id):
transcriptions = Transcription.objects.filter(photo_id=photo_id).order_by('-created').values()
ids = transcriptions.values_list('id', flat=True)
feedback = TranscriptionFeedback.objects.filter(transcription_id__in=ids)
Expand All @@ -1634,7 +1634,7 @@ def feedback_count(elem):
data = {'transcriptions': sorted(list(transcriptions), key=feedback_count, reverse=True)}
return JsonResponse(data, safe=False)

def post(self, request, format=None):
def post(self, request):
try:
photo = get_object_or_404(Photo, id=request.POST['photo'])
user = get_object_or_404(Profile, pk=request.user.profile.id)
Expand Down Expand Up @@ -1687,7 +1687,7 @@ class SubmitTranscriptionFeedback(AjapaikAPIView):
API endpoint for confirming transcription for an image.
'''

def post(self, request, format=None):
def post(self, request):
try:
if TranscriptionFeedback.objects.filter(transcription_id=request.POST['id'],
user_id=request.user.profile.id).exists():
Expand All @@ -1707,7 +1707,7 @@ class UserSettings(AjapaikAPIView):
API endpoint for saving user settings
'''

def post(self, request, format=None):
def post(self, request):
try:
profile = request.user.profile
profile.preferred_language = request.POST['preferredLanguage']
Expand All @@ -1723,7 +1723,7 @@ class ChangeProfileDisplayName(AjapaikAPIView):
API endpoint for changing user display name
'''

def post(self, request, format=None):
def post(self, request):
try:
profile = request.user.profile
profile.display_name = request.POST['display_name']
Expand All @@ -1741,7 +1741,7 @@ class PhotoSuggestion(AjapaikAPIView):
API endpoints for getting photo scene category and updating it
'''

def get(self, request, photo_id, format=None):
def get(self, request, photo_id):
try:
if request.user.is_anonymous:
return JsonResponse({'error': PLEASE_LOGIN}, status=401)
Expand Down Expand Up @@ -1788,7 +1788,7 @@ def get(self, request, photo_id, format=None):
except: # noqa
return JsonResponse({'error': _('Something went wrong')}, status=500)

def post(self, request, format=None):
def post(self, request):
profile = request.user.profile
data = json.loads(request.body.decode('utf-8'))
scene = data['scene']
Expand Down Expand Up @@ -1848,7 +1848,7 @@ class PhotoAppliedOperations(AjapaikAPIView):
API endpoints for getting photo scene category and updating it
'''

def get(self, request, photo_id, format=None):
def get(self, request, photo_id):
try:
if request.user.is_anonymous:
return JsonResponse({'error': PLEASE_LOGIN}, status=401)
Expand Down Expand Up @@ -1899,7 +1899,7 @@ def get(self, request, photo_id, format=None):
except: # noqa
return JsonResponse({'error': _('Something went wrong')}, status=500)

def post(self, request, format=None):
def post(self, request):
profile = request.user.profile
data = json.loads(request.body.decode('utf-8'))
flip = data['flip']
Expand Down Expand Up @@ -1927,6 +1927,7 @@ def post(self, request, format=None):
photo_flip_suggestions = []
photo_rotated_suggestions = []
photo_invert_suggestions = []
original_rotation = 0

for photo_id in photo_ids:
if not photo_id.isdigit():
Expand Down Expand Up @@ -1975,5 +1976,6 @@ def post(self, request, format=None):
PhotoFlipSuggestion.objects.bulk_create(photo_flip_suggestions)
PhotoInvertSuggestion.objects.bulk_create(photo_invert_suggestions)
PhotoRotationSuggestion.objects.bulk_create(photo_rotated_suggestions)

return JsonResponse({'message': response,
'rotated_by_90': (was_rotate_successful and abs(rotated - original_rotation) % 180 == 90)})
2 changes: 2 additions & 0 deletions ajapaik/ajapaik/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class GalleryFilteringForm(forms.Form):
square = forms.BooleanField(initial=False, required=False)
landscape = forms.BooleanField(initial=False, required=False)
panoramic = forms.BooleanField(initial=False, required=False)
date_from = forms.DateField(initial=None, required=False)
date_to = forms.DateField(initial=None, required=False)

def clean_page(self):
page = self.cleaned_data['page']
Expand Down
86 changes: 86 additions & 0 deletions ajapaik/ajapaik/static/js/ajp-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ window.galleryFilters = [
'square',
'landscape',
'panoramic',
'date_from',
'date_to',
];
window.albumFilters = ['film', 'collections', 'people'];

Expand Down Expand Up @@ -457,6 +459,58 @@ $('.ajp-navbar').autoHidingNavbar();
},
);

$(document).on(
'click',
'#removeDateFilters',
function(e) {
e.preventDefault();
let uri = URI(window.location);

if (uri.query().indexOf('date_from') > -1) {
uri.removeQuery('date_from');
}

if (uri.query().indexOf('date_to') > -1) {
uri.removeQuery('date_to');
}

window.location.href = uri;
},
);

$(document).on(
'click',
'#applyDateFilters',
function(e) {
e.preventDefault();
let uri = URI(window.location);
let dateFromValue = document.getElementById('startingFrom').innerText;
let dateFrom = dateFromValue ? new Date(dateFromValue).toISOString().substring(0, 10) : null;

if (uri.query().indexOf('date_to') > -1) {
uri.removeQuery('date_to');
}
if (uri.query().indexOf('date_from') > -1) {
uri.removeQuery('date_from');
}

if (dateFrom !== null) {
uri.addQuery('date_from', dateFrom);
}

let dateToValue = document.getElementById('endingAt').innerText;
let dateTo = dateToValue ? new Date(dateToValue, 11, 31, 23, 59, 59).toISOString().substring(0, 10) : null;


if (dateTo !== null) {
uri.addQuery('date_to', dateTo);
}

window.location.href = uri;
},
);


handleGeolocation = function(position) {
$('#ajp-geolocation-error').hide();
window.location.href =
Expand Down Expand Up @@ -2347,5 +2401,37 @@ $('.ajp-navbar').autoHidingNavbar();
}
window.positionMinimapCTAButton();
});

function getVals() {
// Get slider values
var parent = this.parentNode;
var slides = parent.getElementsByTagName('input');
var slide1 = parseFloat(slides[0].value);
var slide2 = parseFloat(slides[1].value);
// Neither slider will clip the other, so make sure we determine which is larger
if (slide1 > slide2) {
var tmp = slide2;
slide2 = slide1;
slide1 = tmp;
}

var displayElement = parent.getElementsByClassName('rangeValues')[0];
displayElement.innerHTML = slide1 + ' - ' + slide2;
}

window.onload = function() {
// Initialize Sliders
var sliderSections = document.getElementsByClassName('range-slider');
for (var x = 0; x < sliderSections.length; x++) {
var sliders = sliderSections[x].getElementsByTagName('input');
for (var y = 0; y < sliders.length; y++) {
if (sliders[y].type === 'range') {
sliders[y].oninput = getVals;
// Manually trigger event first time to display values
sliders[y].oninput();
}
}
}
};
}
})(jQuery);
4 changes: 3 additions & 1 deletion ajapaik/ajapaik/static/js/ajp-frontpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
});
},
syncStateToUrl = function() {
const currentUrl = window.URI(window.location.href);
let currentUrl = window.URI(window.location.href);
currentUrl
.removeSearch('photo')
.removeSearch('page')
Expand Down Expand Up @@ -535,6 +535,8 @@
categoryMessage = 'No rephotos were found';
} else if (window.location.search.indexOf('myLikes=1') > 0) {
categoryMessage = 'No liked pictures were found';
} else if (window.location.search.indexOf('dateFrom=') > 0 || window.location.search.indexOf('date=') > 0) {
categoryMessage = 'No pictures were found in date range';
} else {
categoryMessage = 'No pictures were found';
}
Expand Down
Loading

0 comments on commit c732ce1

Please sign in to comment.