def login(): if 'username' in session: return redirect(url_for('main.home')) form = userService.LoginForm() if form.validate_on_submit(): authenticated_user = userService.is_authenticated( username=form.username.data, password=form.password.data) if authenticated_user: session['username'] = form.username.data next_page = request.args.get('next') return redirect(next_page) if next_page else redirect( url_for('votes.list_vote')) else: current_app.logger.error( "----------User '{}' Login failed, username/password do not match record----------" .format(form.username.data)) return render_template(LOGIN_PAGE, title='Login', form=form, error=AUTHENTICATION_ERROR), 401 else: if request == 'POST': current_app.logger.error( "----------Internal Error: {}----------".format(form.errors)) return render_template(LOGIN_PAGE, title='Login', form=form, error=INTERNAL_ERROR_MSG), 500 return render_template(LOGIN_PAGE, title='Login', form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('imageManager.get_images')) form = userService.LoginForm() if form.validate_on_submit(): authenticated_user = userService.is_authenticated( username=form.username.data, password=form.password.data) if authenticated_user: login_user(authenticated_user, remember=form.remember_user.data) next_page = request.args.get('next') current_app.logger.info( "----------User '{}' login success !----------".format( authenticated_user.username)) return redirect(next_page) if next_page else redirect( url_for('imageManager.get_images')) else: current_app.logger.error( "----------User '{}' Login failed, username/password do not match record----------" .format(form.username.data)) return render_template(LOGIN_PAGE, title='Login', form=form, error=AUTHENTICATION_ERROR), 401 else: if request == 'POST': current_app.logger.error( "----------Internal Error: {}----------".format(form.errors)) return render_template(LOGIN_PAGE, title='Login', form=form, error=INTERNAL_ERROR_MSG), 500 return render_template(LOGIN_PAGE, title='Login', form=form)
def post_vote(): if 'username' not in session: login_form = userService.LoginForm() error = "To initiate a vote, you need to login to your account first!" return render_template(LOGIN_PAGE, title='Login', form=login_form, error=error) form = voteService.CreateVoteForm() if form.validate_on_submit(): username = session['username'] vote_id = voteService.create_vote(username=session['username'], vote_form=form) message = "Vote with id : " + vote_id + " has been initiated successfully!" userService.update_user_votes(username, vote_id) return render_template(VOTE_CREATION_PAGE, title='Post Vote', form=form, message=message) else: if request == 'POST': current_app.logger.error( "----------Internal Error: {}----------".format(form.errors)) return render_template(VOTE_CREATION_PAGE, title='Post Vote', form=form, error=INTERNAL_ERROR_MSG), 500 return render_template(VOTE_CREATION_PAGE, title='Post Vote', form=form)
def list_my_vote(): if 'username' not in session: login_form = userService.LoginForm() error = "To see your vote topics, you need to login to your account first!" return render_template(LOGIN_PAGE, title='Login', form=login_form, error=error) posted_votes = voteService.list_posted_votes(session['username']) voted_votes = voteService.list_voted_votes(session['username']) return render_template(MY_VOTE_LIST_PAGE, title='List My Vote', votesPosted=posted_votes, votesVoted=voted_votes)
def register(): form = userService.CreateUserForm() if 'username' in session: return redirect(url_for('main.home')) if form.validate_on_submit(): response = userService.get_user_by_username( username=form.username.data) user_with_username = None if 'Item' in response and 'username' in response['Item']: user_with_username = response['Item']['username'] if user_with_username: current_app.logger.error( "----------409 Registration conflict: {} already exists----------" .format(form.username.data)) return render_template(REGISTER_PAGE, title='Register', form=form, error=USER_EXISTED_ERROR.format( form.username.data)), 409 else: try: userService.create_user(username=form.username.data, email=form.email.data, password=form.password.data) login_form = userService.LoginForm() current_app.logger.info( "----------User '{}' register successful----------".format( form.username.data)) return render_template(LOGIN_PAGE, title='Login', form=login_form, message=REG_SUCCESS_MSG) except Exception as e: current_app.logger.error( "----------Database action error: {}----------".format( str(e))) return render_template(REGISTER_PAGE, title='Register', form=form, error=INTERNAL_ERROR_MSG), 500 else: if request.method == 'POST': current_app.logger.error( "----------Internal Error: {}----------".format(form.errors)) return render_template(REGISTER_PAGE, title='Register', form=form, error=INTERNAL_ERROR_MSG), 500 return render_template(REGISTER_PAGE, title='Register', form=form)
def register(): form = userService.CreateUserForm() if current_user.is_authenticated: return redirect(url_for('imageManager.get_images')) if form.validate_on_submit(): user_with_username = userService.get_user_by_username( username=form.username.data) if user_with_username: current_app.logger.error( "----------409 Registration conflict: {} already exists----------" .format(form.username.data)) return render_template(REGISTER_PAGE, title='Register', form=form, error=USER_EXISTED_ERROR.format( form.username.data)), 409 else: try: user = userService.create_user(username=form.username.data, password=form.password.data) os.makedirs( os.path.join(current_app.config["IMAGES_UPLOAD_URL"], form.username.data)) login_form = userService.LoginForm() current_app.logger.info( "----------User '{}' register successful----------".format( user.username)) return render_template(LOGIN_PAGE, title='Login', form=login_form, message=REG_SUCCESS_MSG) except IntegrityError as e: current_app.logger.error( "----------Database action error: {}----------".format( str(e))) return render_template(REGISTER_PAGE, title='Register', form=form, error=INTERNAL_ERROR_MSG), 500 else: if request.method == 'POST': current_app.logger.error( "----------Internal Error: {}----------".format(form.errors)) return render_template(REGISTER_PAGE, title='Register', form=form, error=INTERNAL_ERROR_MSG), 500 return render_template(REGISTER_PAGE, title='Register', form=form)
def vote_details(vote_id, vote_create_time): # post = voteService.list_specific_vote(vote_id, vote_create_time) post = voteService.list_specific_vote(vote_id) if 'username' in session: username = session['username'] ID = voteService.list_voted_IDS(username) if vote_id in ID: return redirect( url_for('votes.vote_results', vote_id=vote_id, vote_create_time=vote_create_time)) post_topic = post["topic"] options = post["options"] option1 = options[0]["content"] # option1 option2 = options[1]["content"] # option2 option3 = options[2]["content"] # option3 try: option4 = options[3]["content"] # option 4 except IndexError: option4 = [] try: option5 = options[4]["content"] # option 5 except IndexError: option5 = [] if request.method == 'POST': if 'username' not in session: login_form = userService.LoginForm() error = "To involve in a vote, you need to login to your account first!" return render_template(LOGIN_PAGE, title='Login', form=login_form, error=error) if request.form.get('name') is None: error = "You need to select one option!" return render_template('votes.detail.html', vote_id=vote_id, vote_create_time=vote_create_time, topic=post_topic, options=options, option1=option1, option2=option2, option3=option3, option4=option4, option5=option5, error=error) num = int(request.form.get('name')) username = session['username'] # ID = voteService.list_voted_IDS(username) # if vote_id in ID: # return redirect(url_for('votes.vote_results', vote_id=vote_id, vote_create_time=vote_create_time)) # else: voteService.vote_update(vote_id, num) userService.update_user_votes(username, vote_id) return redirect( url_for('votes.vote_results', vote_id=vote_id, vote_create_time=vote_create_time)) return render_template('votes.detail.html', vote_id=vote_id, vote_create_time=vote_create_time, topic=post_topic, options=options, option1=option1, option2=option2, option3=option3, option4=option4, option5=option5)