def post_comment(request, postid): # add comment notification notification_add( "comment", request.user, CustomUser.objects.get(id=Post.objects.get(id=postid).username_id), Post.objects.get(id=postid)) if Comment.objects.filter(post_id=postid): for comment in Comment.objects.filter(post_id=postid): notification_add("comment_on_following", request.user, CustomUser.objects.get(id=comment.username_id), Post.objects.get(id=postid)) # add new comment Comment(body=request.POST['body'], post_id=postid, username_id=request.user.id, time=datetime.datetime.now(), title=request.POST['title']).save() comments_dic = {} for comment in Comment.objects.filter(post_id=postid): writer = User.objects.get(id=comment.username_id) comments_dic[comment] = writer return HttpResponse( render(request, 'comment.html', { 'comments': comments_dic, }))
def add_comment(request): check = _check_post_request(request, ['comment', 'post_id']) if check[0]: new_comment = Comment() new_comment.poster = request.user new_comment.text = request.POST['comment'] try: post = Post.objects.get(pk=request.POST['post_id']) new_comment.post = post except Post.DoesNotExist: return HttpResponseBadRequest("There is no Post with id {}".format( request.POST['post_id'])) new_comment.save() return HttpResponseRedirect(reverse('social:home')) else: return HttpResponseBadRequest(check[1])
def _create_comments(self, *args, **options): # load data fake_news_path = (Path(__file__).resolve().parent.parent / 'data' / 'fake_news.csv').resolve() csv.field_size_limit(sys.maxsize) no_of_commnets = options['no_of_comments'][0] fake_news_dict = csv.DictReader(open(str(fake_news_path))) fake_news_list = [] for row in fake_news_dict: if len(row['title']) < 1000 and len(row['text']) < 1000: fake_news_list.append(row) users = User.objects.all() images = ImageConversion.objects.all() for i in range(0, no_of_commnets): # take a random user user = random.choice(users) # take a random upload image = random.choice(images) try: # take a random comment pos = randint(0, len(fake_news_list) - 1) comment_str = fake_news_list[pos]['title'] if ( len(fake_news_list[pos]['title']) > 0 ) else fake_news_list[pos]['text'] fake_news_list.pop(pos) # create a record comment = Comment(author=user, image=image, text=comment_str) comment.save() print( 'Successfully created {}\'s comment for upload {} of user {}' .format(user, image.title, image.author)) except Exception as e: print( 'Could not create {}\'s comment for upload {} of user {}'. format(user, image.title, image.author)) print(e)
def comment(postId): """ Social post method to post a comment """ comment_form = CommentForm() if comment_form.validate_on_submit(): form_comment = comment_form.comment.data comment = Comment(postId, current_user.id, datetime.utcnow(), form_comment) db.session.add(comment) db.session.flush() # increment num_comments post = Post.query.filter_by(id=postId).first() post.num_comments += 1 db.session.commit() if request.args.get('current_page') == 'View': return redirect(url_for('show_social_article', postId=postId)) return redirect(url_for('social'))
data = json.load(dados) for i in data['users']: profile = Profile() profile.name = i['name'] profile.username = i['username'] profile.email = i['email'] profile.street = i['address']['street'] profile.suite = i['address']['suite'] profile.city = i['address']['city'] profile.zipcode = i['address']['zipcode'] profile.save() for j in data['posts']: post = Post() user = Profile.objects.get(id=j['userId']) post.user = user post.title = j['title'] post.body = j['body'] post.save() for k in data['comments']: commet = Comment() post = Post.objects.get(id=k['postId']) commet.post = post commet.name = k['name'] commet.email = k['email'] commet.body = k['body'] commet.save() dados.close()