Esempio n. 1
0
def ajax_search(request):
    search_results = []
    query = request.GET.get('q', None)

    selected_facet = request.GET.get('type', None)
    try:
        limit = int(request.GET.get('limit', DEFAULT_LIMIT))
    except ValueError:
        limit = DEFAULT_LIMIT

    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    search_results = Autocomplete.search(query, restrict_type=selected_facet, limit=limit, offset=offset,
                                         restrict_location = request.GET.get('location', None))

    lat, lon = request.GET.get('lat', None), request.GET.get('lng', None)
    orgs_near_me = _get_orgs_near_me(request, query, lat, lon)
    more_results = (search_results.hits - int(offset)) > limit
    related_searches = _get_related_searches(query)

    if 'format' in request.GET and request.GET['format']=='html':
        ret = {'items': render_string(request, 'search/search_items.html', {'search_results' : search_results, 'query': query}),
               'facets': render_string(request, 'search/facets.html', {'search_results': search_results, 'selected_facet': selected_facet, 'query': query}),
               'related': render_string(request, 'search/related_searches.html', {'related_searches': related_searches}),
               'more_results': render_string(request, 'search/more_results.html', {'more_results': more_results})
               }

        if orgs_near_me:
            ret['nearMe'] = render_string(request, 'search/near_me.html', {'orgs_near_me': orgs_near_me})

        return json_response(ret)
Esempio n. 2
0
def ajax_search(request):
    search_results = []
    query = request.GET.get('q', None)

    selected_facet = request.GET.get('type', None)
    try:
        limit = int(request.GET.get('limit', DEFAULT_LIMIT))
    except ValueError:
        limit = DEFAULT_LIMIT

    try:
        offset = int(request.GET.get('offset', 0))
    except ValueError:
        offset = 0

    search_results = Autocomplete.search(query,
                                         restrict_type=selected_facet,
                                         limit=limit,
                                         offset=offset,
                                         restrict_location=request.GET.get(
                                             'location', None))

    lat, lon = request.GET.get('lat', None), request.GET.get('lng', None)
    orgs_near_me = _get_orgs_near_me(request, query, lat, lon)
    more_results = (search_results.hits - int(offset)) > limit
    related_searches = _get_related_searches(query)

    if 'format' in request.GET and request.GET['format'] == 'html':
        ret = {
            'items':
            render_string(request, 'search/search_items.html', {
                'search_results': search_results,
                'query': query
            }),
            'facets':
            render_string(
                request, 'search/facets.html', {
                    'search_results': search_results,
                    'selected_facet': selected_facet,
                    'query': query
                }),
            'related':
            render_string(request, 'search/related_searches.html',
                          {'related_searches': related_searches}),
            'more_results':
            render_string(request, 'search/more_results.html',
                          {'more_results': more_results})
        }

        if orgs_near_me:
            ret['nearMe'] = render_string(request, 'search/near_me.html',
                                          {'orgs_near_me': orgs_near_me})

        return json_response(ret)
Esempio n. 3
0
def error_500(request):
    if request.is_ajax():
        exception = getattr(request, 'exception', None)
        return json_error(500, exception)

    return HttpResponseServerError(
        render_string(request, 'errors/error.html', {}))
Esempio n. 4
0
def error_404(request):
    if request.is_ajax():
        exception = getattr(request, 'exception', None)
        return json_error(404, exception)

    return HttpResponseNotFound(render_string(request, 'errors/error.html',
                                              {}))
Esempio n. 5
0
def error_500(request):
    if request.is_ajax():
        exception = getattr(request, 'exception', None)
        return json_error(500, exception)

    return HttpResponseServerError(render_string(request, 'errors/error.html', {
            }))
Esempio n. 6
0
def error_404(request):
    if request.is_ajax():
        exception = getattr(request, 'exception', None)
        return json_error(404, exception)

    return HttpResponseNotFound(render_string(request, 'errors/error.html', {
            }))
Esempio n. 7
0
def action_list(request, entity_id, model_name):
    model = get_model(*model_name.split('.'))
    entity = model.objects.get(id=entity_id)
    actions = entity.actions.all()
    html = render_string(request, 'action/includes/action_list.html', {
        'entity': entity,
        'actions': actions,
    })

    return json_response({'html': html})
Esempio n. 8
0
def following_list(request, user_id):
    start = int(request.GET.get('start', 0))
    end = int(request.GET.get('end', 20))
    user = get_object_or_404(User, id=user_id)
    followings = user.get_active_followings()[start:end]

    html = render_string(request, "user/includes/user_list.html", {
        'users': followings,
        'start_index': start,
        'list_type': 'followings',
    })

    return json_response({
        'html': html,
        'has_more': end < user.get_num_users_following,
    })
Esempio n. 9
0
def following_list(request, user_id):
    start = int(request.GET.get('start', 0))
    end = int(request.GET.get('end', 20))
    user = get_object_or_404(User, id=user_id)
    followings = user.get_active_followings()[start:end]

    html = render_string(request, "user/includes/user_list.html", {
        'users': followings,
        'start_index': start,
        'list_type': 'followings',
    })

    return json_response({
        'html': html,
        'has_more': end < user.get_num_users_following,
    })
Esempio n. 10
0
def list(request, entity_id, model_name):
    start = int(request.GET.get('start', 0))
    end = int(request.GET.get('end', 20))
    model = get_model(*model_name.split('.'))
    entity = get_object_or_404(model, id=entity_id)
    commitments = entity.commitments.active()[start:end].select_related()

    html = render_string(request, "commitment/includes/committer_list.html", {
        'commitments': commitments,
        'start_index': start,
    })

    return json_response({
        'html': html,
        'has_more': end < entity.commitments.count(),
    })
Esempio n. 11
0
def followed_issue_list(request, user_id):
    start = int(request.GET.get('start', 0))
    end = int(request.GET.get('end', 20))
    user = get_object_or_404(User, id=user_id)
    issue_commitments = user.commitments.with_issues()[start:end].fetch_generic_relations()
    issues = [commitment.entity for commitment in issue_commitments]
    num_issues = user.commitments.with_issues().count()

    html = render_string(request, "issue/includes/followed_issue_list.html", {
        'issues': issues,
        'start_index': start,
    })

    return json_response({
        'html': html,
        'has_more': end < num_issues,
    })
Esempio n. 12
0
def followed_issue_list(request, user_id):
    start = int(request.GET.get('start', 0))
    end = int(request.GET.get('end', 20))
    user = get_object_or_404(User, id=user_id)
    issue_commitments = user.commitments.with_issues(
    )[start:end].fetch_generic_relations()
    issues = [commitment.entity for commitment in issue_commitments]
    num_issues = user.commitments.with_issues().count()

    html = render_string(request, "issue/includes/followed_issue_list.html", {
        'issues': issues,
        'start_index': start,
    })

    return json_response({
        'html': html,
        'has_more': end < num_issues,
    })
Esempio n. 13
0
def create(request):
    entity_id = request.POST['object_id']
    entity_type = request.POST['content_type']
    content_type = ContentType.objects.get(id=entity_type)
    entity = content_type.get_object_for_this_type(id=entity_id)
    commitment = Commitment(entity=entity, user=request.user)

    response = redirect(entity)
    try:
        commitment.full_clean()
        commitment.save()
        if request.is_ajax():
            button = render_inclusiontag(request, "commitment_button entity", "commitment_tags",
                                         {'entity': entity})
            actions = render_string(request, "action/includes/action_list.html", {
                'entity': entity,
                'actions': entity.actions.all(),
            })
            response = json_response({'button': button, 'actions': actions})
    except ValidationError:
        if request.is_ajax():
            response = json_error(400, "You have already committed to this issue/org.")
    return response