Skip to content

Commit c97fa2a

Browse files
committed
fix locations
1 parent 12d8deb commit c97fa2a

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

apps/accounts/serializers.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,24 @@ def get_can_mentor_on(self, user: ProjectUser) -> list[dict]:
237237

238238

239239
class PeopleGroupLocationSerializer(BaseLocationSerializer):
240+
240241
class Meta(BaseLocationSerializer.Meta):
241242
model = PeopleGroupLocation
242243

243244

245+
class PeopleGroupLocationRelated(serializers.RelatedField):
246+
def get_queryset(self):
247+
return PeopleGroupLocation.objects.all()
248+
249+
def to_representation(self, instance: PeopleGroupLocation) -> dict:
250+
return PeopleGroupLocationSerializer(instance=instance).data
251+
252+
def to_internal_value(self, element: dict) -> PeopleGroupLocation:
253+
if element.get("pk"):
254+
return PeopleGroupLocation.objects.get(pk=element["pk"])
255+
return PeopleGroupLocation(**element)
256+
257+
244258
class PeopleGroupSuperLightSerializer(
245259
AutoTranslatedModelSerializer, serializers.ModelSerializer
246260
):
@@ -478,7 +492,7 @@ class PeopleGroupSerializer(
478492
child=serializers.IntegerField(min_value=1, max_value=17),
479493
required=False,
480494
)
481-
location = PeopleGroupLocationSerializer()
495+
location = PeopleGroupLocationRelated(required=False, allow_null=True)
482496

483497
def get_hierarchy(self, obj: PeopleGroup) -> list[dict[str, str | int]]:
484498
request = self.context.get("request")
@@ -540,7 +554,11 @@ def validate_parent(self, value):
540554
def create(self, validated_data):
541555
team = validated_data.pop("team", {})
542556
featured_projects = validated_data.pop("featured_projects", [])
543-
tags = validated_data.pop("tags", [])
557+
location = validated_data.pop("location", {})
558+
559+
if location:
560+
location.save()
561+
validated_data["id"] = location
544562

545563
people_group = super(PeopleGroupSerializer, self).create(validated_data)
546564
PeopleGroupAddTeamMembersSerializer().create(
@@ -549,23 +567,25 @@ def create(self, validated_data):
549567
PeopleGroupAddFeaturedProjectsSerializer().create(
550568
{"people_group": people_group, "featured_projects": featured_projects}
551569
)
552-
553-
people_group.tags.set(tags)
554570
return people_group
555571

556572
def update(self, instance, validated_data):
557573
validated_data.pop("team", {})
558574
validated_data.pop("featured_projects", [])
559-
tags = validated_data.pop("tags", [])
575+
location = validated_data.pop("location")
576+
577+
if not location and getattr(instance, "location", None):
578+
instance.location.delete()
579+
validated_data["location"] = None
580+
elif location:
581+
location.save()
582+
validated_data["location"] = location
583+
560584
people_group = super(PeopleGroupSerializer, self).update(
561585
instance, validated_data
562586
)
563-
people_group.tags.set(tags)
564587
return people_group
565588

566-
def save(self, **kwargs):
567-
return super().save(**kwargs)
568-
569589
class Meta:
570590
model = PeopleGroup
571591
read_only_fields = ["is_root", "slug", "modules"]

apps/projects/models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,6 @@ def duplicate(self, project: "Project") -> "Goal":
864864

865865
class AbstractLocation(
866866
HasAutoTranslatedFields,
867-
ProjectRelated,
868867
DuplicableModel,
869868
models.Model,
870869
):
@@ -910,10 +909,6 @@ class Meta:
910909
default=LocationType.TEAM,
911910
)
912911

913-
def get_related_project(self) -> Optional["Project"]:
914-
"""Return the projects related to this model."""
915-
return self.project
916-
917912
def get_related_organizations(self) -> list["Organization"]:
918913
"""Return the organizations related to this model."""
919914
return self.project.get_related_organizations()
@@ -924,7 +919,7 @@ def duplicate(self) -> Self:
924919
return copy
925920

926921

927-
class Location(AbstractLocation):
922+
class Location(ProjectRelated, AbstractLocation):
928923
"""A project location on Earth.
929924
930925
Attributes
@@ -939,6 +934,10 @@ class Location(AbstractLocation):
939934
Project, on_delete=models.CASCADE, related_name="locations"
940935
)
941936

937+
def get_related_project(self) -> Optional["Project"]:
938+
"""Return the projects related to this model."""
939+
return self.project
940+
942941
def duplicate(self, project: Project) -> "Location":
943942
copy = super().duplicate()
944943
copy.project = project

0 commit comments

Comments
 (0)