def addcomment(request, pk): p = get_object_or_404(Post, pk=pk) if not request.user.is_authenticated(): if request.method == "POST": request.session['post'] = request.POST print(request.session['post']) return redirect_to_login(request.path) else: if request.method == "POST": f = CommentForm(request.POST, request.FILES) if f.is_valid(): comment = Post() comment.op = request.user comment.desc = f.cleaned_data['desc'] comment.parent = p comment.save() comment.votes.add(comment.op) while p.parent: # traverse back to topmost post p = p.parent return HttpResponseRedirect(reverse('detail', kwargs={'pk':p.id}))
def load_fixtures(): # see whether the fixtures were already loaded num_ops = 3 num_posts_per_op = 5 if (len(User.objects.all()) > num_ops and len(Post.objects.all()) > num_posts_per_op): return # gen & load 10 users (ops) for o in range(num_ops): username, email = gen_rand_username_and_email() new_op = User.objects.create_user(username, email, username) new_op.save() # gen & load 30 posts per use for p in range(num_posts_per_op): new_post = Post(op = new_op, title = gen_rand_text(random.randrange(10))) if random.choice(range(2)) > 0: new_post.desc = gen_rand_text(random.randrange(50)) else: new_post.link = gen_rand_link() new_post.save()