Skip to content

Commit

Permalink
Hide posts, courses that are not live form SEO
Browse files Browse the repository at this point in the history
  • Loading branch information
nilandev committed Jan 11, 2024
1 parent 0f1013f commit c44a732
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
23 changes: 23 additions & 0 deletions bloggy/templates/errors/403.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% extends "base-with-header.html" %}
{% load static %}
{% load custom_widgets %}
{% load define_action %}
{% block content %}
<div class="container py-5">
<p style="font-weight: 400;" class="h2">{{ errorCode }}
<span style="font-weight: 300;" class="text-muted">{{ errorMessage }}</span>
</p>
<p style="max-width:600px; font-weight: 300;" class="h3 text-muted">{{ errorDescription }}.
Please do return to our <a href="{% url 'index' %}">homepage</a> and continue learning.</p>

<div class="row align-items-start my-5">
<div class="col-6">
<h3 class="h3 fw-normal mb-2">Here are some of the useful links:</h3>
{% include "errors/error_page_links.html" %}
</div>
<div class="col-4">
{% include "errors/search_widget.html" %}
</div>
</div>
</div>
{% endblock content %}
18 changes: 15 additions & 3 deletions bloggy/views/courses_view.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.http import Http404
from django.http import Http404, HttpResponseForbidden
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -50,8 +50,20 @@ class CourseDetailsView(HitCountDetailView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)

# check if article is published? if live no issues.
if self.object.publish_status == "DRAFT":
logged_user = self.request.user

# If not live, check for the context parameter and the user login status
# If user is the owner of the post or user is an admin, can preview the post
if not logged_user:
raise HttpResponseForbidden("You do not have permission to view this page.")
if not (logged_user.username.__eq__(self.object.author.username) or logged_user.is_superuser):
raise HttpResponseForbidden("You do not have permission to view this page.")

set_seo_settings(post=self.object, context=context)
context["posts"] = Post.objects.filter(course=self.object).order_by(
context["posts"] = Post.objects.filter(course=self.object).filter(publish_status="LIVE").order_by(
"display_order").all()
return context

Expand All @@ -66,7 +78,7 @@ class LessonDetailsView(TemplateView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
guide_slug = context["course"]
post = Post.objects.filter(course__slug=guide_slug).filter(slug=context["slug"]).order_by(
post = Post.objects.filter(course__slug=guide_slug).filter(slug=context["slug"], publish_status="LIVE").order_by(
"display_order").first()
if not post:
raise Http404
Expand Down
6 changes: 3 additions & 3 deletions bloggy/views/posts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.http import HttpResponse
from django.http import HttpResponseForbidden
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -61,9 +61,9 @@ def get_context_data(self, **kwargs):
# If not live, check for the context parameter and the user login status
# If user is the owner of the post or user is an admin, can preview the post
if not logged_user:
raise HttpResponse('Unauthorized', status=401)
raise HttpResponseForbidden("You do not have permission to view this page.")
if not (logged_user.username.__eq__(self.object.author.username) or logged_user.is_superuser):
raise HttpResponse('Unauthorized', status=401)
raise HttpResponseForbidden("You do not have permission to view this page.")

context = super().get_context_data(**kwargs)
set_seo_settings(post=self.object, context=context)
Expand Down
18 changes: 15 additions & 3 deletions bloggy/views/quizzes_view.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.http import HttpResponseForbidden
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_page
from django.views.decorators.vary import vary_on_cookie
Expand Down Expand Up @@ -25,9 +26,9 @@ def get_context_data(self, **kwargs):
return context


@method_decorator(
[cache_page(settings.CACHE_TTL, key_prefix="quiz_single"), vary_on_cookie],
name='dispatch'
@method_decorator([
cache_page(settings.CACHE_TTL, key_prefix="quiz_single"),
vary_on_cookie],name='dispatch'
)
class QuizDetailView(HitCountDetailView):
model = Quiz
Expand All @@ -38,5 +39,16 @@ def dispatch(self, request, *args, **kwargs):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# check if article is published? if live no issues.
if self.object.publish_status == "DRAFT":
logged_user = self.request.user

# If not live, check for the context parameter and the user login status
# If user is the owner of the post or user is an admin, can preview the post
if not logged_user:
raise HttpResponseForbidden("You do not have permission to view this page.")
if not (logged_user.username.__eq__(self.object.author.username) or logged_user.is_superuser):
raise HttpResponseForbidden("You do not have permission to view this page.")

set_seo_settings(post=self.object, context=context)
return context

0 comments on commit c44a732

Please sign in to comment.