def blog(): #page = request.args.get('page', 1, type=int) #posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=10) posts = [] allPost = db.child("blog").get() for single in allPost.each(): singlePost = single.val() print(singlePost) print(type(singlePost)) #add username usersDetails = db.child("users").child(singlePost['userID']).get() u = usersDetails.val() allDetails = list(u.items()) print(allDetails) name = (allDetails[3][1]) affiliation = (allDetails[0][1]) avatar = storage.child(allDetails[1][1]).get_url(None) email = (allDetails[2][1]) userDict = {'name' : name, 'affiliation' : affiliation, 'avatar' : avatar, 'email' : email} singlePost.update(userDict) posts.append(singlePost) print(posts) return render_template("blog.html", posts=posts, title='Blog')
def post(post_id): print(post_id) allPost = db.child("blog").child(post_id).get() allP = (allPost.val()) allP = list(allP.items()) print(allP) content = allP[0][1] postID = allP[1][1] timestamp = allP[2][1] title = allP[3][1] userID = allP[4][1] print(content) #add username usersDetails = db.child("users").child(singlePost['userID']).get() u = usersDetails.val() allDetails = list(u.items()) print(allDetails) name = (allDetails[3][1]) affiliation = (allDetails[0][1]) avatar = storage.child(allDetails[1][1]).get_url(None) email = (allDetails[2][1]) #userDict = {'name' : name, 'affiliation' : affiliation, 'avatar' : avatar, 'email' : email} #singlePost.update(userDict) #post = Post.query.get_or_404(post_id) return render_template('post.html', title=title, content = content, postID = postID, timestamp = timestamp, userID = userID, name=name, affiliation = affiliation, avatar = avatar, email = email)
def delete_post(post_id): db.child("blog").child(post_id).remove() #db.session.delete(post) #db.session.commit() flash('Your post has been deleted!', 'success') return redirect(url_for('blog'))
def update_post(post_id): #post = Post.query.get_or_404(post_id) #if post.author != current_user: # abort(403) form = PostForm() if form.validate_on_submit(): title = form.title.data content = form.content.data ''' data = { "blog/"+str(post_id): { "title": title }, "blog/"+str(post_id): { "content" : content } } ''' db.child("blog").child(post_id).update({"title": title}) db.child("blog").child(post_id).update({"content": content}) #db.update(data) #db.session.commit() flash('Your post has been updated!', 'success') return redirect(url_for('post', post_id=post_id)) elif request.method == 'GET': allPost = db.child("blog").child(post_id).get() allP = (allPost.val()) allP = list(allP.items()) print(allP) content = allP[0][1] postID = allP[1][1] timestamp = allP[2][1] title = allP[3][1] userID = allP[4][1] print(content) form.title.data = title form.content.data = content return render_template('create_post.html', title='Update Post', form=form, legend='Update Post')
def new_post(): if 'login' in session: form = PostForm() print(session['login'], session['userID']) if form.validate_on_submit(): title=form.title.data content=form.content.data #author=current_user x = datetime.datetime.now() x = x.strftime("%Y-%m-%d %H:%M:%S") tmp = (random.randint(100000000000000,999999999999999)) tmp = str(tmp) data = { "title": title , "content" : content, "userID" : session['userID'], "timestamp" : x, "id" : tmp} result = db.child("blog").child(tmp).set(data) return redirect(url_for('blog')) return render_template('create_post.html', title='New Post', form=form, legend='New Post') else: return redirect(url_for('login'))
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): #hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8') username=form.username.data affiliation=form.affiliation.data email=form.email.data password=form.password.data print(username, affiliation, email, password) user = auth.create_user_with_email_and_password(email, password) print(user) uid = user['localId'] data = { "name": username, "email" : email, "affiliation" : affiliation, } print( user) result = db.child("users").child(uid).set(data) return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def blog(): #page = request.args.get('page', 1, type=int) #posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=10) posts = [] allPost = db.child("blog").get() for single in allPost.each(): singlePost = single.val() print(singlePost) print(type(singlePost)) #add username usersDetails = db.child("users").child(singlePost['userID']).get() u = usersDetails.val() allDetails = list(u.items()) print(allDetails) name = (allDetails[3][1]) affiliation = (allDetails[0][1]) avatar = storage.child(allDetails[1][1]).get_url(None) email = (allDetails[2][1]) userDict = { 'name': name, 'affiliation': affiliation, 'avatar': avatar, 'email': email, 'date': singlePost['timestamp'].split(" ")[0] } singlePost.update(userDict) posts.append(singlePost) print(posts) #x = singlePost['timestamp'] #posts.sort(key=lambda x: time.strptime(x, '%Y-%m-%d %H:%M:%S')[0:6], reverse=True) if 'login' in session: return render_template("blog.html", posts=posts, title='Blog', loggedin=True) else: return render_template("blog.html", posts=posts, title='Blog', loggedin=False)
def account(): if 'login' in session: form = UpdateAccountForm() if form.validate_on_submit(): if form.picture.data: print(form.picture.data) #filename = secure_filename(form.picture.data.filename) #form.picture.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #tmp = random.randint(100000,999999) picture_file = save_picture(form.picture.data) #time.sleep(10) #storage.child("profile_pics").put("static/profile_pics/"+picture_file) #path = os.path.abspath() storage.child("profile_pics/"+str(picture_file)).put("imagenflask/static/profile_pics/"+picture_file) image_url = "profile_pics/"+picture_file else: image_url = "NA" #current_user.image_file = picture_file username = form.username.data affiliation = form.affiliation.data email = form.email.data db.child("users").child(session['userID']).update({"name": username}) db.child("users").child(session['userID']).update({"email": email}) db.child("users").child(session['userID']).update({"avatar": image_url}) db.child("users").child(session['userID']).update({"affiliation": affiliation}) #db.session.commit() #flash('Your account has been updated!', 'success') return redirect(url_for('account')) elif request.method == 'GET': print(session['userID']) allPost = db.child("users").child(session['userID']).get() allP = (allPost.val()) allP = list(allP.items()) print(allP) form.username.data = allP[3][1] form.affiliation.data = allP[0][1] form.email.data = allP[2][1] #image_file = image_file = storage.child(allP[1][1]).get_url(None) return render_template('account.html', title='Account', form=form, image = image_file) else: return redirect(url_for('login'))