Skip to content

Commit

Permalink
Merge 4267faf into bcc96d3
Browse files Browse the repository at this point in the history
  • Loading branch information
gannetson authored Oct 5, 2022
2 parents bcc96d3 + 4267faf commit f6a8fff
Show file tree
Hide file tree
Showing 48 changed files with 593 additions and 725 deletions.
63 changes: 36 additions & 27 deletions bluebottle/activities/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from bluebottle.fsm.admin import StateMachineAdmin, StateMachineFilter
from bluebottle.fsm.forms import StateMachineModelForm
from bluebottle.funding.models import Funding, Donor, MoneyContribution
from bluebottle.geo.models import Location
from bluebottle.impact.admin import ImpactGoalInline
from bluebottle.initiatives.models import InitiativePlatformSettings
from bluebottle.segments.models import SegmentType
Expand Down Expand Up @@ -276,9 +277,9 @@ class ActivityChildAdmin(PolymorphicChildModelAdmin, StateMachineAdmin):

def lookup_allowed(self, key, value):
if key in [
'initiative__location__id__exact',
'initiative__location__subregion__id__exact',
'initiative__location__subregion__region__id__exact',
'office_location__id__exact',
'office_location__subregion__id__exact',
'office_location__subregion__region__id__exact',
]:
return True
return super(ActivityChildAdmin, self).lookup_allowed(key, value)
Expand Down Expand Up @@ -311,7 +312,6 @@ def save_model(self, request, obj, form, change):
'stats_data',
'review_status',
'send_impact_reminder_message_link',
'location_link'
]

detail_fields = (
Expand All @@ -320,6 +320,10 @@ def save_model(self, request, obj, form, change):
'owner'
)

office_fields = (
'office_location',
)

description_fields = (
'slug',
'description',
Expand Down Expand Up @@ -361,11 +365,11 @@ def get_list_filter(self, request):
settings = InitiativePlatformSettings.objects.get()
from bluebottle.geo.models import Location
if Location.objects.count():
filters = filters + ['initiative__location']
filters = filters + ['office_location']
if settings.enable_office_regions:
filters = filters + [
'initiative__location__subregion',
'initiative__location__subregion__region']
'office_location__subregion',
'office_location__subregion__region']

if settings.team_activities:
filters = filters + ['team_activity']
Expand All @@ -376,7 +380,7 @@ def get_list_display(self, request):
fields = list(self.list_display)
from bluebottle.geo.models import Location
if Location.objects.count():
fields = fields + ['location_link']
fields = fields + ['office_location']
return fields

def get_status_fields(self, request, obj):
Expand All @@ -387,8 +391,9 @@ def get_status_fields(self, request, obj):
return fields

def get_detail_fields(self, request, obj):
settings = InitiativePlatformSettings.objects.get()
fields = self.detail_fields
if obj and obj.initiative.is_global:
if obj and Location.objects.count() and not settings.enable_office_regions:
fields = list(fields)
fields.insert(3, 'office_location')
fields = tuple(fields)
Expand Down Expand Up @@ -417,19 +422,23 @@ def initiative_link(self, obj):
)
initiative_link.short_description = _('Initiative')

def location_link(self, obj):
if not obj.initiative.location:
return "-"
url = reverse('admin:geo_location_change', args=(obj.initiative.location.id,))
return format_html('<a href="{}">{}</a>', url, obj.initiative.location)
location_link.short_description = _('office')

def get_fieldsets(self, request, obj=None):
settings = InitiativePlatformSettings.objects.get()
fieldsets = [
(_('Detail'), {'fields': self.get_detail_fields(request, obj)}),
(_('Description'), {'fields': self.get_description_fields(request, obj)}),
(_('Status'), {'fields': self.get_status_fields(request, obj)}),
]

if Location.objects.count():
if settings.enable_office_restrictions and 'office_restriction' not in self.office_fields:
self.office_fields += (
'office_restriction',
)
fieldsets.insert(1, (
_('Office'), {'fields': self.office_fields}
))

if request.user.is_superuser:
fieldsets += [
(_('Super admin'), {'fields': (
Expand Down Expand Up @@ -552,15 +561,15 @@ class ActivityAdmin(PolymorphicParentModelAdmin, StateMachineAdmin):
CollectActivity
)
date_hierarchy = 'transition_date'
readonly_fields = ['link', 'review_status', 'location_link']
readonly_fields = ['link', 'review_status']
list_filter = [PolymorphicChildModelFilter, StateMachineFilter, 'highlight', ]

def lookup_allowed(self, key, value):
if key in [
'goals__type__id__exact',
'initiative__location__id__exact',
'initiative__location__subregion__id__exact',
'initiative__location__subregion__region__id__exact',
'office_location__id__exact',
'office_location__subregion__id__exact',
'office_location__subregion__region__id__exact',
]:
return True
return super(ActivityAdmin, self).lookup_allowed(key, value)
Expand All @@ -571,11 +580,11 @@ def get_list_filter(self, request):

from bluebottle.geo.models import Location
if Location.objects.count():
filters = filters + ['initiative__location']
filters = filters + ['office_location']
if settings.enable_office_regions:
filters = filters + [
'initiative__location__subregion',
'initiative__location__subregion__region'
'office_location__subregion',
'office_location__subregion__region'
]

if settings.team_activities:
Expand All @@ -587,17 +596,17 @@ def get_list_filter(self, request):
'link', 'highlight']

def location_link(self, obj):
if not obj.initiative.location:
if not obj.office_location:
return "-"
url = reverse('admin:geo_location_change', args=(obj.initiative.location.id,))
return format_html('<a href="{}">{}</a>', url, obj.initiative.location)
url = reverse('admin:geo_location_change', args=(obj.office_location.id,))
return format_html('<a href="{}">{}</a>', url, obj.office_location)
location_link.short_description = _('office')

def get_list_display(self, request):
fields = list(self.list_display)
from bluebottle.geo.models import Location
if Location.objects.count():
fields = fields + ['location_link']
fields = fields + ['office_location']
return fields

search_fields = ('title', 'description',
Expand Down
68 changes: 38 additions & 30 deletions bluebottle/activities/documents.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from builtins import str

from django_elasticsearch_dsl import Document, fields
from elasticsearch_dsl.field import DateRange

from bluebottle.activities.models import Activity
from bluebottle.funding.models import Donor
from bluebottle.initiatives.models import Initiative, Theme
from bluebottle.members.models import Member
from bluebottle.utils.documents import MultiTenantIndex
from bluebottle.activities.models import Activity
from bluebottle.utils.search import Search
from elasticsearch_dsl.field import DateRange
from bluebottle.members.models import Member

from bluebottle.initiatives.models import Initiative, Theme


class DateRangeField(fields.DEDField, DateRange):
Expand Down Expand Up @@ -110,6 +110,16 @@ class ActivityDocument(Document):
}
)

office_restriction = fields.NestedField(
attr='office_restriction',
properties={
'restriction': fields.TextField(),
'office': fields.LongField(),
'office_subregion': fields.LongField(),
'office_region': fields.LongField(),
}
)

contributors = fields.DateField()
contributor_count = fields.IntegerField()
donation_count = fields.IntegerField()
Expand Down Expand Up @@ -186,8 +196,6 @@ def prepare_type(self, instance):

def prepare_country(self, instance):
country_ids = []
if instance.initiative.location:
country_ids.append(instance.initiative.location.country_id)
if hasattr(instance, 'office_location') and instance.office_location:
country_ids.append(instance.office_location.country_id)
if instance.initiative.place:
Expand All @@ -198,6 +206,7 @@ def prepare_location(self, instance):
locations = []
if hasattr(instance, 'location') and instance.location:
locations.append({
'id': instance.location.id,
'name': instance.location.formatted_address,
'locality': instance.location.locality,
'country_code': instance.location.country.alpha2_code,
Expand All @@ -219,31 +228,30 @@ def prepare_location(self, instance):
),
'type': 'office'
})
elif instance.initiative.location:

locations.append({
'id': instance.initiative.location.pk,
'name': instance.initiative.location.name,
'locality': instance.initiative.location.city,
'country_code': (
instance.initiative.location.country.alpha2_code if
instance.initiative.location.country else None
),
'country': (
instance.initiative.location.country.name if
instance.initiative.location.country else None
),
'type': 'initiative_office'
})
elif instance.initiative.place:
locations.append({
'locality': instance.initiative.place.locality,
'country_code': instance.initiative.place.country.alpha2_code,
'country': instance.initiative.place.country.name,
'type': 'impact_location'
})
if instance.initiative.place.country:
locations.append({
'locality': instance.initiative.place.locality,
'country_code': instance.initiative.place.country.alpha2_code,
'country': instance.initiative.place.country.name,
'type': 'impact_location'
})
else:
locations.append({
'locality': instance.initiative.place.locality,
'type': 'impact_location'
})
return locations

def prepare_office_restriction(self, instance):
office = instance.office_location
return {
'restriction': instance.office_restriction,
'office': office.id if office else None,
'subregion': office.subregion.id if office and office.subregion_id else None,
'region': office.subregion.region.id if office and office.subregion and office.subregion.region else None
}

def prepare_expertise(self, instance):
if hasattr(instance, 'expertise') and instance.expertise:
return [
Expand Down Expand Up @@ -271,7 +279,7 @@ def prepare_is_online(self, instance):
return instance.is_online

def prepare_position(self, instance):
return None
return []

def prepare_end(self, instance):
return None
Expand Down
30 changes: 27 additions & 3 deletions bluebottle/activities/filters.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import re
import dateutil
from datetime import datetime, time

import dateutil
from django.conf import settings

from elasticsearch_dsl.function import ScriptScore
from elasticsearch_dsl.query import (
FunctionScore, SF, Terms, Term, Nested, Q, Range, ConstantScore
)
from elasticsearch_dsl.function import ScriptScore

from bluebottle.activities.documents import activity
from bluebottle.geo.models import Location
from bluebottle.initiatives.models import InitiativePlatformSettings
from bluebottle.utils.filters import ElasticSearchFilter


Expand All @@ -34,6 +36,7 @@ class ActivitySearchFilter(ElasticSearchFilter):
'status',
'upcoming',
'location.id',
'office',
'segment',
'team_activity',
'initiative.id',
Expand Down Expand Up @@ -162,6 +165,27 @@ def get_upcoming_filter(self, value, request):
if value == 'false':
return Terms(status=['succeeded', 'partially_funded'])

def get_office_filter(self, value, request):
office = Location.objects.filter(id=value).first()
initiative_settings = InitiativePlatformSettings.load()
if initiative_settings.enable_office_restrictions:
return Nested(
path='office_restriction',
query=Term(
office_restriction__restriction='all'
) | (
Term(office_restriction__office=office.id) &
Term(office_restriction__restriction='office')
) | (
Term(office_restriction__subregion=office.subregion.id) &
Term(office_restriction__restriction='office_subregion')
) | (
Term(office_restriction__region=office.subregion.region.id) &
Term(office_restriction__restriction='office_region')
)
)
return []

def get_duration_filter(self, value, request):
start = request.GET.get('filter[start]')
end = request.GET.get('filter[end]')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.24 on 2022-09-08 12:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('activities', '0059_auto_20220804_1214'),
]

operations = [
migrations.AddField(
model_name='activity',
name='office_restriction',
field=models.CharField(choices=[('office', 'Only people from the same office are allowed to participate'), ('office_subregion', 'Only people within the same group are allowed to participate'), ('office_region', 'Only people within the same region are allowed to participate'), ('all', 'Everybody is allowed to participate')], default='all', max_length=100, verbose_name='Office restriction'),
),
]
24 changes: 24 additions & 0 deletions bluebottle/activities/migrations/0060_auto_20220831_1507.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.2.24 on 2022-08-31 13:07

from django.db import migrations


def migrate_office_to_activities(apps, schema_editor):
Initiative = apps.get_model('initiatives', 'Initiative')
for initiative in Initiative.objects.filter(location__isnull=False).all():
initiative.activities.update(office_location=initiative.location)


class Migration(migrations.Migration):

dependencies = [
('activities', '0059_auto_20220804_1214'),
]

operations = [
migrations.RunPython(
migrate_office_to_activities,
migrations.RunPython.noop
)
]

14 changes: 14 additions & 0 deletions bluebottle/activities/migrations/0061_merge_20220909_1019.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.2.24 on 2022-09-09 08:19

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('activities', '0060_auto_20220831_1507'),
('activities', '0060_activity_office_restriction'),
]

operations = [
]
Loading

0 comments on commit f6a8fff

Please sign in to comment.