def register(): form = {k: request.form[k].strip() for k in request.form} # form = {'username': '******', 'password': '******', 'email_addr': '*****@*****.**'} print(form['username'], form['password']) if utils.validate_username(form['username']): code = None if MAIL_USERNAME: code = utils.get_check_code() content = 'Hello! your checking code is:' + code thread = Thread(target=send_mail, args=[app, content, form['email_addr']]) thread.start() user = User(username=form['username'], password=form['password'], code=code) db.session.add(user) db.session.commit() # user = User.query.filter_by(username=form['username']).first() # success = user.update(code=code) # print(success) # db.session.commit() # login_user(user, remember=True) print('valid') return Validity(True).get_resp() # 'register succeeds' else: print('invalid') return Validity(False, 'Register fails: Invalid username or password.' ).get_resp() # 'register fails'
def add_member(): form = {k: request.form[k].strip() for k in request.form} if 'user_id' not in form: assert 'user_username' in form if utils.validate_username(form['user_username']): return Validity( False, 'User ' + form['user_username'] + ' does not exist.').get_resp() form['user_id'] = utils.get_userid(form['user_username']) if not utils.validate_userid(int(form['user_id'])): return Validity(False, 'Invalid user.').get_resp() if utils.validate_groupid(group_id=int(form['group_id'])): if utils.validate_groupreqs(int(form['user_id']), int(form['group_id'])): return Validity(False, 'Invitation already sent.').get_resp() if not utils.validate_membership(int(form['user_id']), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.add_memberReq(int(form['user_id'])) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Already in the group').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def post(self): raw_username = self.request.get("username") raw_password = self.request.get("password") raw_email = self.request.get("email") raw_verify = self.request.get("verify") username = utils.validate_username(raw_username) password = utils.validate_password(raw_password) email = utils.validate_email(raw_email) if not (username and password and raw_password==raw_verify and email): form_values = {'username':raw_username, 'email':raw_email} if not username: form_values['username_error'] = 'Invalid username!' form_values['isUserErrorOrSuccess'] = 'error' if not password: form_values['password_error'] = 'Invalid password!' form_values['isPassErrorOrSuccess'] = 'error' if raw_password!=raw_verify: form_values['verify_error'] = "Ooops! Doesn't match to the password above" form_values['isVerifyErrorOrSuccess'] = 'error' if not email: form_values['email_error'] = "Invalid email address!" form_values['isEmailErrorOrSuccess'] = 'error' self.render(signup_template, **form_values) else: self.redirect("welcome?user=%s" % str(raw_username))
def deny_friendReqs(): form = {k: request.form[k].strip() for k in request.form} if 'friend_id' not in form: assert 'friend_username' in form if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() form['friend_id'] = utils.get_userid(form['friend_username']) if not utils.validate_friendreqs(int(form['friend_id']), int(current_user.id)): return Validity(False, 'Request does not exist.').get_resp() friend = User.query.filter_by(id=int(form['friend_id'])).first() friend.deny_friendReq(int(current_user.id)) db.session.commit() return Validity(True).get_resp()
def get_friend_tasklist(): if request.form is None or 'friend_username' not in request.form: ret = [] for friend in current_user.get_friends(): ret.extend([task for task in friend.get_public_tasks()]) ret = sorted(ret, key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'friend task list': ret}).get_resp() else: form = {k: request.form[k].strip() for k in request.form} if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() friend = User.query.filter_by(username=form['friend_username']).first() ret = sorted([task for task in friend.get_public_tasks()], key=lambda v: v.finish_time) ret = [task.get_info_map() for task in ret] return Validity(True, {'friend task list': ret}).get_resp()
def delete_member(): form = {k: request.form[k].strip() for k in request.form} if 'user_id' not in form: assert 'user_username' in form if utils.validate_username(form['user_username']): return Validity( False, 'User ' + form['user_username'] + ' does not exist.').get_resp() form['user_id'] = utils.get_userid(form['user_username']) if utils.validate_groupid(group_id=int(form['group_id'])): if utils.validate_membership(int(form['user_id']), int( form['group_id'])) and not utils.validate_ownership( int(form['user_id']), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.delete_member(int(form['user_id'])) db.session.commit() return Validity(True).get_resp() else: return Validity(False, 'Can not quit the group').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def add_friend(): form = {k: request.form[k].strip() for k in request.form} if 'friend_id' not in form: assert 'friend_username' in form if utils.validate_username(form['friend_username']): return Validity( False, 'User ' + form['friend_username'] + ' does not exist.').get_resp() form['friend_id'] = utils.get_userid(form['friend_username']) if utils.validate_friendship(int(current_user.id), int(form['friend_id'])): return Validity( False, 'User ' + form['friend_username'] + ' has already been your friend.').get_resp() if utils.validate_friendreqs(int(current_user.id), int(form['friend_id'])): return Validity(False, 'Request already sent.').get_resp() # friend = User.query.filter_by(id = int(form['friend_id'])).first() current_user.add_friendReq(int(form['friend_id'])) # friend.add_friend(int(current_user.id)) db.session.commit() return Validity(True).get_resp()
def update_group(): form = {k: request.form[k].strip() for k in request.form} if 'owner_id' not in form and 'owner_username' in form: #assert 'owner_username' in form if utils.validate_username(form['owner_username']): return Validity( False, 'User ' + form['owner_username'] + ' does not exist.').get_resp() form['owner_id'] = utils.get_userid(form['owner_username']) if utils.validate_groupid(int(form['group_id'])): if utils.validate_ownership(int(current_user.id), int(form['group_id'])): group = Group.query.filter_by(id=int(form['group_id'])).first() group.update(name=(None if 'name' not in form else form['name']), owner_id=(None if 'owner_id' not in form else int( form['owner_id'])), info=(None if 'info' not in form else form['info'])) db.session.commit() return Validity(True, group.get_info_map()).get_resp() else: return Validity(False, 'No access').get_resp() else: return Validity(False, 'Invalid group id').get_resp()
def sign_up(): if redirect_is_logged(session) != True: return redirect_is_logged(session) page_name = "login - sign up" valid = True error_array = {} if request.method == 'POST': fullname = request.form.get('fullname', None) username = request.form.get('username', None) email = request.form.get('email', None) password = request.form.get('password', None) repeat_password = request.form.get('repeat_password', None) if validate_password(password) == False: error_array['password'] = '******' valid = False if password != repeat_password: error_array['password_repeat'] = 'password does\'nt match' valid = False if len(fullname) < 2: error_array['fullname'] = 'fullname too short' valid = False if validate_username(username) == False: error_array['username'] = '******' valid = False if User.query.filter_by(username=username).first() is not None: error_array['username'] = '******' valid = False if validate_email(email) == False: error_array['email'] = 'email is not valid' valid = False if User.query.filter_by(email=email).first() is not None: error_array['email'] = 'email is already taken' valid = False if valid == True: user = User(username=username, full_name=fullname, email=email, password=password) db.session.add(user) db.session.commit() if user == False: error_array['system'] = 'error insert db' else: session['id'] = user.id session['username'] = username session['full_name'] = fullname session['email'] = email # return render_template('index.html', page_name='Home', session=session) return redirect(url_for('index')) return render_template('login.html', page_name=page_name, error_array=error_array)
def signup(): if config.REGISTRATION_ENABLED: if request.method == "POST": # Validate data form = request.form email = form.get("email").strip().lower() username = form.get("username").strip() display_name = form.get("display-name").strip() password = form.get("password").strip() confirm_password = form.get("confirm-password").strip() form_items = { k: v for k, v in request.form.items() if "password" not in k } if utils.validate_email(email): username_available, username_reason = utils.validate_username( username) if username_available: if password == confirm_password: if password: if captcha.verify(): # Use referral code if available referral_code = form.get( "referral-code").strip() referrer = None if referral_code: referrer = models.ReferralCode.use( referral_code) # Create user account models.Crab.create_new( username=username, email=email, password=password, display_name=display_name, referrer=referrer, address=request.remote_addr, ) # "Log in" current_user = models.Crab.query.filter_by( username=username, deleted=False, banned=False, ).first() session["current_user"] = current_user.id session[ "current_user_ts"] = current_user.register_timestamp # Redirect on success return redirect("/signupsuccess") else: return utils.show_error( "Captcha verification failed", new_arguments=form_items, ) else: return utils.show_error( "Password cannot be blank", new_arguments=form_items, ) else: return utils.show_error( "Passwords do not match", new_arguments=form_items, ) else: return utils.show_error(username_reason, new_arguments=form_items) else: return utils.show_error( "An account with that email address already exists", new_arguments=form_items, ) elif session.get("current_user"): return redirect("/") else: signup_failed = request.args.get("failed") is not None error_msg = request.args.get("error_msg") return render_template( "signup.html", current_page="signup", hide_sidebar=True, signup_failed=signup_failed, error_msg=error_msg, referral_code=request.args.get("referral-code"), ) else: return render_template( "registration-closed.html", current_page="registration-closed", hide_sidebar=True, )