def get_post(post_id): post_response= table.scan( FilterExpression=Attr('Id').eq(post_id) ) if post_response['Items'] and len(post_response['Items']).__eq__(1): user_response = table.scan( FilterExpression=Attr('Id').eq(post_response['Items'][0]['UserId']) ) if user_response['Items'] and len(user_response['Items']).__eq__(1): post = Post(post_response['Items'][0]['Id'], post_response['Items'][0]['UserId'], post_response['Items'][0]['Title'], post_response['Items'][0]['Content'], post_response['Items'][0]['Date_Posted'], user_response['Items'][0]['Username']) return post return Post()
def validate_email(self, email): if email.data != current_user.email: response = table.scan( FilterExpression=Attr('Email').eq(email.data)) if response['Items']: raise ValidationError( 'That username is taken. Please choose a different one.')
def validate_username(self, username): if current_user.username != username.data: response = table.scan( FilterExpression=Attr('Username').eq(username.data)) if response['Items']: raise ValidationError( 'That username is taken. Please choose a different one.')
def user_posts(user_id): posts = [] post_response = table.scan(FilterExpression=Attr('UserId').eq(user_id)) user_response = table.scan(FilterExpression=Attr('Id').eq(user_id)) if user_response['Items'] and len(user_response['Items']).__eq__(1): user = User(user_response['Items'][0]['Id'], user_response['Items'][0]['Username'], user_response['Items'][0]['Email'], user_response['Items'][0]['Password']) if post_response['Items']: for i in post_response['Items']: post = Post(i['Id'], i['UserId'], i['Title'], i['Content'], i['Date_Posted'], user.username) posts.append(post) print(posts) return render_template('user_posts.html', posts=posts, user=user, total_post=len(posts))
def home(): posts = [] if current_user.is_authenticated: response = table.scan(FilterExpression=Attr('UserId').eq(current_user.id)) if response['Items']: for i in response['Items']: post = Post(i['Id'], i['UserId'], i['Title'], i['Content'], i['Date_Posted'], current_user.username) posts.append(post) else: response = table.scan( FilterExpression=Attr('Username').not_exists() ) if response['Items']: for i in response['Items']: user_response = table.scan( FilterExpression=Attr('Id').eq(i['UserId']) ) if user_response['Items'] and len(user_response['Items']).__eq__(1): post = Post(i['Id'], i['UserId'], i['Title'], i['Content'], i['Date_Posted'], user_response['Items'][0]['Username']) posts.append(post) return render_template('home.html', posts=posts, total_post = len(posts))
def login(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = LoginForm() if form.validate_on_submit(): response = table.scan( FilterExpression=Attr('Email').eq(form.email.data)) if response['Items'] and len(response['Items']) == 1: user = User(response['Items'][0]['Id'], response['Items'][0]['Username'], response['Items'][0]['Email'], response['Items'][0]['Password']) 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('main.home')) else: flash('Login Unsuccessful. Please check username and password', 'danger') else: flash('No user found with given email!', 'danger') return render_template('login.html', title='Login', form=form)