def post_edit(request, pk): 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") post = get_object_or_404(Post, blog=blog, pk=pk) if request.method == "POST": form = PostForm(request.user, request.POST, instance=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() else: form = PostForm(request.user, instance=post) return render( request, 'dashboard/post_edit.html', { 'form': form, 'blog': blog, 'post': post, 'root': get_root(blog.subdomain), })
def dashboard(request): try: blog = Blog.objects.get(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 = BlogForm(request.POST, instance=blog) if form.is_valid(): blog_info = form.save(commit=False) blog_info.save() else: form = BlogForm(instance=blog) except Blog.DoesNotExist: blog = Blog(user=request.user, title=f"{request.user.username}'s blog", subdomain=slugify(f"{request.user.username}-new"), content="Hello World!", created_date=timezone.now()) blog.save() form = BlogForm(instance=blog) return render(request, 'dashboard/dashboard.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain) })
def dashboard(request): try: blog = Blog.objects.get(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 = BlogForm(request.POST, instance=blog) if form.is_valid(): blog_info = form.save(commit=False) blog_info.save() else: form = BlogForm(instance=blog) return render(request, 'dashboard/dashboard.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain) }) except Blog.DoesNotExist: if request.method == "POST": form = BlogForm(request.POST) if form.is_valid(): blog = form.save(commit=False) blog.user = request.user blog.created_date = timezone.now() blog.save() return render(request, 'dashboard/dashboard.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain), }) return render(request, 'dashboard/dashboard.html', {'form': form}) else: form = BlogForm() return render(request, 'dashboard/dashboard.html', {'form': form})
def resolve_address(request): http_host = request.META['HTTP_HOST'] sites = Site.objects.all() if any(http_host == site.domain for site in sites): # Homepage return False elif any(site.domain in http_host for site in sites): # Subdomained blog blog = get_object_or_404( Blog, subdomain=tldextract.extract(http_host).subdomain) return {'blog': blog, 'root': get_root(blog.subdomain)} else: # Custom domain blog return { 'blog': get_object_or_404(Blog, domain=http_host), 'root': http_host }
def domain_edit(request): blog = Blog.objects.get(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 = DomainForm(request.POST, instance=blog) if form.is_valid(): blog_info = form.save(commit=False) blog_info.save() else: form = DomainForm(instance=blog) return render(request, 'dashboard/domain_edit.html', { 'form': form, 'blog': blog, 'root': get_root(blog.subdomain), })