def submit(request, form):
    """
    Ingest a piece of text into the database
    """

    args = {'user': request.user, 'form': form,
            'authenticated': request.user.is_authenticated()}

    (text, sentences) = parse_text_from_request(request)
    if None in (text, sentences):
        return _render(request, 'invalid.html', args)

    source_text = source_text_from_form(form)
    if source_text.is_dirty():
        source_text.save()

    volume = volume_from_form(source_text, form, text)
    if volume.is_dirty():
        volume.save()

    volume.sourcetextsentence_set.all().delete()
    with reversion.create_revision():
        reversion.set_user(request.user)
        for sent in sentences:
            (text, begin, end) = sent
            SourceTextSentence.objects.create(volume=volume, sentence=text, start_line=begin, end_line=end)

    return _render(request, 'submitted.html', args)
def show_search(request, language, level):
    """
    The core search entry point
    """

    args = {'language': language, 'user': request.user,
            'authenticated': request.user.is_authenticated(),
            'form': SimpleSearchForm()}

    if language in ('latin', 'greek', 'english'):
        if language == 'latin' and level == 'basic':
            return _render(request, 'basic_search.html', args)
        elif level == 'advanced':
            return _render(request, 'advanced_search.html', args)

    raise Http404()
def do_search(request, language, level):
    """
    A search form was submitted
    """

    if level == 'basic':
        form = SimpleSearchForm(request.GET)
        if form.is_valid():
            return _search_basic(request, form, language)
        else:
            args = {'language': language, 'user': request.user,
                    'authenticated': request.user.is_authenticated(),
                    'form': form }
            return _render(request, 'basic_search.html', args)

    raise Http404()
def _search_basic(request, form, language):
    """
    The user wants to do a basic search
    """

    from custom_solr import basic_search
    source = form.cleaned_data['source']
    target = form.cleaned_data['target']
    initial_offset = form.cleaned_data['start']
    rows_per_page = form.cleaned_data['rows']

    stop_words = form.cleaned_data['sw']
    if stop_words is not None:
        stop_words = stop_words.strip()
        if len(stop_words) == 0:
            stop_words = None

    results = basic_search(source, target, language, start=initial_offset,
                           rows=rows_per_page, stopword_list=stop_words)

    if results.has_key('error'):
        raise RuntimeError(results['error']['msg'])

    qtime = results['responseHeader']['QTime']
    matches = results['matches']
    matchTotal = results['matchTotal']
    matchStart = results['matchOffset'] + 1
    matchEnd = results['matchOffset'] + results['matchCount']
    stopList = results['stopList']
    stopListStr = ', '.join(sorted(stopList))

    myFirstIndex = results['matchOffset']
    myLastIndex = results['matchOffset'] + results['matchCount'] - 1

    def isThisPage(beginMatchIndex, endMatchIndex):
        if beginMatchIndex >= myFirstIndex and endMatchIndex <= myLastIndex:
            return True
        return False

    indicesRemaining = matchTotal
    currentStartIndex = None

    pageInfo = []
    pageCounter = 0
    first = True

    while indicesRemaining > 0:
        if currentStartIndex is None:
            currentStartIndex = 0
        else:
            currentStartIndex += rows_per_page
        if indicesRemaining >= rows_per_page:
            currentEndIndex = currentStartIndex + rows_per_page - 1
        else:
            currentEndIndex = currentStartIndex + indicesRemaining - 1
        collected = currentEndIndex - currentStartIndex + 1
        indicesRemaining -= collected
        currentPageCounter = pageCounter
        pageCounter += 1

        href = '/search/' + language + '/basic/search?start=' + \
               str(currentStartIndex) + '&rows=' + str(rows_per_page) + \
               '&source=' + str(source.id) + '&target=' + str(target.id)

        if stop_words is not None:
            href += '&sw=' + str(stop_words)

        page = { 'num': pageCounter, 'start': currentStartIndex,
                 'active': isThisPage(currentStartIndex, currentEndIndex),
                 'not_first': not first, 'href': href}

        pageInfo.append(page)
        first = False

    firstPage, lastPage, windowedPages = _window_pages(pageInfo, 9)

    args = { 'language': language, 'user': request.user,
             'authenticated': request.user.is_authenticated(),
             'source': source, 'target': target,
             'qtime': qtime, 'matches': matches,
             'matchTotal': matchTotal, 'matchStart': matchStart,
             'matchEnd': matchEnd, 'pageInfo': windowedPages,
             'firstPage': firstPage, 'lastPage': lastPage,
             'stopList': stopList, 'stopListStr': stopListStr }

    return _render(request, 'search_results.html', args)