def setUpClass(cls): os.environ['TESTING'] = 'true' from web import app, db cls.app = app.test_client() db.create_all() # add default user db.session.add(User(email='*****@*****.**', name='tests', password=pw_hash('password1234'))) db.session.commit()
def signup(): if request.method == 'GET': return redirect(url_for('login')) elif request.method == 'POST': if not imgtl.validator.email(request.form['email']): flash(i18n('invalidemail'), 'error') return redirect(url_for('signup')) if not imgtl.validator.username(request.form['username']): flash(i18n('invalidusername'), 'error') return redirect(url_for('signup')) if not imgtl.validator.password(request.form['password']): flash(i18n('invalidpassword'), 'error') return redirect(url_for('signup')) if request.form['password'] != request.form['passwordconfirm']: flash(i18n('passwordmismatch'), 'error') return redirect(url_for('signup')) user = User.query.filter((User.email == request.form['email']) | ( User.name == request.form['username'])).first() if user: if user.email == request.form['email']: flash(i18n('alreadyexistemail'), 'error') return redirect(url_for('signup')) elif user.name == request.form['username']: flash(i18n('alreadyexistname'), 'error') return redirect(url_for('signup')) user = User(email=request.form['email'], name=request.form['username'], password=imgtl.lib.pw_hash(request.form['password'])) while 1: try: user.token = imgtl.lib.make_token() db.session.commit() except IntegrityError: db.session.rollback() continue else: break db.session.add(user) db.session.commit() login_user(user, remember=True) do_log('web', 'signup', user.id) flash(i18n('signupsuccess'), 'success') return redirect(url_for('index'))
def settings(): if not (current_user.is_authenticated() or (request.method == 'GET' and 'oauth-signup' in session) or (request.method == 'POST' and 'oauth-signup-post' in session)): return redirect(url_for('login', next=url_for('settings'))) if request.method == 'GET': if 'oauth-signup' in session: user = User(email='', name=session['oauth-signup']['name'], oauth_uid=session['oauth-signup']['oauth_uid']) session['oauth-signup-post'] = session['oauth-signup'] del session['oauth-signup'] return render_template('settings.html', user=user) else: data = None if current_user.oauth_uid is not None: resp = twitter.get('users/show.json', data={'user_id': current_user.oauth_uid}, token='') if resp.status == 200: data = resp.data return render_imgtl_template('settings.html', twitter=data) elif request.method == 'POST': if request.form['what'] == 'token': while 1: try: current_user.token = imgtl.lib.make_token() db.session.commit() except IntegrityError: db.session.rollback() continue else: break return jsonify({'token': current_user.token}) elif request.form['what'] == 'update': if 'oauth-signup-post' in session: user = User( email='', name=session['oauth-signup-post']['name'], oauth_uid=session['oauth-signup-post']['oauth_uid']) else: user = current_user if request.form.get('password', '') != '': if not imgtl.validator.password(request.form['password']): flash(i18n('invalidpassowrd'), 'error') return redirect(url_for('settings')) elif request.form['password'] != request.form[ 'passwordconfirm']: flash(i18n('passwordmismatch'), 'error') return redirect(url_for('settings')) else: user.password = imgtl.lib.pw_hash(request.form['password']) new_email = request.form['email'] new_username = request.form['username'] if not imgtl.validator.email(new_email): flash(i18n('invalidemail'), 'error') return redirect(url_for('settings')) if not imgtl.validator.username(new_username): flash(i18n('invalidusername'), 'error') return redirect(url_for('settings')) if user.email != new_email: if User.query.filter_by(email=new_email).first(): flash(i18n('alreadyexistemail'), 'error') return redirect(url_for('settings')) if user.name != new_username: if User.query.filter_by(name=new_username).first(): flash(i18n('alreadyexistname'), 'error') return redirect(url_for('settings')) user.email = new_email user.name = new_username db.session.add(user) db.session.commit() if 'oauth-signup-post' in session: while 1: try: user.token = imgtl.lib.make_token() db.session.commit() except IntegrityError: db.session.rollback() continue else: break login_user(user, remember=True) do_log('web', 'signup_by_oauth', user.id) del session['oauth-signup-post'] flash(i18n('signupsuccess'), 'success') return redirect(url_for('index')) else: flash(i18n('accupdatesuccess'), 'success') return redirect(url_for('settings'))