def search(request): """search method for url /search?q=&type=""" if request.method == 'GET': #get pageindex page_index = request.GET.get('page_index', '') if page_index: out = cache.get(str(ip)) return HttpResponse(out[str(page_index)]) else: q = request.GET.get('q', '') types = request.GET.get('types', '') else: q = request.POST.get('q', '') types = request.POST.get('type', '') if not types: types = '0' if isinstance(q, unicode): q = q.encode('utf-8') first_page, tot_search_outcome, category, total_page_number = search_engine( request, q, types) #save search record cdic = { 'outcome': first_page, 'key_word': str(q), 'is_auth': request.user.is_authenticated(), 'user': request.user, 'total_page': total_page_number, 'category': category, 'total': tot_search_outcome } return RTR('result.html', cdic, context_instance=RequestContext(request))
def register(request): if request.user.is_authenticated(): return HttpResponseRedirect('/') if request.method == 'GET': return RTR('register.html', {'error_msg': False}, context_instance=RequestContext(request)) #get validationg code validate_code = request.POST.get('validate', '') if not_legal(validate_code) or validate_code.lower( ) != request.session['validate'].lower(): return RTR('register.html', {'error_msg': True}, context_instance=RequestContext(request)) email = request.POST.get('email', '') password = request.POST.get('password', '') name = request.POST.get('name', '') remember = request.POST.get('remember', '') try: try: user = Account.objects.get(name=name) return HttpResponse("you ren le") except: pass #build shelf shelf = Shelf(fiction_number=0) shelf.save() user = Account(name=name, password=hashlib.md5(password).hexdigest(), is_active='0', email=email, shelf=shelf) user.save() user.nid = create_nid(user.id) user.save() user = authenticate(name=user.name, password=password) if user: login(request, user) response = HttpResponseRedirect('/' + user.nid) if remember == 'on': response.set_cookie('login', 'True', max_age=7 * 24 * 60 * 60) return response else: return HttpResponse('ok') except: HttpResponse("服务器故障,请稍候再试<a href = '%s'>返回<a/>" % request.META['HTTP_REFERER'])
def home(request): """ url method for index page """ if request.user.is_authenticated: print 'y' else: print 'm' cdic = { 'is_authenticated': request.user.is_authenticated, 'user': request.user, } return RTR('index.html', cdic)
def login_view(request): if request.user.is_authenticated(): return HttpResponseRedirect('/') if 'GET' == request.method: return RTR('login.html', {}, context_instance=RequestContext(request)) name = request.POST.get('name', '') password = request.POST.get('password', '') user = authenticate(name=str(name), password=str(password)) if user: login(request, user) return HttpResponseRedirect('/' + user.nid) else: try: user = Account.objects.get(name=name) except: return RTR('login.html', {'msg': '无此用户'}, context_instance=RequestContext(request)) return RTR('login.html', {'msg': '密码错误'}, context_instance=RequestContext(request))
def index(request): is_auth = request.user.is_authenticated() chapters = NewestChapter.objects.all()[:16] for item in chapters: item.url = item.charpter_url item.frm = WEB_SITE[item.source.title]['name'] item.time = str(item.record_time)[:19] item.show_color = WEB_SITE[item.source.title]['color'] user = request.user if is_auth else None fiction = Fiction.objects.all()[:30] fiction = list(fiction) fiction.sort(cmp=lambda x, y: -cmp(0.6 * x.rec_time + 0.4 * x.click_time, 0.6 * y.rec_time + 0.4 * y.click_time)) fiction_word = fiction[6:12] fiction = fiction[:9] style = { '1': '奇幻', '2': '武侠', '22': '仙侠', '4': '都市', '5': '历史', '6': '军事', '7': '游戏', '8': '竞技', '9': '科幻', '10': '灵异', '12': '同人', '14': '图文', '31': '文学', '41': '女生', '21': '玄幻', '15': '青春' } for item in chapters: item.style = style[item.fiction.fiction_style.encode('utf-8')] try: hot_words = SearchKeyWord.objects.all().order_by('-record_time')[:5] except: hot_words = None fictions = Fiction.objects.all().order_by('-click_time')[:4] print fictions cdic = { 'chapter': chapters, 'rec_fiction': fiction, 'rec_fiction_word': fiction_word, 'is_auth': is_auth, 'hot_words': fictions, 'user': user } return RTR('index.html', cdic, context_instance=RequestContext(request))
def login_view(request): """ login page """ if request.method == 'POST': email = request.POST['email'] password = request.POST['password'] print email, password user = authenticate(email=email, password=password) if user: #save the user information login(request, user) return HttpResponseRedirect('/') else: return HttpResponse('login failed') return RTR('login.html', '', context_instance=RequestContext(request))
def register_view(request): """ register page """ if request.method == 'GET': return RTR('register.html', '', context_instance=RequestContext(request)) else: email = request.POST['email'] password = request.POST['password'] user = Account(email=email, password=hashlib.md5(password).hexdigest(), is_active='N', first_name='kymo') user.save() if user: return HttpResponse('yes') else: return HttpResponse('no')
def feedback(request): if request.method == 'GET': user = request.user is_auth = request.user.is_authenticated() ret = {'user': user, 'is_auth': is_auth} return RTR('feedback.html', ret, context_instance=RequestContext(request)) if request.META.has_key('HTTP_X_FORWARDED_FOR'): ip = request.META['HTTP_X_FORWARDED_FOR'] else: ip = request.META['REMOTE_ADDR'] feedback = request.POST.get('feedback', '') contact = request.POST.get('contact', '') feed = Feedback(feedback=feedback, contact=contact, ip=ip) feed.save() return HttpResponse('/feedback/')
def index_iknow(request): ret = {'user' : request.user, 'is_auth' : request.user.is_authenticated()} return RTR('iknow.html', ret, context_instance = RequestContext(request))
def manage_ad(request): """ manage page """ #TODO judge whether the user is the super user return RTR('manage_ad.html', {})
def version(request): ret = {'user': request.user, 'is_auth': request.user.is_authenticated()} return RTR('version.html', ret)
def contact(request): ret = {'user': request.user, 'is_auth': request.user.is_authenticated()} return RTR('contact.html', ret)
def helps(request): ret = {'user': request.user, 'is_auth': request.user.is_authenticated()} return RTR('help.html', '')
def home(request, nid): if not request.user.is_authenticated(): return HttpResponse('/login/') else: if nid != request.user.nid: return HttpResponseRedirect('/' + request.user.nid) #get shelfs' books try: ships = ShelfFictionRelationship.objects.filter( shelf=request.user.shelf) except: ships = [] out = 0 fiction = [] style = { '1': '奇幻', '2': '武侠', '22': '仙侠', '4': '都市', '5': '历史', '6': '军事', '7': '游戏', '8': '竞技', '9': '科幻', '10': '灵异', '12': '同人', '14': '图文', '31': '文学', '41': '女生', '21': '玄幻', '15': '青春' } key = {} for item in ships: key[item.fiction.fiction_style] = 0 style = { '1': '奇幻', '2': '武侠', '22': '仙侠', '4': '都市', '5': '历史', '6': '军事', '7': '游戏', '8': '竞技', '9': '科幻', '10': '灵异', '12': '同人', '14': '图文', '31': '文学', '41': '女生', '21': '玄幻', '15': '青春' } shelf_out, all_fics = [], [] for tem in ships: all_fics.append(tem.fiction) for item in key.keys(): fics = [] for tem in ships: if tem.fiction.fiction_style == item: fics.append(tem.fiction) shelf_out.append({ 'key': style[item], 'number': len(fics), 'id': item }) out = [] for item in key.keys(): fictions = Fiction.objects.filter(fiction_style=item).order_by( '-click_time', '-rec_time')[:100] click_time = 0 for fics in fictions: breaks = False for temp in ships: if fics.fiction_nid == temp.fiction.fiction_nid: breaks = True break click_time += temp.fiction.click_time if not breaks: out.append({ 'key': style[item], 'fictions': fictions[:8], 'click_time': click_time }) out.sort(cmp=lambda x, y: -cmp(x['click_time'], y['click_time'])) is_auth = True cdic = { 'user': request.user, 'out': out[:5], 'is_auth': is_auth, 'shelf_fic': shelf_out, 'all': all_fics[:40] } return RTR('home.html', cdic, context_instance=RequestContext(request))
def error(request): print 'error' return RTR('error.html', {})
def member(request): if request.user.is_authenticated(): return HttpResponseRedirect('/' + request.user.nid) else: return RTR('register.html', {'tips': '注册启书,拥有电子书架,并能自动推荐你可能喜欢的小说.'}, context_instance=RequestContext(request))