-
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 #18 from Django-Wanted-Internship-3-Team/feature/i…
…ssue-004 통계 조회 API 작성
- Loading branch information
Showing
26 changed files
with
607 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM python:3.11-slim | ||
|
||
ENV PYTHONDONTWRITEBYTECODE 1 | ||
ENV PYTHONUNBUFFERED 1 | ||
ENV POETRY_VERSION=1.6.1 | ||
ENV POETRY_HOME=/opt/poetry | ||
ENV POETRY_VENV=/opt/poetry-venv | ||
|
||
RUN python3 -m venv $POETRY_VENV \ | ||
&& $POETRY_VENV/bin/pip install -U pip setuptools \ | ||
&& $POETRY_VENV/bin/pip install poetry==${POETRY_VERSION} | ||
|
||
ENV PATH="${PATH}:${POETRY_VENV}/bin" | ||
|
||
RUN mkdir /app/ | ||
WORKDIR /app/ | ||
|
||
COPY pyproject.toml ./ | ||
|
||
RUN poetry install | ||
|
||
COPY . /app |
Empty file.
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class CommonConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "common" |
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,30 @@ | ||
from common.utils import mandatory_key, optional_key | ||
|
||
|
||
def mandatories(*keys): | ||
def decorate(func): | ||
def wrapper(View, *args, **kwargs): | ||
mandatory = dict() | ||
for key in keys: | ||
data = mandatory_key(View.request, key) | ||
mandatory[key] = data | ||
return func(View, m=mandatory, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorate | ||
|
||
|
||
def optionals(*keys): | ||
def decorate(func): | ||
def wrapper(View, *args, **kwargs): | ||
optional = dict() | ||
for arg in keys: | ||
for key, val in arg.items(): | ||
data = optional_key(View.request, key, val) | ||
optional[key] = data | ||
return func(View, o=optional, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorate |
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,20 @@ | ||
from rest_framework import status | ||
from rest_framework.exceptions import APIException | ||
|
||
|
||
class MissingMandatoryParameterException(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = "Missing mandatory parameter" | ||
default_code = "missing_mandatory_parameter" | ||
|
||
|
||
class InvalidParameterException(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = "Invalid parameter" | ||
default_code = "invalid_parameter" | ||
|
||
|
||
class UnknownServerErrorException(APIException): | ||
status_code = status.HTTP_400_BAD_REQUEST | ||
default_detail = "Unknown server error" | ||
default_code = "unknown_server_error" |
Empty file.
Empty file.
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,38 @@ | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from rest_framework.test import APITestCase | ||
|
||
|
||
class QueryTest(APITestCase): | ||
def test_get_query(self): | ||
response = self.client.get( | ||
path=reverse("query"), | ||
data={ | ||
"name": "John", | ||
"age": "30", | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["name"], "John") | ||
self.assertEqual(response.data["age"], "30") | ||
|
||
def test_post_query(self): | ||
response = self.client.post( | ||
path=reverse("query"), | ||
data={ | ||
"city": "New York", | ||
"occupation": "Engineer", | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(response.data["city"], "New York") | ||
self.assertEqual(response.data["occupation"], "Engineer") | ||
|
||
def test_query_fail(self): | ||
response = self.client.get( | ||
path=reverse("query"), | ||
data={ | ||
"name": "John", | ||
}, | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) |
Empty file.
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,16 @@ | ||
from django.test import TestCase | ||
from django.utils import timezone | ||
|
||
from common.utils import get_before_week, get_now | ||
|
||
|
||
class DateUtilityTest(TestCase): | ||
def test_get_now(self): | ||
now = get_now() | ||
current_date = timezone.now().strftime("%Y-%m-%d") | ||
self.assertEqual(now, current_date) | ||
|
||
def test_get_before_week(self): | ||
before_week = get_before_week() | ||
expected_date = (timezone.now() - timezone.timedelta(days=7)).strftime("%Y-%m-%d") | ||
self.assertEqual(before_week, expected_date) |
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,7 @@ | ||
from django.urls import path | ||
|
||
from common.views import QueryTestView | ||
|
||
urlpatterns = [ | ||
path("query/", QueryTestView.as_view(), name="query"), | ||
] |
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,57 @@ | ||
from django.utils import timezone | ||
from rest_framework.request import Request | ||
|
||
from common.exceptions import MissingMandatoryParameterException | ||
|
||
|
||
#################### | ||
# Request Decorator | ||
#################### | ||
def mandatory_key(request: Request, name: str) -> any: | ||
try: | ||
if request.method == "GET": | ||
data = request.GET[name] | ||
else: | ||
data = request.POST[name] | ||
if data in ["", None]: | ||
raise MissingMandatoryParameterException() | ||
except Exception: | ||
try: | ||
json_body = request.data | ||
data = json_body[name] | ||
if data in ["", None]: | ||
raise MissingMandatoryParameterException() | ||
except Exception: | ||
raise MissingMandatoryParameterException() | ||
|
||
return data | ||
|
||
|
||
def optional_key(request: Request, name: str, default_value="") -> any: | ||
try: | ||
if request.method == "GET": | ||
data = request.GET[name] | ||
else: | ||
data = request.POST[name] | ||
if data in ["", None]: | ||
data = default_value | ||
except Exception: | ||
try: | ||
json_body = request.data | ||
data = json_body[name] | ||
if data in ["", None]: | ||
data = default_value | ||
except Exception: | ||
data = default_value | ||
return data | ||
|
||
|
||
#################### | ||
# Date | ||
#################### | ||
def get_now() -> timezone: | ||
return timezone.now().strftime("%Y-%m-%d") | ||
|
||
|
||
def get_before_week() -> timezone: | ||
return (timezone.now() - timezone.timedelta(days=7)).strftime("%Y-%m-%d") |
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,16 @@ | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from common.dacorator import mandatories, optionals | ||
|
||
|
||
class QueryTestView(APIView): | ||
@mandatories("name", "age") | ||
def get(self, request, m): | ||
response_data = {"name": m["name"], "age": m["age"]} | ||
return Response(response_data) | ||
|
||
@optionals({"city": "New York", "occupation": "Engineer"}) | ||
def post(self, request, o): | ||
response_data = {"city": o["city"], "occupation": o["occupation"]} | ||
return Response(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
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,33 @@ | ||
version: '3.9' | ||
|
||
volumes: | ||
postgres: {} | ||
|
||
services: | ||
postgres: | ||
container_name: postgres | ||
image: postgres:16.0-alpine | ||
volumes: | ||
- postgres:/var/lib/postgresql/data/ | ||
environment: | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=password | ||
- POSTGRES_DB=repo_1 | ||
- TZ=Asia/Seoul | ||
restart: on-failure | ||
|
||
django: | ||
container_name: django | ||
build: | ||
context: . | ||
dockerfile: Dockerfile | ||
command: poetry run python manage.py runserver 0.0.0.0:8000 | ||
volumes: | ||
- .:/app | ||
environment: | ||
- POSTGRESQL_HOST=postgres | ||
ports: | ||
- "8000:8000" | ||
depends_on: | ||
- postgres | ||
restart: on-failure |
Oops, something went wrong.