def createThread(request): response = {} try: title = request.POST.get("title") body = request.POST.get("body") except: response["result"] = "failure" response["errors"] = "Missing data from template." return HttpResponse(json.dumps(response)) t = Thread() t.name = title t.save() c = Comment() c.owner = request.user c.thread = t c.index = 0 c.body = body c.save() response["result"] = "success" response["thread_id"] = t.id return HttpResponse(json.dumps(response))
def comment(request): """ Customer leaves a comment to an image :param request: JSON string {string} cid: Customer ID {string} date: Comment date {string} txt: {string} pid: picture id :return: HttpResponse('Fail to comment'): when fail to save info HttpResponse('Comment successfully') """ req = json.loads(request.body) cid = req['cid'] date = req['date'] txt = req['txt'] pid = req['pid'] try: c = Comment(cid=cid, date=date, text=txt, img_id=pid) c.save() except Exception as e: logger = log.getLogger('django') logger.error(e) return HttpResponse("Fail to comment") return HttpResponse("Comment successfully")
def post(self, request, post_id, format=None): form = CommentForm(request.POST) if form.is_valid(): comment = CommentModel() comment.content = request.POST.get('content') comment.post = PostModel.objects.get(id=post_id) comment.author = request.user comment.save() serializer = CommentSerializer(comment) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(form.errors, status=status.HTTP_400_BAD_REQUEST)
def saveComment(request): response = {} if not request.POST: response["result"] = "failure" response["errors"] = "Incorrect request type" return HttpResponse(json.dumps(response)) body = request.POST.get("body") path_id = request.POST.get("path_id") comment_type = request.POST.get("type") ## Check all params, save comment, ## then send back data needed to update DOM if len(body) > 0 and int(path_id) > 0 and len(comment_type) > 0: try: c = Comment() c.owner = request.user c.path = SavedPath.objects.get(pk=int(path_id)) c.type = comment_type c.body = body c.save() except: response = { "result":"failure", "errors":"Error creating comment @ db level" } return HttpResponse(json.dumps(response)) else: response = { "result":"failure", "errors":"one or more blank parameters" } return HttpResponse(json.dumps(response)) response["user_name"] = request.user.profile.full_name() response["profile_pic"] = request.user.profile.default_profile_pic() response["date_created"] = helpers._formatted_date(c.created) ## need to format this response["result"] = "success" return HttpResponse(json.dumps(response))
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 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)
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()