Exemplo n.º 1
0
def text(request, hashkey=None, words=None):
    
    if not hashkey:
        hashkey = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(5))
        key = "text:%s" % hashkey
    
        if request.user.is_authenticated():
            user = request.user.email
        else:
            user = '******'
            
        mapping = {
            'user': user,
            'title': '', 
            'chars': words,
            'timestamp': time.time(),
            'hash': hashkey,
            'url' : '',
        }
        
        # ADD IT TO REDIS
        r_server = _get_redis()
        r_server.hmset(key, mapping)
                        
    else:
        key = 'text:%s' % hashkey
    
    
    obj = None
    if _search_redis(key, lookup=False):
        obj = _search_redis(key)
    
    if not obj:
        if request.is_ajax():
            html = render_to_string('website/problem_snippet.html', locals())
            return HttpResponse(html)
        
        return _render(request, 'website/problem.html', locals())
    
    title = 'Article'
    try:
        url = urlparse(obj['url']).netloc
    except KeyError:
        pass
        
    chars = obj['chars'].decode('utf-8') # because redis stores things as strings...
    things = _split_unicode_chrs(chars)
    obj_list = _group_words(things) 
    
    
    list_template = 'creader/text_page_snippet.html' 
    
    if request.GET.get('page'):
        template = 'creader/text_page_snippet.html'
        return render_to_response(template, locals())
        
    return _render(request, 'creader/text.html', locals())
Exemplo n.º 2
0
def search(request, search_string=None, title='Search', words=None):
    
    r_server = _get_redis()
        
    # replace search string underscores with spaces
    if search_string:
        search_string = search_string.strip().replace('_', ' ')        
               

    # HANDLES EMPTY OR NULL SEARCH STRING
    if search_string == None and request.method != 'POST':
        form = SearchForm()
        return _render(request, 'website/search.html', locals())
          
          
    # CHECK IF IT'S A POST REQUEST OR URL SEARCH
    if search_string == None and request.method == 'POST':
        form = SearchForm(request.POST)
        if form.is_valid():
            search_string = form.cleaned_data['char']

        else:
            # POST AND NO SEARCH STRING - SHOW THEM THE PLAIN JANE SEARCH PAGE
            form = SearchForm()
            return _render(request, 'website/search.html', locals())


    # HANDLES AN AMBIGUOUS SEARCH
    if _is_ambiguous(search_string):
        message = messages.AMBIGUOUS_WORD
        return render(request, 'problem.html', locals())


    if r_server.exists((settings.PINYIN_WORD_KEY % _pinyin_to_ascii(search_string))):  
        return _pinyin_search(request, search_string)


    if _is_english(search_string):
        return _english_search(request, search_string)


    # IF THE SEARCH IS OVER 10 CHARACTERS, RETURN A TEXT
    #if len(search_string) > 12:
    #    from creader.views import text                
    #    return text(request, words=search_string)
    
    
    if not words:
        things = _split_unicode_chrs(search_string)        
        words = _group_words(things)   

        
    # IF THE USER WAS LOGGED IN, RECORD IT IN THEIR 'SAVED WORDS'
    if request.user.is_authenticated():
        for x in words:
            word_searched.send(
                sender=word_searched, 
                word=x.chars, 
                time=datetime.datetime.now(), 
                user_id=request.user.email
            )
    
    
    # if there's only 1 word, take us straight to the single word definition
    if len(words) == 1:
        word = words[0]
        url = reverse('single_word', args=[word])
        return HttpResponseRedirect(url)
    
    return _render(request, 'website/wordlist.html', locals())