Skip to content

Commit

Permalink
context_processors
Browse files Browse the repository at this point in the history
  • Loading branch information
almazkun committed Oct 13, 2023
1 parent a1055fd commit 6f1dc59
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 221 deletions.
52 changes: 19 additions & 33 deletions apps/articles/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,6 @@ def test_home(self):
self.assertTemplateUsed(response, "articles/home.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=normal_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("home"))

self.assertEqual(response.context["main_author"], main_author)


class TestArticleListView(TestCase):
def setUp(self):
Expand All @@ -181,14 +173,6 @@ def test_articles(self):
self.assertTemplateUsed(response, "articles/articles.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=normal_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("articles"))

self.assertEqual(response.context["main_author"], main_author)


class TestArticleDetailView(TestCase):
def setUp(self):
Expand All @@ -207,16 +191,6 @@ def test_article_detail(self):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "articles/article_detail.html")

def test_main_author(self):
main_author = CustomUser.objects.get(username=normal_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(
reverse("article_detail", kwargs={"slug": test_article["slug"]})
)

self.assertEqual(response.context["main_author"], main_author)


class TestTagDetailView(TestCase):
"""Test Tag detail view.
Expand Down Expand Up @@ -270,13 +244,25 @@ def test_tag_detail(self):
self.assertEqual(str(response.context["products"]), str(products))
self.assertTemplateUsed(response, "articles/tag_detail.html")

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()

response = self.client.get(
reverse("tag_detail", kwargs={"slug": self.test_tag["slug"]})
class TestContextProcessors(TestCase):
def setUp(self):
CustomUser.objects.create_user(**normal_user)
test_article["author"] = CustomUser.objects.get(
username=normal_user["username"]
)
Article.objects.create(**test_article)

self.assertEqual(response.context["main_author"], main_author)
def test_context_processors(self):
key_list = [
"github_link",
"linkedin_link",
"cv_link",
"email_link",
]
response = self.client.get(
reverse("article_detail", kwargs={"slug": test_article["slug"]})
)
context = response.context
for key in key_list:
self.assertTrue(key in context)
13 changes: 0 additions & 13 deletions apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from apps.shop.models import Product
from apps.tools.models import Tool
from apps.users.models import CustomUser

from .models import Article, Tag

Expand All @@ -15,7 +14,6 @@ class HomepageListView(ListView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
context["tools"] = Tool.objects.all()[:5]
context["products"] = Product.objects.all()[:5]
return context
Expand All @@ -27,11 +25,6 @@ class ArticleListView(ListView):
template_name = "articles/articles.html"
queryset = Article.objects.all().is_published()

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context


class ArticleDetailView(DetailView):
model = Article
Expand All @@ -44,11 +37,6 @@ def get_object(self):
obj.content = obj.content_to_markdown()
return obj

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context


class TagDetailView(DetailView):
model = Tag
Expand All @@ -57,7 +45,6 @@ class TagDetailView(DetailView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
context["articles"] = self.object.article_set.all()
context["tools"] = self.object.tool_set.all()
context["products"] = self.object.product_set.all()
Expand Down
16 changes: 0 additions & 16 deletions apps/orders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ def test_order_create_view(self):
self.assertTemplateUsed(response, "orders/order_form.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("order_create"))

self.assertEqual(response.context["main_author"], main_author)


class TestSuccessView(TestCase):
"""Test Success View"""
Expand All @@ -78,11 +70,3 @@ def test_order_success_view(self):

self.assertTemplateUsed(response, "orders/success_created.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("success_created"))

self.assertEqual(response.context["main_author"], main_author)
11 changes: 0 additions & 11 deletions apps/orders/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView

from apps.users.models import CustomUser

from .models import Order

Expand All @@ -11,11 +10,6 @@
class SuccessView(TemplateView):
template_name = "orders/success_created.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context


class OrderCreateView(CreateView):
model = Order
Expand All @@ -28,8 +22,3 @@ def form_valid(self, form):
response = super().form_valid(form)
self.object.send_email()
return response

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context
16 changes: 0 additions & 16 deletions apps/shop/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ def test_product_list_view(self):
self.assertTemplateUsed(response, "shop/products_list.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("products_list"))

self.assertEqual(response.context["main_author"], main_author)


class TestProductsDetailViews(TestCase):
def setUp(self):
Expand Down Expand Up @@ -111,11 +103,3 @@ def test_product_detail_view(self):
response.context["product"].tags.all()[0].tag_name,
self.test_tag["tag_name"],
)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("products_list"))

self.assertEqual(response.context["main_author"], main_author)
11 changes: 0 additions & 11 deletions apps/shop/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.views.generic import DetailView, ListView

from apps.users.models import CustomUser

from .models import Product

Expand All @@ -11,18 +10,8 @@ class ProductListView(ListView):
context_object_name = "products"
template_name = "shop/products_list.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context


class ProductDetailView(DetailView):
model = Product
context_object_name = "product"
template_name = "shop/product_detail.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context
16 changes: 0 additions & 16 deletions apps/tools/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,6 @@ def test_tools_list_view(self):
self.assertTemplateUsed(response, "tools/tools_list.html")
self.assertEqual(response.status_code, 200)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("tools_list"))

self.assertEqual(response.context["main_author"], main_author)


class TestToolsDetailViews(TestCase):
def setUp(self):
Expand Down Expand Up @@ -101,11 +93,3 @@ def test_tools_detail_view(self):
self.assertEqual(
response.context["tool"].tags.all()[0].tag_name, self.test_tag["tag_name"]
)

def test_main_author(self):
main_author = CustomUser.objects.get(username=self.test_user["username"])
main_author.main_user = True
main_author.save()
response = self.client.get(reverse("tools_list"))

self.assertEqual(response.context["main_author"], main_author)
11 changes: 0 additions & 11 deletions apps/tools/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.views.generic import DetailView, ListView

from apps.users.models import CustomUser

from .models import Tool

Expand All @@ -13,18 +12,8 @@ class ToolListView(ListView):
template_name = "tools/tools_list.html"
queryset = Tool.objects.all()[:5]

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context


class ToolDetailView(DetailView):
model = Tool
context_object_name = "tool"
template_name = "tools/tool_detail.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context
1 change: 0 additions & 1 deletion apps/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def setUp(self):
def test_about(self):
self.assertTemplateUsed(self.response, "users/about.html")
self.assertEqual(self.response.status_code, 200)
self.assertEqual(self.response.context["main_author"], self.main_author)


class UsersManagersTests(TestCase):
Expand Down
7 changes: 0 additions & 7 deletions apps/users/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
from django.views.generic import TemplateView

from .models import CustomUser


class AboutTemplateView(TemplateView):
template_name = "users/about.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["main_author"] = CustomUser.objects.filter(main_user=True).first()
return context
10 changes: 10 additions & 0 deletions settings/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.conf import settings


def about_links(request):
return {
"github_link": settings.AB_GITHUB_LINK,
"linkedin_link": settings.AB_LINKEDIN_LINK,
"cv_link": settings.AB_CV_LINK,
"email_link": settings.AB_EMAIL_LINK,
}
8 changes: 8 additions & 0 deletions settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"settings.context_processors.about_links",
],
},
},
Expand Down Expand Up @@ -186,3 +187,10 @@
"level": "DEBUG",
},
}


# About links
AB_GITHUB_LINK = os.environ.get("AB_GITHUB_LINK", "")
AB_LINKEDIN_LINK = os.environ.get("AB_LINKEDIN_LINK", "")
AB_CV_LINK = os.environ.get("AB_CV_LINK", "")
AB_EMAIL_LINK = os.environ.get("AB_EMAIL_LINK", "")
Loading

0 comments on commit 6f1dc59

Please sign in to comment.