def login(): form = LoginForm() if form.validate_on_submit(): if "login_user_id" in session: flash("账号已登录", category='err') return render_template("login.html", form=form) data = form.data user = User.query.filter_by(name=data['name']).first() if user: if not check_pwd(user.pwd, data['pwd'], user.salt): flash("密码错误", category='err') return redirect(UrlManager.build_url_path("user_page.login")) session['login_user'] = user.name session['login_user_id'] = user.id session['quit'] = False userlog = UserLog.query.filter_by(user_id=user.id).first() if userlog: userlog.ip = request.remote_addr userlog.login_time = datetime.now() else: userlog = UserLog(user_id=user.id, ip=request.remote_addr) db.session.add(userlog) db.session.commit() else: flash("用户名错误", category='err') return redirect(UrlManager.build_url_path("user_page.login")) if "keyword" in session: if session['keyword'] is not None and session['keyword'] != "": return redirect(UrlManager.build_url_path("index_page.search")) return redirect(UrlManager.build_url_path("index_page.index")) return render_template("login.html", form=form)
def edit(): if "login_user_id" not in session: flash("请先登录", category='err') return redirect(UrlManager.build_url_path("user_page.login")) login_user = User.query.get_or_404(int(session['login_user_id'])) form = UserEditForm(name=login_user.name, email=login_user.email, info=login_user.info) form.face.validators = [] form.face.render_kw = {"required": False} if form.validate_on_submit(): data = form.data if form.face.data: face_save_path = UrlManager.build_image_url("/") if not os.path.exists(face_save_path): os.makedirs(face_save_path) import stat os.chmod(face_save_path, stat.S_IRWXU) if form.face.data: if login_user.face and os.path.exists( os.path.join(face_save_path, login_user.face)): os.remove(os.path.join(face_save_path, login_user.face)) file_face = secure_filename(form.face.data.filename) from common.libs.utils import change_filename login_user.face = change_filename(file_face) form.face.data.save(face_save_path + login_user.face) if login_user.name != data['name'] and User.query.filter_by( name=data['name'].count) == 1: flash("账号已存在", category='err') return redirect(UrlManager.build_url_path("user_page.edit")) login_user.name = data['name'] if login_user.email != data['email'] and User.query.filter_by( email=data['email']).count() == 1: flash("邮箱已存在", category='err') return redirect(UrlManager.build_url_path("user_page.edit")) login_user.email = data['email'] login_user.info = data['info'] db.session.commit() flash("修改资料成功", category='ok') return redirect(UrlManager.build_url_path("user_page.info")) return render_template("edit.html", form=form, login_user=login_user)
def find(): data = {} if "keyword" in session: data['keyword'] = session['keyword'] if session['find']: # flash("查找数据完成,请点击上面搜索按钮继续搜索!", category='ok) return redirect(UrlManager.build_url_path("index_page.search")) return render_template("find.html", data=data)
def info(): if "login_user_id" not in session: flash("请先登录", category='err') return redirect(UrlManager.build_url_path("user_page.login")) login_user = User.query.get_or_404(int(session['login_user_id'])) form = UserInfoForm(name=login_user.name, email=login_user.email, info=login_user.info) return render_template("info.html", form=form, login_user=login_user)
def reset_pwd(): login_user = User.query.get_or_404(int(session['login_user_id'])) form = PwdForm() if form.validate_on_submit(): data = form.data if check_pwd(login_user.pwd, data['old_pwd'], login_user.salt): new_pwd = generate_password(data['new_pwd'], login_user.salt) if login_user.pwd == new_pwd: flash('新密码不能和原密码相同', category='err') return redirect(UrlManager.build_url_path('user_page.pwd')) login_user.pwd = new_pwd db.session.commit() flash('密码修改成功,请重新登录', category='ok') return redirect(UrlManager.build_url_path('user_page.login')) else: flash('原密码不正确', category='err') return redirect(UrlManager.build_url_path("user_page.pwd")) return render_template('pwd.html', form=form)
def index(): form = IndexForm() data = {"keyword": None, "url": ""} session['show'] = False if form.validate_on_submit(): session['keyword'] = form.data['keyword'] data['keyword'] = session['keyword'] if session['keyword'] is not None and session['keyword'] != "": if not check_login_status(session): return redirect(UrlManager.build_url_path("user_page.login")) session['show'] = True url = UrlManager.build_url_path( "index_page.search") + "?search_word=" + data['keyword'] data['url'] = APPLICATION['domain'] + url session['word'] = session['keyword'] session['keyword'] = None return redirect(data['url']) return render_template("index.html", form=form, data=data)
def register(): form = RegisterForm() if form.validate_on_submit(): data = form.data user = User( name=data['name'], pwd=generate_password(data['pwd'], generate_salt(len(data['name']))), email=data['email'], ) db.session.add(user) db.session.commit() flash("注册成功", category='ok') return redirect(UrlManager.build_url_path("user_page.login")) return render_template("register.html", form=form)
def check_keyword(keyword): if len(keyword) < 2: flash("输入的关键词太短", category='err') return redirect(UrlManager.build_url_path("index_page.find"))
def search(): page = request.args.get("p", "") # 页数 show_home = False # 显示首页状态 if page == "": page = 1 else: page = int(page) if page > 1: show_home = True page_size = PAGE_SIZE # 起始 session['find'] = False keyword = None if "keyword" in session and session[ 'keyword'] is not None and session['keyword'] != "": keyword = session['keyword'] else: keyword = request.values.get("search_word") if keyword is not None and keyword != "": keyword = keyword.strip() else: flash("请输入关键词搜索", category='err') return redirect(UrlManager.build_url_path("index_page.find")) session['keyword'] = keyword if not check_login_status(session): return redirect(UrlManager.build_url_path("user_page.login")) url = APPLICATION['domain'] + "/search?search_word=" + keyword data = { "keyword": keyword, "is_page": False, "show_home": show_home, "total": 0, "page": int(page), # "dic_list": "", "url": url, "is_login": False, "results": [] } client = pymongo.MongoClient(URI) db = client[SEARCH_DB] collection = db[SEARCH_COL] seg_list = [] seg = Segmentation() seg.set_sentence(keyword) seg.rmm_seg() segment = seg.get_result_dict() max_len = get_max_length(segment) for value in segment.values(): for val in value: if len(val) >= max_len: seg_list.append(val) app.logger.info("分词结果:{}".format(seg_list)) # for value in segment.values(): # for val in value: # if len(val) > 1: # seg_list.append(val) # app.logger.info("分词结果:{}".format(seg_list)) word = [] words = collection.find() for value in words: jump = False for s in seg_list: if s == value['keyword'] or value['keyword'] in s: if s not in word: word.append(s) elif s in value['keyword']: if value['keyword'] not in word: word.append(value['keyword']) jump = True break if jump: break # word = "" # words = collection.find_one({'keyword': {"$in": seg_list}}) # if words: # word += words # else: # string = "" # for li in seg_list: # string += li # if string: # word += string if not word: if data['keyword'] is not None and data['keyword'] != "": pool = Pool() count = 0 result_list = [] for base in DATABASE.values(): result = pool.apply_async( run_spider, (base, data['keyword'], IP_PROXY, MAX_PAGE)) result_list.append(result) pool.close() pool.join() for re in result_list: if re.get() is False: count += 1 app.logger.info("crawler has an error") if count >= len(DATABASE): session['find'] = False data['is_page'] = False flash("暂时没有找到数据", category='err') return redirect( UrlManager.build_url_path("index_page.find")) data['is_page'] = True app.logger.info("crawler was successful") session['find'] = True return redirect(UrlManager.build_url_path("index_page.find")) else: data = None else: res_list = [] number = 0 # 分页使用 page_size = int(page_size / len(word)) if page_size < 1: page_size = 1 for value in DATABASE.values(): db = client[value] for val in COLLECTION.values(): if val == value + "Item": collection = db[val] for key in word[:3]: res = collection.find({ "search_word": key }).sort([('weight', pymongo.DESCENDING), ('_id', pymongo.DESCENDING) ]).limit(page_size).skip(page_size * page) if res is not None or res != "": res_list.append(res) number += res.count() if len(res_list) != 0: data['is_page'] = True data['results'].append(res_list) else: flash("没有找到数据", category='err') data['is_page'] = False session['find'] = False return redirect( UrlManager.build_url_path("index_page.find")) else: continue total = int((math.ceil(number / page_size) / len(DATABASE)) / len(word)) + 1 # 总页数 if "login_user_id" in session: data['total'] = total data['is_login'] = True else: data['total'] = 1 # dic = get_page(total, page) # data['dic_list'] = dic client.close() session['keyword'] = "" return render_template("search.html", data=data)
def logout(): session.pop('login_user', None) session.pop('login_user_id', None) session['quit'] = True flash('退出登陆成功', category='ok') return redirect(UrlManager.build_url_path("user_page.login"))