Esempio n. 1
0
def user(request):
    """Return json representation of the currently logged in SpecifyUser."""
    from specifyweb.specify.api import obj_to_data, toJson
    data = obj_to_data(request.specify_user)
    data['isauthenticated'] = request.user.is_authenticated()
    data['available_collections'] = users_collections(connection.cursor(), request.specify_user.id)
    data['agent'] = obj_to_data(request.specify_user_agent)
    if settings.RO_MODE or not request.user.is_authenticated():
        data['usertype'] = "readonly"
    return HttpResponse(toJson(data), content_type='application/json')
Esempio n. 2
0
def user(request):
    """Return json representation of the currently logged in SpecifyUser."""
    from specifyweb.specify.api import obj_to_data, toJson
    data = obj_to_data(request.specify_user)
    data['isauthenticated'] = request.user.is_authenticated
    data['available_collections'] = users_collections(connection.cursor(), request.specify_user.id)
    data['agent'] = obj_to_data(request.specify_user_agent)
    if settings.RO_MODE or not request.user.is_authenticated:
        data['usertype'] = "readonly"
    return HttpResponse(toJson(data), content_type='application/json')
Esempio n. 3
0
def user(request):
    """Return json representation of the currently logged in SpecifyUser."""
    from specifyweb.specify.api import obj_to_data, toJson
    data = obj_to_data(request.specify_user)
    data['isauthenticated'] = request.user.is_authenticated()
    if settings.RO_MODE or not request.user.is_authenticated():
        data['usertype'] = "readonly"
    return HttpResponse(toJson(data), content_type='application/json')
Esempio n. 4
0
def querycbx_search(request, modelname):
    table = datamodel.get_table(modelname)
    model = getattr(models, table.name)

    fields = [
        table.get_field(fieldname, strict=True) for fieldname in request.GET
        if fieldname not in ('limit', 'offset', 'forcecollection')
    ]

    if 'forcecollection' in request.GET:
        collection = Collection.objects.get(pk=request.GET['forcecollection'])
    else:
        collection = request.specify_collection

    filters = []
    for field in fields:
        filters_for_field = []
        terms = parse_search_str(collection, request.GET[field.name.lower()])
        logger.debug("found terms: %s for %s", terms, field)
        for term in terms:
            filter_for_term = term.create_filter(table, field)
            if filter_for_term is not None:
                filters_for_field.append(filter_for_term)

        logger.debug("filtering %s with %s", field, filters_for_field)
        if len(filters_for_field) > 0:
            filters.append(reduce(or_, filters_for_field))

    if len(filters) > 0:
        with models.session_context() as session:
            combined = reduce(and_, filters)
            query = session.query(getattr(model,
                                          table.idFieldName)).filter(combined)
            query = filter_by_collection(model, query, collection).limit(10)
            ids = [id for (id, ) in query]
    else:
        ids = []

    from specifyweb.specify.api import get_model_or_404, obj_to_data
    specify_model = get_model_or_404(modelname)
    qs = specify_model.objects.filter(id__in=ids)

    results = [obj_to_data(obj) for obj in qs]
    return HttpResponse(toJson(results), content_type='application/json')
Esempio n. 5
0
def querycbx_search(request, modelname):
    table = datamodel.get_table(modelname)
    model = getattr(models, table.name)

    fields = [table.get_field(fieldname, strict=True)
              for fieldname in request.GET
              if fieldname not in ('limit', 'offset', 'forcecollection')]

    if 'forcecollection' in request.GET:
        collection = Collection.objects.get(pk=request.GET['forcecollection'])
    else:
        collection = request.specify_collection

    filters = []
    for field in fields:
        filters_for_field = []
        terms = parse_search_str(collection, request.GET[field.name.lower()])
        logger.debug("found terms: %s for %s", terms, field)
        for term in terms:
            filter_for_term = term.create_filter(table, field)
            if filter_for_term is not None:
                filters_for_field.append(filter_for_term)

        logger.debug("filtering %s with %s", field, filters_for_field)
        if len(filters_for_field) > 0:
            filters.append(reduce(or_, filters_for_field))

    if len(filters) > 0:
        with models.session_context() as session:
            combined = reduce(and_, filters)
            query = session.query(getattr(model, table.idFieldName)).filter(combined)
            query = filter_by_collection(model, query, collection).limit(100)
            ids = [id for (id,) in query]
    else:
        ids = []

    from specifyweb.specify.api import get_model_or_404, obj_to_data
    specify_model = get_model_or_404(modelname)
    qs = specify_model.objects.filter(id__in=ids)

    results = [obj_to_data(obj) for obj in qs]
    return HttpResponse(toJson(results), content_type='application/json')