Quellcode durchsuchen

Project working

Marcos Dumay de Medeiros vor 9 Jahren
Ursprung
Commit
584528d3dd

+ 3 - 0
.gitignore

@@ -43,3 +43,6 @@ docs/_build/
 
 #Virtualenv dir
 ENV/
+
+#Pycharm project
+.idea/

+ 0 - 3
README.rst

@@ -21,9 +21,6 @@ template looks as following:
        <script src="link to jquery"></script>
    </head>
    <body>
-       {% load rapid_crud %}{% load rapid_filters %}
-       {% register_filters %}
-       {% register_overlay %}
        {% load rapid_menu %}
        {% menu request %}
        {% block body %}{% endblock %}

+ 0 - 2
setup.py

@@ -26,8 +26,6 @@ here = path.abspath(path.dirname(__file__))
 rapid = path.join('src', 'rapid')
 rapid_files = list(_recursive_find(os.path.join(here, rapid), ''))
 
-print rapid_files
-
 # Get the long description from the relevant file
 with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
     long_description = f.read()

+ 58 - 51
src/rapid/permissions.py

@@ -1,65 +1,72 @@
 __author__ = 'marcos.medeiros'
 
-from rapid import wrappers
+class Permission:
+    def __init__(self, model, instances):
+        self.model = model
+        self.instances = instances
 
-def default_read(model):
-    m = wrappers.ModelData(model)
+def all_instances(model):
+    def i(request):
+        if model(request):
+            return {}
+        else:
+            return None
+    return i
 
-    def perm(request):
-        if request.user.is_authenticated() and request.user.is_staff():
-            return m.default_manager()
-        return None
-    return perm
+def apply_instances_permission(model, perm):
+    if perm is None:
+        return []
+    if hasattr(perm, 'keys'):
+        return model.default_manager().filter(**perm)
+    if hasattr(perm, '__iter__'):
+        return perm
+    if hasattr(perm, 'all'):
+        return perm
+    return []
 
-def default_write(model):
-    def perm(request):
-        return None
-    return perm
+def has_instance(model, perm, instance):
+    p = apply_instances_permission(model, perm)
+    if hasattr(p, 'filter'):
+        return p.filter(pk=instance.pk).exists()
+    if hasattr(p, '__iter__'):
+        return bool([f for f in p if f.pk == instance.pk])
+    return False
 
-def to_profile(model, profile):
-    m = wrappers.ModelData(model)
+def to_profile(profile):
     if hasattr(profile, "__iter__"):
-
-        def perm(request):
+        def m(request):
             if not request.user.is_authenticated():
-                return None
+                return False
             up = [p.pk for p in request.user.profile_set]
             for p in up:
                 if p in profile:
-                    return m.default_manager()
-            return None
-        return perm
-
-    def perm(request):
-        if not request.user.is_authenticated():
-            return None
-        up = [p.pk for p in request.user.profile_set]
-        if profile in up:
-            return m.default_manager()
-        return None
-    return perm
-
-def to_staff(model):
-    m = wrappers.ModelData(model)
-
-    def perm(request):
-        if request.user.is_authenticated() and request.user.is_staff():
-            return m.default_manager()
-        return None
-    return perm
-
-def to_all(model):
-    m = wrappers.ModelData(model)
+                    True
+            return False
+    else:
+        def m(request):
+            if not request.user.is_authenticated():
+                return False
+            up = [p.pk for p in request.user.profile_set]
+            if profile in up:
+                return True
+            return False
+    return Permission(m, all_instances(m))
 
-    def perm(request):
-        return m.default_manager()
-    return perm
+def to_staff():
+    def m(request):
+        if request.user.is_authenticated() and request.user.is_staff:
+            return True
+        return False
+    return Permission(m, all_instances(m))
 
-def to_admins(model):
-    m = wrappers.ModelData(model)
+def to_all():
+    def m(request):
+        return True
+    return Permission(m, all_instances(m))
 
-    def perm(request):
-        if request.user.is_authenticated() and request.user.is_admin():
-            return m.default_manager()
-        return None
-    return perm
+def to_superusers():
+    def m(request):
+        if request.user.is_authenticated() and request.user.is_superuser:
+            return True
+        return False
+    return Permission(m, all_instances(m))

+ 2 - 8
src/rapid/templates/rapid/main_window.html

@@ -1,6 +1,8 @@
 {% extends 'base.html' %}
 
 {% block body %}
+{% load rapid_filters %}{% register_filters %}
+{% load rapid_crud %}{% register_overlay %}
 <div class="data">
     <style scoped>
         a.reload{
@@ -10,12 +12,4 @@
     <a class="reload" href="{{ this_url }}"></a>
     <div class="reload-here">{{ body_text|safe }}</div>
 </div>
-<script>
-    $(document).ready(function(){
-        $("body").on("click", "a.rapid-submit-form", function(event){
-            event.preventDefault();
-            $(this).closest("form").submit();
-        });
-    });
-</script>
 {% endblock %}

+ 2 - 2
src/rapid/templatetags/rapid_menu.py

@@ -16,8 +16,8 @@ def _app_menu(app, request):
     for m in models:
         st = registry.model_entry(m).get('list')
         if st:
-            read = st.permission_set(request)
-            if read and read.exists():
+            read = st.permission_set.model(request)
+            if read:
                 has_model = True
                 cd = ModelData(st.model)
                 sub += '<li><a href="%s">%s</a></li>\n' % (

+ 10 - 5
src/rapid/urls.py

@@ -14,14 +14,19 @@ from rapid import permissions
 
 def _can_manage_users(request):
     if not request.user.is_authenticated:
-        return None
+        return []
     return request.user.application.managed_applications.profile_set
 
+_manage_users_permistion = permissions.Permission(
+    lambda r: False,
+    _can_manage_users
+)
+
 urlpatterns = views.register_model(Application, 'aplicacao',
-                             write_set=permissions.to_admins(Application), read_set=permissions.to_all(Application)) +\
-    views.register_model(Profile, write_set=permissions.to_admins(Profile), read_set=permissions.to_staff(Profile)) +\
+                             write_set=permissions.to_superusers(), read_set=permissions.to_all()) +\
+    views.register_model(Profile, write_set=permissions.to_superusers(), read_set=permissions.to_staff()) +\
     views.register_instance_form(Profile, 'manage_users', u'Gerenciar Usuários',
-                            ManageUsers, _can_manage_users, "fa-users",
+                            ManageUsers, _manage_users_permistion, "fa-users",
                             Action.Visibility.list) +\
-    views.register_simple_select(User, ['username'], permissions.to_staff(User), 'usuario')
+    views.register_simple_select(User, ['username'], permissions.to_staff(), 'usuario')
 

+ 8 - 18
src/rapid/wrappers.py

@@ -7,6 +7,7 @@ from rapid import filters
 
 import itertools
 from django.db import models
+from rapid import permissions
 
 
 class InstanceData:
@@ -56,18 +57,10 @@ class InstanceData:
         return self.model.is_controlled()
 
     def can_read(self):
-        if self.can_write():
-            return True
-        vw = registry.model_entry(self.model.model).permission_set(self.request)
-        if vw:
-            return vw.filter(pk=self.instance.pk).exists()
-        return False
+        return self.has_permission(self.request, 'view')
 
     def can_write(self):
-        ed = registry.model_entry(self.model.model)['edit'].permission_set(self.request)
-        if ed:
-            return ed.filter(pk=self.instance.pk).exists()
-        return False
+        return self.has_permission(self.request, 'edit')
 
     def view_url(self):
         return registry.get_url_of_action(self.model.model, "view", pk=self.instance.pk)
@@ -101,8 +94,7 @@ class InstanceData:
         acts = registry.model_entry(self.model.model)
         if self.request:
             for a in acts.values():
-                if a.permission_set(self.request) and\
-                        a.permission_set(self.request).filter(pk=self.instance.pk).exists() and\
+                if self.has_permission(self.request, a.action.name) and\
                         a.action.visibility > Action.Visibility.hidden:
                     yield (a, a.get_url(self.instance))
 
@@ -124,8 +116,8 @@ class InstanceData:
     def has_permission(self, request, action_name):
         m = registry.model_entry(self.model.model).get(action_name)
         if m:
-            return m.permission_set(request) and\
-                m.permission_set(request).filter(pk=self.instance.pk).exists()
+            perm = m.permission_set.instances
+            return permissions.has_instance(self.model, perm(request), self.instance)
         return False
 
     def __unicode__(self):
@@ -211,8 +203,7 @@ class ModelData:
         acts = registry.model_entry(self.model)
         if self.request:
             for a in acts.values():
-                if a.permission_set(self.request) and\
-                        a.permission_set(self.request).exists() and\
+                if self.has_permission(self.request, a.action.name) and\
                         not a.action.query_parameters and\
                         a.action.visibility > Action.Visibility.hidden:
                     yield (a, a.get_url())
@@ -220,8 +211,7 @@ class ModelData:
     def has_permission(self, request, action_name):
         m = registry.model_entry(self.model).get(action_name)
         if m:
-            return m.permission_set(request) and\
-                m.permission_set(request).exists()
+            return bool(m.permission_set.model(request))
         return False
 
     def field_by_name(self, field_name):