diff --git a/project/blog/__init__.py b/project/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/project/blog/urls.py b/project/blog/urls.py new file mode 100644 index 0000000..eec053e --- /dev/null +++ b/project/blog/urls.py @@ -0,0 +1,6 @@ +from django.conf.urls import url +from . import views + +urlpatterns = [ + url(r'^(?P.+)/$', views.BlogView.as_view(), name='blog-view'), +] diff --git a/project/blog/utils.py b/project/blog/utils.py new file mode 100644 index 0000000..81eefc5 --- /dev/null +++ b/project/blog/utils.py @@ -0,0 +1,15 @@ +import requests +from django.conf import settings + +API_PATH = "https://public-api.wordpress.com/rest/v1.1/sites/{0}/posts/slug:{1}" + +def get_post(slug): + if not slug: + return + url = API_PATH.format(settings.WORDPRESS_URL, slug) + response = requests.get(url) + + if response.status_code != 200: + return + data = response.json() + return data if "ID" in data else False diff --git a/project/blog/views.py b/project/blog/views.py new file mode 100644 index 0000000..02bd386 --- /dev/null +++ b/project/blog/views.py @@ -0,0 +1,19 @@ +from project.common.views import CustomTemplate +from .utils import get_post +from django.http import Http404 + + +class BlogView(CustomTemplate): + template_name="blog/posts.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context['blog'] = self.blog_data + return context + + def dispatch(self, request, *args, **kwargs): + self.blog_data = get_post(kwargs['slug']) + if not self.blog_data: + raise Http404 + self.html_title = self.blog_data['title'] + return super().dispatch(request, *args, **kwargs) diff --git a/project/settings.py b/project/settings.py index 777489a..d60e379 100644 --- a/project/settings.py +++ b/project/settings.py @@ -93,3 +93,5 @@ JOBS = { 'tasks': ['project.common.jobs.send_email'], } } + +WORDPRESS_URL = "realorangeone.wordpress.com" diff --git a/project/urls.py b/project/urls.py index 9725703..5415f68 100644 --- a/project/urls.py +++ b/project/urls.py @@ -12,6 +12,7 @@ urlpatterns = [ url(r'^core/', include('project.pages.urls.core', namespace='core')), url(r'^projects/', include('project.pages.urls.projects', namespace='projects')), url(r'^robotics/', include('project.pages.urls.robotics', namespace='robotics')), + url(r'^blog/', include('project.blog.urls', namespace='blog')), url(r'', include('project.pages.urls.core', namespace='pages')) ] diff --git a/templates/blog/posts.html b/templates/blog/posts.html new file mode 100644 index 0000000..7c0ff69 --- /dev/null +++ b/templates/blog/posts.html @@ -0,0 +1,14 @@ +{% extends 'content_base.html' %} + +{% block pageTitle %}{{ html_title }}{% endblock %} + +{% block content %} + +
+
+
+ {{ blog.content | safe}} +
+
+
+{% endblock %}