def blog_add(request): if (request.method == 'POST'): title = request.POST['title'] text = request.POST['text'] blog = Post(title=title, text=text) blog.save() return redirect('/blogs') else: return render(request, "blog/blog_add.html", {"active_menu": "blog"})
def post(self): title = self.request.get("subject").strip() text = self.request.get("content").strip() if title == "" or text == "": self.render(title, text, error = "You must enter a title and text.") else: p = Post(title = title, text = text) p.put() self.redirect("/%d" % p.key().id())
def addPost(request): if request.method == 'POST': title = request.POST.get('postTitle') author = request.POST.get('postAuthor') slug = request.POST.get('postSlug') content = request.POST.get('postContent') post = Post(title=title, author=author, slug=slug, content=content) post.save() return redirect('home') count = Post.objects.count() + 1 context = {'count': count} return render(request, 'home/addPost.html', context)
def test_convert_youtube_to_embed_invalidURL(self): invalid_URL1 = "dsfwtj" invalid_URL2 = "www.facebook.com" invalid_URL3 = "www.youtube.com" post4 = Post(title="post4 title", videoURL=invalid_URL1) self.assertEqual('', convert_youtube_to_embed(post4.videoURL)) post5 = Post(title="post5 title", videoURL=invalid_URL2) self.assertEqual('', convert_youtube_to_embed(post5.videoURL)) post6 = Post(title="post6 title", videoURL=invalid_URL3) self.assertEqual('', convert_youtube_to_embed(post6.videoURL))
def new(request): if request.POST: post = request.POST["post"] title = request.POST["title"] p = Post(title=title, text=post, p_date=datetime.now() ) p.save() return render( request, "Blog/postCreate.html" )
def post_g(request, blog_id): if request.method == 'GET': u = get_user(request) if u is None: return JsonResponse({'status': -1, 'message': 'invalid token'}) iddd = request.GET.get('id') p = Post.objects.filter(post_id=iddd).first() post = { 'datetime': p.time, 'id': p.post_id, 'title': p.title, 'summary': p.summary, 'text': p.text } res = {'status': 1, 'post': post} return JsonResponse(res) if request.method == 'POST': f = Post_f(request.POST) if f.is_valid(): t = f.cleaned_data['text'] title = f.cleaned_data["t"] s = f.cleaned_data["s"] blog = Super_b.objects.filter(blog_id=blog_id).first() if blog is not None: post = Post.create(blog, title, s, t) post.save() return JsonResponse({ 'status': 1, 'message': 'Successfully Added The Post' }) else: print("else")
def index(): form = PostForm() if form.validate_on_submit(): post = Post(content=form.content.data, author=current_user) db.session.add(post) db.session.commit() form.content.data = '' return render_template('index.html', form=form)
def test_convert_youtube_to_embed_validURL(self): post3 = Post(title="post3 title", content="somecontent", videoURL="http://www.youtube.com/watch?v=0EEfjUGc1Io") post3_embed = convert_youtube_to_embed(post3.videoURL) self.assertEqual( '<iframe width="560" height="315" src="//www.youtube.com/embed/0EEfjUGc1Io" frameborder="0" allowfullscreen></iframe>', post3_embed)
def new_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('home')) return render_template('create_post.html', form=form, title='New Post', legend='New Post')
def fake_posts(count=50): for i in range(count): post = Post(title=fake.sentence(), body=fake.text(2000), category=Category.query.get( random.randint(1, Category.query.count())), timestamp=fake.date_time_this_year()) db.session.add(post) db.session.commit()
def submitPost(): if request.method == "GET": return render_template('submitPost.html', year=datetime.now().year) elif request.method == "POST": newPost = Post(title = request.form["title"], userid = current_user.id, category = request.form["category"], body = request.form["content"]) db.session.add(newPost) db.session.commit() return redirect(url_for('home'))
def content_html(self): #直接渲染模板 from Blog.models import Post from Comment.models import Comment result='' if self.display_type == self.DISPLAY_HTML: result = self.content elif self.display_type == self.DISPLAY_LATEST: context={ 'posts':Post.latest_posts() } result = render_to_string('Template/default/Config/blocks/sidebar_posts.html',context) elif self.display_type == self.DISPLAY_HOT: context={ 'posts':Post.hot_posts() } result = render_to_string('Template/default/Config/blocks/sidebar_posts.html',context) elif self.display_type == self.DISPLAY_COMMENT: context={ 'comments':Comment.objects.filter(status=Comment.STATUS_NORMAL) } result = render_to_string('Template/default/Config/blocks/sidebar_comment.html', context)
class PostDetailView(CommonViewMixin, DetailView): queryset = Post.latest_posts() template_name = 'Template/new_style/Blog/detail.html' context_object_name = 'post' pk_url_kwarg = 'post_id' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) comment_list = Comment.get_by_target(self.request.path) count = comment_list.count() context.update({ 'comment_form': CommentForm, 'comment_list': comment_list, }) print(context) return context
def test_creating_posts(self): self.assertEqual("post1 title", self.post1.title) self.assertEqual('', self.post1.content) self.assertEqual('', self.post1.videoURL) #no time argument was given for post1 so dateCreated=None self.assertEqual(None, self.post1.dateCreated) post2_creation_time = timezone.now() post2 = Post(title="post2 title", content="blah", videoURL="http://www.youtube.com/watch?v=vTIsuCSFuj8", dateCreated=post2_creation_time) self.assertEqual("post2 title", post2.title) self.assertEqual("blah", post2.content) self.assertEqual("http://www.youtube.com/watch?v=vTIsuCSFuj8", post2.videoURL) self.assertEqual(post2_creation_time, post2.dateCreated)
def create_post(): form = PostForm() if form.validate_on_submit(): if form.picture.data: picture_file = save_picture_post(form.picture.data) else: picture_file = None post = Post(title=form.title.data, content=form.content.data, category=form.category.data, image_path=picture_file, author=current_user) db.session.add(post) db.session.commit() flash('Your post has been created!', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='New Post', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data category = Category.query.get(form.category.data) body = form.body.data # print("时间",form.uploadtime.data) print(type(form.uploadtime.data)) uploadtime = form.uploadtime.data if form.uploadtime.data else None post = Post(title=title, body=body, category=category, timestamp=uploadtime) db.session.add(post) db.session.commit() flash('Post created.', 'success') return redirect(url_for('blog.show_post', param=post.title)) return render_template('admin/post.html', form=form)
def add(): # app.logger.debug(request.form) if request.method == 'POST': title = request.form['title'] body = request.form['text'] pattern = re.compile(r'\n') body = re.sub(pattern, '<br />', body) draft = True if request.form.has_key( 'draft') else False # don't show drafts on the index page category = Category(request.form['category']) if request.form.has_key( 'category') else 'default' post_on = datetime.now() form_data = Post(title, body, draft, category, post_on) db.session.add(form_data) db.session.commit() else: app.logger.error('Invalid method!') return redirect(url_for('index'))
def postg(request, bid): if request.method == 'GET': id = get_user(request) if id is None: return JsonResponse({'status': -1, 'message': 'invalid token'}) pid = request.GET.get('id') p = Post.objects.filter(post_id=pid).first() post = {'datetime': p.time, 'id': p.post_id, 'title': p.title, 'summary': p.summary, 'text': p.text} res = {'status': 1, 'post': post} return JsonResponse(res) if request.method == 'POST': form = AddPostForm(request.POST) if form.is_valid(): text = form.cleaned_data['text'] title = form.cleaned_data['title'] summary = form.cleaned_data['summary'] blog = Blog.objects.filter(blog_id=bid).first() if blog is not None: post = Post.create(blog, title, summary, text) post.save() return JsonResponse({'status': 1, 'message': 'Successfully Added The Post'})
def NouPost(request): if request.method == 'POST': formulariPost = NouPostForm(request.POST) if formulariPost.is_valid(): autor = request.user data = str(date.today()) tema = formulariPost.cleaned_data['tema'] titol = formulariPost.cleaned_data['titol'] entrada = formulariPost.cleaned_data['entrada'] post = Post() post.autor = autor post.data_publicacio = data post.entrada = entrada post.tema = tema post.titol = titol post.save() messages.add_message(request, messages.INFO, 'Post publicat correctament.') return HttpResponseRedirect('/') else: messages.add_message(request, messages.INFO, 'Error publicant el post.') else: formulariPost = NouPostForm() return render(request,'Blog/nouPost.html', {'formulariPost' : formulariPost})
def upload(): if request.method == 'POST' and 'file' in request.files: f = request.files.get('file') filename = f.filename filetype = filename.split('.', 1)[1] if filetype in ['md', 'txt']: path = os.path.join(current_app.config['FILE_UPLOAD_PATH'], filename) else: path = os.path.join(current_app.config['IMAGE_UPLOAD_PATH'], filename) f.save(path) if filetype in ['md', 'txt']: with open(path, 'r', encoding='UTF-8', errors='ignore') as essay: s = essay.read() print(s) title = filename.split('.', 1)[0] new_post = Post(title=title, body=s) db.session.add(new_post) db.session.commit() return render_template('admin/upload.html')
def setup(self): self.post1 = Post(title="post1 title")
def test_str(self): title = Post(title="this is a title") self.assertEquals(str(title), "this is a title")
class IndexView(CommonViewMixin, ListView): #首页 queryset = Post.latest_posts() context_object_name = 'post_list' template_name = 'Template/default/Blog/list.html'
def test_post_user(self): #the bidirectory relationship of post_user with extra data is many to many #i use a association class Post_User #the tests must be for bidirectory print() post_titles = ['first', 'second', 'third', 'fouth', 'fifth'] posts = [Post(title=title) for title in post_titles] #如果先把posts加入,就可能会引发nullable=False错,所在,post不要先加入 #db.session.add_all(posts) #db.session.commit() micheal = User.query.filter_by(username='******').first() kfl = User.query.filter_by(username='******').first() #micheal have three posts, and is the first author of first post #请注意这时posts并没有被add到数据库,更没有commit m_first = Post_User(is_first_author=True) m_first.writer = micheal posts[0].writers.append(m_first) m_second = Post_User() m_second.writer = micheal posts[1].writers.append(m_second) m_third = Post_User() m_third.writer = micheal posts[2].writers.append(m_third) #kfl have five posts, and is the first autor of the 2~5 posts respectively kfl_first = Post_User() kfl_first.writer = kfl posts[0].writers.append(kfl_first) kfl_second = Post_User(is_first_author=True) kfl_second.writer = kfl posts[1].writers.append(kfl_second) kfl_third = Post_User(is_first_author=True) kfl_third.writer = kfl posts[2].writers.append(kfl_third) kfl_fouth = Post_User(is_first_author=True) kfl_fouth.writer = kfl posts[3].writers.append(kfl_fouth) kfl_fifth = Post_User(is_first_author=True) kfl_fifth.writer = kfl posts[4].writers.append(kfl_fifth) db.session.add_all(posts) db.session.add_all([ m_first, m_second, m_third, kfl_first, kfl_second, kfl_third, kfl_fouth, kfl_fifth ]) db.session.commit() #from user to post self.assertEqual(len(micheal.posts), 3) micreal_post_association_first_author = [] for a in micheal.posts: if a.is_first_author: micreal_post_association_first_author.append(a) micheal_post_first_author = [ a.post for a in micreal_post_association_first_author ] self.assertEqual(micheal_post_first_author[0].title, post_titles[0]) self.assertEqual(len(kfl.posts), 5) kfl_post_association_first_author = [] for a in kfl.posts: if a.is_first_author: kfl_post_association_first_author.append(a) self.assertEqual(len(kfl_post_association_first_author), 4) kfl_post_title_first_author = [ a.post.title for a in kfl_post_association_first_author ] self.assertIn(post_titles[1], kfl_post_title_first_author) self.assertIn(post_titles[2], kfl_post_title_first_author) self.assertIn(post_titles[3], kfl_post_title_first_author) self.assertIn(post_titles[4], kfl_post_title_first_author) #from post to user self.assertEqual(len(posts[0].writers), 2) self.assertEqual(len(posts[1].writers), 2) self.assertEqual(len(posts[2].writers), 2) self.assertEqual(len(posts[3].writers), 1) self.assertEqual(len(posts[4].writers), 1) post_first_first_author_association = [] for a in posts[0].writers: if a.is_first_author: post_first_first_author_association.append(a) self.assertEqual(len(post_first_first_author_association), 1) _micheal = post_first_first_author_association[0].writer self.assertEqual(_micheal.username, micheal.username)
def get(self, id): post = Post.get_by_id(int(id)) self.render("permalink.html", {"post":post})