Added basic blog integration
This commit is contained in:
parent
95aff4d87a
commit
73656174fd
7 changed files with 57 additions and 0 deletions
0
project/blog/__init__.py
Normal file
0
project/blog/__init__.py
Normal file
6
project/blog/urls.py
Normal file
6
project/blog/urls.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.conf.urls import url
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^(?P<slug>.+)/$', views.BlogView.as_view(), name='blog-view'),
|
||||
]
|
15
project/blog/utils.py
Normal file
15
project/blog/utils.py
Normal file
|
@ -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
|
19
project/blog/views.py
Normal file
19
project/blog/views.py
Normal file
|
@ -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)
|
|
@ -93,3 +93,5 @@ JOBS = {
|
|||
'tasks': ['project.common.jobs.send_email'],
|
||||
}
|
||||
}
|
||||
|
||||
WORDPRESS_URL = "realorangeone.wordpress.com"
|
||||
|
|
|
@ -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'))
|
||||
]
|
||||
|
||||
|
|
14
templates/blog/posts.html
Normal file
14
templates/blog/posts.html
Normal file
|
@ -0,0 +1,14 @@
|
|||
{% extends 'content_base.html' %}
|
||||
|
||||
{% block pageTitle %}{{ html_title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-sm-8">
|
||||
{{ blog.content | safe}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
Reference in a new issue