def post_comment(request, pk): if request.method == 'POST': form = CommentModelForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.user = request.user comment.post = Post.objects.get(id=pk) comment.save() return redirect('posts:post-list')
def list_post(request): qs=Post.objects.all() profile=Profile.objects.get(user=request.user) post_form=PostModelForm() comment_form=CommentModelForm() profile=Profile.objects.get(user=request.user) post_added=False if "submit_p_form" in request.POST: post_form=PostModelForm(request.POST,request.FILES) if post_form.is_valid(): instance=post_form.save(commit=False) instance.author=profile instance.save() post_form=PostModelForm() post_added=True if "submit_c_form" in request.POST: comment_form=CommentModelForm(request.POST) if comment_form.is_valid(): instance=comment_form.save(commit=False) instance.user=profile instance.post=Post.objects.get(id=request.POST.get('post_id')) instance.save() comment_form=CommentModelForm() post_added=True context={ 'qs':qs, 'profile':profile, 'post_form':post_form, 'comment_form':comment_form, 'post_added':post_added, } return render(request,'posts/main.html',context)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user = User.objects.get(username__iexact=self.request.user) profile = Profile.objects.get(username=user) detailuser_posts_qs = Post.objects.filter(author=profile) c_form = CommentModelForm() rel_r = Relationship.objects.filter(sender=profile) rel_s = Relationship.objects.filter(receiver=profile) rel_receiver = [] rel_sender = [] for item in rel_r: rel_receiver.append(item.receiver.username) for item in rel_s: rel_sender.append(item.sender.username) context['c_form'] = c_form context['posts'] = detailuser_posts_qs context["rel_receiver"] = rel_receiver context["rel_sender"] = rel_sender context['is_empty'] = False context['posts'] = self.get_object().get_all_authors_posts() context['friends_qs'] = self.get_object().get_friends() context['len_posts'] = True if len( self.get_object().get_all_authors_posts()) > 0 else False return context
def user_detail(request, username): user_info = get_object_or_404(User, username=username) form = CommentModelForm() return render(request, 'accounts/user_detail.html', { 'user_info': user_info, 'form': form })
def post_list(request): form = CommentModelForm() posts = Post.objects.all() comments = Comment.objects.all() context = { 'posts': posts, 'form': form, 'comments': comments, } return render(request, 'posts/post_list.html', context)
def post(self, request, *args, **kwargs): comment_form = CommentModelForm() profile = Profile.objects.get(user=request.user) if 'submit_c_form' in request.POST: comment_form = CommentModelForm(request.POST or None) if comment_form.is_valid(): instance = comment_form.save(commit=False) instance.user = profile post_id = request.POST.get('post_id') instance.post = Post.objects.get(id=post_id) instance.save() comment_form = CommentModelForm() return HttpResponseRedirect(self.request.path_info)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user = User.objects.get(username__iexact=self.request.user) profile = Profile.objects.get(username=user) rel_r = Relationship.objects.filter(sender=profile) rel_s = Relationship.objects.filter(receiver=profile) rel_receiver = [] rel_sender = [] for item in rel_r: rel_receiver.append(item.receiver.username) for item in rel_s: rel_sender.append(item.sender.username) if 'submit_c_form' in self.request.POST: #COMENTÁRIOS c_form = CommentModelForm(self.request.POST) if c_form.is_valid(): instance = c_form.save(commit=False) instance.user = profile instance.post = Post.objects.get( id=self.request.POST.get('post_id')) instance.save() c_form = CommentModelForm() return redirect("/timeline/") context["rel_receiver"] = rel_receiver context["rel_sender"] = rel_sender context['is_empty'] = False if len(self.get_queryset()) == 0: context['is_empty'] = True return context
def my_profile_view(request): profile = Profile.objects.get(user=request.user) form = ProfileModelForm(request.POST or None, request.FILES or None, instance=profile) confirm = False if request.method == 'POST': if form.is_valid(): form.save() confirm = True qs = Post.objects.filter(author=profile) # initials p_form = PostModelForm() c_form = CommentModelForm() post_added = False if 'submit_p_form' in request.POST: print(request.POST) p_form = PostModelForm(request.POST, request.FILES) if p_form.is_valid(): instance = p_form.save(commit=False) instance.author = profile instance.save() p_form = PostModelForm() post_added = True if 'submit_c_form' in request.POST: c_form = CommentModelForm(request.POST) if c_form.is_valid(): instance = c_form.save(commit=False) instance.user = profile instance.post = Post.objects.get(id=request.POST.get('post_id')) instance.save() c_form = CommentModelForm() context = { 'qs': qs, 'profile': profile, 'p_form': p_form, 'c_form': c_form, 'post_added': post_added, 'form': form, 'confirm': confirm, } return render(request, 'profiles/myprofile.html', context)
def my_profile_view(request): profile = Profile.objects.get(username=request.user) form = ProfileModelForm(request.POST or None, request.FILES or None, instance=profile) friends_qs = Profile.get_friends(request.user) confirm = False no_posts = False no_friends = False posts_qs = Post.objects.filter(author=profile) if bool(posts_qs) == False: no_posts = True c_form = CommentModelForm() if bool(friends_qs) == False: no_friends = True if 'update_profile' in request.POST: if form.is_valid(): form.save() confirm = True if 'submit_c_form' in request.POST: #COMENTÁRIOS c_form = CommentModelForm(request.POST) if c_form.is_valid(): instance = c_form.save(commit=False) instance.user = profile instance.post = Post.objects.get(id=request.POST.get('post_id')) instance.save() c_form = CommentModelForm() return redirect("perfil:my-profile-view") context = { 'no_friends': no_friends, 'no_posts': no_posts, 'user_posts': posts_qs, 'friendlist': friends_qs, 'profile': profile, 'form': form, 'confirm': confirm, 'c_form': c_form, } return render(request, 'perfil/myprofile.html', context)
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) sender_profile = get_object_or_404(Profile, id=self.request.user.id) receiver_profile = get_object_or_404(Profile, id=self.object.id) frnd_btn_state = Relationship.objects.relation_status( sender=sender_profile, receiver=receiver_profile, ) context['frnd_btn_state'] = frnd_btn_state context['frnd_req_rcvd'] = Relationship.objects.invitation_recieved( receiver=sender_profile) context['form'] = CommentModelForm() context['profile'] = profile = Profile.objects.get( user=self.request.user) return context
def profile_detail_view(request, slug): context = {} user = User.objects.get(username__iexact=request.user) requester_profile = Profile.objects.get(user=user) rel_r = Relationship.objects.filter(sender=requester_profile) rel_s = Relationship.objects.filter(receiver=requester_profile) rel_receiver = [] rel_sender = [] for item in rel_r: rel_receiver.append(item.receiver.user) for item in rel_s: rel_sender.append(item.sender.user) context["rel_receiver"] = rel_receiver context["rel_sender"] = rel_sender profile = Profile.objects.get(slug=slug) qs = Post.objects.filter(author=profile) # initials p_form = PostModelForm() c_form = CommentModelForm() post_added = False if 'submit_c_form' in request.POST: c_form = CommentModelForm(request.POST) if c_form.is_valid(): instance = c_form.save(commit=False) instance.user = requester_profile instance.post = Post.objects.get(id=request.POST.get('post_id')) instance.save() c_form = CommentModelForm() context = { 'qs': qs, 'profile': profile, 'p_form': p_form, 'c_form': c_form, 'post_added': post_added, 'object': profile, 'requester_profile': requester_profile, } return render(request, 'profiles/detail.html', context)
def dashboard(request): profile = Profile.objects.get(user=request.user) qs = Post.objects.filter(author=profile) connections = Profile.objects.get_all_friends_profile(request.user) if 'search_keyword' in request.GET: q = request.GET['search_keyword'] all_posts = Post.objects.filter(content__icontains=q) else: all_posts = Post.objects.all() all_jobs = Job.objects.all() related_jobs = [] connected_posts = [] print("Connections of profile : ", profile) for post in all_posts: if post.author in connections: connected_posts.append(post) for job in all_jobs: if job.work_area == profile.work_area: related_jobs.append(job) post_form = PostModelForm() job_form = JobModelForm() comment_form = CommentModelForm() post_added = False job_added = False comment_added = False if 'submit_post_form' in request.POST: post_form = PostModelForm(request.POST, request.FILES) if post_form.is_valid(): instance = post_form.save(commit=False) instance.author = profile instance.save() post_form = PostModelForm() post_added = True if 'submit_job_form' in request.POST: job_form = JobModelForm(request.POST, request.FILES) if job_form.is_valid(): instance = job_form.save(commit=False) instance.author = profile instance.save() instance.title = job_form.cleaned_data.get('title') instance.description = job_form.cleaned_data.get('description') instance.image = job_form.cleaned_data.get('image') instance.work_area = job_form.cleaned_data.get('work_area') instance.skills.add(*job_form.cleaned_data.get('skills')) instance.salary = job_form.cleaned_data.get('salary') job_form = JobModelForm() job_added = True if 'submit_comment_form' in request.POST: comment_form = CommentModelForm(request.POST, request.FILES) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = profile comment.post = Post.objects.get(id=request.POST.get('post_id')) comment.save() comment_form = CommentModelForm() comment_added = False context = { 'qs': qs, 'profile': profile, 'post_form': post_form, 'comment_form': comment_form, 'comment_added': comment_added, 'post_added': post_added, 'connections': connections, 'connected_posts': connected_posts, 'job_form': job_form, 'job_added': job_added, 'related_jobs': related_jobs, } return render(request, 'profiles/dashboard.html', context)
def user_detail(request, username): user_info = User.objects.get(username=username) return render(request, 'accounts/user_detail.html', { 'user_info': user_info, 'comment_form': CommentModelForm() })