Skip to content

Commit 8ed6406

Browse files
Refactor code and add jazzmin
1 parent e654e1f commit 8ed6406

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+4420
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@
223223
- Optimize imports
224224
- Docker Scripts Update
225225
- Tooling: added scripts to recompile the SCSS files
226-
- `core/static/assets/` - gulpfile.js
227-
- `core/static/assets/` - package.json
226+
- `config/static/assets/` - gulpfile.js
227+
- `config/static/assets/` - package.json
228228
- `Update README` - [Recompile SCSS](https://github.com/app-generator/django-soft-ui-dashboard#recompile-css) (new section)
229229
- Fixes:
230230
- Patch 500 Error when authenticated users access `admin` path (no slash at the end)

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ COPY . .
1515
RUN python manage.py migrate
1616

1717
# gunicorn
18-
CMD ["gunicorn", "--config", "gunicorn-cfg.py", "core.wsgi"]
18+
CMD ["gunicorn", "--config", "gunicorn-cfg.py", "config.wsgi"]
File renamed without changes.

apps/charts/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.

apps/charts/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 ChartsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'apps.charts'
File renamed without changes.
File renamed without changes.
File renamed without changes.

apps/charts/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
3+
from apps.charts import views
4+
5+
urlpatterns = [
6+
path("", views.index, name="charts"),
7+
]

apps/charts/views.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from django.shortcuts import render
2+
from django.core import serializers
3+
from apps.pages.models import *
4+
5+
# Create your views here.
6+
7+
def index(request):
8+
products = serializers.serialize('json', Product.objects.all())
9+
context = {
10+
'segment': 'charts',
11+
'products': products
12+
}
13+
return render(request, 'charts/index.html', context)

apps/dyn_api/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+

apps/dyn_api/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+
6+
from django.contrib import admin
7+
8+
# Register your models here.

apps/dyn_api/apps.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+
6+
from django.apps import AppConfig
7+
8+
class DynApiConfig(AppConfig):
9+
default_auto_field = 'django.db.models.BigAutoField'
10+
name = 'apps.dyn_api'

apps/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, importlib
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+
36+
model_name = name.split('.')[-1]
37+
model_import = name.replace('.'+model_name, '')
38+
39+
module = importlib.import_module(model_import)
40+
cls = getattr(module, model_name)
41+
42+
return cls
43+
44+
def check_permission(function):
45+
@wraps(function)
46+
def wrap(viewRequest, *args, **kwargs):
47+
48+
try:
49+
50+
# Check user
51+
if viewRequest.request.user.is_authenticated:
52+
53+
return function(viewRequest, *args, **kwargs)
54+
55+
# For authentication for guests
56+
return HttpResponseRedirect('/login/')
57+
58+
except Exception as e:
59+
60+
# On error
61+
return HttpResponse( 'Error: ' + str( e ) )
62+
63+
return wrap
File renamed without changes.

apps/dyn_api/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+
6+
from django.test import TestCase
7+
8+
# Create your tests here.

apps/dyn_api/urls.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# -*- encoding: utf-8 -*-
2+
"""
3+
Copyright (c) 2019 - present AppSeed.us
4+
"""
5+
6+
from django.contrib import admin
7+
from django.urls import path
8+
from apps.dyn_api import views
9+
10+
urlpatterns = [
11+
path('api/', views.index, name="dynamic_api"),
12+
13+
path('api/<str:model_name>/' , views.DynamicAPI.as_view(), name="model_api"),
14+
path('api/<str:model_name>/<str:id>' , views.DynamicAPI.as_view()),
15+
path('api/<str:model_name>/<str:id>/' , views.DynamicAPI.as_view()),
16+
]

apps/dyn_api/views.py

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

apps/dyn_dt/.gitkeep

Whitespace-only changes.

apps/dyn_dt/__init__.py

Whitespace-only changes.

apps/dyn_dt/admin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib import admin
2+
from .models import *
3+
4+
# Register your models here.
5+
6+
admin.site.register(PageItems)
7+
admin.site.register(HideShowFilter)
8+
admin.site.register(ModelFilter)

apps/dyn_dt/apps.py

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

apps/dyn_dt/forms.py

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

0 commit comments

Comments
 (0)