This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from SELab-2/visit_api
Fixed visit API and added some tests for it
- Loading branch information
Showing
13 changed files
with
192 additions
and
18 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
backend/drtrottoir/migrations/0013_remove_visit_building_visit_building_in_tour.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 4.1.7 on 2023-03-16 18:05 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('drtrottoir', '0012_alter_waste_building'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='visit', | ||
name='building', | ||
), | ||
migrations.AddField( | ||
model_name='visit', | ||
name='building_in_tour', | ||
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='drtrottoir.buildingintour', verbose_name='id of building in tour'), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
from django.db import models | ||
|
||
from .building import Building | ||
from .building_in_tour import BuildingInTour | ||
from .custom_user import CustomUser | ||
|
||
|
||
class Visit(models.Model): | ||
arrival = models.DateTimeField(verbose_name="time of arrival") | ||
building = models.ForeignKey(Building, verbose_name="id of building", on_delete=models.CASCADE) | ||
building_in_tour = models.ForeignKey(BuildingInTour, | ||
verbose_name="id of building in tour", | ||
on_delete=models.CASCADE) | ||
user = models.ForeignKey(CustomUser, verbose_name="id of user", on_delete=models.CASCADE) | ||
comment = models.TextField(verbose_name="Comment on the visit", blank=True) | ||
|
||
def __str__(self): | ||
return f"{self.user.first_name}, {self.building}: {self.arrival}" | ||
return f"{self.user.first_name}, {self.building_in_tour.building.nickname}: {self.arrival}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
backend/drtrottoir/serializers/building_in_tour_partial.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from drtrottoir.models import BuildingInTour | ||
from rest_framework import serializers | ||
|
||
|
||
class BuildingInTourPartialSerializer(serializers.HyperlinkedModelSerializer): | ||
""" | ||
A serializer for buildings in tours, not showing recursive relations | ||
""" | ||
building_data = serializers.SerializerMethodField() | ||
tour_name = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = BuildingInTour | ||
fields = [ | ||
'url', | ||
'order_index', | ||
'building', | ||
'building_data', | ||
'tour', | ||
'tour_name' | ||
] | ||
|
||
def get_building_data(self, obj): | ||
building = obj.building | ||
return {"nickname": building.nickname, "description": building.description} | ||
|
||
def get_tour_name(self, obj): | ||
return obj.tour.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from drtrottoir.models import Tour | ||
from rest_framework import serializers | ||
|
||
|
||
class TourPartialSerializer(serializers.HyperlinkedModelSerializer): | ||
""" | ||
A serializer for buildings, not showing recursive relations | ||
""" | ||
region_name = serializers.CharField( | ||
source='region.region_name', | ||
read_only=True | ||
) | ||
|
||
class Meta: | ||
model = Tour | ||
fields = [ | ||
'url', | ||
'name', | ||
'region_name' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,34 @@ | ||
from rest_framework import serializers | ||
from drtrottoir.models import Visit | ||
from .building_partial import BuildingPartialSerializer | ||
from .user_partial import UserPartialSerializer | ||
from drtrottoir.models import Visit, CustomUser, BuildingInTour | ||
|
||
|
||
class VisitSerializer(serializers.HyperlinkedModelSerializer): | ||
""" | ||
A serializer for visits | ||
""" | ||
|
||
user = UserPartialSerializer() | ||
building = BuildingPartialSerializer() | ||
user = serializers.HyperlinkedRelatedField(queryset=CustomUser.objects.all(), view_name='customuser-detail') | ||
user_data = serializers.SerializerMethodField() | ||
building_in_tour = serializers.HyperlinkedRelatedField(queryset=BuildingInTour.objects.all(), | ||
view_name='buildingintour-detail') | ||
building_in_tour_data = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = Visit | ||
fields = '__all__' | ||
|
||
def get_user_data(self, obj): | ||
user = obj.user | ||
return {"email": user.email, | ||
"first_name": user.first_name, | ||
"last_name": user.last_name | ||
} | ||
|
||
def get_building_in_tour_data(self, obj): | ||
building_in_tour = obj.building_in_tour | ||
tour = building_in_tour.tour | ||
building = building_in_tour.building | ||
return {"nickname": building.nickname, | ||
"description": building.description, | ||
"tour_name": tour.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
backend/drtrottoir/tests/factories/building_in_tour_factory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from factory.django import DjangoModelFactory | ||
import factory | ||
from drtrottoir.models import BuildingInTour | ||
from .tour_factory import TourFactory | ||
from .building_factory import BuildingFactory | ||
|
||
|
||
class BuildingInTourFactory(DjangoModelFactory): | ||
order_index = factory.Faker('pyint', min_value=0, max_value=100) | ||
building = factory.SubFactory(BuildingFactory) | ||
tour = factory.SubFactory(TourFactory) | ||
|
||
class Meta: | ||
model = BuildingInTour |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters