Beispiel #1
0
 def decorated(request, *args, **kwargs):
     token = optional_parameter(request, 'token', None)
     if token is None:
         if 'user' in request:
             request['user_from_token'] = request['user']
             request['args']['token'] = request['user'].token
             return wrapped(request, *args, **kwargs)
         else:
             required_parameter(request, 'token')
     user = User.get('token', token)
     if user is not None:
         request['user_from_token'] = user
         return wrapped(request, *args, **kwargs)
     else:
         raise UserException(INVALID_TOKEN % token)
Beispiel #2
0
def _get_inbox(request, number_of_items=30, offset=0):
    search_term = optional_parameter(request, 'searchTerm', None)
    if search_term is not None:
        search_term = search_term.lower()
        items = [
            item for item in mock_data.inbox
            if search_term in item['question'].lower()
        ]
    else:
        items = mock_data.inbox
    items = true_false_both_filter(request, items, 'read')
    items = true_false_both_filter(request, items, 'answered')
    total_items = len(items)
    items = items[offset:offset + number_of_items]
    return {'totalItems': total_items, 'items': items}
Beispiel #3
0
def _get_documents(request, number_of_items=30, offset=0, document_ids=None):
    if document_ids:
        items = [
            item for item in mock_data.documents if item['id'] in document_ids
        ]
    else:
        items = mock_data.documents

    search_term = optional_parameter(request, 'searchTerm', None)
    if search_term is not None:
        search_term = search_term.lower()
        items = [
            item for item in items if (search_term in item['title'].lower()
                                       or search_term in item['text'].lower())
        ]

    total_items = len(items)
    items = items[offset:offset + number_of_items]
    return {'totalItems': total_items, 'items': items}
Beispiel #4
0
def _upload_document(request):
    user_token = request['user'].token
    title = required_parameter(request, 'title')
    if 'text' in request['args']:
        document_content = request['args']['text']
        document_type = 'text'
    elif 'file' in request.files:
        document_file = request.files.get('file')
        document_content = document_file.body.decode()
        document_type = 'file'
    else:
        raise UserException(ERROR_REQUIRED_PARAMETER % "text' or 'file")

    if 'documentid' in request['args'] and request['args']['documentid'] != '':
        document_id = request['args']['documentid']
    else:
        document_id = sha256(document_content.encode('utf-8')).hexdigest()

    if 'origin' in request['args'] and request['args']['origin'] != '':
        origin = request['args']['origin']
    else:
        origin = ''

    document_type = optional_parameter(request, 'type', document_type)
    replace = 'replace' in request['args'] and request['args']['replace'].lower() == 'true'

    DocumentStore.create_document(user_id=user_token,
                                  document_id=document_id,
                                  title=title,
                                  text=document_content,
                                  origin=origin,
                                  document_type=document_type,
                                  replace=replace,
                                  get_embedding=Responder.get_document_embeddings)

    return {'documentId': document_id}
Beispiel #5
0
def test_missing_optional_parameter():
    request = {'args': {'test': 'testing'}}
    test = optional_parameter(request, 'missing', 'hello')
    assert test == 'hello'
def _get_annotations(request, number_of_items=30, offset=0, annotation_ids=None, document_ids=None, pages=None):
    search_term = optional_parameter(request, 'searchTerm', None)
    annotations = AnnotationStore.get_annotations(request['user'].token, search_term, annotation_ids, document_ids,
                                                  pages, saved_replies=False)
    return {'totalItems': len(annotations), 'items': annotations[offset:offset + number_of_items]}
Beispiel #7
0
def _get_documents(request, number_of_items=30, offset=0, document_ids=None):
    user_token = request['user'].token
    search_term = optional_parameter(request, 'searchTerm', None)
    documents = DocumentStore.get_documents(user_token, document_ids=document_ids, search_term=search_term)
    return {'totalItems': len(documents), 'items': documents[offset:offset + number_of_items]}