From 6e3e7a52923ea29c18612c5dc9f904415e90125b Mon Sep 17 00:00:00 2001 From: Loek van Gent Date: Wed, 24 Aug 2022 11:42:35 +0200 Subject: [PATCH] fix some APIs and tests --- bluebottle/collect/views.py | 2 +- bluebottle/geo/models.py | 6 ++---- bluebottle/geo/tests/test_api.py | 13 +++++++------ bluebottle/geo/views.py | 3 ++- bluebottle/initiatives/views.py | 2 +- bluebottle/time_based/tests/test_api.py | 4 ++-- bluebottle/time_based/views.py | 5 ----- bluebottle/utils/views.py | 8 ++++---- 8 files changed, 19 insertions(+), 24 deletions(-) diff --git a/bluebottle/collect/views.py b/bluebottle/collect/views.py index 31d562400e..f0e0585a00 100644 --- a/bluebottle/collect/views.py +++ b/bluebottle/collect/views.py @@ -124,7 +124,7 @@ class CollectTypeList(TranslatedApiViewMixin, JsonApiViewMixin, ListAPIView): pagination_class = NoPagination def get_queryset(self): - return super().get_queryset().order_by('translations__name') + return super().get_queryset() class CollectTypeDetail(TranslatedApiViewMixin, JsonApiViewMixin, RetrieveAPIView): diff --git a/bluebottle/geo/models.py b/bluebottle/geo/models.py index af4fe2f1ce..ea195404c3 100644 --- a/bluebottle/geo/models.py +++ b/bluebottle/geo/models.py @@ -8,11 +8,10 @@ from django.template.defaultfilters import slugify from django.utils.translation import gettext_lazy as _ from future.utils import python_2_unicode_compatible -from parler.models import TranslatedFields +from parler.models import TranslatedFields, TranslatableModel from sorl.thumbnail import ImageField from timezonefinder import TimezoneFinder -from bluebottle.utils.models import SortableTranslatableModel from bluebottle.utils.validators import FileMimetypeValidator, validate_file_infection from .validators import Alpha2CodeValidator, Alpha3CodeValidator, \ NumericCodeValidator @@ -21,7 +20,7 @@ @python_2_unicode_compatible -class GeoBaseModel(SortableTranslatableModel): +class GeoBaseModel(TranslatableModel): """ Abstract base model for the UN M.49 geoscheme. Refs: http://unstats.un.org/unsd/methods/m49/m49.htm @@ -106,7 +105,6 @@ def code(self): return self.alpha2_code class Meta(GeoBaseModel.Meta): - ordering = ['translations__name'] verbose_name = _("country") verbose_name_plural = _("countries") diff --git a/bluebottle/geo/tests/test_api.py b/bluebottle/geo/tests/test_api.py index 5347a2ec38..e29ec5ea9f 100644 --- a/bluebottle/geo/tests/test_api.py +++ b/bluebottle/geo/tests/test_api.py @@ -50,13 +50,14 @@ def test_api_country_list_data(self): """ Ensure get request returns record with correct data. """ - response = self.client.get(reverse('country-list')) - - country = response.data[0] + response = self.client.get( + reverse('country-list'), + HTTP_X_APPLICATION_LANGUAGE='nl' + ) - self.assertEqual(country['id'], self.country_1.id) - self.assertEqual(country['name'], self.country_1.name) - self.assertEqual(country['code'], 'GE') + countries = response.data + self.assertTrue('Abchaziƫ' in [c['name'] for c in countries]) + self.assertTrue('Zwitserland' in [c['name'] for c in countries]) class UsedCountryListTestCase(GeoTestCase): diff --git a/bluebottle/geo/views.py b/bluebottle/geo/views.py index 6453f48e77..f4b427345d 100644 --- a/bluebottle/geo/views.py +++ b/bluebottle/geo/views.py @@ -24,6 +24,7 @@ def get(self, request, *args, **kwargs): def get_queryset(self): qs = super(CountryList, self).get_queryset() + if 'filter[used]' in self.request.GET: qs = qs.filter( Q(location__initiative__status='approved') | @@ -33,7 +34,7 @@ def get_queryset(self): Q(geolocation__dateactivityslot__activity__status__in=self.public_statuses) & Q(geolocation__dateactivityslot__status__in=self.public_statuses) ) - ).distinct() + ) return qs diff --git a/bluebottle/initiatives/views.py b/bluebottle/initiatives/views.py index 329ed59c81..7bf8581a36 100644 --- a/bluebottle/initiatives/views.py +++ b/bluebottle/initiatives/views.py @@ -178,7 +178,7 @@ class ThemeList(TranslatedApiViewMixin, JsonApiViewMixin, ListAPIView): pagination_class = NoPagination def get_queryset(self): - return super().get_queryset().order_by('translations__name') + return super().get_queryset() class ThemeDetail(TranslatedApiViewMixin, JsonApiViewMixin, RetrieveAPIView): diff --git a/bluebottle/time_based/tests/test_api.py b/bluebottle/time_based/tests/test_api.py index 7ba49b7bf6..2642cb88ef 100644 --- a/bluebottle/time_based/tests/test_api.py +++ b/bluebottle/time_based/tests/test_api.py @@ -3211,14 +3211,14 @@ def setUp(self): MemberPlatformSettings.objects.update(closed=True) self.url = reverse('skill-list') Skill.objects.all().delete() - self.skill = SkillFactory.create_batch(40) + SkillFactory.create_batch(10) self.client = JSONAPITestClient() def test_get_skills_authenticated(self): user = BlueBottleUserFactory.create() response = self.client.get(self.url, user=user) self.assertEqual(response.status_code, 200) - self.assertEqual(len(response.data['results']), 40) + self.assertEqual(len(response.data['results']), 10) def test_get_skills_unauthenticated(self): response = self.client.get(self.url) diff --git a/bluebottle/time_based/views.py b/bluebottle/time_based/views.py index c3c89d07a7..25ce9074b8 100644 --- a/bluebottle/time_based/views.py +++ b/bluebottle/time_based/views.py @@ -15,7 +15,6 @@ ActivitySegmentPermission ) from bluebottle.activities.views import RelatedContributorListView -from bluebottle.clients import properties from bluebottle.initiatives.models import InitiativePlatformSettings from bluebottle.members.models import MemberPlatformSettings from bluebottle.segments.models import SegmentType @@ -694,10 +693,6 @@ class SkillList(TranslatedApiViewMixin, JsonApiViewMixin, ListAPIView): permission_classes = [TenantConditionalOpenClose, ] pagination_class = SkillPagination - def get_queryset(self): - lang = self.request.LANGUAGE_CODE or properties.LANGUAGE_CODE - return super().get_queryset().translated(lang).order_by('translations__name') - class SkillDetail(TranslatedApiViewMixin, JsonApiViewMixin, RetrieveAPIView): serializer_class = SkillSerializer diff --git a/bluebottle/utils/views.py b/bluebottle/utils/views.py index 0bcb0b1d56..bd9c3c0339 100644 --- a/bluebottle/utils/views.py +++ b/bluebottle/utils/views.py @@ -1,5 +1,6 @@ import mimetypes import os +import re from io import BytesIO from operator import attrgetter @@ -15,7 +16,6 @@ from django.views.generic import TemplateView from django.views.generic.base import View from django.views.generic.detail import DetailView -from parler.utils.i18n import get_language from rest_framework import generics from rest_framework import views, response from rest_framework.pagination import PageNumberPagination @@ -32,7 +32,7 @@ from bluebottle.utils.permissions import ResourcePermission from .models import Language from .serializers import LanguageSerializer -import re +from .utils import get_current_language mime = magic.Magic(mime=True) @@ -233,8 +233,8 @@ def get_queryset(self): class TranslatedApiViewMixin(object): def get_queryset(self): qs = super(TranslatedApiViewMixin, self).get_queryset().active_translations( - get_language() - ) + get_current_language() + ).distinct('id').order_by('id') qs = qs.order_by(*qs.model._meta.ordering) return qs