Skip to content

Commit 67b65ad

Browse files
add initial
1 parent 2dccf6a commit 67b65ad

File tree

15 files changed

+450
-1
lines changed

15 files changed

+450
-1
lines changed

.gitignore

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
pip-wheel-metadata/
24+
share/python-wheels/
25+
*.egg-info/
26+
.installed.cfg
27+
*.egg
28+
MANIFEST
29+
30+
# PyInstaller
31+
# Usually these files are written by a python script from a template
32+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
33+
*.manifest
34+
*.spec
35+
36+
# Installer logs
37+
pip-log.txt
38+
pip-delete-this-directory.txt
39+
40+
# Unit test / coverage reports
41+
htmlcov/
42+
.tox/
43+
.nox/
44+
.coverage
45+
.coverage.*
46+
.cache
47+
nosetests.xml
48+
coverage.xml
49+
*.cover
50+
*.py,cover
51+
.hypothesis/
52+
.pytest_cache/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
target/
76+
77+
# Jupyter Notebook
78+
.ipynb_checkpoints
79+
80+
# IPython
81+
profile_default/
82+
ipython_config.py
83+
84+
# pyenv
85+
.python-version
86+
87+
# pipenv
88+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
90+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
91+
# install all needed dependencies.
92+
#Pipfile.lock
93+
94+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
95+
__pypackages__/
96+
97+
# Celery stuff
98+
celerybeat-schedule
99+
celerybeat.pid
100+
101+
# SageMath parsed files
102+
*.sage.py
103+
104+
# Environments
105+
.env
106+
.venv
107+
env/
108+
venv/
109+
ENV/
110+
env.bak/
111+
venv.bak/
112+
113+
# Spyder project settings
114+
.spyderproject
115+
.spyproject
116+
117+
# Rope project settings
118+
.ropeproject
119+
120+
# mkdocs documentation
121+
/site
122+
123+
# mypy
124+
.mypy_cache/
125+
.dmypy.json
126+
dmypy.json
127+
128+
# Pyre type checker
129+
.pyre/
130+
131+
# Other
132+
.idea

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
# django_rest_structure_sample
1+
## This is sample project that use `django-rest-structure`
2+
3+
### install requirements:
4+
5+
`pip install requirements.txt`
6+
7+
### run project
8+
9+
`python manage.py runserver`
10+
11+
### then call this urls:
12+
`/api/v1/profile/`
13+
14+
`/api/v1/exception/`
15+
16+
then see what happen

api/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
from django.urls import path, include
3+
4+
urlpatterns = [
5+
path('v1/', include('api.v1.urls')),
6+
]

api/v1/serializers.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from django_rest_structure.api.serializers import BaseSerializer
2+
from rest_framework import serializers
3+
from django_rest_structure.api.validations import StrFieldValidation
4+
from django_rest_structure.results.response import ResponseStructure
5+
from django_rest_structure.requests.tools import get_client_ip
6+
7+
from core.exception import ResultMessages
8+
9+
10+
def first_name_validation(value, **extra_params):
11+
return value.capitalize()
12+
13+
14+
def last_name_validation(value, **extra_params):
15+
return value.capitalize()
16+
17+
18+
class ProfileSerializer(BaseSerializer):
19+
first_name = serializers.CharField(validators=[StrFieldValidation(
20+
regex=r'^[A-Za-z]+$',
21+
error_message='just enter alphabet',
22+
extra_function=first_name_validation
23+
)])
24+
last_name = serializers.CharField(validators=[StrFieldValidation(
25+
regex=r'^[A-Za-z]+$',
26+
error_message='just enter alphabet',
27+
extra_function=last_name_validation
28+
)])
29+
30+
age = serializers.IntegerField()
31+
32+
def get_detail(self):
33+
response_data = {
34+
'request_id': self.request.request_uid,
35+
'ip_address': get_client_ip(self.request),
36+
'first_name': self.validated_data['first_name'],
37+
'last_name': self.validated_data['last_name'],
38+
'full_name': f"{self.validated_data['first_name']} {self.validated_data['last_name']}",
39+
'age': self.validated_data['age']
40+
}
41+
return ResponseStructure(ResultMessages.GET_SUCCESSFULLY, response_data)

api/v1/urls.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
from django.urls import path, include
3+
4+
from api.v1.views import ProfileView, TestExceptionView
5+
6+
urlpatterns = [
7+
path('profile/', ProfileView.as_view()),
8+
path('exception/', TestExceptionView.as_view()),
9+
10+
]

api/v1/views.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django_rest_structure.api.views import BaseApiView
2+
from django_rest_structure.results.response import ResponseStructure
3+
4+
from api.v1.serializers import ProfileSerializer
5+
from core.exception import ResultMessages, Ex
6+
7+
8+
class ProfileView(BaseApiView):
9+
def post_method(self, request, *args, **kwargs):
10+
return ProfileSerializer(data=request.data, check_is_valid=True, request=request).get_detail()
11+
12+
13+
class TestExceptionView(BaseApiView):
14+
def post_method(self, request, *args, **kwargs):
15+
raise Ex(Ex.TEST_RESULT)

core/exception.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from django_rest_structure.results.exception import Err, ResultMessages
2+
from django_rest_structure.results.codes import ResultMessageStructure
3+
from rest_framework import status
4+
5+
6+
class CustomResultMessages(ResultMessages):
7+
TEST_RESULT = ResultMessageStructure(10, 'this is my test error', False, status.HTTP_400_BAD_REQUEST)
8+
9+
10+
class Ex(CustomResultMessages, Err):
11+
def __init__(self, *args, **kwargs):
12+
super(Ex, self).__init__(*args, **kwargs)

core/response.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def response_handler(response):
2+
return {
3+
'status': {
4+
'code': response.message.code,
5+
'message': response.message.message,
6+
},
7+
'result': response.body
8+
}

django_rest_structure_sample/__init__.py

Whitespace-only changes.

django_rest_structure_sample/asgi.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for django_rest_structure_sample project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_rest_structure_sample.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)