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 post_update_view(request,slug): unique_post = get_object_or_404(Post,slug = slug,) form = PostModelForm(request.POST or None, request.FILES or None, instance = unique_post) context = { 'form' : form } if form.is_valid(): form.save() messages.info(request, 'Successfully updated your blog post.') return redirect('/') return render(request,"posts/post_create.html",context)
def post_create_view(request): author,created = Author.objects.get_or_create( user = request.user, email = request.user.email, phone_no = 9896165054 ) form = PostModelForm(request.POST or None, request.FILES or None ) context = { 'form' : form } if form.is_valid(): form.instance.author = author form.save() return redirect('/') messages.info(request, 'Successfully created a new blog post!') return render(request,"posts/post_create.html",context)
def post_create(request): if request.method == 'POST': form = PostModelForm(request.POST, request.FILES) post = form.save(commit=False) post.author = request.user post.save() return redirect('posts:post-list') form = PostModelForm() context = {'form': form} return render(request, 'posts/post_create.html', context)
def get_context_data(self, **kwargs): context = super(UserDetailView, self).get_context_data(**kwargs) user = self.request.user following = UserProfile.objects.is_following(self.request.user, self.get_object()) context['following'] = following context['recommended'] = UserProfile.objects.recommended( self.request.user) # Pasamos el form de crear post para que aparezca en el post list context['create_form'] = PostModelForm() # Pasamos el url que me permite hacer un POST de un post context['create_url'] = reverse_lazy("post:create") user_to_display = User.objects.get(slug=self.kwargs.get('slug')) # We pass the active articles of user context['articles'] = Article.objects.active().filter( author__slug=self.kwargs.get('slug')) # We pass the active lodging offers of user context['lodging_offers'] = LodgingOffer.objects.active().filter( created_by__slug=self.kwargs.get('slug')) # We pass the active study offers of user context['educational_offers'] = StudiesOffert.objects.active().filter( created_by__slug=self.kwargs.get('slug')) # We pass the active ayni offers of user context['ayni_offers'] = AyniOffer.objects.active().filter( created_by__slug=self.kwargs.get('slug')) # We pass the active daily life offers of user context['daily_life_offers'] = DailyLifeOffer.objects.active().filter( created_by__slug=self.kwargs.get('slug')) # We pass the active entrepreneurship offers of user context[ 'entrepreneurship_offers'] = EntrepreneurshipOffer.objects.active( ).filter(created_by__slug=self.kwargs.get('slug')) # if user_to_display.user_type != "0": speaklanguages = User.objects.get(slug=self.kwargs.get('slug')) speak_languages_query = speaklanguages.speak_languages.all() context['speak_languages'] = speak_languages_query # entertainmentactivities = User.objects.get(slug=self.kwargs.get('slug')) # entertainment_activities_query = entertainmentactivities.entertainment_activities.all() # context['entertainment_activities'] = entertainment_activities_query if user.is_authenticated(): context['userprofile'] = user.profile 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 exibir_perfil(request, perfil_id): perfil = Perfil.objects.get(id=perfil_id) messages = Feedback.objects.filter(perfil=get_perfil_logado(request)) feedbacks = [] for msg in messages: feedbacks.append(msg.message) msg.delete() posts = perfil.postagens.all() ITEMS_PER_PAGE = 5 paginator = Paginator(posts, ITEMS_PER_PAGE) page = request.GET.get('page', 1) try: posts_page = paginator.get_page(page) except InvalidPage: posts_page = paginator.get_page(1) form = PostModelForm() if perfil.bloqueado: perfil.error_mensage = 'Conta bloqueada pelos administradores!' return render(request, 'perfis/perfil.html', { 'perfil_logado': get_perfil_logado(request), 'perfil': perfil }) if perfil.ativa: perfil_logado = get_perfil_logado(request) perfil_logado.convidavel = perfil_logado.pode_convidar(perfil) if perfil_logado.usuario.is_superuser: return render( request, 'perfis/perfil.html', { 'perfis': Perfil.objects.all()[:10], 'perfil': perfil, 'perfil_logado': perfil_logado, 'form': form, 'posts': posts_page, 'messages': feedbacks, 'sugestao': get_sugestao(perfil_logado) }) return render( request, 'perfis/perfil.html', { 'perfil': perfil, 'perfil_logado': perfil_logado, 'form': form, 'posts': posts_page, 'messages': feedbacks, 'sugestao': get_sugestao(perfil_logado) }) else: perfil.error_mensage = 'Conta temporariamente desativada!' return render(request, 'perfis/perfil.html', { 'perfil_logado': get_perfil_logado(request), 'perfil': perfil })
def post_update(request, id): post = get_object_or_404(Post, id=id) form = PostModelForm(instance=post) if request.method == 'POST': form = PostModelForm(request.POST, request.FILES, instance=post) if form.is_valid(): form.save() return redirect('home') return render(request, 'posts/post_create.html', { 'form': form, 'action_url': 'post_update' })
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 index(request): perfil_logado = get_perfil_logado(request) messages = Feedback.objects.filter(perfil=perfil_logado) feedbacks = [] ITEMS_PER_PAGE = 10 posts = getPosts(perfil_logado) paginator = Paginator(posts, ITEMS_PER_PAGE) page = request.GET.get('page', 1) try: posts_page = paginator.get_page(page) except InvalidPage: posts_page = paginator.get_page(1) for msg in messages: feedbacks.append(msg) msg.delete() if perfil_logado.ativa == False or perfil_logado.bloqueado == True: return render(request, 'perfis/transicao.html', { 'perfil_logado': perfil_logado, 'messages': feedbacks }) form = PostModelForm() if perfil_logado.usuario.is_superuser: return render( request, 'perfis/index.html', { 'perfis': Perfil.objects.all()[:10], 'perfil_logado': perfil_logado, 'form': form, 'posts': posts_page, 'messages': feedbacks, 'sugestao': get_sugestao(perfil_logado) }) return render( request, 'perfis/index.html', { 'perfis': Perfil.objects.filter(ativa=True, bloqueado=False), 'perfil_logado': perfil_logado, 'form': form, 'posts': posts_page, 'messages': feedbacks, 'sugestao': get_sugestao(perfil_logado) })
def post_create(request): form = PostModelForm() # form = PostForm() if request.method == 'POST': # form = PostForm(request.POST) form = PostModelForm(request.POST, request.FILES) if form.is_valid(): print("Valid data") print(form.cleaned_data) # obj = Post.objects.create(**form.cleaned_data) obj = form.save(commit=False) obj.user = request.user obj.save() return redirect('home') return render(request, 'posts/post_create.html', { "form": form, 'action_url': 'post_create' })
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)