def link_post(request): if request.method == 'POST': # form is sent form = SharedPostForm(data=request.POST) if form.is_valid(): # form data is valid cd = form.cleaned_data tags = cd['tags'] new_item = form.save(commit=False) # assign current user to the item #new_item.slug = slugify('title') new_item.user = request.user new_item.save() create_action(request.user, 'bookmarked image', new_item) for tag in tags: new_item.tags.add(tag) messages.success(request, 'Post linked successfully') # redirect to new created item detail view return redirect(new_item.get_absolute_url()) else: # build form with data provided by the bookmarklet via GET form = SharedPostForm(data=request.GET) return render(request, 'posts/link_post.html', { 'section': 'images', 'form': form })
def post_share(request, post_id): # Retrieve post by id post = get_object_or_404(Post, id=post_id, status='published') sent = False if request.method == 'POST': # Form was submitted form = EmailPostForm(request.POST) if form.is_valid(): # Form fields passed validation cd = form.cleaned_data post_url = request.build_absolute_uri(post.get_absolute_url()) subject = '{} ({}) recommends you reading "{}"'.format( cd['name'], cd['email'], post.title) message = 'Read "{}" at {}\n\n{}\'s comments: {}'.format( post.title, post_url, cd['name'], cd['comments']) send_mail(subject, message, '*****@*****.**', [cd['to']]) sent = True create_action(request.user, 'shared via email a post titled ', post) return render(request, 'posts/share.html', { 'post': post, 'form': form, 'sent': sent, 'cd': cd }) # ... send email else: form = EmailPostForm() return render(request, 'posts/share.html', { 'post': post, 'form': form, 'sent': sent })
def user_follow(request, to): user = User.objects.get(username=to) all_posts = Post.objects.filter(status="published", user=user) if request.user in user.followers.all(): Contact.objects.filter(user_from=request.user, user_to=user).delete() else: Contact.objects.get_or_create(user_from=request.user, user_to=user) create_action(request.user, 'is following', user) return render(request, 'posts/user_detail.html', { 'user': user, 'all_posts': all_posts })
def edit(request): if request.method == 'POST': profile_form = ProfileEditForm(instance=request.user.profile, data=request.POST, files=request.FILES) if profile_form.is_valid(): profile_form.save() create_action(request.user, 'updated their profile') messages.success(request, 'Profile updated successfully') else: profile_form = ProfileEditForm(instance=request.user.profile) messages.error(request, 'Error updating your profile') return render(request, 'posts/edit.html', {'profile_form': profile_form})
def add_favorite(request, post_id): if not request.user.is_authenticated: return render(request, 'posts/login.html') else: post = get_object_or_404(Post, pk=post_id) comments = post.comment_set.filter(active=True) #bookmark, created = PostFavorite.objects.get_or_create(user=request.user, post=post) bookmark = PostFavorite.objects.filter(user=request.user, post=post) if bookmark: bookmark.delete() else: bookmark = PostFavorite(user=request.user, post=post) bookmark.save() create_action(request.user, 'likes', post) return render(request, 'posts/details.html', { 'post': post, 'bookmark': bookmark, 'comments': comments })
def create_post(request, tag_slug=None): if not request.user.is_authenticated: return render(request, 'posts/login.html') else: form = PostForm(request.POST or None, request.FILES or None) if form.is_valid(): tags = form.cleaned_data['tags'] post = form.save(commit=False) post.user = request.user post.image = request.FILES['image'] post.slug = slugify(post.title) post.save() if post.status == "draft": all_posts = Post.objects.filter(user=request.user, status="draft") actions = Action.objects.filter( user=request.user).select_related( 'user', 'user__profile').prefetch_related('target') tag = None if tag_slug: tag = get_object_or_404(Tag, slug=tag_slug) all_posts = all_posts.filter(tags__in=[tag]) # return render (request, "posts/index.html", {'all_posts': posts, 'page':page, 'tag':tag}) return render( request, "posts/profile.html", { 'all_posts': all_posts, 'tag': tag, 'profile': request.user.profile, 'actions': actions }) create_action(request.user, 'created a post titled ', post) for tag in tags: post.tags.add(tag) return render(request, 'posts/details.html', {'post': post}) context = { "form": form, } return render(request, 'posts/create_post.html', context)
def register(request): form = UserForm(request.POST or None) if form.is_valid(): user = form.save(commit=False) username = form.cleaned_data['username'] password = form.cleaned_data['password'] user.set_password(password) user.save() profile = Profile() profile.user = user profile.save() create_action(user, 'has joined Viberr') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) post = Post.objects.filter(user=request.user) return render(request, 'posts/index.html', {'all_posts': post}) context = { "form": form, } return render(request, 'posts/register.html', context)
def create_comment(request, post_id): if not request.user.is_authenticated: return render(request, 'posts/login.html') else: form = CommentForm(request.POST or None, request.FILES or None) post = get_object_or_404(Post, pk=post_id) comments = post.comment_set.filter(active=True) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.user = request.user comment.save() create_action(request.user, 'commented on a post titled ', post) return render(request, 'posts/details.html', { 'post': post, 'new_comment': comment, 'comments': comments }) context = { 'post': post, 'form': form, } return render(request, 'posts/create_comment.html', context)
def publish_post(request, post_id): post = get_object_or_404(Post, id=post_id) post.status = "Published" post.save() create_action(request.user, 'created a post titled ', post) return render(request, 'posts/details.html', {'post': post})