def post(request, slug): http_host = request.META['HTTP_HOST'] if http_host == 'bearblog.dev' or http_host == 'localhost:8000': return redirect('/') elif 'bearblog.dev' in http_host or 'localhost:8000' in http_host: extracted = tldextract.extract(http_host) if is_protected(extracted.subdomain): return redirect(get_base_root(extracted)) blog = get_object_or_404(Blog, subdomain=extracted.subdomain) root = get_root(extracted, blog.subdomain) else: blog = get_object_or_404(Blog, domain=http_host) root = http_host ip_address = client_ip(request) if request.method == "POST": upvoted_pose = get_object_or_404(Post, blog=blog, slug=slug) posts_upvote_dupe = upvoted_pose.upvote_set.filter( ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=upvoted_pose, ip_address=ip_address) upvote.save() if request.GET.get('preview'): all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).all().order_by('-published_date') else: all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).filter( publish=True).order_by('-published_date') post = get_post(all_posts, slug) upvoted = False for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted = True content = markdown(post.content, extensions=['fenced_code']) return render( request, 'post.html', { 'blog': blog, 'content': content, 'post': post, 'nav': get_nav(all_posts), 'root': root, 'meta_description': unmark(post.content)[:160], 'upvoted': upvoted })
def discover(request): http_host = request.META['HTTP_HOST'] if not (http_host == 'bearblog.dev' or http_host == 'localhost:8000'): raise Http404("No Post matches the given query.") if request.method == "POST": pk = request.POST.get("pk", "") post = get_object_or_404(Post, pk=pk) ip_address = client_ip(request) posts_upvote_dupe = post.upvote_set.filter(ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=post, ip_address=ip_address) upvote.save() posts_per_page = 20 page = 0 gravity = 1.8 if request.GET.get('page'): page = int(request.GET.get('page')) posts_from = page * posts_per_page posts_to = (page * posts_per_page) + posts_per_page newest = request.GET.get('newest') if newest: posts = Post.objects.annotate(upvote_count=Count('upvote'), ).filter( publish=True, show_in_feed=True, published_date__lte=timezone.now()).order_by( '-published_date').select_related('blog')[posts_from:posts_to] else: posts = Post.objects.annotate( upvote_count=Count('upvote'), score=ExpressionWrapper( ((Count('upvote')) / ((Seconds(Now() - F('published_date'))) + 2)**gravity) * 100000, output_field=FloatField())).filter( publish=True, show_in_feed=True, published_date__lte=timezone.now()).order_by( '-score', '-published_date').select_related( 'blog')[posts_from:posts_to] return render( request, 'discover.html', { 'posts': posts, 'next_page': page + 1, 'posts_from': posts_from, 'gravity': gravity, 'newest': newest, })
def post(request, slug): address_info = resolve_address(request) if not address_info: return redirect('/') blog = address_info['blog'] ip_address = client_ip(request) if request.method == "POST": upvoted_pose = get_object_or_404(Post, blog=blog, slug=slug) posts_upvote_dupe = upvoted_pose.upvote_set.filter( ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=upvoted_pose, ip_address=ip_address) upvote.save() if request.GET.get('preview'): all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).all().order_by('-published_date') else: all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).filter( publish=True).order_by('-published_date') post = get_post(all_posts, slug) upvoted = False for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted = True content = mistune.html(post.content) return render( request, 'post.html', { 'blog': blog, 'content': content, 'post': post, 'nav': get_nav(all_posts), 'root': address_info['root'], 'meta_description': unmark(post.content)[:160], 'upvoted': upvoted })
def post(request, slug): blog = resolve_address(request) if not blog: return not_found(request) ip_address = client_ip(request) if request.method == "POST": if request.POST.get("pk", ""): # Upvoting pk = sanitise_int(request.POST.get("pk", ""), 7) post = get_object_or_404(Post, pk=pk) posts_upvote_dupe = post.upvote_set.filter(ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=post, ip_address=ip_address) upvote.save() if request.GET.get('preview'): all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).all().order_by('-published_date') else: all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).filter( publish=True).order_by('-published_date') post = get_post(all_posts, slug) upvoted = False for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted = True meta_description = post.meta_description or unmark(post.content)[:160] return render( request, 'post.html', { 'blog': blog, 'content': post.content, 'post': post, 'root': blog.useful_domain(), 'meta_description': meta_description, 'meta_image': post.meta_image or blog.meta_image, 'upvoted': upvoted })
def post(request, slug): blog = get_object_or_404(Blog, user=request.user) ip_address = client_ip(request) if request.method == "POST": if request.POST.get("pk", ""): # Upvoting pk = request.POST.get("pk", "") post = get_object_or_404(Post, pk=pk) posts_upvote_dupe = post.upvote_set.filter(ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=post, ip_address=ip_address) upvote.save() if request.GET.get('preview'): all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).all().order_by('-published_date') else: all_posts = blog.post_set.annotate( upvote_count=Count('upvote')).filter( publish=True).order_by('-published_date') post = get_post(all_posts, slug) upvoted = False for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted = True return render( request, 'post.html', { 'blog': blog, 'content': post.content, 'post': post, 'root': blog.useful_domain(), 'upvoted': upvoted })
def post_new(request): blog = get_object_or_404(Blog, user=request.user) if not resolve_subdomain(request.META['HTTP_HOST'], blog): return redirect(f"http://{get_root(blog.subdomain)}/dashboard") if request.method == "POST": form = PostForm(request.user, request.POST) if form.is_valid(): post = form.save(commit=False) post.blog = blog if not post.published_date: post.published_date = timezone.now() post.save() form.save_m2m() upvote = Upvote(post=post, ip_address=client_ip(request)) upvote.save() return redirect(f"/dashboard/posts/{post.id}/") else: form = PostForm(request.user) return render(request, 'dashboard/post_edit.html', { 'form': form, 'blog': blog })
def discover(request): http_host = request.META['HTTP_HOST'] get_object_or_404(Site, domain=http_host) ip_address = client_ip(request) if request.method == "POST": pk = request.POST.get("pk", "") post = get_object_or_404(Post, pk=pk) posts_upvote_dupe = post.upvote_set.filter(ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=post, ip_address=ip_address) upvote.save() posts_per_page = 20 page = 0 gravity = 1.2 if request.GET.get('page'): page = int(request.GET.get('page')) posts_from = page * posts_per_page posts_to = (page * posts_per_page) + posts_per_page newest = request.GET.get('newest') if newest: posts = Post.objects.annotate(upvote_count=Count('upvote'), ).filter( publish=True, show_in_feed=True, published_date__lte=timezone.now()).order_by( '-published_date').select_related('blog')[posts_from:posts_to] else: posts = Post.objects.annotate( upvote_count=Count('upvote'), score=ExpressionWrapper( ((Count('upvote')) / ((Seconds(Now() - F('published_date'))) + 2)**gravity) * 100000, output_field=FloatField()), ).filter( publish=True, show_in_feed=True, published_date__lte=timezone.now()).order_by( '-score', '-published_date').select_related( 'blog').prefetch_related('upvote_set')[posts_from:posts_to] upvoted_posts = [] for post in posts: for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted_posts.append(post.pk) return render( request, 'discover.html', { 'site': Site.objects.get_current(), 'posts': posts, 'next_page': page + 1, 'posts_from': posts_from, 'gravity': gravity, 'newest': newest, 'upvoted_posts': upvoted_posts })
def discover(request): ip_address = client_ip(request) if request.method == "POST": pk = sanitise_int(request.POST.get("pk", ""), 7) post = get_object_or_404(Post, pk=pk) posts_upvote_dupe = post.upvote_set.filter(ip_address=ip_address) if len(posts_upvote_dupe) == 0: upvote = Upvote(post=post, ip_address=ip_address) upvote.save() page = 0 if request.GET.get("page", 0): page = sanitise_int(request.GET.get("page"), 7) posts_from = page * posts_per_page posts_to = (page * posts_per_page) + posts_per_page newest = request.GET.get("newest") if newest: posts = (Post.objects.annotate(upvote_count=Count("upvote"), ).filter( publish=True, blog__reviewed=True, blog__blocked=False, show_in_feed=True, published_date__lte=timezone.now(), ).order_by("-published_date").select_related("blog") [posts_from:posts_to]) else: posts = (Post.objects.annotate( upvote_count=Count("upvote"), score=ExpressionWrapper( ((Count("upvote") - 1) / ((Seconds(Now() - F("published_date"))) + 4)**gravity) * 100000, output_field=FloatField(), ), ).filter( publish=True, blog__reviewed=True, blog__blocked=False, show_in_feed=True, published_date__lte=timezone.now(), ).order_by("-score", "-published_date").select_related( "blog").prefetch_related("upvote_set")[posts_from:posts_to]) upvoted_posts = [] for post in posts: for upvote in post.upvote_set.all(): if upvote.ip_address == ip_address: upvoted_posts.append(post.pk) return render( request, "discover.html", { "site": Site.objects.get_current(), "posts": posts, "previous_page": page - 1, "next_page": page + 1, "posts_from": posts_from, "gravity": gravity, "newest": newest, "upvoted_posts": upvoted_posts, }, )