def setUp(self): admin = AdminSite() self.ma = PostAdmin(Post, admin) for author in User.objects.all(): title = "%s's title" % author.username post = Post(title=title, author=author) post.save() self.client.login(username='******', password='******')
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1, 11): post = Post(title=f"Post {count} Title", text="foo", author=author) if count < 6: pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1, 11): post = Post(title="Post %d Title" % count, text="foo", author=author, published_date=self.now) post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1, 11): post = Post(title='Post %d Title' % count, text='foo', author=author) if count < 6: #publish the first five posts pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta= datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1,11): post = Post(title="Post %d Title" % count, text="foo", author=author) if count < 6: # publish the first five posts pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) genre1 = Genre(genre="poetry") genre1.save() for count in range(1, 11): post = Post(title="Post %d Title" % count, text="foo", author=author, genre=genre1) post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) for count in range(1, 11): title = "Post %d Title" % count # print("NEXT TITLE:", title) post = Post(title="Post %d Title" % count, text="foo", author=author) if count < 6: # print("setting published date") # create 5 records with a published date pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save()
def setUp(self): self.now = datetime.datetime.utcnow().replace(tzinfo=utc) self.timedelta = datetime.timedelta(15) author = User.objects.get(pk=1) self.category = Category(name='A Category') self.category.save() for count in range(1,11): post = Post(title="Post %d Title" % count, text="foo", author=author) if count < 6: # publish the first five posts pubdate = self.now - self.timedelta * count post.published_date = pubdate post.save() if bool(count & 1): # put odd items in category: self.category.posts.add(post)
def create_post(request): user = request.user blog_id = request.POST.get('blog_id') blog = Blog.objects.get(pk = blog_id) post_id = request.POST.get('post_id') if user not in blog.authors.all(): return HttpResponse("Sorry you are not authorized to post here") else: if post_id: post = Post.objects.get(pk=post_id) if user != post.author: return HttpResponse("Sorry you are not authorized to edit here") else: post = Post() post.author = user post.blog = blog post.title = request.POST.get('title') post.body = request.POST.get('body') tags = request.POST.get('tags') tag_names = tags.split(',') if not tags.strip(): tag_names = [constant.NO_TAG] tag_set = set(tag_names) tag_list = [] for tname in tag_set: tname_s = tname.strip() try: tag = Tag.objects.get(name__iexact=tname_s) except Tag.DoesNotExist: tag = None if not tag: tag = Tag() tag.name = tname_s tag.save() tag_list.append(tag) post.save() post.tags = tag_list; post.save() return HttpResponseRedirect(reverse('myblog:index'))
def post(self): ''' Create new post ''' post_id = None if 'id' in request.json: post_id = request.json['id'] title = request.json['title'] body = request.json['body'] tags = request.json['tags'] tags = [slugify(item) for item in tags] if (not title) or (not body): return jsonify({ 'error': 'title & body is required.', 'data': {'title': title, 'body': body, 'slug': slugify(title) } }) if post_id: post = Post.objects.get(id=post_id) else: post = Post(author=current_user.id) post.title = title post.body = body post.slug = slugify(title) post.tags = tags post.save() return jsonify({ 'success': 'success', 'post' : post, 'edit_url': post.get_edit_url() })
To run this you must first start a django shell: python manage.py shell Once the shell is up, go: run ./test_data.py It will silently populate the database. """ from myblog.models import Post from django.contrib.auth.models import User p1 = Post(title='My First Post', text='This is the first post I\'ve written') all_users = User.objects.all() p1.author = all_users[0] p1.full_clean() p1.save() p1.title = p1.title + " (updated)" p1.save() p2 = Post(title="Another post", text="The second one created", author=all_users[0]).save() p3 = Post(title="The third one", text="With the word 'heffalump'", author=all_users[0]).save() p4 = Post(title="Posters are a great decoration", text="When you are a poor college student", author=all_users[0]).save()