Skip to content

Commit

Permalink
Merge e59b896 into 68c9b9b
Browse files Browse the repository at this point in the history
  • Loading branch information
gannetson committed Sep 26, 2022
2 parents 68c9b9b + e59b896 commit 75f1b0f
Show file tree
Hide file tree
Showing 51 changed files with 538 additions and 726 deletions.
59 changes: 32 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,11 @@ def save_model(self, request, obj, form, change):
'owner'
)

office_fields = (
'office_location',
'office_restriction',
)

description_fields = (
'slug',
'description',
Expand Down Expand Up @@ -361,11 +366,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 +381,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 +392,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 +423,18 @@ 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 settings.enable_office_regions:
fieldsets.insert(1, (
_('Office'), {'fields': self.office_fields}
))

if request.user.is_superuser:
fieldsets += [
(_('Super admin'), {'fields': (
Expand Down Expand Up @@ -552,15 +557,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 +576,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 +592,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
39 changes: 14 additions & 25 deletions bluebottle/activities/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,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 +196,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,29 +218,19 @@ 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_expertise(self, instance):
Expand Down Expand Up @@ -271,7 +260,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
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 = [
]
54 changes: 38 additions & 16 deletions bluebottle/activities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,47 @@
from bluebottle.files.fields import ImageField
from bluebottle.follow.models import Follow
from bluebottle.fsm.triggers import TriggerMixin
from bluebottle.initiatives.models import Initiative
from bluebottle.geo.models import Location
from bluebottle.initiatives.models import Initiative, InitiativePlatformSettings
from bluebottle.utils.models import ValidatedModelMixin, AnonymizationMixin
from bluebottle.utils.utils import get_current_host, get_current_language, clean_html


@python_2_unicode_compatible
class Activity(TriggerMixin, AnonymizationMixin, ValidatedModelMixin, PolymorphicModel):

class TeamActivityChoices(DjangoChoices):
teams = ChoiceItem('teams', label=_("Teams"))
individuals = ChoiceItem('individuals', label=_("Individuals"))

class OfficeRestrictionChoices(DjangoChoices):
office = ChoiceItem(
'office',
label=_("Only people from the same office are allowed to participate")
)
office_subregion = ChoiceItem(
'office_subregion',
label=_("Only people within the same group are allowed to participate")
)
office_region = ChoiceItem(
'office_region',
label=_("Only people within the same region are allowed to participate")
)
all = ChoiceItem(
'all',
label=_("Everybody is allowed to participate")
)

owner = models.ForeignKey(
'members.Member',
verbose_name=_('activity manager'),
related_name='activities',
on_delete=models.CASCADE
)

highlight = models.BooleanField(default=False,
help_text=_('Highlight this activity to show it on homepage'))
highlight = models.BooleanField(
default=False,
help_text=_('Highlight this activity to show it on homepage')
)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)
Expand All @@ -50,11 +70,16 @@ class TeamActivityChoices(DjangoChoices):
initiative = models.ForeignKey(Initiative, related_name='activities', on_delete=models.CASCADE)

office_location = models.ForeignKey(
'geo.Location', verbose_name=_('office'),
help_text=_("Office is set on activity level because the "
"initiative is set to 'global' or no initiative has been specified."),
'geo.Location', verbose_name=_('Host office'),
null=True, blank=True, on_delete=models.SET_NULL)

office_restriction = models.CharField(
_('Restrictions'),
default=OfficeRestrictionChoices.all,
choices=OfficeRestrictionChoices.choices,
blank=True, null=True, max_length=100
)

title = models.CharField(_('Title'), max_length=255)
slug = models.SlugField(_('Slug'), max_length=100, default='new')
description = models.TextField(
Expand Down Expand Up @@ -101,20 +126,18 @@ class TeamActivityChoices(DjangoChoices):
def activity_date(self):
raise NotImplementedError

@property
def fallback_location(self):
return self.initiative.location or self.office_location

@property
def stats(self):
return {}

@property
def required_fields(self):
if self.initiative_id and self.initiative.is_global:
return ['office_location']
else:
return []
fields = []
if Location.objects.count():
fields.append('office_location')
if InitiativePlatformSettings.load().enable_office_regions:
fields.append('office_restriction')
return fields

class Meta(object):
verbose_name = _("Activity")
Expand Down Expand Up @@ -254,7 +277,6 @@ def __str__(self):


class EffortContribution(Contribution):

class ContributionTypeChoices(DjangoChoices):
organizer = ChoiceItem('organizer', label=_("Activity Organizer"))
deed = ChoiceItem('deed', label=_("Deed particpant"))
Expand Down
Loading

0 comments on commit 75f1b0f

Please sign in to comment.