Explorar o código

Solved some catch 22 on deploying new applications and registering them.

Marcos Dumay de Medeiros %!s(int64=9) %!d(string=hai) anos
pai
achega
92d0814ade
Modificáronse 4 ficheiros con 30 adicións e 10 borrados
  1. 1 0
      src/rapid/models.py
  2. 21 5
      src/rapid/permissions.py
  3. 6 2
      src/rapid/registry.py
  4. 2 3
      src/rapid/urls.py

+ 1 - 0
src/rapid/models.py

@@ -34,6 +34,7 @@ class Profile(models.Model):
     class Meta(object):
         verbose_name = u'perfil'
         verbose_name_plural = u'perfis'
+        unique_together = (('application', 'name'),)
 
 
 class DocumentTemplate(models.Model):

+ 21 - 5
src/rapid/permissions.py

@@ -1,7 +1,11 @@
 # coding=utf-8
+from django.core.exceptions import ObjectDoesNotExist
+from rapid.models import Application, Profile
 
 __author__ = 'marcos.medeiros'
 
+from registry import _caller_urls_module
+
 
 class Permission(object):
     """
@@ -57,21 +61,31 @@ def has_instance(model, perm, instance):
     return False
 
 
-def to_profile(profile):
+def to_profile(profile_name):
     """
     Grants permission over the model and all instances to the given profile(s)
-    :param profile: A profile.id or an iterable of those.
+    :param profile_name: Name or list of names of profiles that'll receive the permission.
     """
-    if hasattr(profile, "__iter__"):
+    app = Application.objects.get(python_name=_caller_urls_module())  # Should never fail
+    if hasattr(profile_name, "__iter__"):
+        profiles = [p.pk for p in Profile.objects.filter(name__in=profile_name, application=app).all()]
+
         def m(request):
             if not request.user.is_authenticated():
                 return False
             up = [p.pk for p in request.user.profile_set]
             for p in up:
-                if p in profile:
+                if p in profiles:
                     return True
             return False
     else:
+        profile = None
+        try:
+            profile = Profile.objects.get(name=profile_name, application=app)
+        except ObjectDoesNotExist:
+            # Profile is not registered yet. May happen after a deploy.
+            return False
+
         def m(request):
             if not request.user.is_authenticated():
                 return False
@@ -114,11 +128,13 @@ def to_superusers():
     return Permission(m, all_instances(m))
 
 
-def to_application_managers(app):
+def to_application_managers(python_name):
     """
     Grants permission over the model and all instances to the manager of the given application
     :param app: Application.id of the desired application
     """
+    app = [a.pk for a in Application.objects.filter(python_name=python_name).all()]
+
     def m(request):
         if not request.user.is_authenticated():
             return False

+ 6 - 2
src/rapid/registry.py

@@ -147,8 +147,12 @@ class _Registry:
         if not module_name:
             raise Exception("Unidentified python module registering " + str(model))
         if not registry._modules.has_key(module_name):
-            logging.error("Module " + module_name + " is not set-up for registering cruds")
-            return None
+            if Application.objects.filter(python_name=module_name):
+                registry._modules[module_name] = Application.objects.get(python_name=module_name)
+            else:
+                a = Application(name=module_name, python_name=module_name)
+                a.save()
+                registry._modules[module_name] = a
 
         module_entry = registry._modules[module_name]
         module_entry.models.add(model)

+ 2 - 3
src/rapid/urls.py

@@ -13,7 +13,6 @@ from rapid import permissions
 
 
 def _can_manage_users(request):
-    import pdb; pdb.set_trace()
     if not request.user.is_authenticated():
         return []
     p = []
@@ -21,7 +20,7 @@ def _can_manage_users(request):
         p.extend(a.profile_set.all())
     return p
 
-_manage_users_permistion = permissions.Permission(
+_manage_users_permission = permissions.Permission(
     lambda r: False,
     _can_manage_users
 )
@@ -29,7 +28,7 @@ _manage_users_permistion = permissions.Permission(
 urlpatterns = register.crud(Application, write_set=permissions.to_superusers(), read_set=permissions.to_all()) +\
     register.crud(Profile, write_set=permissions.to_superusers(), read_set=permissions.to_staff()) +\
     register.instance_form(Profile, 'manage_users', u'Gerenciar Usuários',
-                            ManageUsers, _manage_users_permistion, icon="fa-users",
+                            ManageUsers, _manage_users_permission, icon="fa-users",
                             visibility=Action.Visibility.list) +\
     register.select(User, ['username'], permissions.to_staff(), 'usuario')