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())
def _pinyin_search(request, search_string): # CLEAN UP THE INCOMING PINYIN clean_string = _normalize_pinyin(search_string) ascii_string = _pinyin_to_ascii(search_string) key = settings.PINYIN_WORD_KEY % ascii_string suggested = [] words = [] r_server = _get_redis() try: for o in json.loads(r_server.get(key)): word = ChineseWord(chars=o) for i in word.meanings: # IF THE CLEANED SEARCH STRING AND THE CONVERTED PINYIN MATCH if _normalize_pinyin(i['pinyin']) == clean_string: words.append(word) # IF THERE'S NO NUMBERS IN THE CLEANED_STRING, ADD IT elif not any(ext in clean_string for ext in ['1', '2', '3', '4', '5']): words.append(word) else: suggested.append(word) except TypeError: pass return _render(request, 'website/wordlist.html', locals())
def _english_search(request, search_string): r_server = _get_redis() words = [] try: key = settings.ENGLISH_WORD_KEY % (len(search_string.split(' ')), search_string.lower()) obj = json.loads(r_server.get(key)) for x in obj['characters']: words.append(ChineseWord(chars=x)) except TypeError: # split up the string and search each word individually pass suggested = [] for x in search_string.split(' '): try: key = settings.ENGLISH_WORD_KEY % (len(x.split(' ')), x.lower()) obj = json.loads(r_server.get(key)) for x in obj['characters']: suggested.append(ChineseWord(chars=x)) except TypeError: pass return _render(request, 'website/wordlist.html', locals())
def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME, authentication_form=CustomAuthenticationForm, current_app=None, extra_context=None): redirect_to = request.POST.get(redirect_field_name, request.GET.get(redirect_field_name, '')) if request.method == "POST": form = authentication_form(request, data=request.POST) if form.is_valid(): # Ensure the user-originating redirection url is safe. if not is_safe_url(url=redirect_to, host=request.get_host()): redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL) # Okay, security check complete. Log the user in. from django.contrib.auth import login login(request, form.get_user()) return HttpResponseRedirect(redirect_to) else: form = authentication_form(request) context = { 'form': form, redirect_field_name: redirect_to, } if extra_context is not None: context.update(extra_context) return _render(request, template_name, context)
def stats(request): if request.user.is_superuser: users = User.objects.all() else: users = User.objects.filter(email=request.user.email) return _render(request, 'website/stats.html', locals())
def review_new(request): words = request.user.get_review_items() items = [] for x in words: thing = {} thing['word'] = ChineseWord(chars=x['chars']) thing['personal'] = x items.append(thing) return _render(request, 'srs/review_new.html', locals())
def single_word(request, chars): word = ChineseWord(chars) # chars = _split_unicode_chrs(smart_unicode(word['chars'])) if request.user.is_authenticated(): word_viewed.send( sender=word_viewed, word=word, time=datetime.datetime.now(), user_id=request.user.email ) return _render(request, 'cedict/single.html', locals())
def url(request): # TODO if it's already been scanned and saved, don't bother parsing it again…. if request.GET.get('url'): url = request.GET.get('url') # PARSE THE WEBPAGE AND RETURN A LIST OF CHARACTERS html = urllib2.urlopen(url).read() text = readabilityParser(html) title = Document(html).title() new_text = strip_tags(text) # GIVE IT AN ID this_id = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(5)) key = "text:%s" % this_id if request.user.is_authenticated(): user = request.user.email else: user = '******' mapping = { 'user': user, 'title': title, 'chars': new_text, 'timestamp': time.time(), 'hash': this_id, 'url' : url, } # ADD IT TO REDIS r_server = _get_redis() r_server.hmset(key, mapping) if request.user.is_authenticated(): article_saved.send(sender=article_saved, article_id=this_id, time=time.time(), user_id=request.user.pk) return HttpResponseRedirect(reverse('text', args=[this_id])) else: problem = "TODO: Make a proper page here which explains the reader and how it works" return _render(request, 'website/problem.html', locals())
def view_wordlist(request): return _render(request, 'users/wordlist.html', locals())
def search_starts_with(request, word): words = ChineseWord()._starts_with(word) return _render(request, 'website/wordlist.html', locals())
def get_examples(request, word): words = ChineseWord().starting_with(word) print words return _render(request, 'website/examples.html', locals())
def test(request): # COLLECT THE WORDS words = request.user.get_test_items() # DECIDE HOW TO TEST THE ITEMS items = [] count = 1 for x in words: to_test = [] testing = ['pinyin', 'characters', 'meaning'] for t in testing: # work out what level we should test this item at level = int( x.get(('%s_test_level' % t), 0)) if level < settings.MAX_TEST_LEVEL and x.get(('%s_pass' % t), False): level += 1 # setup some values vals = {} vals['level'] = level vals['testing'] = t to_test.append(vals) # randomly decide which item to test this time. test = random.choice(to_test) word = ChineseWord(chars=x['chars']) # PREPARE THE CHOICES for m in word.meanings: test['choices'] = [] test['chars'] = word.chars # PINYIN CHOICES if test['testing'] == 'pinyin': test['main'] = word.chars test['other'] = m['meaning'] test['answer'] = m['pinyin'] test['choices'].append(m['pinyin_numbered']) # EASY if test['level'] == 0: new = ChineseWord()._get_random(number=1, chars=word.chars)['meanings'][0] test['choices'].append(new['pinyin_numbered']) # MEDIUM if test['level'] == 1: test['choices'].append( _alt_pinyin(m['pinyin_numbered']) ) # CHARACTER CHOICES if test['testing'] == 'characters': test['main'] = m['pinyin'] test['other'] = m['meaning'] test['answer'] = word.chars test['choices'].append(word.chars) # EASY if test['level'] == 0: new = ChineseWord()._get_random(number=1, chars=word.chars) test['choices'].append( new['chars'] ) # MEDIUM if test['level'] == 1: new = ChineseWord()._get_random(number=1, chars=word.chars) test['choices'].append( new['chars'] ) # MEANING CHOICES if test['testing'] == 'meaning': test['main'] = word.chars test['other'] = m['pinyin'] test['answer'] = m['meaning'] test['choices'].append(m['meaning']) # EASY if test['level'] == 0: new = ChineseWord()._get_random(number=1, chars=word.chars)['meanings'][0] test['choices'].append( new['meaning'] ) # MEDIUM if test['level'] == 1: new = ChineseWord()._get_random(number=1, chars=word.chars)['meanings'][0] test['choices'].append( new['meaning'] ) random.shuffle(test['choices']) html = render_to_string( 'srs/tests/base_test.html', {'test': test,}, context_instance=RequestContext(request) ) # if we get here, add it to the items list if html: count += 1 items.append(html) return _render(request, 'srs/test.html', locals())
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())
def page(request, slug): template = 'website/pages/%s.html' % slug return _render(request, template, locals())
def search_contains(request, word): words = ChineseWord()._contains(word) return _render(request, 'website/wordlist.html', locals())
def home(request): return _render(request, 'website/home.html', locals())
def user(request): return _render(request, 'users/user.html', locals())