def user(request): if request.method == "GET": return HttpResponse("nothing") if request.method == "POST": login_request = json.loads(request.body) try: login_user = User.objects.get(login_id=login_request["id"]) if login_user.password == login_request["password"]: jsonData = OrderedDict() jsonData["u_id"] = login_user.u_id return HttpResponse(json.dumps(jsonData, ensure_ascii=False, indent="\t"), content_type="application/json") else: return HttpResponse("user.post.fail:wrongPassword") except User.DoesNotExist: return HttpResponse("user.post.fail:notExists") if request.method == "PUT": signUp_request = json.loads(request.body) if User.objects.filter(login_id=signUp_request["id"]).exists(): return HttpResponse("user.put.fail:idExists") new_user = User(name=signUp_request["name"], birth_date=signUp_request["birthDate"], login_id=signUp_request["id"], password=signUp_request["password"]) new_user.save() return HttpResponse("user.put.success") if request.method == "DELETE": return HttpResponse("nothing")
def login(): content = copy(context_base) if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): user = User().query_by_username(form.username.data) if user is None or not user.check_password(form.password.data): flash('Invalid username or password') return redirect(url_for('login')) else: login_user(user, remember=form.remember_me.data) session['store_id'] = '1' next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': if current_user.type == "customer": next_page = url_for('shopping') else: next_page = '/manage/movies' return redirect(next_page) content['title'] = 'Sign In' content['form'] = form return render_template('login.html', **content)
def admin(username, password): '''创建管理员账户''' db.create_all() user = User.query.first() if user is not None: click.echo('Updating user...') user.username = username user.set_password(password) # 设置密码 else: click.echo('Creating user...') user = User(username=username, name='Admin') user.set_password(password) db.session.add(user) db.session.commit() click.echo('Admin Create over!!!')
def setUp(self): # 配置更新 app.config.update(TESTING=True, SQLALCHEMY_DATABASE_URI='sqlite:///:memory:') # 创建数据库和表 db.create_all() # 创建测试数据 user = User(name="Test", username='******') user.set_password('123') movie = Movie(title='Test Movie', year='2020') # 使用add_all放管服测试添加多个实例 db.session.add_all([user, movie]) db.session.commit() self.client = app.test_client() # 创建客户端测试 self.runner = app.test_cli_runner() # 创建测试命令运行器
def register(request): if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): data = form.cleaned_data name = data['name'] email = data['email'] passwd = data['passwd'] person = User(name=name, email=email, passwd=passwd) person.save() return HttpResponseRedirect('/movie/') else: form = RegisterForm() return render_to_response('register.html', {'form': form}, context_instance=RequestContext(request))
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password).save() flash(f'Account created for {form.username.data}!You will be able to login', 'success') return redirect(url_for('login')) return render_template('register.html', title="Register", form=form)
def forge(): '''Generate fake data.''' db.create_all() name = 'Han Star' movies = [ { 'title': 'My Neighbor Totoro', 'year': '1988' }, { 'title': 'Dead Poets Society', 'year': '1989' }, { 'title': 'A Perfect World', 'year': '1993' }, { 'title': 'Leon', 'year': '1994' }, { 'title': 'Mahjong', 'year': '1996' }, { 'title': 'Swallowtail Butterfly', 'year': '1996' }, { 'title': 'King of Comedy', 'year': '1999' }, { 'title': 'Devils on the Doorstep', 'year': '1999' }, { 'title': 'WALL-E', 'year': '2008' }, { 'title': 'The Pork of Music', 'year': '2012' }, ] user = User(name=name) db.session.add(user) for m in movies: movie = Movie(title=m['title'], year=m['year']) db.session.add(movie) db.session.commit() click.echo("Done....")
def login(): if current_user.is_authenticated: return redirect(url_for('home')) form = LoginForm() if form.validate_on_submit(): user = User.objects(email = form.email.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember = form.remember.data) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect(url_for('home')) else: flash('Login Unsuccessful', 'danger') return render_template('login.html', title="Login", form=form)
def register(): content = copy(context_base) if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): User().new_user(form.username.data, form.name.data, form.password.data, 'customer', form.email.data, form.phone_number.data) flash('Congratulations, you are now a registered user!') return redirect(url_for('login')) content['title'] = 'Register' content['form'] = form return render_template('register.html', **content)
def register(): form = RegisterForm() if form.validate_on_submit(): data = form.data user = User(name=data["name"], email=data["email"], phone=data["phone"], pwd=generate_password_hash(data["pwd"]), uuid=uuid.uuid4().hex, add_time=datetime.datetime.now()) db.session.add(user) db.session.commit() flash("注册成功^_^, 请登录", "OK") return redirect(url_for("home.login")) return render_template("home/register.html", form=form)
def validate_username(self, username): from movie.models import User user = User().query_by_username(username.data) if user is not None: raise ValidationError('Please use a different username.')
def validate_email(self, email): user = User.objects(email=email.data).first() if user: raise ValidationError('Email already used. Please use another one')
def validate_username(self, username): user = User.objects(username=username.data).first() if user: raise ValidationError( 'Username already taken. Please take another one')
def fileToDB(request): print("===========================") print("----- fileToDB start -----") print(datetime.datetime.now()) count = 1 data = recommender.data_for_db("C:\\Users\\isaac\\Desktop\\project\\server") # 디비에 저장 print("transfer user data") print(datetime.datetime.now()) for i in range(1, data.user_num + 1): if i % 100 == 0: print("doing : " + str(i)) newUser = User(u_id=i, login_id="test"+str(i), password="******"+str(i)) newUser.unknown_rate = data.user_profile[i][recommender._unknown] newUser.action_rate = data.user_profile[i][recommender._action] newUser.adventure_rate = data.user_profile[i][recommender._adventure] newUser.animation_rate = data.user_profile[i][recommender._animation] newUser.children_rate = data.user_profile[i][recommender._children] newUser.comedy_rate = data.user_profile[i][recommender._comedy] newUser.crime_rate = data.user_profile[i][recommender._crime] newUser.documentary_rate = data.user_profile[i][recommender._documentary] newUser.drama_rate = data.user_profile[i][recommender._drama] newUser.fantasy_rate = data.user_profile[i][recommender._fantasy] newUser.film_noir_rate = data.user_profile[i][recommender._film_noir] newUser.horror_rate = data.user_profile[i][recommender._horror] newUser.musical_rate = data.user_profile[i][recommender._musical] newUser.mystery_rate = data.user_profile[i][recommender._mystery] newUser.romance_rate = data.user_profile[i][recommender._romance] newUser.sci_fi_rate = data.user_profile[i][recommender._sci_fi] newUser.thriller_rate = data.user_profile[i][recommender._thriller] newUser.war_rate = data.user_profile[i][recommender._war] newUser.western_rate = data.user_profile[i][recommender._western] newUser.unknown_num = data.user_profile[i][19 + recommender._unknown] newUser.action_num = data.user_profile[i][19 + recommender._action] newUser.adventure_num = data.user_profile[i][19 + recommender._adventure] newUser.animation_num = data.user_profile[i][19 + recommender._animation] newUser.children_num = data.user_profile[i][19 + recommender._children] newUser.comedy_num = data.user_profile[i][19 + recommender._comedy] newUser.crime_num = data.user_profile[i][19 + recommender._crime] newUser.documentary = data.user_profile[i][19 + recommender._documentary] newUser.drama_num = data.user_profile[i][19 + recommender._drama] newUser.fantasy_num = data.user_profile[i][19 + recommender._fantasy] newUser.film_noir_num = data.user_profile[i][19 + recommender._film_noir] newUser.horror_num = data.user_profile[i][19 + recommender._horror] newUser.musical_num = data.user_profile[i][19 + recommender._musical] newUser.mystery_num = data.user_profile[i][19 + recommender._mystery] newUser.romance_num = data.user_profile[i][19 + recommender._romance] newUser.sci_fi_num = data.user_profile[i][19 + recommender._sci_fi] newUser.thriller_num = data.user_profile[i][19 + recommender._thriller] newUser.war_num = data.user_profile[i][19 + recommender._war] newUser.western_num = data.user_profile[i][19 + recommender._western] newUser.save() print("transfer movie data") print(datetime.datetime.now()) for i in range(1, data.movie_num + 1): if i % 100 == 0: print("doing : " + str(i)) newMovie = Movie(m_id=i, title=data.movies.movie_list[i].title) newMovie.is_unknown = True if data.movie_profile[recommender._unknown][i] == 1 else False newMovie.is_action = True if data.movie_profile[recommender._action][i] == 1 else False newMovie.is_adventure = True if data.movie_profile[recommender._adventure][i] == 1 else False newMovie.is_animation = True if data.movie_profile[recommender._animation][i] == 1 else False newMovie.is_children = True if data.movie_profile[recommender._children][i] == 1 else False newMovie.is_comedy = True if data.movie_profile[recommender._comedy][i] == 1 else False newMovie.is_crime = True if data.movie_profile[recommender._crime][i] == 1 else False newMovie.is_documentary = True if data.movie_profile[recommender._documentary][i] == 1 else False newMovie.is_drama = True if data.movie_profile[recommender._drama][i] == 1 else False newMovie.is_fantasy = True if data.movie_profile[recommender._fantasy][i] == 1 else False newMovie.is_film_noir = True if data.movie_profile[recommender._film_noir][i] == 1 else False newMovie.is_horror = True if data.movie_profile[recommender._horror][i] == 1 else False newMovie.is_musical = True if data.movie_profile[recommender._musical][i] == 1 else False newMovie.is_mystery = True if data.movie_profile[recommender._mystery][i] == 1 else False newMovie.is_romance = True if data.movie_profile[recommender._romance][i] == 1 else False newMovie.is_sci_fi = True if data.movie_profile[recommender._sci_fi][i] == 1 else False newMovie.is_thriller = True if data.movie_profile[recommender._thriller][i] == 1 else False newMovie.is_war = True if data.movie_profile[recommender._war][i] == 1 else False newMovie.is_western = True if data.movie_profile[recommender._western][i] == 1 else False newMovie.save() print("transfer rating data") print(datetime.datetime.now()) for i in range(1, data.user_num + 1): if i % 20 == 0: print("doing user : "******"----- fileToDB end -----") print(datetime.datetime.now()) print("===========================") print("===============================") print("----- prepareSVDpp start -----") print(datetime.datetime.now()) global td temp = recommender.total_data() print("----- prepareSVDpp end -----") print(datetime.datetime.now()) print("===============================") print("==========================") print("----- doSVDpp start -----") print(datetime.datetime.now()) temp.start_svd() temp.start_prediction() temp.start_recommendation() td = temp print("----- doSVDpp end -----") print(datetime.datetime.now()) print("==========================") return HttpResponse("fileToDB, doSVDpp가 완료되었습니다.")