def write(request): form = Article_form() if request.method == 'POST': title = request.POST.get('title') content = request.POST.get('content') category = Category.objects.get(id=1) article = Article(title=title, content=content, category=category) article.save() return HttpResponseRedirect('/myblog') return render(request, 'myblog/write.html', {'form': form})
def init_db(): db.drop_all() db.create_all() # adding test user data hashed_pass1 = bcrypt.generate_password_hash('password') hashed_pass2 = bcrypt.generate_password_hash('testing') user = User(username='******', email='*****@*****.**', password=hashed_pass1) user1 = User(username='******', email='*****@*****.**', password=hashed_pass2) db.session.add(user) db.session.add(user1) db.session.commit() # adding testing project data project = Project(title='test', content='test', user_id=user.id, id=1) project1 = Project(title='test', content='test', user_id=user1.id, id=2) db.session.add(project) db.session.add(project1) db.session.commit() #adding test article data article = Article(title='test', content='test', user_id=user.id, id=1) article1 = Article(title='test', content='test', user_id=user1.id, id=2) db.session.add(article) db.session.add(article1) db.session.commit() #adding test comment data reply = Reply(content='test_comment', username=user.username, project_id=project.id) db.session.add(reply) db.session.commit() #adding test reply data tore = Tore(content='test_reply', username=user.username, reply_id=reply.id) db.session.add(tore) db.session.commit() # tests happen HERE! yield db db.drop_all()
def post_add(request): user_id = request.session["user_id"] if (user_id == None): return HttpResponseRedirect("/login") if (request.method == 'POST'): form = ArticleForm(request.POST) if form.is_valid(): cd = form.cleaned_data post = Article(title=cd["title"], content=cd["content"], tags=cd["tags"], types=cd["types"], author=User(id=user_id)) post.save() return HttpResponseRedirect("/articles/%d" % post.id) else: form = ArticleForm() return render_to_response("article/post_edit.html", {'form': form}, context_instance=RequestContext(request))
def init_db(scope="session"): db.session.close() db.drop_all() db.create_all() # adding test user data hashed_pass1 = bcrypt.generate_password_hash("password").decode("utf-8") hashed_pass2 = bcrypt.generate_password_hash("testing").decode("utf-8") user = User(username="******", email="*****@*****.**", password=hashed_pass1) user1 = User(username="******", email="*****@*****.**", password=hashed_pass2) db.session.add(user) db.session.add(user1) db.session.commit() # adding testing project data project = Project(title="test", content="test", user_id=user.id) project1 = Project(title="test", content="test", user_id=user1.id) db.session.add(project) db.session.add(project1) db.session.commit() #adding test article data article = Article(title="test", content="test", user_id=user.id) article1 = Article(title="test", content="test", user_id=user1.id) db.session.add(article) db.session.add(article1) db.session.commit() #adding test comment data reply = Reply(content="test_comment",username=user.username, project_id=project.id) db.session.add(reply) db.session.commit() #adding test reply data tore = Tore(content="test_reply",username=user.username, reply_id=reply.id) db.session.add(tore) db.session.commit() # tests happen HERE! yield db db.session.close() db.drop_all()
def fabu(request): if not isinstance(request.user, User): return redirect(to='myblog_index') userID = request.user userinfo = Userinfo.objects.get(belong=userID) if request.method == 'GET': form = Article_Form if request.method == 'POST': form = Article_Form title = request.POST['title'] img = request.FILES['suolvetu'] content = request.POST['content'] new_article = Article(title=title, content=content, img=img) new_article.save() return redirect('/mycms/list') context = { 'userinfo': userinfo, 'form': form } return render(request, 'mycms/fabu.html', context)
def post_add(request): user_id = request.session["user_id"] if (user_id == None): return HttpResponseRedirect("/login") if (request.method == 'POST'): form = ArticleForm(request.POST) if form.is_valid(): cd = form.cleaned_data post = Article( title=cd["title"], content=cd["content"], tags=cd["tags"], types=cd["types"], author=User(id=user_id) ) post.save() return HttpResponseRedirect("/articles/%d" % post.id) else: form = ArticleForm() return render_to_response("article/post_edit.html", {'form': form}, context_instance=RequestContext(request))
def new_article(): article_form = ArticleForm() username = User.query.filter_by( username=current_user.username).first_or_404() if article_form.validate_on_submit(): post = Article(title=article_form.title.data, content=article_form.content.data, author=current_user) db.session.add(post) db.session.commit() flash("Your article has been created", 'success') return redirect(url_for('main.article', user=current_user.username)) return render_template('create_article.html', title="New Article", form=article_form, user=username, legend='Upload a new article')
def main(): content_list = [] files = os.listdir(data_path) for name in files: f = os.path.join(data_path, name) with open(f, 'r', encoding='utf-8') as f: content = f.read() item = (name[:-4], content[:100], content) content_list.append(item) # Article.objects.all().delete() for item in content_list: print('saving article: %s' % item[0]) article = Article() article.title = item[0] article.brief_content = item[1] article.content = item[2] article.save()
def comment_add(request, id): article = get_object_or_404(Article, pk=id) #add comment if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): cd = form.cleaned_data #comment_parent=Comment(id=cd["comment_parent_id"]), post = Comment(article=Article(id=id), user=User(id=cd["user_id"]), email=cd["email"], author=cd["author"], content=cd["content"]) if cd["comment_parent_id"] != None: post.comment_parent = Comment(id=cd["comment_parent_id"]) post.save() article.comment_count += 1 article.save() print("%r" % form.errors) return HttpResponseRedirect("/articles/%d#comment" % int(id))
def test_was_published_not_recently(self): old_article = Article("21255", pub_date=timezone.now() - datetime.timedelta(days=30), author=self.joe) self.assertEqual(old_article.was_published_recently(), False)
def test_was_published_recently(self): recent_article = Article("21255", pub_date=timezone.now() - datetime.timedelta(hours=1), author=self.joe) self.assertEqual(recent_article.was_published_recently(), True)
def test_was_published_with_future_date(self): future_article = Article("21255", pub_date=timezone.now() + datetime.timedelta(days=30), author=self.joe) self.assertEqual(future_article.was_published_recently(), False)
def new_article(): article = Article(title="test",content="test",user_id=1,id=1) return article
def write(request): id = request.POST.get('id') title = request.POST.get('title') desc = request.POST.get('desc') author = request.session.get('username', None) content = request.POST.get('content') wordcount = request.POST.get('words') updatetime = datetime.now() username = request.session.get('username', None) user = User.objects.get(username=username) if id: article = Article.objects.get(id=id) else: article = Article() article.createtime = datetime.now() article.user = user article.title = title article.desc = desc article.author = author article.content = content article.wordcount = wordcount article.updatetime = updatetime # 添加字数 user.words = user.words + wordcount result = {} try: article.save() user.save() result['status'] = 200 except Exception: result['status'] = 500 return HttpResponse(json.dumps(result), content_type="application/json")
def add_article(request): if request.method == 'GET': col_list = Columns.objects.all() c_id = request.GET.get('c_id') if c_id: return render(request, 'add-article.html', { 'col_list': col_list, 'c_id': c_id, 'flag': 1 }) else: return render(request, 'add-article.html', { 'col_list': col_list, 'flag': 1 }) if request.method == 'POST': title = request.POST.get('title') keywords = request.POST.get('keywords') describe = request.POST.get('describe') category_id = int(request.POST.get('category')) content = request.POST.get('content') state = request.POST.get('visibility') cover = request.FILES.get('titlepic') # user_id 登录后将数据存入session,使用中间件 user_id = request.user.id art = Article() art.title = title art.keyword = keywords art.describe = describe art.col_id = category_id art.content = content art.state = state art.title_img = cover art.user_id = user_id art.save() for t in request.POST.get('tags').split(','): if Label.objects.filter(name=t).first(): art.lab.add(Label.objects.filter(name=t).first()) # 给文章添加标签 else: Label.objects.create(name=t) art.lab.add(Label.objects.filter(name=t).first()) # 给文章添加标签 return HttpResponseRedirect(reverse('myblog:article'))