def get_posts(): params = converts_keys(request.args.to_dict(), case='snake') set_filter_params(DEFAULT_POST_LIMIT, MAX_POST_LIMIT, params) cookies = request.cookies if 'token' in cookies: with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Users.get_user_id(), cookies) record = cursor.fetchone() user_id = record['user_id'] else: user_id = 0 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: if 'search' in params: params['search'] = '%' + params['search'] + '%' cursor.execute(Posts.filter(**params), { 'user_id': user_id, **params }) posts = cursor.fetchall() cursor.execute(Posts.count(**params), params) record = cursor.fetchone() for post in posts: put_out_author(post) return jsonify(converts_keys({'posts': posts, **record}, case='camel'))
def get(self, format=None): post = Posts(id=self.id, slug=self.slug, type=self.type) postData = post.get() if postData is None: return {'code': 404, 'message': 'NOT FOUND'} postData['date'] = time.mktime(postData['date'].timetuple())*1000 if format is None: postData['content'] = markdown.markdown(postData['content'], extensions = ['codehilite']) return {'code': 200, 'data': postData}
def get(self, ignore=None): post = Posts(type=self.type, date=self.date, tag=self.tag, author=self.author) posts = post.archives(page=self.page) if posts is None: return {'code': 404, 'message': 'NOT EXSITS'} posts['data'] = self.__formatListDate(posts['data']) if ignore is not None: for i, post in enumerate(posts['data']): posts['data'][i].pop(ignore) return {'code': 200, 'data': posts['data'], 'pager': posts['pager']}
def get(self, id=None): header = self.get_argument('header', default=None) content = self.get_argument('content', default=None) author = self.get_argument('author', default=None) categories = self.get_arguments('categories') post = Posts(header, content, author, id) try: Posts.update(post, categories) except: self.send_error(400)
def get(self): header = self.get_argument('header') content = self.get_argument('content') author = self.get_argument('author') categories = self.get_arguments('categories') if not categories: self.send_error(400) else: post = Posts(header, content, author) Posts.add(post, categories)
def save(self, data): self.__setPostDate(data) empty = self.__isEmpty(['title', 'tag', 'type', 'content']) if len(empty) > 0: return {'code': 403, 'empty': empty} post = Posts(id=self.id, title=self.title, content=self.content, tag=self.tag, type=self.type, author=self.author, slug=self.slug) if self.id is None: post.create() else: post.update() return {'code': 200}
def get(self, id=None): if id == None: result = Posts.get_all() posts = [] for post in result: posts.append(post.toDict()) self.write({'posts': posts}) else: try: post = Posts.get(id) self.write(post.toDict()) except: self.send_error(400)
def delete_post(post_id): cookies = request.cookies if 'token' not in cookies: return jsonify(), 401 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Users.get_user_id(), cookies) user_id = cursor.fetchone()['user_id'] cursor.execute(Posts.get_author_id(), {'id': post_id}) author_id = cursor.fetchone()['author_id'] if user_id != author_id: return jsonify(), 401 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Posts.delete(), {'id': post_id}) return jsonify(), 205
def post(): form = PostForm() src = [{'filetype': 'None', 'path': 'None'}] privacy = request.form['privacy'] if request.method == 'POST': if 'media' in request.files: media = request.files.getlist('media') process = upload(media, privacy=privacy, used_as='post') if process['result'] == 'done': path = process['path'] type_ = process['filetype'] postname = process['postname'] src = [{'path': path, 'filetype': type_, 'filename': postname}] pass if form.validate_on_submit(): new_post = Posts(post_content={ 'headline': form.headline.data, 'body': form.body.data, 'media': src }, privacy=privacy, timestamp=getDate()) db.session.add(new_post) db.session.commit() flash("Post successfull!", "success") return redirect(url_for('home')) return redirect(url_for("home")) abort(400)
def upload_post(): form = UploadPostForm() if form.validate_on_submit(): title = form.title.data.strip(' ') content = form.content.data.strip(' ') image_name = form.image_name.data.strip(' ') old = Posts.query.filter_by(title=title).first() if old: flash('post already collected') return redirect(url_for('admin.upload_post')) post = Posts(title=title, content=content) post.add_tag(form.tags.data) post.image_name = image_name post.description = form.description.data post.author = current_user db.session.add(post) db.session.commit() flash('upload successful') return redirect(url_for('admin.upload_post')) return render_template('admin/upload_post.html',form=form)
def update_post(post_id): payload = converts_keys(loads(request.data), case='snake') check_only_required_payload_props(payload, 'category', 'content') cookies = request.cookies if 'token' not in cookies: return jsonify(), 401 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Users.get_user_id(), cookies) user_id = cursor.fetchone()['user_id'] cursor.execute(Posts.get_author_id(), {'id': post_id}) author_id = cursor.fetchone()['author_id'] if user_id != author_id: return jsonify(), 401 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Posts.update(), {'id': post_id, **payload}) post = cursor.fetchone() return jsonify(converts_keys(post, case='camel'))
def unlike(): params = converts_keys(request.args.to_dict(), case='snake') cookies = request.cookies if 'token' not in cookies: return jsonify(), 401 with connect(DSN) as connection: with connection.cursor(cursor_factory=RealDictCursor) as cursor: cursor.execute(Users.get_user_id(), cookies) record = cursor.fetchone() if 'post_id' in params: cursor.execute(Posts.unlike(), {**record, **params}) elif 'comment_id' in params: cursor.execute(Comments.unlike(), {**record, **params}) elif 'reply_id' in params: cursor.execute(Replies.unlike(), {**record, **params}) else: jsonify(), 400 return jsonify(), 205
from database import Posts from flask import Flask, render_template, request, url_for, redirect import time app = Flask(__name__) #Instantiated Flask Object newpost = Posts() @app.route("/") @app.route("/home") def home(): return render_template('home01.html', posts = newpost.posts, comment = len(newpost.posts)) @app.route("/about") def about(): return render_template('about01.html', title = 'about') @app.route("/post") def post(): return render_template('post.html') @app.route("/create", methods = ["POST"]) def create_post(): author = request.form['author'] title = request.form['title'] date_posted = time.strftime("%e %B, %Y", time.localtime()) content = request.form['content'] newpost.get_Dict(author, title, content, date_posted) newpost.StoreDatabase() return redirect(url_for('home')) if __name__ == "__main__":
def get(self, id=None): try: Posts.delete(id) except: self.send_error(400)