Repository: 3cn-ecn/nantralPlatform

Issue:
[Colocs] Bug d'affichage des adresses avec une coloc future
Si on ajoute une coloc avec une date de début dans le futur, elle devient la dernière coloc associée à cette adresse, et comme elle n'est pas encore occupée, elle disparaît de la carte


Discussion:

Resolving patch:
diff --git a/server/apps/roommates/api_views.py b/server/apps/roommates/api_views.py
--- a/server/apps/roommates/api_views.py
+++ b/server/apps/roommates/api_views.py
@@ -2,7 +2,8 @@
 from rest_framework.response import Response
 from rest_framework.views import APIView
 
-from .serializers import HousingLastRoommatesSerializer #HousingSerializer, RoommatesGroupSerializer, RoommatesMemberSerializer
+# HousingSerializer, RoommatesGroupSerializer, RoommatesMemberSerializer
+from .serializers import HousingLastRoommatesSerializer
 from .models import Housing, NamedMembershipRoommates, Roommates
 from apps.student.models import Student
 from apps.utils.geocoding import geocode
@@ -31,7 +32,6 @@ def get_queryset(self):
         return query
 
 
-
 class CheckAddressView(APIView):
     """An API view to wether wether a housing already exists at selected address.
     Returns the pk if it does, None otherwise"""
@@ -40,9 +40,9 @@ class CheckAddressView(APIView):
     def post(self, request):
         query = Housing.objects.filter(address=request.data.get("address"))
         data = [{
-                    'pk':housing.pk, 
-                    'name': f'{housing.address} - {housing.details} ({housing.last_roommates})'
-                } for housing in query ]
+            'pk': housing.pk,
+            'name': f'{housing.address} - {housing.details} ({housing.current_roommates})'
+        } for housing in query]
         return Response(data=data)
 
 
@@ -136,4 +136,4 @@ class RoommatesMemberView(generics.RetrieveUpdateDestroyAPIView):
 
     def get_queryset(self):
         return NamedMembershipRoommates.objects.filter(id=self.kwargs['pk'])
-'''
\ No newline at end of file
+'''
diff --git a/server/apps/roommates/models.py b/server/apps/roommates/models.py
--- a/server/apps/roommates/models.py
+++ b/server/apps/roommates/models.py
@@ -4,7 +4,9 @@
 from apps.group.models import Group, NamedMembership
 from apps.student.models import Student
 from apps.utils.geocoding import geocode
+from django.db.models import Q
 
+from django.utils import timezone
 
 
 class Housing(models.Model):
@@ -17,19 +19,19 @@ class Housing(models.Model):
 
     def save(self, *args, **kwargs):
         coordinates = geocode(self.address)[0]
-        if not self.latitude or not self.longitude or abs(self.latitude-coordinates['lat'])>5e-3 or abs(self.longitude-coordinates['long'])>5e-3:
+        if not self.latitude or not self.longitude or abs(self.latitude-coordinates['lat']) > 5e-3 or abs(self.longitude-coordinates['long']) > 5e-3:
             self.latitude = coordinates['lat']
             self.longitude = coordinates['long']
         super(Housing, self).save(*args, **kwargs)
 
     def __str__(self):
         return self.address if self.address else self.id
-    
-    @property
-    def last_roommates(self):
-        last_roommates = Roommates.objects.filter(housing=self).order_by('begin_date').last()
-        return last_roommates
 
+    @property
+    def current_roommates(self):
+        now = timezone.now()
+        return Roommates.objects.filter(Q(housing=self) & (Q(Q(begin_date__lte=now) & (
+            Q(end_date__gte=now) | Q(end_date=None))) | (Q(members=None)))).order_by('begin_date').last()
 
 
 class Roommates(Group):
@@ -46,7 +48,6 @@ class Meta:
         verbose_name = "coloc"
 
 
-
 class NamedMembershipRoommates(NamedMembership):
     group = models.ForeignKey(
         to=Roommates, on_delete=models.CASCADE)
@@ -58,4 +59,3 @@ def __str__(self):
             return f'{self.nickname} ({self.student.name})'
         else:
             return self.student.name
-            
diff --git a/server/apps/roommates/serializers.py b/server/apps/roommates/serializers.py
--- a/server/apps/roommates/serializers.py
+++ b/server/apps/roommates/serializers.py
@@ -1,5 +1,8 @@
 from rest_framework import serializers
 from .models import Housing, NamedMembershipRoommates, Roommates
+from django.db.models import Q
+
+from django.utils import timezone
 
 
 class HousingLastRoommatesSerializer(serializers.ModelSerializer):
@@ -10,23 +13,22 @@ class HousingLastRoommatesSerializer(serializers.ModelSerializer):
 
     class Meta:
         model = Housing
-        fields='__all__'
-    
+        fields = '__all__'
+
     def get_roommates(self, obj):
-        roommates = Roommates.objects.filter(housing=obj).order_by('begin_date').last()
-        return RoommatesSerializer(roommates, many=False, context=self._context).data
+        return RoommatesSerializer(obj.current_roommates, many=False, context=self._context).data
 
 
 class RoommatesMemberSerializer(serializers.ModelSerializer):
     '''Serializer for a member of roommates'''
-    
+
     #student = StudentSerializer(read_only=True)
     name = serializers.SerializerMethodField()
 
     class Meta:
         model = NamedMembershipRoommates
         fields = ['nickname', 'name']
-    
+
     def get_name(self, obj):
         return obj.student.name
 
@@ -34,7 +36,8 @@ def get_name(self, obj):
 class RoommatesSerializer(serializers.ModelSerializer):
     '''Serializer for roommates'''
 
-    members = serializers.SerializerMethodField() #RoommatesMemberSerializer(read_only=True, many=True)
+    # RoommatesMemberSerializer(read_only=True, many=True)
+    members = serializers.SerializerMethodField()
     url = serializers.SerializerMethodField()
 
     class Meta:
@@ -44,14 +47,11 @@ class Meta:
     def get_members(self, obj):
         members = NamedMembershipRoommates.objects.filter(group=obj)
         return RoommatesMemberSerializer(members, many=True, context=self._context).data
-    
+
     def get_url(self, obj):
         return obj.get_absolute_url
 
 
-
-
-
 '''
 class HousingSerializer(serializers.ModelSerializer):
     edit_url = serializers.HyperlinkedIdentityField(
@@ -120,4 +120,4 @@ def create(self, validated_data):
                 nickname=member['nickname']
             )
         return roommates
-'''
\ No newline at end of file
+'''


Test patch: