Skip to content

Commit

Permalink
Fix tests; optimize queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rwakulszowa committed Jul 15, 2021
1 parent e00afe3 commit 88c2cd4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion backend-project/small_eod/collections/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def get_extra_kwargs(self):
return dict(collection_pk=self.collection.pk, case_pk=self.obj.case.pk)

def validate_item(self, item):
self.assertEqual(self.obj.reference_number, item["referenceNumber"])
self.assertEqual(self.obj.reference_number.name, item["referenceNumber"])

def increase_list(self):
children = self.factory_class.create_batch(case=self.obj.case, size=5)
Expand Down
2 changes: 1 addition & 1 deletion backend-project/small_eod/collections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_queryset(self):
case = Case.objects.filter(**parse_query(collection.query)).get(
pk=self.kwargs["case_pk"]
)
return self.model.objects.filter(case=case).all()
return self.model.objects.filter(case=case).with_nested_resources().all()


class EventCollectionViewSet(BaseSubCollection):
Expand Down
7 changes: 7 additions & 0 deletions backend-project/small_eod/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from ..generic.models import TimestampUserLogModel


class EventQuerySet(models.QuerySet):
def with_nested_resources(self):
return self


class Event(TimestampUserLogModel):
objects = EventQuerySet.as_manager()

date = models.DateTimeField(verbose_name=_("Date"), help_text=_("Date of event."))
name = models.CharField(
max_length=256, verbose_name=_("Name"), help_text=_("Name of event.")
Expand Down
7 changes: 7 additions & 0 deletions backend-project/small_eod/letters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ def __str__(self):
return self.name


class LetterQuerySet(models.QuerySet):
def with_nested_resources(self):
return self.select_related("reference_number")


class Letter(TimestampUserLogModel):
objects = LetterQuerySet.as_manager()

class Direction(models.TextChoices):
IN = "IN", "Received"
OUT = "OUT", "Sent"
Expand Down
20 changes: 13 additions & 7 deletions backend-project/small_eod/letters/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Meta:


class LetterSerializer(UserLogModelSerializer):
reference_number = serializers.CharField()
reference_number = serializers.CharField(default=None)
document_type = serializers.PrimaryKeyRelatedField(
many=False, default=None, queryset=DocumentType.objects.all()
)
Expand Down Expand Up @@ -63,9 +63,12 @@ class Meta:
def create(self, validated_data):
# Reference numbers use the "tag" mode - they're provided by value and
# created if not matching any known objects.
reference_number = ReferenceNumber.objects.get_or_create(
name=validated_data.pop("reference_number")
)[0]
reference_number_value = validated_data.pop("reference_number")
reference_number = (
ReferenceNumber.objects.get_or_create(name=reference_number_value)[0]
if reference_number_value is not None
else None
)

channel = validated_data.pop("channel")
document_type = validated_data.pop("document_type")
Expand Down Expand Up @@ -98,9 +101,12 @@ def update(self, instance, validated_data):
# Create a new reference number if necessary.
# See comment in `create`.
if "reference_number" in validated_data:
reference_number = ReferenceNumber.objects.get_or_create(
name=validated_data.pop("reference_number")
)[0]
reference_number_value = validated_data.pop("reference_number")
reference_number = (
ReferenceNumber.objects.get_or_create(name=reference_number_value)[0]
if reference_number_value is not None
else None
)
validated_data["reference_number"] = reference_number

return super().update(instance, validated_data)
Expand Down
7 changes: 7 additions & 0 deletions backend-project/small_eod/notes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from ..generic.models import TimestampUserLogModel


class NoteQuerySet(models.QuerySet):
def with_nested_resources(self):
return self


class Note(TimestampUserLogModel):
objects = NoteQuerySet.as_manager()

comment = models.CharField(max_length=256, verbose_name=_("Comment"))
case = models.ForeignKey(
to=Case,
Expand Down

0 comments on commit 88c2cd4

Please sign in to comment.