def new_post(request): post_form = NewPostForm(request.POST or None, files=request.FILES or None) if post_form.is_valid(): instance_form = post_form.save(commit=False) instance_form.author = request.user instance_form.save() return redirect("index") return render(request, "new_post.html", {"form": post_form})
def dashboard(request, username): user = User.objects.get(username=username) profile = Profile.objects.get(user=user) postSubmission = NewPostForm(request.POST) allusers = Profile.objects.exclude( friends__user__username__exact=user.username) posts = Post.objects.filter(author__in=profile.friends.all()).all().union( Post.objects.all().filter( author__user__username=username)).order_by('-shareDate') context = { 'profile': profile, 'username': user.username, 'name': profile.name, 'bio': profile.bio, 'freinds': profile.friends.all(), 'allusers': allusers.exclude(user__username=username).exclude(user__username='******'), 'avatar': profile.profile_photo, 'posts': posts, 'posting': postSubmission, # 'p':Post.objects.all().exclude(author__user__username__in=profile.friends.all())[1], } return render(request, 'accounts/dashbord.html', context)
def NewPost(request): user = request.user tags_objs = [] files_objs = [] if request.method == 'POST': form = NewPostForm(request.POST, request.FILES) if form.is_valid(): picture = form.cleaned_data.get('picture') caption = form.cleaned_data.get('caption') tags_form = form.cleaned_data.get('tags') subject = form.cleaned_data.get('subject') tags_list = list(tags_form.split(', ')) for tag in tags_list: t, created = Tag.objects.get_or_create(title=tag) tags_objs.append(t) if not picture: p, created = Post.objects.get_or_create(subject=subject, caption=caption, user=user) p.tags.set(tags_objs) p.save() else: p, created = Post.objects.get_or_create(picture=picture, subject=subject, caption=caption, user=user) p.tags.set(tags_objs) p.save() return redirect('index') else: form = NewPostForm() context = { 'form': form, } return render(request, 'new_post.html', context)
def post(self, request): new_post = Post(author=request.user) form = NewPostForm(request.POST, request.FILES, instance=new_post, user=request.user) if form.is_valid(): new_post = form.save() messages.success(request, 'Post {0} created successfully!'.format(new_post.title)) form = NewPostForm(user=request.user) return render(request, 'posts/new-post.html', {'form': form})
def post(self, request): if request.user.is_authenticated is False: return redirect('home') form = NewPostForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') url = form.cleaned_data.get('url') introduction = form.cleaned_data.get('intro') body = form.cleaned_data.get('body') categories = form.cleaned_data.get('categories') publication_date = form.cleaned_data.get('pub_date') post = Post(owner=User.objects.get(username=request.user.username), title=title, url=url, introduction=introduction, body=body, publication_date=publication_date) post.save() post.categories.set(categories) url = request.GET.get('next', 'home') return redirect(url) return self.render_template_with_form(request, form)
def post_edit(request, username, post_id): post = get_object_or_404(Post, author__username=username, id=post_id) post_form = NewPostForm(request.POST or None, files=request.FILES or None, instance=post) if request.user != post.author: return redirect("post", username=username, post_id=post_id) if post_form.is_valid(): post_form.author = request.user post_form.save() return redirect("post", username=username, post_id=post_id) return render(request, "new_post.html", {"form": post_form, "post": post})
def group_detail(request, pk): try: user = request.user group = Group.objects.get(pk=pk) posts = Post.objects.filter(group=group) users = User.objects.filter(id__in=group.users.all())[:4] discussions = Discussion.objects.filter(group=group)[:3] if group.is_private: button_status = 'none' if user not in group.users.all(): button_status = 'not_in_group' if len( GroupJoinRequest.objects.filter(from_user=request.user, to_group=group)) == 1: button_status = 'group_join_request_sent' if user in group.users.all(): button_status = 'joined' else: button_status = 'none' if request.method == 'POST': if request.user in group.owners.all( ) or request.user == group.admin: form = NewPostForm(request.POST, request.FILES) if form.is_valid(): new_post = form.save(commit=False) new_post.group = group new_post.save() return redirect('group_detail', pk=group.pk) else: return HttpResponse('Action not allowed') else: form = NewPostForm() return render( request, 'groups/group_detail.html', { 'group': group, 'form': form, 'posts': posts, 'button_status': button_status, 'users': users, 'discussions': discussions, 'title': group.title }) except Exception as ex: return HttpResponse(ex)
def get(self, request): form = NewPostForm(user=request.user) return render(request, 'posts/new-post.html', {'form': form})
def get(self, request): if request.user.is_authenticated is False: return redirect('home') form = NewPostForm() return self.render_template_with_form(request, form)
def profile(request, pk): try: user = User.objects.get(pk=pk) title = user.name if request.user in user.blocked_users.all(): return HttpResponse('This user blocked you.') else: online_users = see_online_users() friends = user.friends.all() friends_for_display = friends[:5] images = Image.objects.filter( album__user=user).order_by('-added_at')[:8] groups_for_display = Group.objects.filter(users__id=user.pk)[:3] user_posts = Post.objects.filter(user=user) profile_posts = Post.objects.filter(profile=user) posts = user_posts.union(profile_posts).order_by( 'date_posted').reverse() posts_paginator = Paginator(posts, 5) page_number = request.GET.get('page') page_obj = posts_paginator.get_page(page_number) # Check if user is friend: button_status = 'none' if user not in request.user.friends.all(): button_status = 'not_friend' if len( FriendRequest.objects.filter(from_user=request.user, to_user=user)) == 1: button_status = 'friend_request_sent' if user in request.user.friends.all(): button_status = 'friend' if request.method == 'POST': form = NewPostForm(request.POST, request.FILES) if form.is_valid(): new_post = form.save(commit=False) new_post.user = request.user new_post.profile = user if request.user == user: new_post.is_private = False else: new_post.is_private = True new_post.save() if request.user != user: notification = Notification.objects.create( type='P', text='New Post in profile!', user=user, post=new_post) notification.save() return redirect(request.META.get('HTTP_REFERER', '/')) else: form = NewPostForm(instance=None) return render( request, 'user_profile/profile.html', { 'user': user, 'friends': friends, 'friends_for_display': friends_for_display, 'groups_for_display': groups_for_display, 'images': images, 'posts': posts, 'form': form, 'button_status': button_status, 'online_users': online_users, 'page_obj': page_obj, 'title': title }) except Exception as ex: return HttpResponse(ex)
def get(self, request): form = NewPostForm() return render(request, 'posts/new_post.html', {'form': form})