Skip to content

Commit

Permalink
Merge pull request #291 from kartoza/fix-not-found-issue
Browse files Browse the repository at this point in the history
patch: update serializer
  • Loading branch information
NyakudyaA authored Nov 28, 2023
2 parents 94f871b + 921bdb6 commit 5b8fc02
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions django_project/monitor/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,43 @@
Observations,
Sites
)
from minisass_authentication.serializers import LookupSerializer

class SitesSerializer(serializers.ModelSerializer):
class Meta:
model = Sites
fields = '__all__'

class ObservationsSerializer(serializers.ModelSerializer):
site = serializers.SerializerMethodField()
site = SitesSerializer()
sitename = serializers.CharField(source='site.site_name')
rivername = serializers.CharField(source='site.river_name')
sitedescription = serializers.CharField(source='site.description')
rivercategory = serializers.CharField(source='site.river_cat')
longitude = serializers.FloatField(source='site.the_geom.x')
latitude = serializers.FloatField(source='site.the_geom.y')
collectorsname = serializers.SerializerMethodField()
organisationtype = serializers.SerializerMethodField()

class Meta:
model = Observations
fields = '__all__'

def get_site(self, obj):
return {
'sitename': obj.site.site_name,
'rivername': obj.site.river_name,
'sitedescription': obj.site.description,
'rivercategory': obj.site.river_cat,
'longitude': obj.site.the_geom.x,
'latitude': obj.site.the_geom.y,
}

def get_collectorsname(self, obj):
user_profile = self.get_user_profile(obj)
try:
user_profile = UserProfile.objects.get(user=obj.user)
except UserProfile.DoesNotExist:
user_profile = None

return (
f"{user_profile.user.first_name} {user_profile.user.last_name}"
if user_profile and user_profile.user.first_name and user_profile.user.last_name
else user_profile.user.username if user_profile else ""
)

def get_organisationtype(self, obj):
user_profile = self.get_user_profile(obj)
return LookupSerializer(user_profile.organisation_type).data if user_profile else ""

def get_user_profile(self, obj):
try:
return UserProfile.objects.get(user=obj.user)
user_profile = UserProfile.objects.get(user=obj.user)
except UserProfile.DoesNotExist:
return None
user_profile = None

return user_profile.organisation_type if user_profile else ""

0 comments on commit 5b8fc02

Please sign in to comment.