def index(): if not current_user.is_anonymous: user = current_user page = request.args.get('page', 1, type=int) posts = user.friends_posts().paginate(page, app.config['POST_PER_PAGE'], False) next_url = url_for('index', page=posts.next_num) if posts.has_next else None prev_url = url_for('index', page=posts.prev_num) if posts.has_prev else None post_form = CreatePost() if post_form.validate_on_submit(): media_id = upload_media(post_form) post = Post(body=post_form.text.data, photo_id=media_id, user_id=current_user.id) db.session.add(post) db.session.commit() return redirect(url_for('index')) return render_template('index.html', title='Homepage', posts=posts.items, post_form=post_form, next_url=next_url, prev_url=prev_url) return render_template('index.html', title='Homepage')
def post_by_id(post_id): post = Post.query.get(post_id) if request.method == "GET": return post.to_dict() elif request.method == "PUT": form = CreatePost() if form.validate_on_submit(): form.populate_obj(post) db.session.commit() if "images" in request.files: images = request.files.getlist("images") for image in images: if allowed_file(image.filename): image.filename = get_unique_filename(image.filename) image_url = upload_file_to_s3(image, Config.S3_BUCKET) image = PostsImage(post_id=post.id, image_url=image_url) db.session.add(image) db.session.commit() return post.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)} elif request.method == "DELETE": post.title = "[DELETED]" post.body = "[DELETED]" posts_images = PostsImage.query.filter( PostsImage.post_id == post_id).all() for image in posts_images: db.session.delete(image) db.session.commit() return post.to_dict() return post.to_dict()
def user(username): user = User.query.filter_by(username=username).first_or_404() profile = user.profile post_form = CreatePost() if post_form.validate_on_submit(): media_id = upload_media(post_form) post = Post(body=post_form.text.data, photo_id=media_id, user_id=current_user.id) db.session.add(post) db.session.commit() return redirect(url_for('user', username=username)) page = request.args.get('page', 1, type=int) posts = user.posts.filter_by(is_deleted=False).order_by( Post.timestamp.desc()).paginate(page, app.config['POST_PER_PAGE'], False) next_url = url_for('user', username=user.username, page=posts.next_num) if posts.has_next else None prev_url = url_for('user', username=user.username, page=posts.prev_num) if posts.has_prev else None return render_template('profile.html', user=user, profile=profile, posts=posts.items, post_form=post_form, next_url=next_url, prev_url=prev_url)
def create(): form = CreatePost() if form.validate_on_submit(): file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) post = Post(title = form.title.data, body=form.body.data, image_path = filename) user = User.query.get(current_user.get_id()) user.posts.append(post) db.session.add(user) for tag in form.tags.data.split(' '): if Tag.query.filter(Tag.body == tag).first(): add_tag = Tag.query.filter(Tag.body == tag).first() post.tags.append(add_tag) else: add_tag = Tag(body=tag) post.tags.append(add_tag) db.session.add(add_tag) db.session.add(post) db.session.commit() return redirect(url_for('post.index')) return render_template('post/create.html', title='Create Post', form=form)
def make_post(): form = CreatePost() if form.validate_on_submit(): post = Post(title = form.title.data, body = form.body.data, user_id = current_user.id) db.session.add(post) db.session.commit() flash('Post Published') return redirect(url_for('index')) return render_template('make_post.html', title = 'Create Post', form = form)
def update(id): form = CreatePost() post = Post.query.get(id) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data db.session.commit() flash('Your changes have been saved.') return redirect(url_for('post.index')) elif request.method == "GET": form.title.data = post.title form.body.data = post.body return render_template('post/update.html', title='Update Post', form = form, post=post )
def account(): create_form = CreatePost() if create_form.validate_on_submit(): sql = text( "insert into post(user_id, title, content, date, views) values (\'{}\', \'{}\', \'{}\', \'{}\', \'{}\')" .format(current_user.id, create_form.title.data, create_form.content.data, datetime.datetime.utcnow(), 0)) result = db.engine.execute(sql) return redirect(url_for('index')) is_auth = False if current_user.is_authenticated: is_auth = True return render_template("account.html", is_auth=is_auth, create_form=create_form)
def createpost(option): createpost = CreatePost() textpost=createpost.text.data postDate='2020-04-24' postTime='12:09:00' if request.method == "POST": # print("went into function") if(textpost!=""): print("textpost") db.engine.execute("insert into posts(content,ctype, postDateTime) values('"+textpost+"','text','"+str(datetime.datetime.now())+"')") else: photo= createpost.image.data # created_date=format_date_joined(datetime.datetime.now()) filename=secure_filename(photo.filename) photo.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) db.engine.execute("insert into gallery(photourl) values('"+'/static/uploads/'+filename+"')") lastphotoid= db.engine.execute("select photoid from gallery order by photoid desc limit 1") for last in lastphotoid: photoid=last.photoid # db.engine.execute("insert into addphoto(photoid ,userid) values ('"+str(photoid)+"','"+session['userid']+"')") cur.execute("CALL addphotos("+str(photoid)+",'"+str(session['userid'])+"')") conn.commit() db.engine.execute("insert into posts(content,ctype, postDateTime) values('"+'/static/uploads/'+filename+"','image','"+str(datetime.datetime.now())+"')") lastpostid= db.engine.execute("select postId from posts order by postid desc limit 1") for last in lastpostid: postId=last.postid # db.engine.execute("insert into user_post_log(postid ,userid) values ('"+str(postId)+"','"+session['userid']+"')") cur.execute("CALL adduserposts("+str(postId)+",'"+str(session['userid'])+"')") conn.commit() return redirect(url_for('posts'))
def add_post(): form = CreatePost() if not form.validate_on_submit(): flash(list(form.errors.values())[0]) return redirect(url_for('admin_bp.create_post')) Post(author=current_user.get_username(), title=request.form.get('title'), content=re.sub(r'\s+', '', request.form.get('content')), date_time=Utilities.post_timestamp(), status=request.form.get('status'), category=Category().custom_query( 'id', request.form.get('category')).category_name).add_post() flash('Your post has been created.') return redirect(url_for('admin_bp.posts'))
def groupposts(groupid): commentform=Comment() uploadform=UploadProfilePic() form=CreatePost() grouppostinfo=[] groupinfo=db.engine.execute("SELECT * FROM groups join profiles on profiles.userid=groups.createdby WHERE groupid = '"+groupid+"'") groupmembers = db.engine.execute("SELECT * FROM joinsgroup JOIN users ON users.userid = joinsgroup.userid INNER JOIN profiles ON profiles.userid = users.userid JOIN gallery ON gallery.photoid = profiles.profilepic WHERE groupid = '"+groupid+"'") nonMembers = db.engine.execute("SELECT * FROM users u JOIN profiles p ON u.userid = p.userid INNER JOIN gallery g ON g.photoid = p.profilepic WHERE u.userid NOT IN (SELECT userid FROM joinsgroup WHERE groupid = "+groupid+") order by u.userid desc limit 20") ingroup=False for x in groupmembers: if(int(session['userid'])==x.userid): ingroup=True if(ingroup): groupposts=db.engine.execute("select user_post_log.postid, user_post_log.userid, content, ctype, postdatetime, profileno, profilepic, username, countryliving, photoid, photourl from user_post_log join (SELECT * FROM posts WHERE postid IN (SELECT postid FROM groupposts WHERE groupid = "+groupid+")) AS posts on posts.postid=user_post_log.postid INNER join profiles on profiles.userid=user_post_log.userid INNER join gallery on profiles.profilepic=gallery.photoid order by posts.postid desc") else: groupposts=[] for a in groupinfo: creatorid = a.createdby groupname = a.groupname createddate = a.createddate groupcreator = a.username return render_template('groupPosts.html',editprofile=EditProfile(), form=form, uploadform=uploadform, searchform=SearchForm(), creatorid=int(creatorid), groupid = groupid, groupname=groupname, groupmembers=groupmembers,nonMembers=nonMembers, createddate=createddate, commentform=commentform, creator=groupcreator, groupinfo=groupinfo, posts=groupposts,profilepic=session['profilepic'],fname=session['fname'],username= session['username'],lname=session['lname'],email=session['email'],location=session['location'],biography=session['biography'],followers=session['followers'],following=session['following'],userid=int(session['userid']))
def sell(): form = CreatePost() # Get all data from the form, process it and send it to the db if form.validate_on_submit(): new_post = PostData(item=form.item.data, stock=form.stock.data, content=form.content.data, author=current_user.username) if form.img.data: new_post.img = save_img(form.img.data) db.session.add(new_post) db.session.commit() return redirect(url_for('user_posts')) else: # Get post from db and order it by most voted posts in descending order feed_posts = PostData.query.order_by(PostData.likes.desc()).all() return render_template("sell.html", form=form, feed=feed_posts)
def edit_post(postID): query = Post().custom_query('id', postID) if not query: return redirect(url_for('admin_bp.dashboard')) return render_template('/admin/edit-post.html', form=CreatePost(), message=get_flashed_messages(), post=query)
def searchuser(): form=CreatePost() fform=FriendType() uploadform=UploadProfilePic() searcform=SearchForm() commentform=Comment() searchusername=searcform.username.data searchusers=db.engine.execute(" select username,photourl,firstname,lastname,countryliving,profile.userid from (SELECT username,countryliving,userid,profilepic FROM profiles WHERE lower(username) LIKE '"+searchusername.lower()+"%%') as profile inner join users on profile.userid=users.userid inner join gallery on gallery.photoid=profile.profilepic limit 10") return render_template('searchlist.html',editprofile=EditProfile(),uploadform=uploadform,searchusers=searchusers,searchform=SearchForm(), fform=fform,commentform=commentform,posts=posts,form=form,profilepic=session['profilepic'],fname=session['fname'],username= session['username'],lname=session['lname'],email=session['email'],location=session['location'],biography=session['biography'],followers=session['followers'],following=session['following'],userid=session['userid'])
def userposts(userid): form=CreatePost() fform=FriendType() commentform=Comment() users=db.engine.execute("select * from profiles join users on profiles.userid=users.userid where users.userid='"+userid+"'") # posts=db.engine.execute("select * from (select * from texts union select * from images)as allpost join posts on posts.postid= allpost.postid join profiles on posts.userid=profiles.userid where posts.userid='"+str(userid)+"' order by posts.postid desc") posts=db.engine.execute("select * from user_post_log join posts on posts.postid=user_post_log.postid where posts.userid='"+str(userid)+"' order by posts.postid desc") return render_template('profilepage.html',editprofile=EditProfile(),searchform=SearchForm(), fform=fform,commentform=commentform,posts=posts,form=form,users=users,profilepic=session['profilepic'],fname=session['fname'],username= session['username'],lname=session['lname'],email=session['email'],location=session['location'],biography=session['biography'],followers=session['followers'],following=session['following'],userid=session['userid'])
def create_post(): form = CreatePost() if form.validate_on_submit(): post = Post() form.populate_obj(post) db.session.add(post) db.session.commit() if "images" in request.files: images = request.files.getlist("images") for image in images: if allowed_file(image.filename): image.filename = get_unique_filename(image.filename) image_url = upload_file_to_s3(image, Config.S3_BUCKET) image = PostsImage(post_id=post.id, image_url=image_url) db.session.add(image) db.session.commit() return post.to_dict() return {"errors": validation_errors_to_error_messages(form.errors)}
def posts(): # form=CreatePost() commentform=Comment() form=CreatePost() posts=db.engine.execute("select * from user_post_log inner join posts on posts.postid=user_post_log.postid inner join friendship on friendship.fuserid=user_post_log.userid inner join profiles on profiles.userid=user_post_log.userid inner join gallery on profiles.profilepic=gallery.photoid where friendship.userid="+session['userid']+"order by posts.postid desc") uploadform=UploadProfilePic() return render_template('posts.html',editprofile=EditProfile(),searchform=SearchForm(), uploadform=uploadform,commentform=commentform,posts=posts,form=form,profilepic=session['profilepic'],fname=session['fname'],username= session['username'],lname=session['lname'],email=session['email'],location=session['location'],biography=session['biography'],followers=session['followers'],following=session['following'],userid=session['userid'])
def creategrouppost(groupID, postType): createpost = CreatePost() textpost=createpost.text.data if request.method == "POST": #SELECT userid FROM joinsgroup WHERE status = 'Editor' AND userid = 42 AND groupid = 1; value=db.engine.execute("SELECT userid FROM joinsgroup WHERE status = 'Editor' AND userid = '"+session['userid']+"' AND groupid = '"+groupID+"';") isEditor = 0 for val in value: isEditor = val if isEditor: if postType == 'text' and textpost!="": db.engine.execute("insert into posts(content,ctype, postDateTime) values('"+textpost+"','text','"+str(datetime.datetime.now())+"')") lastTextPostID= db.engine.execute("SELECT postid FROM posts ORDER BY postid DESC LIMIT 1") for last in lastTextPostID: postid=last.postid db.engine.execute("INSERT INTO groupposts(groupid,postid) values('"+groupID+"', '"+str(postid)+"');") # db.engine.execute("insert into user_post_log(postid ,userid) values ('"+str(postid)+"','"+session['userid']+"')") cur.execute("CALL adduserposts("+str(postid)+",'"+str(session['userid'])+"')") conn.commit() return groupposts(groupID) elif postType == 'image': photo= createpost.image.data filename=secure_filename(photo.filename) photo.save(os.path.join(app.config['UPLOAD_FOLDER'],filename)) db.engine.execute("insert into gallery(photourl) values('"+'/static/uploads/'+filename+"')") lastphotoid= db.engine.execute("select photoid from gallery order by photoid desc limit 1") for last in lastphotoid: photoid=last.photoid # db.engine.execute("insert into addphoto(photoid ,userid) values ('"+str(photoid)+"','"+session['userid']+"')") cur.execute("CALL addphotos("+str(photoid)+",'"+str(session['userid'])+"')") conn.commit() db.engine.execute("insert into posts(content,ctype, postDateTime) values('"+'/static/uploads/'+filename+"','image','"+str(datetime.datetime.now())+"')") lastpostid= db.engine.execute("select postId from posts order by postid desc limit 1") for last in lastpostid: postId=last.postid # db.engine.execute("insert into user_post_log(postid ,userid) values ('"+str(postId)+"','"+session['userid']+"')") cur.execute("CALL adduserposts("+str(postId)+",'"+str(session['userid'])+"')") conn.commit() db.engine.execute("INSERT INTO groupposts(groupid,postid) values('"+groupID+"', '"+str(postId)+"');") return groupposts(groupID) else: flash('You are not a Content Editor for this group!', 'danger') return groupposts(groupID)
def myprofile(): form=CreatePost() fform=FriendType() commentform=Comment() uploadform=UploadProfilePic() users=db.engine.execute("select * from profiles join users on profiles.userid=users.userid join gallery on gallery.photoid=profiles.profilepic where users.userid='"+session['userid']+"'") # posts=db.engine.execute("select * from (select * from texts union select * from images)as allpost join posts on posts.postid= allpost.postid join profiles on posts.userid=profiles.userid where posts.userid='"+str(userid)+"' order by posts.postid desc") posts=db.engine.execute("select * from user_post_log join posts on posts.postid=user_post_log.postid join profiles on profiles.userid=user_post_log.userid join gallery on profiles.profilepic=gallery.photoid where profiles.userid='"+str(session['userid'])+"' AND user_post_log.postid NOT IN(SELECT postid FROM groupposts) order by posts.postid desc") postsCounts= db.engine.execute("SELECT COUNT(postid) AS post_counts FROM user_post_log where userid="+session['userid']+" and postid NOT IN(select postid from groupposts)") for x in postsCounts: postcount=x.post_counts return render_template('myprofilepage.html',postcount=postcount,editprofile=EditProfile(),searchform=SearchForm(), form=form,uploadform=uploadform,commentform=commentform,fform=fform,posts=posts,users=users,profilepic=session['profilepic'],fname=session['fname'],username= session['username'],lname=session['lname'],email=session['email'],location=session['location'],biography=session['biography'],followers=session['followers'],following=session['following'],userid=session['userid'])
def create_post(): return render_template('/admin/create-post.html', form=CreatePost(), message=get_flashed_messages())