def publish(user_id): form = PublishForm() posts = Post() if user_id != current_user.id: flash('Sorry, you can only edit your publish!', 'error') return redirect(url_for('index')) if form.validate_on_submit(): blog_body = request.form.get('body') if not len(blog_body.strip()): flash("The content is necessray!") return redirect(url_for('publish', user_id = user_id)) posts.body = blog_body posts.timestamp = datetime.datetime.now() posts.user_id = user_id try: db.session.add(posts) db.session.commit() except: flash('Database error!') return redirect(url_for('publish', user_id = user_id)) flash('Publish Successful!') return redirect(url_for('publish', user_id = user_id)) return render_template('publish.html', form = form)
def createpost(): if request.method == "POST": title = request.form['title'] body = request.form['post'] tickers = request.form.getlist('tickers[]') tickers = checkTickers(tickers) if title == "": return jsonify({'error': "Please add a title"}) elif tickers == []: return jsonify({'error': "Please enter a valid ticker"}) elif body == "": return jsonify({'error': "Please create a post"}) p = Post() p.title = title p.body = body p.user_id = current_user.id for tick in tickers: p.tickers.append(tick) db.session.add(p) db.session.commit() return jsonify({'success': "Congratulations you've created a post"}) tickerList = [{ "title": str(ticker.symbol) } for ticker in Ticker.query.all()] return render_template('createpost.html', tickers=tickerList)
def edit(id=0): form = PostForm() if id == 0: post = Post(author_id=current_user.id) if post.title is None: post.title = "" else: post = Post.query.get_or_404(id) if request.method == "GET": form.title.data = post.title form.body.data = post.body if form.validate_on_submit(): post.body = form.body.data post.title = form.title.data db.session.add(post) db.session.commit() # print(form.body) return redirect(url_for("main.post", id=post.id)) mode = "添加新文章" if id > 0: mode = "编辑" # print(post.body) return render_template("posts/edit.html", title="{0}{1}".format(mode, post.title), post=post, form=form)
def create(): form = CreateForm() tags = Tag.query.all() form.tags.choices = [(t.id, t.text) for t in tags] if form.validate_on_submit(): post = Post() post.title = form.title.data post.body = form.body.data post.user_id = current_user.id post.url = title_to_url(form.title.data) tags = [] for tag in form.tags.data: t = Tag.query.filter_by(text=tag).first() if t is None: t = Tag(text=tag) tags.append(t) post.tags = tags db.session.add(post) db.session.commit() flash('Post created!') return redirect(url_for('blog.index')) return render_template( 'admin/create.html', title='Create post', form=form )
def edit(id=0): form = PostForm() if id == 0: post = Post(author=current_user) else: post = Post.query.get_or_404(id) if form.validate_on_submit(): post.body = form.body.data post.title = form.title.data db.session.add(post) db.session.commit() return redirect(url_for('.post', id=post.id)) form.title.data = post.title form.body.data = post.body title = _(u'添加新文章') if id > 0: title = _(u'编辑 - %(title)', title=post.title) return render_template('posts/edit.html', title=title, form=form, post=post)
def edit_article(postID): post = Post().query.get(int(postID)) form = ArticleForm() if not post: flash('No such article Found !', 'error') return redirect(url_for('file_not_found(404)')) if current_user.username == post.author: if form.validate_on_submit(): post.heading = form.heading.data post.body = form.body.data post.timestamp = datetime.utcnow() db.session.commit() flash('Your article has been successfully updated !', 'success') return redirect(url_for('dashboard')) elif request.method == 'GET': form.heading.data = post.heading form.body.data = post.body return render_template('user/edit_article.html', post=post, title='Edit Article', form=form) else: flash( 'You are not the author of this article.\n If you want any changes in this article please contact Administrator through Contact Page.', 'error') return redirect(url_for('blog'))
def get_latest_post(): post = Post.query.order_by(Post.timestamp.desc()).first() if post: return jsonify(map_post_dto(post)) else: post = Post() post.timestamp = datetime.now() post.title = 'Coming soon' post.body = 'Voorlopig is er nog niets gepost!' return jsonify(map_post_dto(post))
def test_post_cascade_reaction(self): post = Post(title='test2') post.timestamp = datetime.now() post.body = 'test message' post.user_id = 1 db.session.add(post) db.session.commit() user = User.query.filter_by(id=1).first() db.session.delete(user) db.session.commit() post = Post.query.filter_by(title='test2').first() self.assertIsNone(post)
def add_post(): form = AddpostForm() post = Post() if form.validate_on_submit(): post.user_id = current_user.id post.body = form.body.data # post.timestamp = datetime.utcnow() db.session.add(post) db.session.commit() flash('Cons, new post successfully') return redirect(url_for('add_post')) return render_template('add_post.html ', title='Add Post', form=form)
def new_post(): if current_user.is_authenticated: form = PostForm() if form.validate_on_submit(): post = Post(author=current_user) post.title = form.title.data post.body = form.body.data db.session.add(post) db.session.commit() flash('You added a post') return redirect(url_for('post_list')) return render_template('new_post.html', title='Add new post', form=form)
def addPost(): post = Post() #print('post recieved') incoming = request.get_json() #print(incoming['username']) #print(incoming['body']) post.body = incoming['body'] post.user_id = incoming['username'] db.session.add(post) db.session.commit() app.logger.info('post created by %s added',post.user_id) return render_template('post.html',title='Home', post=post)
def user(username): user = User.query.filter_by(username=username).first_or_404() posts = user.followed_posts() form = LogActivity() form2 = EmptyForm() #posts = current_user.followed_posts().all() if form.validate_on_submit(): post = Post(action=form.action.data, item=form.item.data, body=form.comment.data, author=current_user) post.calculate_points() if form.action.data != 'have something else to share': if post.points == 1: points_str = 'point' else: points_str = 'points' if post.item is None or len(form.item.data) == 0: post.body = ' says: "I {}." They have earned {} {}!'.format( post.action.lower(), str(post.points), points_str) else: post.body = ' says: "I {} {}." They have earned {} points!'.format( post.action.lower(), post.item, str(post.points)) if form.comment.data is not None or len(form.comment.data) > 0: post.body = post.body + '\n' + form.comment.data post.timestamp = datetime.now().strftime("%H:%M:%S %d-%m-%Y ") user.points += post.points db.session.add(post) db.session.commit() form = LogActivity() return redirect(request.url) return render_template("user.html", user=user, posts=posts, form=form, points=user.points)
def new_post(): form = EditPostForm() if form.validate_on_submit(): post = Post() post.title = form.title.data post.body = form.body.data post.timestamp = datetime.now() post.user_id = current_user.id db.session.add(post) db.session.commit() flash('Your blog post has been created!') return redirect(url_for('blog')) return render_template("create_post.html", form=form)
def save(): form = BlogPostForm() if form.validate_on_submit(): if request.form['action'] == 'draft': print('Saving to redis') redis_client.set(form.title.data, form.body.data) else: print('Saving to postgres') model = Post() model.title = form.title.data model.body = form.body.data model.date = form.date.data model.author = form.author.data db.session.add(model) db.session.commit() return render_template('new.html', form=form)
def new_post(): form = PostForm() post = Post() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.author = current_user db.session.add(post) db.session.commit() tag_ids = form.tags.data for tag_id in tag_ids: post_tags = PostTags(post_id=post.id, tag_id=tag_id) db.session.add(post_tags) flash('文章已发布.') return redirect(url_for('.post', title=post.url_title)) return render_template('edit_post.html', form=form, is_new=True)
def edit(post_id): form = EditPostForm() post = Post().query.get(post_id) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data db.session.commit() flash('Post was successfully updated!') return redirect(url_for('posts.edit', post_id=post_id)) elif request.method == 'GET': form.title.data = post.title form.body.data = post.body return render_template('posts/edit_post.html', title='Edit Post', form=form, post_id=post.id)
def edit_post(pid=0): form = PostForm() post = Post.query.filter_by(id=pid).first() if form.validate_on_submit() and current_user.role == ROLE_ADVISER: if post is None: post = Post(title=form.title.data, body=form.body.data, timestamp=datetime.utcnow(), user_id=current_user.id) else: post.title = form.title.data post.body = form.body.data post.timestamp = datetime.utcnow() post.user_id = user.id file = form.fileName.data if file and allowed_file(file.filename): filename = secure_filename(file.filename) file_path = op.join(UPLOAD_HOUSE_FOLDER, filename) file.save(file_path) post.img = op.join('/static/house_photo/', filename) if post.img is None: post.img = op.join('/static/house_photo/', 'house_default.jpeg') post.location = form.location.data post.price = form.price.data post.style = form.style.data post.bedroom_no = form.bedroom_no.data post.bathroom_no = form.bathroom_no.data post.garage_no = form.garage_no.data post.address = form.address.data post.coordinate = map_address(post.address + " " + post.location) db.session.add(post) db.session.commit() flash("Your post is alive now. ") return redirect(url_for('.user', nickname=current_user.nickname)) elif request.method != "POST": form = PostForm(obj=post) return render_template('edit_post.html', form=form)
def edit(id=0): form = PostForm() if id == 0: post = Post(author_id=current_user.id) else: post = Post.query.get_or_404(id) if form.validate_on_submit(): post.body = form.body.data post.title = form.title.data db.session.add(post) db.session.commit() return redirect(url_for('.post', id=post.id)) title = _('添加新文章') if id > 0: title = _('编辑 - %(title)', title=post.title) return render_template('posts/edit.html', title=title, form=form, post=post)
def update(post: Post, post_dict: dict): if 'date' in post_dict.keys(): post.published_at = datetime.strptime(post_dict['date'], "%Y-%m-%d") if 'html_url' in post_dict.keys(): post.html_url = post_dict['html_url'] if 'category' in post_dict.keys(): try: category = Category.objects.get(name=post_dict['category']) except Category.DoesNotExist: category = Category(name=post_dict['category']) category.save() post.category = category if 'preface' in post_dict.keys(): post.preface = post_dict['preface'] if 'thumbnail' in post_dict.keys(): post.thumbnail = post_dict['thumbnail'] post.body = post_dict['body'] return post
def index(): if request.method == 'POST': form = PostForm(request.form) if form.validate_on_submit(): post = Post() post.name = form.name.data post.body = form.body.data post.title = form.title.data db.session.add(post) db.session.commit() posts = Post.query.all() form = PostForm() return render_template( 'top.html', posts=posts, form=form, )
def test_comment_cascade_reaction(self): post = Post(title='test2') post.timestamp = datetime.now() post.body = 'test message' post.user_id = 1 db.session.add(post) db.session.commit() comment = Comment(user_id=1, post_id=1) comment.date = datetime.now() comment.text = 'test' db.session.add(comment) db.session.commit() # delete user first user = User.query.filter_by(id=1).first() db.session.delete(user) db.session.commit() # post is also deleted post = Post.query.filter_by(title='test2').first() self.assertIsNone(post) # comment is also deleted comment = Comment.query.filter_by(id=1).first() self.assertIsNone(comment)
def truy_xuat_gia_chu(username): thong_tin_gia_chu = chuoi_HTML_gia_chu(username) # Gửi thông tin liên hệ ghi nhận lên web user = UserDb.query.filter_by(username=username).first() form_contact = PostForm() if request.method == "POST": post = Post() post.user_id = user.id post.body = form_contact.post.data post.title = user.username try: db.session.add(post) db.session.commit() flash( 'Nội dung góp ý đã cập nhật. Vui lòng xem mục "Câu hỏi liên quan" !' ) except Exception as e: flash('Phát sinh lỗi {}'.format(e)) db.session.rollback() #return redirect(url_for('index')) elif request.method == "GET": form_contact.username.data = user.username form_contact.email.data = user.email # Hiển thị ngày tháng năm ngay_thang_nam = date.today().strftime('%d/%m/%Y') #Hiển thị các bài post liên quan đến user user_id = user.id posts = xuat_post(user_id) return render_template('auth/user.html', form_contact=form_contact, Thong_Tin_Gia_Chu=Markup(thong_tin_gia_chu), NgayThangNam=ngay_thang_nam, POST=posts)
def publish(user_id): form = PublishBlogForm() posts = Post() if form.validate_on_submit(): blog_body = request.form.get("body") if not len(blog_body.strip()): flash("The content is necessray!") return redirect(url_for("publish", user_id=user_id)) posts.body = blog_body posts.timestamp = datetime.datetime.now() posts.user_id = user_id try: db.session.add(posts) db.session.commit() except: flash("Database error!") return redirect(url_for("publish", user_id=user_id)) flash("Publish Successful!") return redirect(url_for("publish", user_id=user_id)) return render_template("publish.html", form=form)
def admin_posts(): form = PostForm() posts = Post.query.all() if form.validate_on_submit(): post = Post() post.user_id = current_user.id post.title = form.title.data post.slug = form.title.data.translate(translate).replace(' ', '-').lower() post.body = form.body.data if form.featured_image.data is not None: post.featured_image = upload_file('featured_image', 'post') message = Markup( '<div class="alert alert-success alert-dismissible"><button type="button" class="close" data-dismiss="alert">×</button> {} was posted sucessfully. </div>' .format(form.title.data)) db.session.add(post) db.session.commit() flash(message) return redirect(url_for('admin_posts')) return render_template('admin/posts.html', form=form, posts=posts)
def savePost(url): savedUrl = url.replace('~', '/') r = requests.get(savedUrl) html = BeautifulSoup(str(r.content), "lxml") post = html.body.find_all('div', attrs={'class': 'tweet-content'}) newPost = Post() newPost.url = savedUrl newPost.body = html.body.find_all('div', attrs={ 'class': 'main-tweet' })[0].find_all('div', attrs={'class': 'tweet-content'})[0].text newPost.username = html.body.find('a', 'username').text.replace("@", "") newPost.timestamp = html.body.find_all('p', attrs={'class': 'tweet-published'})[0].text newPost.user_id = current_user.id try: db.session.add(newPost) db.session.commit() except: flash( "Post could not be saved. Either it was already saved or there was an error." ) return redirect(url_for('index'))
c.slug = categoria['slug'] c.css_class = categoria['css_class'] db.session.add(c) db.session.commit() for user in users: u = User() u.id = user['id'] u.username = user['username'] u.email = user['email'] u.social_id = user['social_id'] u.bio = user['bio'] u.pic = user['pic'] db.session.add(u) db.session.commit() for post in posts: p = Post() p.id = post['id'] p.title = post['title'] p.body = post['body'] p.user_id = post['user_id'] p.categoria_id = post['categoria_id'] p.publish_date = post['publish_date'] p.pic = post['pic'] p.abstract = post['abstract'] p.featured = post['featured'] p.start_page = post['start_page'] db.session.add(p) db.session.commit()