Skip to content

Commit

Permalink
Merge pull request #290 from kartoza/fix-not-null-constraint
Browse files Browse the repository at this point in the history
Fix not null constraint
  • Loading branch information
NyakudyaA authored Nov 28, 2023
2 parents 49453c2 + 602c960 commit b3bbf02
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
6 changes: 6 additions & 0 deletions django_project/minisass_authentication/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rest_framework import serializers
from django.contrib.auth.models import User
from minisass_authentication.models import Lookup

class UserSerializer(serializers.ModelSerializer):
class Meta:
Expand All @@ -14,3 +15,8 @@ def create(self, validated_data):

class PasswordResetRequestSerializer(serializers.Serializer):
email = serializers.EmailField()

class LookupSerializer(serializers.ModelSerializer):
class Meta:
model = Lookup
fields = '__all__'
2 changes: 1 addition & 1 deletion django_project/monitor/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Migration(migrations.Migration):
('score', models.DecimalField(max_digits=4, decimal_places=2, default=0)),
('site', models.ForeignKey(related_name='observation',on_delete=models.CASCADE, db_column='site', to='monitor.Sites',blank=True, null=True)),
('time_stamp', models.DateTimeField(auto_now=True,null=True, blank=True)),
('comment', models.CharField(max_length=255, blank=True)),
('comment', models.CharField(max_length=255,null=True, blank=True)),
('obs_date', models.DateField(blank=True, null=True)),
('flag', models.CharField(default='dirty', max_length=5)),
('water_clarity', models.DecimalField(max_digits=8, decimal_places=1, blank=True)),
Expand Down
35 changes: 19 additions & 16 deletions django_project/monitor/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,46 @@
Observations,
Sites
)
from minisass_authentication.serializers import LookupSerializer

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

class ObservationsSerializer(serializers.ModelSerializer):
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')
site = serializers.SerializerMethodField()
collectorsname = serializers.SerializerMethodField()
organisationtype = serializers.SerializerMethodField()

class Meta:
model = Observations
fields = '__all__'

def get_collectorsname(self, obj):
try:
user_profile = UserProfile.objects.get(user=obj.user)
except UserProfile.DoesNotExist:
user_profile = None
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)
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:
user_profile = UserProfile.objects.get(user=obj.user)
return UserProfile.objects.get(user=obj.user)
except UserProfile.DoesNotExist:
user_profile = None

return user_profile.organisation_type if user_profile else ""
return None
3 changes: 2 additions & 1 deletion django_project/monitor/test_observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setUp(self):
leeches=False,
crabs_shrimps=False,
site=self.site,
comment='test_comment',
score=4.5,
obs_date=date.today(),
flag='clean',
Expand Down Expand Up @@ -85,4 +86,4 @@ def test_observation_delete_view(self):

# Check if the object is actually deleted from the database
with self.assertRaises(Observations.DoesNotExist):
Observations.objects.get(gid=observation.gid)
Observations.objects.get(gid=observation.gid)

0 comments on commit b3bbf02

Please sign in to comment.