Skip to content

Commit

Permalink
fix: Removed more default group references
Browse files Browse the repository at this point in the history
  • Loading branch information
josebui committed Nov 1, 2023
1 parent 60ae822 commit 35bb645
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 84 deletions.
27 changes: 0 additions & 27 deletions terraso_backend/apps/core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
# along with this program. If not, see https://www.gnu.org/licenses/.

from django.contrib import admin
from django.urls import reverse
from django.utils.safestring import mark_safe

from .models import (
Group,
Expand Down Expand Up @@ -54,37 +52,12 @@ class LandscapeAdmin(admin.ModelAdmin):
list_display = ("name", "slug", "location", "website", "created_at")
raw_id_fields = ("membership_list",)

readonly_fields = ("default_group",)

def default_group(self, obj):
group = obj.get_default_group()
url = reverse("admin:core_landscapedefaultgroup_change", args=[group.pk])
return mark_safe(f'<a href="{url}">{group}</a>')

default_group.short_description = "Default Group"


class LandscapeDefaultGroup(Group):
class Meta:
proxy = True


@admin.register(LandscapeDefaultGroup)
class LandscapeDefaultGroupAdmin(admin.ModelAdmin):
list_display = ("name", "slug", "website", "created_at")
inlines = [MembershipInline]

def get_queryset(self, request):
qs = super().get_queryset(request)
landscape_group_ids = [
values[0]
for values in LandscapeGroup.objects.filter(
is_default_landscape_group=True
).values_list("group__id")
]
return qs.filter(id__in=landscape_group_ids)


@admin.register(LandscapeGroup)
class LandscapeGroupAdmin(admin.ModelAdmin):
list_display = ("landscape", "group")
Expand Down
10 changes: 3 additions & 7 deletions terraso_backend/apps/core/management/commands/loadsampledata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see https://www.gnu.org/licenses/.

from apps.collaboration.models import MembershipList
from apps.core.models import Group, Landscape, LandscapeGroup

from ._base_airtable import BaseAirtableCommand
Expand Down Expand Up @@ -47,13 +48,8 @@ def handle(self, *args, **kwargs):
name=landscape_name, defaults=model_data
)

# Creates Landscape default group
default_group, _ = Group.objects.update_or_create(name=f"{landscape_name} Group")
landscape_group, _ = LandscapeGroup.objects.update_or_create(
landscape=landscape,
group=default_group,
defaults={"is_default_landscape_group": True},
)
# Creates Landscape membership list
membership_list, _ = MembershipList.objects.update_or_create(landscape=landscape)

# Creates Partnership group
partnership_name = landscape_data.get("Landscape Partnership Name")
Expand Down
26 changes: 3 additions & 23 deletions terraso_backend/apps/core/models/landscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,35 +137,15 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def delete(self, *args, **kwargs):
default_group = self.get_default_group()
membership_list = self.membership_list

with transaction.atomic():
ret = super().delete(*args, **kwargs)
# default group should be deleted as well
if default_group is not None:
default_group.delete()
if membership_list is not None:
membership_list.delete()

return ret

def get_default_group(self):
"""
A default Group in a Landscape is that Group where any
individual (associated or not with other Groups) is added when
associating directly with a Landscape.
"""
try:
# associated_groups is the related_name defined on
# LandscapeGroup relationship with Landscape. It returns a
# queryset of LandscapeGroup
landscape_group = self.associated_groups.get(is_default_landscape_group=True)
except LandscapeGroup.DoesNotExist:
logger.error(
"Landscape has no default group, but it must have", extra={"landscape_id": self.pk}
)
return None

return landscape_group.group

def __str__(self):
return self.name

Expand Down
8 changes: 8 additions & 0 deletions terraso_backend/apps/graphql/schema/group_associations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import graphene
import structlog
from django.core.exceptions import ValidationError
from django.db.models import Q
from graphene import relay
from graphene_django import DjangoObjectType

Expand Down Expand Up @@ -47,6 +48,13 @@ class Meta:
interfaces = (relay.Node,)
connection_class = TerrasoConnection

@classmethod
def get_queryset(cls, queryset, info):
return queryset.exclude(
Q(parent_group__associated_landscapes__is_default_landscape_group=True)
| Q(child_group__associated_landscapes__is_default_landscape_group=True)
)


class GroupAssociationAddMutation(BaseAuthenticatedMutation):
group_association = graphene.Field(GroupAssociationNode)
Expand Down
9 changes: 6 additions & 3 deletions terraso_backend/apps/graphql/schema/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

class GroupFilterSet(django_filters.FilterSet):
memberships__email = django_filters.CharFilter(method="filter_memberships_email")
associated_landscapes__is_default_landscape_group = django_filters.BooleanFilter(
method="filter_associated_landscapes"
)
associated_landscapes__isnull = django_filters.BooleanFilter(
method="filter_associated_landscapes"
)
Expand Down Expand Up @@ -84,6 +81,12 @@ class Meta:
interfaces = (relay.Node,)
connection_class = TerrasoConnection

@classmethod
def get_queryset(cls, queryset, info):
return queryset.exclude(
associated_landscapes__is_default_landscape_group=True,
)

def resolve_account_membership(self, info):
user = info.context.user
if user.is_anonymous:
Expand Down
2 changes: 1 addition & 1 deletion terraso_backend/apps/graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Query {
"""The ID of the object"""
id: ID!
): GroupAssociationNode!
groups(offset: Int, before: String, after: String, first: Int, last: Int, name: String, name_Icontains: String, name_Istartswith: String, slug: String, slug_Icontains: String, description_Icontains: String, memberships_Email: String, associatedLandscapes_IsDefaultLandscapeGroup: Boolean, associatedLandscapes_Isnull: Boolean, associatedLandscapes_IsPartnership: Boolean): GroupNodeConnection
groups(offset: Int, before: String, after: String, first: Int, last: Int, name: String, name_Icontains: String, name_Istartswith: String, slug: String, slug_Icontains: String, description_Icontains: String, memberships_Email: String, associatedLandscapes_Isnull: Boolean, associatedLandscapes_IsPartnership: Boolean): GroupNodeConnection
landscapes(offset: Int, before: String, after: String, first: Int, last: Int, name_Icontains: String, description_Icontains: String, slug: String, slug_Icontains: String, website_Icontains: String, location_Icontains: String, membershipList_Memberships_User_Email: String): LandscapeNodeConnection
users(offset: Int, before: String, after: String, first: Int, last: Int, email: String, email_Icontains: String, email_Iexact: String, firstName_Icontains: String, lastName_Icontains: String, project: String): UserNodeConnection
landscapeGroups(offset: Int, before: String, after: String, first: Int, last: Int, landscape: ID, landscape_Slug_Icontains: String, group: ID, group_Slug_Icontains: String, isPartnership: Boolean): LandscapeGroupNodeConnection
Expand Down
6 changes: 3 additions & 3 deletions terraso_backend/apps/graphql/schema/visualization_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from graphene import relay
from graphene_django import DjangoObjectType

from apps.collaboration.models import Membership as CollaborationMembership
from apps.core.gis.mapbox import get_publish_status
from apps.core.models import Group, Landscape, Membership
from apps.graphql.exceptions import GraphQLNotAllowedException
Expand Down Expand Up @@ -105,9 +106,8 @@ def get_queryset(cls, queryset, info):
user__id=user_pk, membership_status=Membership.APPROVED
).values_list("group", flat=True)
user_landscape_ids = Landscape.objects.filter(
associated_groups__group__memberships__user__id=user_pk,
associated_groups__group__memberships__membership_status=Membership.APPROVED,
associated_groups__is_default_landscape_group=True,
membership_list__memberships__user__id=user_pk,
membership_list__memberships__membership_status=CollaborationMembership.APPROVED,
).values_list("id", flat=True)
all_ids = list(user_groups_ids) + list(user_landscape_ids)
return queryset.filter(data_entry__shared_resources__target_object_id__in=all_ids)
Expand Down
19 changes: 0 additions & 19 deletions terraso_backend/tests/core/models/test_landscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,6 @@ def test_landscape_groups_creation_explicitly():
assert group.associated_landscapes.count() == 1


def test_landscape_get_default_group():
landscape = mixer.blend(Landscape)
groups = mixer.cycle(3).blend(Group)
default_group = groups.pop()

LandscapeGroup.objects.create(
landscape=landscape, group=default_group, is_default_landscape_group=True
)
landscape.groups.add(*groups)

assert landscape.get_default_group() == default_group


def test_landscape_get_default_group_without_group_returns_none():
landscape = mixer.blend(Landscape)

assert landscape.get_default_group() is None


def test_landscape_creator_becomes_manager():
user = mixer.blend(User)
landscape = mixer.blend(Landscape, created_by=user)
Expand Down
4 changes: 3 additions & 1 deletion terraso_backend/tests/graphql/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,9 @@ def landscape_common_group(landscapes, groups):
group = groups[1]
landscape = landscapes[0]

common_group = mixer.blend(LandscapeGroup, landscape=landscape, group=group)
common_group = mixer.blend(
LandscapeGroup, landscape=landscape, group=group, is_default_landscape_group=False
)

return common_group

Expand Down

0 comments on commit 35bb645

Please sign in to comment.