-
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 #8 from jourdanrodrigues/development
Development
- Loading branch information
Showing
6 changed files
with
117 additions
and
18 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from rest_framework import serializers | ||
|
||
from controk_webservice.addresses.models import Address | ||
|
||
|
||
class AddressSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Address | ||
fields = ['place', 'place_name', 'number', 'complement', 'neighborhood', 'city', 'state', 'cep'] |
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,9 +1,23 @@ | ||
from rest_framework import serializers | ||
|
||
from controk_webservice.addresses.serializers import AddressSerializer | ||
from controk_webservice.clients.models import Client | ||
|
||
|
||
class ClientSerializer(serializers.ModelSerializer): | ||
class ClientListSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Client | ||
fields = ['id', 'name', 'email', 'cpf', 'observation'] | ||
|
||
|
||
class ClientSerializer(serializers.ModelSerializer): | ||
place_options = serializers.SerializerMethodField() | ||
address = AddressSerializer() | ||
|
||
@staticmethod | ||
def get_place_options(client): | ||
return dict(client.address.PLACES) | ||
|
||
class Meta: | ||
model = Client | ||
fields = ['phone', 'mobile', 'address', 'place_options'] |
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,22 +1,27 @@ | ||
from rest_framework import status | ||
|
||
from assets.models import CustomAPITestCase | ||
from controk_webservice.clients.models import Client | ||
from controk_webservice.clients.models import Client, Address | ||
|
||
|
||
class ClientTest(CustomAPITestCase): | ||
fixtures = ['clients', 'addresses'] | ||
items = ['id', 'name', 'email', 'cpf', 'observation'] | ||
|
||
def test_clients_list(self): | ||
response = self.client.get('/api/v1/clients/', **self.request_kwargs) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.bulkAssertIn(self.items, response.data, is_list=True) | ||
|
||
items = ['id', 'name', 'email', 'cpf', 'observation'] | ||
self.bulkAssertIn(items, response.data, is_list=True) | ||
|
||
def test_retrieve_client(self): | ||
client_id = Client.objects.values_list('id', flat=True).first() | ||
response = self.client.get('/api/v1/clients/{}/'.format(client_id), **self.request_kwargs) | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.bulkAssertIn(self.items, response.data) | ||
|
||
items = [{'address': ['place', 'place_name', 'number', 'complement', 'neighborhood', 'city', 'state', 'cep'], | ||
'place_options': dict(Address.PLACES).keys()}, | ||
'phone', 'mobile'] | ||
self.bulkAssertIn(items, response.data) |
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,8 +1,21 @@ | ||
from rest_framework import viewsets | ||
|
||
from controk_webservice.clients.serializers import Client, ClientSerializer | ||
from assets.models import MultiSerializerViewSet | ||
from controk_webservice.clients.serializers import Client, ClientListSerializer, ClientSerializer | ||
|
||
|
||
class ClientsViewSet(viewsets.ReadOnlyModelViewSet): | ||
serializer_class = ClientSerializer | ||
class ClientsViewSet(MultiSerializerViewSet, viewsets.ReadOnlyModelViewSet): | ||
queryset = Client.objects.all() | ||
serializers = { | ||
'default': ClientSerializer, | ||
'list': ClientListSerializer | ||
} | ||
|
||
def get_queryset(self): | ||
queryset = self.queryset | ||
|
||
# Bring the address within the main query only on retrieve | ||
if self.action == 'retrieve': | ||
queryset = queryset.select_related('address') | ||
|
||
return queryset |