Skip to content

Commit

Permalink
Refactor: clean code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Jan 21, 2025
1 parent d3d5e3d commit a456964
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
14 changes: 5 additions & 9 deletions apps/fsm/views/fsm_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from apps.accounts.serializers.user_serializer import UserSerializer
from apps.accounts.utils.user_management import find_user_in_website
from apps.fsm.models.fsm import State
from apps.fsm.models.fsm import Player, State
from apps.fsm.pagination import StandardPagination
from apps.fsm.serializers.papers.state_serializer import StateSerializer
from errors.error_codes import ErrorCodes, serialize_error
Expand Down Expand Up @@ -98,9 +98,6 @@ def enter_fsm(self, request, pk=None):
receipt = get_receipt(user, fsm)
# is_mentor = user in fsm.mentors.all()

# if receipt is None:
# raise ParseError(serialize_error('4079'))

if fsm.fsm_p_type in [FSM.FSMPType.Team, FSM.FSMPType.Hybrid]:
if receipt.team is None:
raise ParseError(serialize_error('4078'))
Expand All @@ -111,10 +108,9 @@ def enter_fsm(self, request, pk=None):
# if fsm.entrance_lock and password != fsm.entrance_lock:
# raise PermissionDenied(serialize_error('4080'))

player = get_players(user, fsm).filter(finished_at__isnull=True).last()

# first time entering fsm
if not player:
try:
player = get_players(user, fsm).get(finished_at__isnull=True)
except Player.DoesNotExist:
serializer = PlayerSerializer(
data={
'user': user.id,
Expand All @@ -135,7 +131,7 @@ def enter_fsm(self, request, pk=None):
player.current_state = fsm.first_state
player.save()

return Response(status=status.HTTP_202_ACCEPTED)
return Response(PlayerMinimalSerializer(player).data, status=status.HTTP_202_ACCEPTED)

@swagger_auto_schema(responses={200: PlayerSerializer}, tags=['player'])
@transaction.atomic
Expand Down
7 changes: 5 additions & 2 deletions apps/fsm/views/player_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@ def transit_to_state(self, request):

@action(detail=True, methods=['get'], url_path='finish-fsm')
def finish_fsm(self, request, pk=None):
"""
Marks the FSM as finished for the player if not already done.
"""
player: Player = self.get_object()

if player.finished_at:
return Response(
data={"message": "you have already finished the court"},
status=status.HTTP_406_NOT_ACCEPTABLE,
{"message": "You have already finished the FSM."},
status=status.HTTP_400_BAD_REQUEST,
)

fsm = player.fsm
Expand Down

0 comments on commit a456964

Please sign in to comment.