class PostAndUserTestCase(TestCase): def setUp(self): uid = uuid.uuid4().hex self.author = User.objects.create(email="{0}@test.com".format(uid), first_name="test", last_name="user") self.comment = Comment(message="Default test embedded comment", author=self.author) self.post = Post( title="Test Article {0}".format(uid), content="I am test content", author=self.author, published=True, tags=["post", "user", "test"], comments=[self.comment], ) def tearDown(self): conn = connect(MONGO_DATABASE_NAME) conn.drop_database(MONGO_DATABASE_NAME) # To reserve database but remove test data # db = conn[MONGO_DATABASE_NAME] # db.post.remove({'title': self.post.title}) # db.user.remove({'email': self.author.email}) def test_user_required_field(self): invalid_author = User(first_name="test", last_name="user") self.assertRaises(errors.ValidationError, invalid_author.save) def test_post_save_method(self): self.post.save() self.assertEquals(self.post.creator.email, self.author.email)
class PostAndUserTestCase(TestCase): def setUp(self): uid = uuid.uuid4().hex self.author = User.objects.create(email='{0}@test.com'.format(uid), first_name='test', last_name='user') self.comment = Comment(message='Default test embedded comment', author=self.author) self.post = Post(title='Test Article {0}'.format(uid), content='I am test content', author=self.author, published=True, tags=['post', 'user', 'test'], comments=[self.comment]) def tearDown(self): conn = connect(MONGO_DATABASE_NAME) conn.drop_database(MONGO_DATABASE_NAME) # To reserve database but remove test data #db = conn[MONGO_DATABASE_NAME] #db.post.remove({'title': self.post.title}) #db.user.remove({'email': self.author.email}) def test_user_required_field(self): invalid_author = User(first_name='test', last_name='user') self.assertRaises(errors.ValidationError, invalid_author.save) def test_post_save_method(self): self.post.save() self.assertEquals(self.post.creator.email, self.author.email)
def add(request): ''' 在数据库中插入一条博客 ''' title = request.POST['title'] content = request.POST['content'] icon = request.POST['icon'] category_id = request.POST['category_id'] c = Category.objects.get(id=category_id) post = Post(title=title, content=content, icon=icon, category=c) post.save() return HttpResponse('success');
def setUp(self): uid = uuid.uuid4().hex self.author = User.objects.create(email='{0}@test.com'.format(uid), first_name='test', last_name='user') self.comment = Comment(message='Default test embedded comment', author=self.author) self.post = Post(title='Test Article {0}'.format(uid), content='I am test content', author=self.author, published=True, tags=['post', 'user', 'test'], comments=[self.comment])
def setUp(self): uid = uuid.uuid4().hex self.author = User.objects.create(email="{0}@test.com".format(uid), first_name="test", last_name="user") self.comment = Comment(message="Default test embedded comment", author=self.author) self.post = Post( title="Test Article {0}".format(uid), content="I am test content", author=self.author, published=True, tags=["post", "user", "test"], comments=[self.comment], )
def upload_view(request): if request.method == 'POST': form = PostForm(request.POST, request.FILES) if form.is_valid(): cmodel = form.save() cmodel.save() post = Post() form = PostForm(None, instance=post) return render_to_response('articles/writearticle.html', { 'post_form': form, 'author_id': request.session['author_id'] }, context_instance=RequestContext(request))
def setUp(self): uid = uuid.uuid4().hex self.author = User.objects.create( email='{0}@test.com'.format(uid), first_name='test', last_name='user' ) self.comment = Comment( message='Default test embedded comment', author=self.author ) self.post = Post( title='Test Article {0}'.format(uid), content='I am test content', author=self.author, published=True, tags=['post', 'user', 'test'], comments=[self.comment] )
def save_post_form(request): if request.method == 'POST': form = PostForm(request.POST) if form.is_valid(): #save to db post = Post() post.title = form.cleaned_data['title'] post.content = form.cleaned_data['content'] post.icon = form.cleaned_data['icon'] post.category = Category.objects.get(pk=form.cleaned_data['category']) post.save() return HttpResponseRedirect(reverse(show_post_detail, args=[post.id])) else: return HttpResponse('Failed') else: return HttpResponse('Failed')