Skip to content
Snippets Groups Projects
Commit 5c34d059 authored by Ernesto Rico Schmidt's avatar Ernesto Rico Schmidt
Browse files

Refactor and Clean-up Viewsets

- Reorder the code inside the methods to fail fast on autorization,
- Replace `int_or_none` with a more generic solution, and move to utils,
- Add `get_values` and move `pk_and_slug` as `get_pk_and_slug` into utils,
- Replace calls to static methos in models local queries,
- Return meaningful status code while creating and updating resources,
- Return `409` when creating or updating a schedule produces a conflict.
parent 161db8bb
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@
#
from datetime import datetime, date, time
from typing import Dict, Optional, Union, Tuple
import requests
from django.utils import timezone
......@@ -73,3 +74,30 @@ def get_audio_url(cba_id):
audio_url = requests.get(url).json()
return audio_url
def get_values(kwargs: Dict[str, str], *keys: str) -> Union[Tuple[Union[int, str, None], ...], int, str, None]:
"""Get the values of the keys from the kwargs."""
def int_if_digit(value: Optional[str]) -> Optional[Union[int, str]]:
return int(value) if value and value.isdigit() else value
values = [kwargs.get(key) for key in keys]
if len(values) > 1:
return tuple(int_if_digit(value) for value in values)
else:
return int_if_digit(values[0])
def get_pk_and_slug(kwargs: Dict[str, str]) -> Tuple[Optional[int], Optional[str]]:
"""Get the pk and the slug from the kwargs."""
pk, slug = None, None
try:
pk = int(kwargs['pk'])
except ValueError:
slug = kwargs['pk']
return pk, slug
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment