Skip to content

Commit

Permalink
refactor: remove perform_create to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 6, 2024
1 parent a9188c9 commit 2394940
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
4 changes: 2 additions & 2 deletions open_prices/api/locations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def create(self, request: Request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# get source
self.source = get_source_from_request(self.request)
source = get_source_from_request(self.request)
# save
location = serializer.save(
source=self.source,
source=source,
)
# return full location
return Response(
Expand Down
7 changes: 2 additions & 5 deletions open_prices/api/prices/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ def get_serializer_class(self):
return PriceUpdateSerializer
return self.serializer_class

def perform_create(self, serializer):
return serializer.save(owner=self.request.user.user_id, source=self.source)

def create(self, request: Request, *args, **kwargs):
# validate
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
# get source
self.source = get_source_from_request(self.request)
source = get_source_from_request(self.request)
# save
price = self.perform_create(serializer)
price = serializer.save(owner=self.request.user.user_id, source=source)
# return full price
return Response(
self.serializer_class(price).data, status=status.HTTP_201_CREATED
Expand Down
4 changes: 2 additions & 2 deletions open_prices/api/proofs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ def upload(self, request: Request) -> Response:
serializer = ProofCreateSerializer(data=proof_create_data)
serializer.is_valid(raise_exception=True)
# get source
self.source = get_source_from_request(self.request)
source = get_source_from_request(self.request)
# save
proof = serializer.save(
owner=self.request.user.user_id,
source=self.source,
source=source,
)
# return full proof
return Response(ProofFullSerializer(proof).data, status=status.HTTP_201_CREATED)

0 comments on commit 2394940

Please sign in to comment.