Verified Commit b6d36baf authored by Micheal Gendy's avatar Micheal Gendy
Browse files

add option to add permissions

parent 64c0fb56
......@@ -3,6 +3,7 @@ from django.utils.html import escape
import os.path
from drf_generators.templates.serializer import SERIALIZER
from drf_generators.templates.permissions import PERMISSION_TEMP
from drf_generators.templates.apiview import API_URL, API_VIEW
from drf_generators.templates.viewset import VIEW_SET_URL, VIEW_SET_VIEW
from drf_generators.templates.function import FUNCTION_URL, FUNCTION_VIEW
......@@ -20,6 +21,7 @@ class BaseGenerator(object):
self.app = app_config.models_module
self.name = app_config.name
self.serializer_template = Template(SERIALIZER)
self.permissions_template = Template(PERMISSION_TEMP)
self.models = self.get_model_names()
self.serializers = self.get_serializer_names()
......@@ -53,6 +55,14 @@ class BaseGenerator(object):
else:
return 'Url generation cancelled'
def generate_permissions(self):
content = self.permissions_content()
filename = 'permissions.py'
if self.write_file(content,filename):
return ' - writing %s' % filename
else:
return 'Permissions generation cancelled'
def serializer_content(self, depth,merged_fields_models):
context = Context({'app': self.name, 'models': self.models,
'depth': depth , 'merged_fields_models':merged_fields_models})
......@@ -67,6 +77,10 @@ class BaseGenerator(object):
context = Context({'app': self.name, 'models': self.models})
return self.url_template.render(context)
def permissions_content(self):
context = Context({'models':self.models})
return self.permissions_template.render(context)
def get_model_names(self):
return [m.__name__ for m in self.app_config.get_models()]
......
......@@ -34,6 +34,8 @@ class Command(AppCommand):
parser.add_argument('--views', dest='views', action='store_true',
help='generate views only'),
parser.add_argument('--permissions',dest='permissions',action='store_true' ,help='generate permissions'),
parser.add_argument('--urls', dest='urls', action='store_true',
help='generate urls only'),
......@@ -57,6 +59,7 @@ class Command(AppCommand):
urls = options['urls']
exclude = options['exclude']
model = options['model']
permissions = options['permissions']
else:
raise CommandError('You must be using Django 1.11, 2.2, or 3.0')
......@@ -74,7 +77,7 @@ class Command(AppCommand):
raise CommandError(message)
if serializers:
if exclude and not model :
if exclude and not model or len(exclude) != len(model) :
message = "You must choose model to exclude fields from"
raise CommandError(message)
result = generator.generate_serializers(depth,exclude.model)
......@@ -82,10 +85,16 @@ class Command(AppCommand):
result = generator.generate_views()
elif urls:
result = generator.generate_urls()
elif permissions :
result = generator.generate_permissions()
else:
if exclude and not model or len(exclude) != len(model) :
message = "You must choose model to exclude fields from"
raise CommandError(message)
result = generator.generate_serializers(depth,exclude,model) + '\n'
result += generator.generate_views() + '\n'
result += generator.generate_urls()
result += generator.generate_permissions()
if options['verbose']:
print(result)
......@@ -8,8 +8,8 @@ from {{ app }} import views
urlpatterns = [
{% for model in models %}
url(r'^{{ model|lower }}/(?P<id>[0-9]+)/$', views.{{ model }}APIView.as_view()),
url(r'^{{ model|lower }}/$', views.{{ model }}APIListView.as_view()),
path('{{ model|lower }}/<int:id>/', views.{{ model }}APIView.as_view()),
path('{{ model|lower }}/', views.{{ model }}APIListView.as_view()),
{% endfor %}
]
"""
......
PERMISSION_TEMP = """
from rest_framework import permissions
from django.contrib.auth.models import Permission
{% for model in models %}
class CanView{{model}}(permissions.BasePermission):
message = 'None of permissions requirements fulfilled.'
def has_permission(self, request, view):
permissions = Permission.objects.filter(user=request.user).values_list('codename',flat=True)
perm = 'view_{{model|lower}}'
return perm in permissions
class CanAdd{{model}}(permissions.BasePermission):
message = 'None of permissions requirements fulfilled.'
def has_permission(self, request, view):
permissions = Permission.objects.filter(user=request.user).values_list('codename',flat=True)
perm = 'add_{{model|lower}}'
return perm in permissions
class CanChange{{model}}(permissions.BasePermission):
message = 'None of permissions requirements fulfilled.'
def has_permission(self, request, view):
permissions = Permission.objects.filter(user=request.user).values_list('codename',flat=True)
perm = 'change_{{model|lower}}'
return perm in permissions
class CanDelete{{model}}(permissions.BasePermission):
message = 'None of permissions requirements fulfilled.'
def has_permission(self, request, view):
permissions = Permission.objects.filter(user=request.user).values_list('codename',flat=True)
perm = 'delete_{{model|lower}}'
return perm in permissions
{% endfor %}
"""
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment