mango r237 - in branches/django: . mango



Author: ovitters
Date: Mon Jun 23 16:26:36 2008
New Revision: 237
URL: http://svn.gnome.org/viewvc/mango?rev=237&view=rev

Log:
	* mango/models.py (LdapObject, LdapObject.search, LdapObject.method,
	  LdapObject._build_filter, Users, Modules, L10nModules, DevModules):
	  Add a _build_filter function to LdapObject. This converts a query
	  filter made up by 'Q' to an ldap filter. It also handles quoting,
	  AND, OR and NOT. Change the LdapObject search function to make use
	  of _build_filter.
	  Further, allow an LdapObject derived class to have a predefined
	  filter. This filter will be AND'ed together with any filters
	  provides to the LdapObject search function.
	  Set default FILTER for Users class. Further, create two new classes
	  derived from Module class; L10nModules and DevModules. Each of these
	  classes has a filter which only returns a certain module.
	* mango/views.py (add_account): Instead of providing a filter to the
	  Modules class, use the newly created L10nModules and DevModules
	  classes.


Modified:
   branches/django/   (props changed)
   branches/django/ChangeLog
   branches/django/mango/models.py
   branches/django/mango/views.py

Modified: branches/django/mango/models.py
==============================================================================
--- branches/django/mango/models.py	(original)
+++ branches/django/mango/models.py	Mon Jun 23 16:26:36 2008
@@ -11,7 +11,10 @@
 from django.conf import settings
 from django.core import validators
 from django.newforms import ModelForm
+from django.utils import tree
+from django.db.models import Q
 import ldap
+import ldap.filter
 
 class AccountRequest(models.Model):
     id = models.AutoField(primary_key=True)
@@ -136,6 +139,7 @@
     
     BASEDN = None
     MULTI_ATTRS = set(('objectClass'))
+    FILTER = None
 
     def __init__(self, dn, attrs):
         for k, i in attrs.items():
@@ -146,12 +150,24 @@
         self.dn = dn
 
     @classmethod
-    def search(cls, filter):
+    def search(cls, filter=None):
         l = LdapUtil.singleton().handle
-        
+
         base = cls.BASEDN
 
-        results = l.search_s(base, ldap.SCOPE_SUBTREE, filter, None)
+        q_object = None
+        for f in (cls.FILTER, filter):
+            if isinstance(f, tree.Node):
+                if q_object:
+                    q_object &= f
+                else:
+                    q_object = f
+        if q_object:
+            ldapfilter = cls._build_filter(q_object)
+        else:
+            ldapfilter = '(objectClass=*)'
+        print "ldapfilter: %s" % ldapfilter
+        results = l.search_s(base, ldap.SCOPE_SUBTREE, ldapfilter, None)
 
         items = []
 
@@ -160,6 +176,30 @@
 
         return items
 
+    @classmethod
+    def _build_filter(cls, q_object):
+        """Builds a LDAP filter using a Q object"""
+        vals = []
+        for child in q_object.children:
+            if isinstance(child, tree.Node):
+                val = cls._build_filter(child)
+            else:
+                val = ldap.filter.filter_format('(%s=%s)', (child[0], child[1]))
+            vals.append(val)
+
+        format = ''
+        if len(vals) == 1:
+            format = '%s'
+        elif q_object.connector == q_object.OR:
+            format = '(|%s)'
+        else:
+            format = '(&%s)'
+
+        if q_object.negated:
+            format = '(!%s)' % format
+
+        return format % ''.join(vals)
+
 class UserGroups(LdapObject):
 
     BASEDN = settings.MANGO_CFG['ldap_groups_basedn']
@@ -170,6 +210,7 @@
 
     BASEDN = settings.MANGO_CFG['ldap_users_basedn']
     MULTI_ATTRS = set(('authorizedKey','objectClass'))
+    FILTER = Q(objectClass='posixAccount')
 
     def __init__(self, *foo):
         self._groups = None
@@ -198,7 +239,22 @@
             node = ET.SubElement(formnode, 'group', {'cn': group.cn})
 
 class Modules(LdapObject):
-
+    """Base class for Module information (maintainer into, etc)"""
     BASEDN = settings.MANGO_CFG['ldap_modules_basedn']
     MULTI_ATTRS = set(('memberUid', 'objectClass'))
 
+class L10nModules(Modules):
+    """Specific filter to only return localization modules
+
+    Note: within LDAP, it is very easy to morph an l10n module into
+    a development one. This class should do as minimal as possible."""
+    FILTER = Q(objectClass='localizationModule')
+
+class DevModules(Modules):
+    """Specific filter to only return development modules
+
+    Note: within LDAP, it is very easy to morph an l10n module into
+    a development one. This class should do as minimal as possible."""
+    FILTER = (~ Q(objectClass='localizationModule')) & Q(objectClass='gnomeModule')
+
+

Modified: branches/django/mango/views.py
==============================================================================
--- branches/django/mango/views.py	(original)
+++ branches/django/mango/views.py	Mon Jun 23 16:26:36 2008
@@ -170,17 +170,19 @@
     doc, root = get_xmldoc('Login Page', request)
     form = ET.SubElement(root, 'newaccount')
 
-    filter = '(&(!(objectClass=localizationModule))(objectClass=gnomeModule))'
-    dev_modules = models.Modules.search(filter)
-
-    filter = '(objectClass=localizationModule)'
-    trans_modules = models.Modules.search(filter)
+    dev_modules = models.DevModules.search()
+    trans_modules = models.L10nModules.search()
 
     for module in dev_modules:
         ET.SubElement(form, 'gnomemodule', {'cn': module.cn})
     for module in trans_modules:
         ET.SubElement(form, 'translation', {'cn': module.cn, 'desc': module.description})
 
+    if request.method == 'POST':
+        f = models.AccountsForm(request.POST)
+        if add_form_errors_to_xml(el, f):
+#            mirror = f.save()
+            return HttpResponseRedirect(u'../view/%s' % unicode(mirror.id))
 
     return get_xmlresponse(doc, "new_account.xsl")
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]