Skip to content

Commit ed5e99f

Browse files
committed
Release v0.0.2 - Draft Code Added
1 parent 721f19f commit ed5e99f

File tree

9 files changed

+225
-1
lines changed

9 files changed

+225
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [0.0.2] 2022-10-24
4+
### Improvements
5+
6+
- Draft Code Added
7+
38
## [0.0.1] 2022-10-24
49
### Initial Version
510

django_dyn_api/admin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

django_dyn_api/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DynApiConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'apps.dyn_api'

django_dyn_api/helpers.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+
6+
import datetime, sys, inspect
7+
8+
from functools import wraps
9+
10+
from django.db import models
11+
from django.http import HttpResponseRedirect, HttpResponse
12+
13+
from rest_framework import serializers
14+
15+
class Utils:
16+
@staticmethod
17+
def get_class(config, name: str) -> models.Model:
18+
return Utils.model_name_to_class(config[name])
19+
20+
@staticmethod
21+
def get_manager(config, name: str) -> models.Manager:
22+
return Utils.get_class(config, name).objects
23+
24+
@staticmethod
25+
def get_serializer(config, name: str):
26+
class Serializer(serializers.ModelSerializer):
27+
class Meta:
28+
model = Utils.get_class(config, name)
29+
fields = '__all__'
30+
31+
return Serializer
32+
33+
@staticmethod
34+
def model_name_to_class(name: str):
35+
all_classes = inspect.getmembers(sys.modules[__name__], inspect.isclass)
36+
for cls in all_classes:
37+
if cls[0] == name:
38+
return cls[1]
39+
40+
# we are confident that never returns None
41+
return None
42+
43+
def check_permission(function):
44+
@wraps(function)
45+
def wrap(viewRequest, *args, **kwargs):
46+
47+
try:
48+
49+
# Check user
50+
if viewRequest.request.user.is_authenticated:
51+
return function(viewRequest, *args, **kwargs)
52+
53+
# All good - allow the processing
54+
return HttpResponseRedirect('/login/')
55+
56+
except Exception as e:
57+
58+
# On error
59+
return HttpResponse( 'Error: ' + str( e ) )
60+
61+
return function(viewRequest, *args, **kwargs)
62+
63+
return wrap

django_dyn_api/models.py

Whitespace-only changes.

django_dyn_api/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

django_dyn_api/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.contrib import admin
2+
from django.urls import path
3+
from .views import DynamicAPI
4+
5+
urlpatterns = [
6+
path('api/<str:model_name>/' , DynamicAPI.as_view()),
7+
path('api/<str:model_name>/<str:id>' , DynamicAPI.as_view()),
8+
path('api/<str:model_name>/<str:id>/' , DynamicAPI.as_view()),
9+
]

django_dyn_api/views.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
from django.http import Http404
2+
3+
from django.contrib.auth.decorators import login_required
4+
from django.utils.decorators import method_decorator
5+
6+
from rest_framework.generics import get_object_or_404
7+
from rest_framework.views import APIView
8+
from rest_framework.response import Response
9+
from core.settings import DYNAMIC_API
10+
11+
from .helpers import Utils
12+
13+
#from .helpers import check_permission
14+
15+
class DynamicAPI(APIView):
16+
17+
# READ : GET api/model/id or api/model
18+
def get(self, request, **kwargs):
19+
20+
model_id = kwargs.get('id', None)
21+
try:
22+
if model_id is not None:
23+
24+
# Validate for integer
25+
try:
26+
model_id = int(model_id)
27+
28+
if model_id < 0:
29+
raise ValueError('Expect positive int')
30+
31+
except ValueError as e:
32+
return Response(data={
33+
'message': 'Input Error = ' + str(e),
34+
'success': False
35+
}, status=400)
36+
37+
thing = get_object_or_404(Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')), id=model_id)
38+
model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(instance=thing)
39+
output = model_serializer.data
40+
else:
41+
all_things = Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')).all()
42+
thing_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))
43+
output = []
44+
for thing in all_things:
45+
output.append(thing_serializer(instance=thing).data)
46+
except KeyError:
47+
return Response(data={
48+
'message': 'this model is not activated or not exist.',
49+
'success': False
50+
}, status=400)
51+
except Http404:
52+
return Response(data={
53+
'message': 'object with given id not found.',
54+
'success': False
55+
}, status=404)
56+
return Response(data={
57+
'data': output,
58+
'success': True
59+
}, status=200)
60+
61+
# CREATE : POST api/model/
62+
#@check_permission
63+
def post(self, request, **kwargs):
64+
try:
65+
model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(data=request.data)
66+
if model_serializer.is_valid():
67+
model_serializer.save()
68+
else:
69+
return Response(data={
70+
**model_serializer.errors,
71+
'success': False
72+
}, status=400)
73+
except KeyError:
74+
return Response(data={
75+
'message': 'this model is not activated or not exist.',
76+
'success': False
77+
}, status=400)
78+
return Response(data={
79+
'message': 'Record Created.',
80+
'success': True
81+
}, status=200)
82+
83+
# UPDATE : PUT api/model/id/
84+
#@method_decorator(login_required, name='dispatch')
85+
#@check_permission
86+
def put(self, request, **kwargs):
87+
try:
88+
thing = get_object_or_404(Utils.get_manager(DYNAMIC_API, kwargs.get('model_name')), id=kwargs.get('id'))
89+
model_serializer = Utils.get_serializer(DYNAMIC_API, kwargs.get('model_name'))(instance=thing,
90+
data=request.data,
91+
partial=True)
92+
if model_serializer.is_valid():
93+
model_serializer.save()
94+
else:
95+
return Response(data={
96+
**model_serializer.errors,
97+
'success': False
98+
}, status=400)
99+
except KeyError:
100+
return Response(data={
101+
'message': 'this model is not activated or not exist.',
102+
'success': False
103+
}, status=400)
104+
except Http404:
105+
return Response(data={
106+
'message': 'object with given id not found.',
107+
'success': False
108+
}, status=404)
109+
return Response(data={
110+
'message': 'Record Updated.',
111+
'success': True
112+
}, status=200)
113+
114+
# DELETE : DELETE api/model/id/
115+
#@method_decorator(login_required, name='dispatch')
116+
#@check_permission
117+
def delete(self, request, **kwargs):
118+
try:
119+
model_manager = Utils.get_manager(DYNAMIC_API, kwargs.get('model_name'))
120+
to_delete_id = kwargs.get('id')
121+
model_manager.get(id=to_delete_id).delete()
122+
except KeyError:
123+
return Response(data={
124+
'message': 'this model is not activated or not exist.',
125+
'success': False
126+
}, status=400)
127+
except Utils.get_class(DYNAMIC_API, kwargs.get('model_name')).DoesNotExist as e:
128+
return Response(data={
129+
'message': 'object with given id not found.',
130+
'success': False
131+
}, status=404)
132+
return Response(data={
133+
'message': 'Record Deleted.',
134+
'success': True
135+
}, status=200)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
setup(
1010
name='django-dynamic-api',
11-
version='0.0.1',
11+
version='0.0.2',
1212
zip_safe=False,
1313
packages=find_packages(),
1414
include_package_data=True,

0 commit comments

Comments
 (0)