def comment(request, slug): """ Comment on a post. :param request: :param slug: :return: """ post = get_object_or_404(Post, slug=slug) form = CommentForm(request.POST) if not form.is_valid(): return render(request, 'post/view.html', {'post': post, 'form': form}) try: anonymous_user = AnonymousUser.objects.filter( identifier=request.session.session_key).get() except AnonymousUser.DoesNotExist: anonymous_user = AnonymousUser() anonymous_user.identifier = get_anonymous_id(request) anonymous_user.ip_address = get_client_ip(request) anonymous_user.save() if not anonymous_user.is_blocked: post_comment = PostComment() post_comment.name = form.cleaned_data['name'] post_comment.comment = form.cleaned_data['comment'] post_comment.anonymous_user = anonymous_user post_comment.post = post post_comment.save() return redirect('view', slug=post.slug)
def visits(request: HttpRequest): """ Картинка про посещения """ Visit.make(request, '/visits') response = HttpResponse(content=get_counter_image( request.GET.get('path'), get_client_ip(request), ).read()) response['Content-Type'] = 'image/png' response['Content-Disposition'] = 'attachment;filename=counter.png' return response
def make(request: HttpRequest, page: str): visited = request.session.get('visited', False) Visit.objects.create( ip=get_client_ip(request), page=page, method=request.method, resolution='', is_view=not visited, os=request.user_agent.os.family, browser_version=request.user_agent.browser.version_string, user_agent=request.META.get('HTTP_USER_AGENT', ''), browser_family=request.user_agent.browser.family, device=request.user_agent.device.family, ) request.session['visited'] = True
async def real_ip_middleware(request: Request) -> None: ip: str = get_client_ip(request) request["ip"] = ip
async def get_my_ip(request: Request) -> HTTPResponse: return json({"ip": get_client_ip(request)})