Exemple #1
0
def search(request, template_name="directories/search.html"):
    filters = get_query_filters(request.user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()
    cat = None

    if not request.user.is_anonymous:
        directories = directories.select_related()

    query = request.GET.get('q', None)

    form = DirectorySearchForm(request.GET,
                               is_superuser=request.user.is_superuser)

    if form.is_valid():
        search_category = form.cleaned_data['search_category']
        query = form.cleaned_data.get('q')
        search_method = form.cleaned_data['search_method']
        cat = form.cleaned_data.get('cat')
        sub_cat = form.cleaned_data.get('sub_cat')
        region = form.cleaned_data.get('region')
        cat = [int(c) for c in cat if c.isdigit()]
        if sub_cat:
            sub_cat = [int(c) for c in sub_cat if c.isdigit()]

        if cat:
            directories = directories.filter(cats__in=list(cat))
        if sub_cat:
            directories = directories.filter(sub_cats__in=list(sub_cat))

        if region:
            directories = directories.filter(region=region)

        if query and 'tag:' in query:
            tag = query.strip('tag:')
            directories = directories.filter(tags__icontains=tag)
        elif query and search_category:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (search_category, search_type): query}
            directories = directories.filter(**search_filter)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    if not get_setting('module', 'directories', 'showresultswithoutsearch'):
        if "?" not in request.get_full_path():
            directories = directories.none()

    return render_to_resp(request=request,
                          template_name=template_name,
                          context={
                              'directories': directories,
                              'form': form,
                              'a_to_z': string.ascii_lowercase
                          })
Exemple #2
0
def search(request, template_name="directories/search.html"):
    filters = get_query_filters(request.user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()
    cat = None
    category = None
    sub_category = None

    if not request.user.is_anonymous():
        directories = directories.select_related()

    query = request.GET.get('q', None)
    # Handle legacy tag links
    if query and "tag:" in query:
        return HttpResponseRedirect(
            "%s?q=%s&search_category=tags__icontains" %
            (reverse('directories'), query.replace('tag:', '')))

    form = DirectorySearchForm(request.GET,
                               is_superuser=request.user.is_superuser)

    if form.is_valid():
        cat = form.cleaned_data['search_category']
        category = form.cleaned_data['category']
        sub_category = form.cleaned_data['sub_category']
        search_method = form.cleaned_data['search_method']

        if query and cat:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (cat, search_type): query}
            directories = directories.filter(**search_filter)

    if category:
        directories = directories.filter(categories__category__id=category)
    if sub_category:
        directories = directories.filter(categories__parent__id=sub_category)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    try:
        category = int(category)
    except:
        category = 0
    categories, sub_categories = Directory.objects.get_categories(
        category=category)

    return render_to_response(template_name, {
        'directories': directories,
        'categories': categories,
        'form': form,
        'sub_categories': sub_categories,
        'a_to_z': string.lowercase[:26]
    },
                              context_instance=RequestContext(request))
Exemple #3
0
def search(request, template_name="directories/search.html"):
    filters = get_query_filters(request.user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()
    cat = None
    category = None
    sub_category = None

    if not request.user.is_anonymous():
        directories = directories.select_related()

    query = request.GET.get('q', None)
    # Handle legacy tag links
    if query and "tag:" in query:
        return HttpResponseRedirect("%s?q=%s&search_category=tags__icontains" %(reverse('directories'), query.replace('tag:', '')))

    form = DirectorySearchForm(request.GET, is_superuser=request.user.is_superuser)

    if form.is_valid():
        cat = form.cleaned_data['search_category']
        category = form.cleaned_data['category']
        sub_category = form.cleaned_data['sub_category']
        search_method = form.cleaned_data['search_method']

        if query and cat:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (cat, search_type): query}
            directories = directories.filter( **search_filter)

    if category:
        directories = directories.filter(categories__category__id=category)
    if sub_category:
        directories = directories.filter(categories__parent__id=sub_category)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    try:
        category = int(category)
    except:
        category = 0
    categories, sub_categories = Directory.objects.get_categories(category=category)

    return render_to_response(template_name,
        {'directories': directories,
        'categories': categories,
        'form' : form,
        'sub_categories': sub_categories,
        'a_to_z': string.lowercase[:26]},
        context_instance=RequestContext(request))
Exemple #4
0
def search(request, template_name="directories/search.html"):
    filters = get_query_filters(request.user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()
    cat = None

    if not request.user.is_anonymous():
        directories = directories.select_related()

    query = request.GET.get('q', None)

    form = DirectorySearchForm(request.GET,
                               is_superuser=request.user.is_superuser)

    if form.is_valid():
        search_category = form.cleaned_data['search_category']
        query = form.cleaned_data.get('q')
        search_method = form.cleaned_data['search_method']
        cat = form.cleaned_data.get('cat')
        sub_cat = form.cleaned_data.get('sub_cat')

        if cat:
            directories = directories.filter(cat=cat)
        if sub_cat:
            directories = directories.filter(sub_cat=sub_cat)

        if query and 'tag:' in query:
            tag = query.strip('tag:')
            directories = directories.filter(tags__icontains=tag)
        elif query and search_category:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (search_category, search_type): query}
            directories = directories.filter(**search_filter)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    return render_to_response(template_name, {
        'directories': directories,
        'form': form,
        'a_to_z': string.lowercase[:26]
    },
                              context_instance=RequestContext(request))
Exemple #5
0
def search(request, template_name="directories/search.html"):
    filters = get_query_filters(request.user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()
    cat = None

    if not request.user.is_anonymous():
        directories = directories.select_related()

    query = request.GET.get('q', None)

    form = DirectorySearchForm(request.GET, is_superuser=request.user.is_superuser)

    if form.is_valid():
        search_category = form.cleaned_data['search_category']
        query = form.cleaned_data.get('q')
        search_method = form.cleaned_data['search_method']
        cat = form.cleaned_data.get('cat')
        sub_cat = form.cleaned_data.get('sub_cat')

        if cat:
            directories = directories.filter(cat=cat)
        if sub_cat:
            directories = directories.filter(sub_cat=sub_cat)

        if query and 'tag:' in query:
            tag = query.strip('tag:')
            directories = directories.filter(tags__icontains=tag)
        elif query and search_category:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (search_category, search_type): query}
            directories = directories.filter( **search_filter)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    return render_to_response(template_name,
        {'directories': directories,
        'form' : form,
        'a_to_z': string.lowercase[:26]},
        context_instance=RequestContext(request))
Exemple #6
0
def search(request, template_name="directories/search.html"):
    user = request.user
    username = request.user.username
    filters = get_query_filters(user, 'directories.view_directory')
    directories = Directory.objects.filter(filters).distinct()

    # Checking for view and change directory permission (staff)
    can_view_pend = can_view_pending_directories(user)

    # Finding pending directories from the directories query we ran above
    pending_dirs = directories.filter(status_detail='pending').distinct()

    # Bypassing this check if the user already has permissions
    # else, we're looking to see if the user is the creator or has
    # membership with the directory
    if not can_view_pend:
        for dir in pending_dirs:
            dir_creator = dir.creator_username
            creator = dir_creator == username

            # Checking to see if the user has a membership with the directory
            id = dir.id
            directory = get_object_or_404(Directory, pk=id)
            member = directory.has_membership_with(user)

            if (not(creator or member)):
                directories = directories.exclude(slug=dir.slug)

    cat = None

    if not request.user.is_anonymous:
        directories = directories.select_related()

    query = request.GET.get('q', None)

    form = DirectorySearchForm(request.GET, is_superuser=request.user.is_superuser)

    if form.is_valid():
        search_category = form.cleaned_data['search_category']
        query = form.cleaned_data.get('q')
        search_method = form.cleaned_data['search_method']
        cat = form.cleaned_data.get('cat')
        sub_cat = form.cleaned_data.get('sub_cat')
        region = form.cleaned_data.get('region')
        cat = [int(c) for c in cat if c.isdigit()]
        if sub_cat:
            sub_cat = [int(c) for c in sub_cat if c.isdigit()]

        if cat:
            directories = directories.filter(cats__in=list(cat))
        if sub_cat:
            directories = directories.filter(sub_cats__in=list(sub_cat))

        if region:
            directories = directories.filter(region=region)

        if query and 'tag:' in query:
            tag = query.strip('tag:')
            directories = directories.filter(tags__icontains=tag)
        elif query and search_category:
            search_type = '__iexact'
            if search_method == 'starts_with':
                search_type = '__istartswith'
            elif search_method == 'contains':
                search_type = '__icontains'

            search_filter = {'%s%s' % (search_category, search_type): query}
            directories = directories.filter( **search_filter)

    directories = directories.order_by('headline')

    EventLog.objects.log()

    if "?" not in request.get_full_path():
        directories = directories.none()

    return render_to_resp(request=request, template_name=template_name,
        context={'directories': directories,
        'form' : form,
        'a_to_z': string.ascii_lowercase})