Ejemplo n.º 1
0
def search(request):

    if request.method == 'GET' and 'q' in request.REQUEST:
        term_list = request.REQUEST['q'].lower().split(' ')
        q = " ".join(term_list)
    else:
        q = ""

    if q != "":

        account_list = bonds.master.accounts()
        group_list = bonds.master.groups()

        for term in term_list:
            if term != "":
                account_list = account_list.filter(tldap.Q(pk__contains=term) | tldap.Q(cn__contains=term) | tldap.Q(description__contains=term))
                group_list = group_list.filter(tldap.Q(pk__contains=term) | tldap.Q(description__contains=term))

    else:

        account_list = []
        group_list = []

    kwargs = {
        'q': q,
        'account_list': account_list,
        'group_list': group_list,
        'request': request,
    }

    return render_to_response('placard/search.html', kwargs, context_instance=RequestContext(request))
Ejemplo n.º 2
0
 def _filter_or_exclude(self, negate, *args, **kwargs):
     clone = self._clone()
     if negate:
         q = ~tldap.Q(*args, **kwargs)
     else:
         q = tldap.Q(*args, **kwargs)
     if clone._query is None:
         clone._query = q
     else:
         clone._query = clone._query & q
     return clone
Ejemplo n.º 3
0
    def _get_search_params(self):
        # set the database we should use as required
        alias = self._alias or tldap.DEFAULT_LDAP_ALIAS
        connection = tldap.connections[alias]

        # get object classes to search
        if self._from_cls is None:
            object_classes = (
                self._cls._meta.search_classes or
                self._cls._meta.object_classes)
        else:
            object_classes = self._from_cls._meta.search_classes

        if self._query is not None:
            # expand query
            requested_query = self._expand_query(self._query)

        # add object classes to search array
        query = tldap.Q()
        for oc in object_classes:
            query = query & tldap.Q(objectClass=oc)

        # do a SUBTREE search
        scope = ldap3.SEARCH_SCOPE_WHOLE_SUBTREE

        # add requested query
        if self._query is not None:
            if requested_query is not None:
                query = query & requested_query
            else:
                query = None

        # create a "list" of base_dn to search
        base_dn = self.get_base_dn()
        assert base_dn is not None

        # get list of field names we support
        field_names = self._cls._meta.get_all_field_names()

        # construct search filter string
        if query is not None:
            search_filter = self._get_filter(query)
        else:
            search_filter = None

        return alias, connection, base_dn, scope, search_filter, field_names
Ejemplo n.º 4
0
    def get_queryset(self):
        request = self.request

        account_list = self.get_default_queryset()

        if request.GET.has_key('group'):
            try:
                group = bonds.master.groups().get(cn=request.GET['group'])
                account_list = account_list.filter(tldap.Q(primary_group=group) | tldap.Q(secondary_groups=group))
            except bonds.master.GroupDoesNotExist:
                pass

        if request.GET.has_key('exclude'):
            try:
                group = bonds.master.groups().get(cn=request.GET['exclude'])
                account_list = account_list.filter(~(tldap.Q(primary_group=group) | tldap.Q(secondary_groups=group)))
            except bonds.master.GroupDoesNotExist:
                pass

        return account_list
Ejemplo n.º 5
0
    def _clone_query(self, q):
        dst = tldap.Q()
        dst.connector = q.connector
        dst.negated = q.negated
        """
        Expands exandable q items, i.e. for relations between objects.
        """
        # scan through every child
        for child in q.children:

            # if this child is a node, then descend into it
            if isinstance(child, django.utils.tree.Node):
                dst.children.append(self._clone_query(child))
            else:
                dst.children.append(child)

        return dst
Ejemplo n.º 6
0
 def get_query(self, q, request):
     return bonds.master.groups().filter(tldap.Q(pk__contains=q) | tldap.Q(description__contains=q))
Ejemplo n.º 7
0
 def get_query(self, q, request):
     return bonds.master.accounts().filter(tldap.Q(cn__contains=q) | tldap.Q(pk__contains=q))
Ejemplo n.º 8
0
    def _expand_query(self, q):
        dst = tldap.Q()
        dst.connector = q.connector
        dst.negated = q.negated
        """
        Expands exandable q items, i.e. for relations between objects.
        """
        # scan through every child
        for child in q.children:

            # if this child is a node, then descend into it
            if isinstance(child, django.utils.tree.Node):
                dst.children.append(self._expand_query(child))
                continue

            # otherwise get the values in this node
            name, value = child

            # split the name if possible
            name, _, operation = name.rpartition("__")
            if name == "":
                name, operation = operation, None

            # replace pk with the real attribute
            if name == "pk":
                name = self._cls._meta.pk

            # dn searches are a special case
            if name == "dn":
                dst.children.append(child)
                continue

            # try to find field associated with name
            try:
                self._cls._meta.get_field_by_name(name)
                dst.children.append(child)
                continue
            except KeyError:
                # no field found, try to lookup linked models
                pass

            # get raw value from class
            cls_value = self._cls.__dict__.get(name, None)

            # fail for cases we don't understand
            if cls_value is None:
                raise ValueError("Cannot do a search on %s "
                                 "as we do not know about it" % name)

            # fail for cases we don't understand
            if not isinstance(cls_value, tldap.manager.LinkDescriptor):
                raise ValueError("Cannot do a search on %s "
                                 "as we do not know the type" % name)

            # ask the LinkDescriptor for a q tree
            child = cls_value.get_q_for_linked_instance(value, operation)

            # if child is None, then no results can be found
            # we need to handle this later.
            dst.children.append(child)

        # go through results
        new_children = []
        for term in dst.children:
            # if result is not None, keep it
            if term is not None:
                new_children.append(term)

            # a result of None means 0 results
            elif q.negated:
                # not 0 results is all results
                return tldap.Q(objectClass='*')
            elif q.connector == tldap.Q.AND:
                # 0 results and anything is still 0 results
                return None
            elif q.connector == tldap.Q.OR:
                # 0 results or anything is just anything
                pass
        dst.children = new_children

        # output the results
        if len(dst.children) == 0:
            # no search terms, all terms were None
            return None
        else:
            # multiple terms
            return dst