Skip to content

Commit

Permalink
Merge pull request #5193 from onepercentclub/master
Browse files Browse the repository at this point in the history
Drin
  • Loading branch information
gannetson committed Aug 10, 2022
2 parents 9105a72 + 723f58c commit 61e125f
Show file tree
Hide file tree
Showing 15 changed files with 1,674 additions and 3,672 deletions.
4 changes: 3 additions & 1 deletion bluebottle/funding/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from bluebottle.test.factory_models.accounts import BlueBottleUserFactory
from bluebottle.test.factory_models.geo import GeolocationFactory
from bluebottle.test.utils import BluebottleTestCase, JSONAPITestClient, APITestCase
from bluebottle.segments.tests.factories import SegmentTypeFactory


class BudgetLineListTestCase(BluebottleTestCase):
Expand Down Expand Up @@ -487,11 +488,12 @@ def test_get_owner_export_disabled(self):
self.assertIsNone(export_url)

def test_get_owner_export_enabled(self):
SegmentTypeFactory.create()
initiative_settings = InitiativePlatformSettings.load()
initiative_settings.enable_participant_exports = True
initiative_settings.save()
DonorFactory.create(activity=self.funding, amount=Money(20, 'EUR'), status='new')
DonorFactory.create(activity=self.funding, amount=Money(35, 'EUR'), status='succeeded')
DonorFactory.create(activity=self.funding, user=None, amount=Money(35, 'EUR'), status='succeeded')
response = self.client.get(self.funding_url, user=self.funding.owner)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.json()['data']
Expand Down
13 changes: 7 additions & 6 deletions bluebottle/funding/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,13 @@ def get(self, request, *args, **kwargs):
):
row = [prep_field(request, donor, field[0]) for field in self.fields]
for segment_type in self.get_segment_types():
segments = ", ".join(
donor.user.segments.filter(
segment_type=segment_type
).values_list('name', flat=True)
)
row.append(segments)
if donor.user:
segments = ", ".join(
donor.user.segments.filter(
segment_type=segment_type
).values_list('name', flat=True)
)
row.append(segments)
sheet.append(row)

return generate_xlsx_response(filename=filename, data=sheet)
12 changes: 7 additions & 5 deletions bluebottle/segments/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from builtins import object

from rest_framework import serializers
from rest_framework_json_api.relations import HyperlinkedRelatedField

from bluebottle.activities.models import Activity
from bluebottle.activities.utils import get_stats_for_activities
Expand All @@ -12,10 +13,12 @@

class SegmentTypeSerializer(serializers.ModelSerializer):
name = serializers.CharField(required=False)

included_serializers = {
'segments': 'bluebottle.segments.serializers.SegmentDetailSerializer',
}
segments = HyperlinkedRelatedField(
many=True,
read_only=True,
related_link_view_name='related-segment-detail',
related_link_url_kwarg='segment_type',
)

class Meta(object):
model = SegmentType
Expand All @@ -25,7 +28,6 @@ class Meta(object):
)

class JSONAPIMeta(object):
included_resources = ['segments', ]
resource_name = 'segment-types'


Expand Down
58 changes: 55 additions & 3 deletions bluebottle/segments/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ def test_list(self):
segment_type = SegmentType.objects.get(pk=result['id'])

self.assertEqual(segment_type.name, result['attributes']['name'])
self.assertEqual(
set(relation['id'] for relation in result['relationships']['segments']['data']),
set(str(segment.pk) for segment in segment_type.segments.all())

self.assertTrue(
result['relationships']['segments']['links']['related'].endswith(
reverse('related-segment-detail', args=(segment_type.pk, ))
)
)

def test_list_anonymous(self):
Expand Down Expand Up @@ -435,3 +437,53 @@ def test_retrieve_activity_with_invisible_segment(self):
len(self.response.data["segments"]),
0
)


class RelatedSegmentListAPITestCase(APITestCase):

def setUp(self):
super().setUp()

self.serializer = SegmentDetailSerializer

self.segment_type = SegmentTypeFactory.create()
self.segments = SegmentFactory.create_batch(10, segment_type=self.segment_type)

self.url = reverse('related-segment-detail', args=(self.segment_type.pk, ))

def test_get(self):
self.perform_get()
self.assertStatus(status.HTTP_200_OK)
self.assertSize(10)

self.assertAttribute('name')
self.assertAttribute('logo')
self.assertAttribute('story')

def test_get_closed_segment(self):
self.segments[0].closed = True
self.segments[0].save()

self.perform_get()
self.assertStatus(status.HTTP_200_OK)
self.assertSize(9)

def test_get_closed_segments_user(self):
self.segments[0].closed = True
self.segments[0].save()
self.user.segments.add(self.segments[0])
self.user.save()

self.perform_get(user=self.user)
self.assertStatus(status.HTTP_200_OK)
self.assertSize(10)

def test_get_closed_platform(self):
with self.closed_site():
self.perform_get()
self.assertStatus(status.HTTP_401_UNAUTHORIZED)

def test_get_closed_platform_logged_in(self):
with self.closed_site():
self.perform_get(user=self.user)
self.assertStatus(status.HTTP_200_OK)
8 changes: 7 additions & 1 deletion bluebottle/segments/urls/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.conf.urls import url

from bluebottle.segments.views import (
SegmentList, SegmentDetail, SegmentPublicDetail, SegmentTypeList
SegmentList, SegmentDetail, RelatedSegmentDetail, SegmentPublicDetail, SegmentTypeList
)


Expand All @@ -17,6 +17,12 @@
name='segment-list'
),

url(
r'^types/(?P<segment_type>\d+)/segments$',
RelatedSegmentDetail.as_view(),
name='related-segment-detail'
),

url(
r'^(?P<pk>\d+)$',
SegmentDetail.as_view(),
Expand Down
31 changes: 31 additions & 0 deletions bluebottle/segments/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.db.models import Q

from rest_framework.pagination import PageNumberPagination

from bluebottle.activities.permissions import ActivitySegmentPermission
Expand Down Expand Up @@ -58,6 +60,35 @@ class SegmentDetail(JsonApiViewMixin, RetrieveAPIView):
]


class RelatedSegmentDetail(JsonApiViewMixin, ListAPIView):
serializer_class = SegmentDetailSerializer
queryset = Segment.objects.filter(segment_type__is_active=True).select_related('segment_type')

pagination_class = None

permission_classes = [
OpenSegmentOrMember,
TenantConditionalOpenClose,
]

def get_queryset(self):
queryset = super().get_queryset().filter(
segment_type_id=self.kwargs['segment_type'],
)

if not self.request.user.is_staff:
if self.request.user.is_authenticated:
user_segments = (segment.pk for segment in self.request.user.segments.all())
else:
user_segments = []

queryset = queryset.filter(
Q(closed=False) | Q(pk__in=user_segments)
)

return queryset


class SegmentPublicDetail(JsonApiViewMixin, RetrieveAPIView):
serializer_class = SegmentPublicDetailSerializer
queryset = Segment.objects.filter(segment_type__is_active=True).select_related('segment_type')
Expand Down
13 changes: 12 additions & 1 deletion bluebottle/time_based/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ def setUp(self):
)
self.slot = DateActivitySlotFactory.create(
activity=self.activity,
start=datetime.datetime(2022, 5, 15, tzinfo=UTC)
start=datetime.datetime(2022, 5, 15, tzinfo=UTC),
status='cancelled'
)

def _get_slot_dates(self):
return [str(s.start.date()) for s in self.activity.slots.all()]

def _get_slot_statuses(self):
return [s.status for s in self.activity.slots.all()]

def test_duplicate_every_day(self):
end = datetime.datetime(2022, 5, 20, tzinfo=UTC).date()
duplicate_slot(self.slot, 'day', end)
Expand All @@ -32,6 +36,13 @@ def test_duplicate_every_day(self):
'2022-05-18', '2022-05-19', '2022-05-20',
]
)
self.assertEqual(
self._get_slot_statuses(),
[
'cancelled', 'finished', 'finished',
'finished', 'finished', 'finished'
]
)

def test_duplicate_every_week(self):
end = datetime.datetime(2022, 7, 1, tzinfo=UTC).date()
Expand Down
1 change: 1 addition & 0 deletions bluebottle/time_based/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ def duplicate_slot(slot, interval, end):
for date in dates:
slot.id = None
slot.start = slot.start.replace(day=date.day, month=date.month, year=date.year)
slot.status = 'open'
slot.save()
2 changes: 1 addition & 1 deletion bluebottle/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def export_as_csv(modeladmin, request, queryset):
row = [prep_field(request, obj, field, manyToManySep) for field in field_names]

# Write extra field data
if queryset.model is Member:
if queryset.model is Member or issubclass(queryset.model, Contributor):
for segment_type in SegmentType.objects.all():
segments = ", ".join(obj.segments.filter(
segment_type=segment_type).values_list('name', flat=True))
Expand Down
14 changes: 7 additions & 7 deletions locale/bg/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ msgid ""
msgstr ""
"Project-Id-Version: bluebottle\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-06-17 12:36+0200\n"
"PO-Revision-Date: 2022-06-17 10:40\n"
"POT-Creation-Date: 2022-06-21 08:49+0200\n"
"PO-Revision-Date: 2022-06-21 10:44\n"
"Last-Translator: \n"
"Language-Team: Bulgarian\n"
"Language: bg\n"
Expand All @@ -15,8 +15,8 @@ msgstr ""
"X-Crowdin-Project: bluebottle\n"
"X-Crowdin-Project-ID: 457932\n"
"X-Crowdin-Language: bg\n"
"X-Crowdin-File: /demo.nlcares-improvements/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 976\n"
"X-Crowdin-File: /master/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 233\n"

#: bluebottle/activities/admin.py:62 bluebottle/activities/admin.py:603
#: bluebottle/contact/models.py:24 bluebottle/fsm/admin.py:235
Expand Down Expand Up @@ -597,11 +597,11 @@ msgid "Create"
msgstr ""

#: bluebottle/activities/states.py:110
msgid "The acivity will be created."
msgid "The activity will be created."
msgstr ""

#: bluebottle/activities/states.py:119
msgid "The acivity will be submitted for review."
msgid "The activity will be submitted for review."
msgstr ""

#: bluebottle/activities/states.py:121 bluebottle/activities/states.py:149
Expand Down Expand Up @@ -7093,7 +7093,7 @@ msgstr ""

#: bluebottle/time_based/templates/admin/time_based/duplicate_slot.html:24
msgid "\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the lock has been entered completely and correctly before you repeat the lock.\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the slot has been entered completely and correctly before you repeat the slot.\n"
" "
msgstr ""

Expand Down
18 changes: 9 additions & 9 deletions locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ msgid ""
msgstr ""
"Project-Id-Version: bluebottle\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-06-17 12:36+0200\n"
"PO-Revision-Date: 2022-06-17 10:40\n"
"POT-Creation-Date: 2022-06-21 08:49+0200\n"
"PO-Revision-Date: 2022-06-21 10:44\n"
"Last-Translator: \n"
"Language-Team: German\n"
"Language: de\n"
Expand All @@ -15,8 +15,8 @@ msgstr ""
"X-Crowdin-Project: bluebottle\n"
"X-Crowdin-Project-ID: 457932\n"
"X-Crowdin-Language: de\n"
"X-Crowdin-File: /demo.nlcares-improvements/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 976\n"
"X-Crowdin-File: /master/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 233\n"

#: bluebottle/activities/admin.py:62 bluebottle/activities/admin.py:603
#: bluebottle/contact/models.py:24 bluebottle/fsm/admin.py:235
Expand Down Expand Up @@ -597,12 +597,12 @@ msgid "Create"
msgstr "Anlegen"

#: bluebottle/activities/states.py:110
msgid "The acivity will be created."
msgstr "Die Versauerung wird geschaffen."
msgid "The activity will be created."
msgstr ""

#: bluebottle/activities/states.py:119
msgid "The acivity will be submitted for review."
msgstr "Die Ewigkeit wird zur Überprüfung eingereicht."
msgid "The activity will be submitted for review."
msgstr ""

#: bluebottle/activities/states.py:121 bluebottle/activities/states.py:149
#: bluebottle/funding/states.py:574 bluebottle/initiatives/states.py:92
Expand Down Expand Up @@ -7352,7 +7352,7 @@ msgstr ""

#: bluebottle/time_based/templates/admin/time_based/duplicate_slot.html:24
msgid "\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the lock has been entered completely and correctly before you repeat the lock.\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the slot has been entered completely and correctly before you repeat the slot.\n"
" "
msgstr ""

Expand Down
18 changes: 9 additions & 9 deletions locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ msgid ""
msgstr ""
"Project-Id-Version: bluebottle\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2022-06-17 12:36+0200\n"
"PO-Revision-Date: 2022-06-17 10:40\n"
"POT-Creation-Date: 2022-06-21 08:49+0200\n"
"PO-Revision-Date: 2022-06-21 10:44\n"
"Last-Translator: \n"
"Language-Team: Spanish (Modern)\n"
"Language: es_EM\n"
Expand All @@ -15,8 +15,8 @@ msgstr ""
"X-Crowdin-Project: bluebottle\n"
"X-Crowdin-Project-ID: 457932\n"
"X-Crowdin-Language: es-EM\n"
"X-Crowdin-File: /demo.nlcares-improvements/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 976\n"
"X-Crowdin-File: /master/locale/en/LC_MESSAGES/django.po\n"
"X-Crowdin-File-ID: 233\n"

#: bluebottle/activities/admin.py:62 bluebottle/activities/admin.py:603
#: bluebottle/contact/models.py:24 bluebottle/fsm/admin.py:235
Expand Down Expand Up @@ -597,12 +597,12 @@ msgid "Create"
msgstr "Crear"

#: bluebottle/activities/states.py:110
msgid "The acivity will be created."
msgstr "Se creará la acividad."
msgid "The activity will be created."
msgstr ""

#: bluebottle/activities/states.py:119
msgid "The acivity will be submitted for review."
msgstr "La acividad se someterá a revisión."
msgid "The activity will be submitted for review."
msgstr ""

#: bluebottle/activities/states.py:121 bluebottle/activities/states.py:149
#: bluebottle/funding/states.py:574 bluebottle/initiatives/states.py:92
Expand Down Expand Up @@ -7105,7 +7105,7 @@ msgstr ""

#: bluebottle/time_based/templates/admin/time_based/duplicate_slot.html:24
msgid "\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the lock has been entered completely and correctly before you repeat the lock.\n"
" It is not possible to make bulk changes to the created slots afterwards. So make sure that all information for the slot has been entered completely and correctly before you repeat the slot.\n"
" "
msgstr ""

Expand Down
Loading

0 comments on commit 61e125f

Please sign in to comment.