def register(): """Registers the user.""" if g.user: return redirect(url_for('timeline')) error = None if request.method == 'POST': if not request.form['username']: error = 'You have to enter a username' elif not request.form['email'] or \ '@' not in request.form['email']: error = 'You have to enter a valid email address' elif not request.form['password']: error = 'You have to enter a password' elif request.form['password'] != request.form['password2']: error = 'The two passwords do not match' elif mt_api.get_user_id(request.form['username']) is not None: error = 'The username is already taken' else: payload = { 'username': request.form['username'], 'email': request.form['email'], 'pw_hash': generate_password_hash(request.form['password']) } print payload url = 'http://localhost:8080/users/Sign_up' r = requests.post(url, json=payload) flash('You were successfully registered and can login now') return redirect(url_for('login')) return render_template('register.html', error=error)
def unfollow_user(username): """Removes the current user as follower of the given user.""" if not g.user: abort(401) whom_id = mt_api.get_user_id(username) if whom_id is None: abort(404) payload = { 'user_id': session['user_id'], 'pw_hash': session['pw_hash'], 'username': session['username'], 'whom_id': whom_id } url = 'http://localhost:8080/users/' + str( session['user_id']) + '/unfollow' r = requests.delete(url, json=payload) flash('You are no longer following "%s"' % username) return redirect(url_for('user_timeline', username=username))
def follow_user(username): """Adds the current user as follower of the given user.""" if not g.user: abort(401) whom_id = mt_api.get_user_id(username) if whom_id is None: abort(404) payload = { 'user_id': session['user_id'], 'pw_hash': session['pw_hash'], 'username': session['username'], 'whom_id': str(whom_id), 'email': session['email'], 'pub_date': int(time.time()) } url = 'http://localhost:8080/users/' + str( session['user_id']) + '/add_follow' r = requests.post(url, json=payload) flash('You are now following "%s"' % username) return redirect(url_for('user_timeline', username=username))