def pub(ctx,request:YuHeLg.Request): payload = request.json post = Post() try: post.title = payload.get("title") post.author_id = request.user.id post.postdate = datetime.datetime.now() cont = Content() cont.content = payload.get("content") post.content = cont tags = payload["tags"] except Exception as e: print(e) raise exc.HTTPBadRequest() taglist = re.split('[\s,]',tags) for tag in taglist: t = session.query(Tag).filter(Tag.tag == tag).first() if t is None: t = Tag() t.tag = tag session.add(t) pt = Post_tag() pt.tag = t pt.post = post session.add(pt) session.add(post) try: session.commit() return jsonify(post_id=post.id) except: session.rollback() raise exc.HTTPInternalServerError()
def new_post(): post = Post() post.title = request.form.get("title","untitled") post.author_id = session['user_id'] post.slug = slugify(post.title) post.created_at = datetime.datetime.now() post.updated_at = datetime.datetime.now() db_session.add(post) db_session.commit() return redirect(url_for("edit", id=post.id))
def post_from_api_tweet_object(tweet, post=None): if not post: post = Post() post.twitter_id = tweet['id_str'] try: post.created_at = datetime.strptime(tweet['created_at'], '%a %b %d %H:%M:%S %z %Y') except ValueError: post.created_at = datetime.strptime(tweet['created_at'], '%Y-%m-%d %H:%M:%S %z') # whyyy post.author_id = 'twitter:{}'.format(tweet['user']['id_str']) if 'favorited' in tweet: post.favourite = tweet['favorited'] if 'entities' in tweet: post.has_media = bool('media' in tweet['entities'] and tweet['entities']['media']) if 'favorite_count' in tweet: post.favourites = tweet['favorite_count'] if 'retweet_count' in tweet: post.reblogs = tweet['retweet_count'] post.is_reblog = 'retweeted_status' in tweet return post