From 97601a7b536c30457c755659641d488563b20b78 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 5 Apr 2016 19:38:09 +0100 Subject: [PATCH] Refactor view code - Compress codebase - Remove duplicate bulky code - Use template directory to dictate sitemap --- data/context.yml | 1 + data/page_context.yml | 9 ++ data/projects.json | 10 -- project/common/data.py | 8 ++ project/common/migrations/__init__.py | 0 project/pages/tests.py | 105 ------------------ project/pages/urls.py | 8 ++ project/pages/urls/__init__.py | 1 - project/pages/urls/about.py | 9 -- project/pages/urls/college.py | 8 -- project/pages/urls/core.py | 7 -- project/pages/urls/projects.py | 8 -- project/pages/urls/robotics.py | 11 -- project/pages/utils.py | 23 ++++ project/pages/views.py | 32 ++++++ project/pages/views/__init__.py | 1 - project/pages/views/about.py | 28 ----- project/pages/views/college.py | 9 -- project/pages/views/core.py | 32 ------ project/pages/views/projects.py | 34 ------ project/pages/views/robotics.py | 27 ----- project/settings.py | 4 + project/urls.py | 14 +-- requirements.txt | 2 +- static/src/js/app.js | 8 +- templates/base.html | 2 +- templates/index.html | 4 +- .../{2014-index.html => 2014/index.html} | 0 .../robotics/{2015-code.md => 2015/code.md} | 0 .../{2015-index.html => 2015/index.html} | 0 .../{2015-robot.html => 2015/robot.html} | 0 templates/robotics/index.html | 4 +- 32 files changed, 96 insertions(+), 313 deletions(-) create mode 100644 data/context.yml create mode 100644 data/page_context.yml delete mode 100644 data/projects.json create mode 100644 project/common/data.py delete mode 100644 project/common/migrations/__init__.py delete mode 100644 project/pages/tests.py create mode 100644 project/pages/urls.py delete mode 100644 project/pages/urls/__init__.py delete mode 100644 project/pages/urls/about.py delete mode 100644 project/pages/urls/college.py delete mode 100644 project/pages/urls/core.py delete mode 100644 project/pages/urls/projects.py delete mode 100644 project/pages/urls/robotics.py create mode 100644 project/pages/utils.py create mode 100644 project/pages/views.py delete mode 100644 project/pages/views/__init__.py delete mode 100644 project/pages/views/about.py delete mode 100644 project/pages/views/college.py delete mode 100644 project/pages/views/core.py delete mode 100644 project/pages/views/projects.py delete mode 100644 project/pages/views/robotics.py rename templates/robotics/{2014-index.html => 2014/index.html} (100%) rename templates/robotics/{2015-code.md => 2015/code.md} (100%) rename templates/robotics/{2015-index.html => 2015/index.html} (100%) rename templates/robotics/{2015-robot.html => 2015/robot.html} (100%) diff --git a/data/context.yml b/data/context.yml new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/data/context.yml @@ -0,0 +1 @@ + diff --git a/data/page_context.yml b/data/page_context.yml new file mode 100644 index 0000000..33c4d4b --- /dev/null +++ b/data/page_context.yml @@ -0,0 +1,9 @@ +index: + body_class: index + html_title: Homepage + +projects/attack-on-blocks: + page_title: Attack On Block Game! + +projects/hipchat-emoticons-for-all: + header_image: https://hipchat-magnolia-cdn.atlassian.com/assets/img/hipchat/hipchat_og_image.jpg diff --git a/data/projects.json b/data/projects.json deleted file mode 100644 index 7fefd88..0000000 --- a/data/projects.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "hipchat-emoticons-for-all": { - "title": "Hipchat Emoticons Plugin", - "image": "https://hipchat-magnolia-cdn.atlassian.com/assets/img/hipchat/hipchat_og_image.jpg" - }, - "attack-on-blocks": { - "title": "Attack on Blocks Game", - "image": "https://image.freepik.com/free-vector/space-invaders-game_62147502273.jpg" - } -} diff --git a/project/common/data.py b/project/common/data.py new file mode 100644 index 0000000..1c395fb --- /dev/null +++ b/project/common/data.py @@ -0,0 +1,8 @@ +import os.path +import yaml + + +def generate_config(base_dir): + default = yaml.load(open(os.path.join(base_dir, 'data/context.yml'))) or {} + page = yaml.load(open(os.path.join(base_dir, 'data/page_context.yml'))) or {} + return default, page diff --git a/project/common/migrations/__init__.py b/project/common/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/project/pages/tests.py b/project/pages/tests.py deleted file mode 100644 index 29f6a23..0000000 --- a/project/pages/tests.py +++ /dev/null @@ -1,105 +0,0 @@ -from django.test import TestCase -from django.core.urlresolvers import reverse -from django.conf import settings -import os.path, json - - -class AboutTestCase(TestCase): - def test_website_accessable(self): - response = self.client.get(reverse('about:website')) - self.assertEqual(response.status_code, 200) - - def test_me_accessable(self): - response = self.client.get(reverse('about:me')) - self.assertEqual(response.status_code, 200) - - def test_index_accessable(self): - response = self.client.get(reverse('about:index')) - self.assertEqual(response.status_code, 200) - - def test_email_send(self): - data = { - 'email': '123@123.123', - 'name': 'Person', - 'message': 'Hi there, things.' - } - response = self.client.post(reverse('about:index'), data) - self.assertRedirects(response, '/about/?sent') - - def test_success_message_shows(self): - response = self.client.get(reverse('about:index') + '?sent') - self.assertContains(response, 'Already Sent') - - -class CorePagesTestCase(TestCase): - def test_404_accessable(self): - response = self.client.get(reverse('404')) - self.assertEqual(response.status_code, 404) - - def test_no_js_accessable(self): - response = self.client.get(reverse('no-js')) - self.assertEqual(response.status_code, 200) - - def test_index_accessable(self): - response = self.client.get(reverse('pages:index')) - self.assertEqual(response.status_code, 200) - - def test_index_projects(self): - projects = json.load(open(os.path.join(settings.BASE_DIR, 'data/projects.json'))) - response = self.client.get(reverse('pages:index')) - for key, project in projects.items(): - self.assertContains(response, project['title']) - if 'image' in project: - self.assertContains(response, project['image']) - - -class ProjectsTestCase(TestCase): - def test_all_accessable(self): - response = self.client.get(reverse('projects:all')) - self.assertEqual(response.status_code, 200) - - def test_test_project_accessable(self): - response = self.client.get(reverse('projects:project', args=['test'])) - self.assertEqual(response.status_code, 200) - - def test_projects_accessable(self): - projects = json.load(open(os.path.join(settings.BASE_DIR, 'data/projects.json'))) - for key, project in projects.items(): - response = self.client.get(reverse('projects:project', args=[key])) - self.assertEqual(response.status_code, 200) - - def test_projects_details(self): - projects = json.load(open(os.path.join(settings.BASE_DIR, 'data/projects.json'))) - for key, project in projects.items(): - response = self.client.get(reverse('projects:project', args=[key])) - self.assertContains(response, project['title']) - if 'image' in project: - self.assertContains(response, project['image']) - - -class RoboticsTestCase(TestCase): - def test_2015_index_accessable(self): - response = self.client.get(reverse('robotics:2015-index')) - self.assertEqual(response.status_code, 200) - - def test_2014_index_accessable(self): - response = self.client.get(reverse('robotics:2014-index')) - self.assertEqual(response.status_code, 200) - - def test_2015_robot_accessable(self): - response = self.client.get(reverse('robotics:2015-robot')) - self.assertEqual(response.status_code, 200) - - def test_2015_code_accessable(self): - response = self.client.get(reverse('robotics:2015-code')) - self.assertEqual(response.status_code, 200) - - -class CollegeTestCase(TestCase): - def test_attack_on_blocks_accessable(self): - response = self.client.get(reverse('college:attack-on-blocks')) - self.assertEqual(response.status_code, 200) - - def test_student_server_accessable(self): - response = self.client.get(reverse('college:student-server')) - self.assertEqual(response.status_code, 200) diff --git a/project/pages/urls.py b/project/pages/urls.py new file mode 100644 index 0000000..f5d7841 --- /dev/null +++ b/project/pages/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import include, url +from .views import page_view, index_view + + +urlpatterns = [ + url(r'^(?P.*)/', page_view, name='page'), + url(r'^$', index_view, name='page'), +] diff --git a/project/pages/urls/__init__.py b/project/pages/urls/__init__.py deleted file mode 100644 index 07d5d1c..0000000 --- a/project/pages/urls/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import about, college, core, projects, robotics diff --git a/project/pages/urls/about.py b/project/pages/urls/about.py deleted file mode 100644 index 2146120..0000000 --- a/project/pages/urls/about.py +++ /dev/null @@ -1,9 +0,0 @@ -from django.conf.urls import url -from project.pages.views import about - - -urlpatterns = [ - url(r'^website/$', about.WebsiteView.as_view(), name='website'), - url(r'^me/$', about.MeView.as_view(), name='me'), - url(r'^$', about.IndexView.as_view(), name='index'), -] diff --git a/project/pages/urls/college.py b/project/pages/urls/college.py deleted file mode 100644 index aa30e11..0000000 --- a/project/pages/urls/college.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.conf.urls import url -from project.pages.views import college - - -urlpatterns = [ - url(r'^attack-on-blocks/$', college.AttackOnBlocksView.as_view(), name='attack-on-blocks'), - url(r'^student-server/$', college.StudentServerView.as_view(), name='student-server'), -] diff --git a/project/pages/urls/core.py b/project/pages/urls/core.py deleted file mode 100644 index f104586..0000000 --- a/project/pages/urls/core.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.conf.urls import url -from project.pages.views import core - - -urlpatterns = [ - url(r'^$', core.IndexView.as_view(), name='index') -] diff --git a/project/pages/urls/projects.py b/project/pages/urls/projects.py deleted file mode 100644 index c1f56c4..0000000 --- a/project/pages/urls/projects.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.conf.urls import url -from project.pages.views import projects - - -urlpatterns = [ - url(r'^$', projects.AllView.as_view(), name="all"), - url(r'^(?P.+)/$', projects.ProjectView.as_view(), name="project") -] diff --git a/project/pages/urls/robotics.py b/project/pages/urls/robotics.py deleted file mode 100644 index a36fc15..0000000 --- a/project/pages/urls/robotics.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.conf.urls import url -from project.pages.views import robotics - - -urlpatterns = [ - url(r'^$', robotics.IndexView.as_view(), name="index"), - url(r'^2014/$', robotics.Index2014View.as_view(), name="2014-index"), - url(r'^2015/robot/$', robotics.Robot2015View.as_view(), name="2015-robot"), - url(r'^2015/code/$', robotics.Code2015View.as_view(), name="2015-code"), - url(r'^2015/$', robotics.Index2015View.as_view(), name="2015-index") -] diff --git a/project/pages/utils.py b/project/pages/utils.py new file mode 100644 index 0000000..cf54b01 --- /dev/null +++ b/project/pages/utils.py @@ -0,0 +1,23 @@ +from django.conf import settings +from bs4 import BeautifulSoup +import markdown2 + + +def get_context(path): + if path in settings.PAGE_CONTEXT: + context = dict(settings.DEFAULT_CONTEXT, **settings.PAGE_CONTEXT[path]) + else: + context = dict(settings.DEFAULT_CONTEXT) + return context + + +def get_title_from_markdown(md): + html_tree = BeautifulSoup(md, "html.parser") + tag = html_tree.find('h1') + return tag.contents[0] + + +def parse_content(content, extension): + if extension == 'md': + return markdown2.markdown(content) + return content diff --git a/project/pages/views.py b/project/pages/views.py new file mode 100644 index 0000000..a0c7ff6 --- /dev/null +++ b/project/pages/views.py @@ -0,0 +1,32 @@ +from project.common.views import CustomTemplate +import os.path +from django.conf import settings +from django.http import HttpResponse, Http404 +from django.template.loader import get_template +from .utils import get_context, parse_content, get_title_from_markdown + + +def page_view(request, path): + template = None + if os.path.isdir(os.path.join(settings.BASE_DIR, 'templates', path)): + path = os.path.join(path, 'index') + for extension in ['md', 'html']: + try: + template = get_template("{}.{}".format(path, extension)) + break + except: + pass + if not template: + raise Http404 + context = get_context(path) + parsed_content = parse_content(template.render(context, request), extension) + if extension == 'md': + template = get_template('markdown_content.html') + context['markdown_content'] = parsed_content + context['page_title'] = get_title_from_markdown(parsed_content) + parsed_content = template.render(context, request) + return HttpResponse(parsed_content) + + +def index_view(request): + return page_view(request, 'index') diff --git a/project/pages/views/__init__.py b/project/pages/views/__init__.py deleted file mode 100644 index 07d5d1c..0000000 --- a/project/pages/views/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from . import about, college, core, projects, robotics diff --git a/project/pages/views/about.py b/project/pages/views/about.py deleted file mode 100644 index 238ff9e..0000000 --- a/project/pages/views/about.py +++ /dev/null @@ -1,28 +0,0 @@ -from project.common.views import CustomTemplate, CustomFormTemplate -from project.common.forms import ContactForm - - -class WebsiteView(CustomTemplate): - template_name = 'about/website.html' - html_title = "About website" - - -class IndexView(CustomFormTemplate): - template_name = 'about/index.html' - html_title = "About" - success_url = '/about/?sent' - form_class = ContactForm - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['sent'] = 'sent' not in self.request.GET - return context - - def form_valid(self, form): - form.send_email() - return super().form_valid(form) - - -class MeView(CustomTemplate): - template_name = 'about/me.html' - html_title = "About Me" diff --git a/project/pages/views/college.py b/project/pages/views/college.py deleted file mode 100644 index d27e54e..0000000 --- a/project/pages/views/college.py +++ /dev/null @@ -1,9 +0,0 @@ -from project.common.views import MarkdownView - - -class AttackOnBlocksView(MarkdownView): - markdown = 'projects/attack-on-blocks.md' - - -class StudentServerView(MarkdownView): - markdown = 'college/student-server.md' diff --git a/project/pages/views/core.py b/project/pages/views/core.py deleted file mode 100644 index a7cfe96..0000000 --- a/project/pages/views/core.py +++ /dev/null @@ -1,32 +0,0 @@ -from project.common.views import CustomTemplate -from django.conf import settings -from json import load -import os.path - - -class IndexView(CustomTemplate): - template_name = 'index.html' - html_title = "Homepage" - body_class = "index" - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['projects'] = load(open(os.path.join(settings.BASE_DIR, 'data/projects.json'))) - return context - - -class NoJavascriptView(CustomTemplate): - template_name = 'core/no-js.html' - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['js_redirect'] = False - return context - - -class Custom404View(CustomTemplate): - template_name = 'core/404.html' - - def get(self, request, *args, **kwargs): - context = self.get_context_data(**kwargs) - return self.render_to_response(context, status=404) diff --git a/project/pages/views/projects.py b/project/pages/views/projects.py deleted file mode 100644 index 081bb9b..0000000 --- a/project/pages/views/projects.py +++ /dev/null @@ -1,34 +0,0 @@ -from project.common.views import CustomTemplate, MarkdownView -from json import load -from django.conf import settings -import os.path - - -PROJECT_DETAILS = load(open(os.path.join(settings.BASE_DIR, 'data/projects.json'))) - - -class AllView(CustomTemplate): - template_name = 'projects/all.html' - - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - context['projects'] = PROJECT_DETAILS - return context - - -class ProjectView(MarkdownView): - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - try: - details = PROJECT_DETAILS[kwargs['project']] - context['html_title'] = details['title'] - context['page_title'] = details['title'] - context['header_image'] = details['image'] - except: - context['html_title'] = kwargs['project'] - context['page_title'] = kwargs['project'] - return context - - def dispatch(self, request, *args, **kwargs): - self.markdown = 'projects/{0}.md'.format(kwargs['project']) - return super().dispatch(request, *args, **kwargs) diff --git a/project/pages/views/robotics.py b/project/pages/views/robotics.py deleted file mode 100644 index f06242a..0000000 --- a/project/pages/views/robotics.py +++ /dev/null @@ -1,27 +0,0 @@ -from project.common.views import CustomTemplate, MarkdownView - - -class IndexView(CustomTemplate): - template_name = 'robotics/index.html' - html_title = 'Student Robotics' - - -# 2015 -class Index2015View(CustomTemplate): - template_name = 'robotics/2015-index.html' - html_title = 'Student Robotics 2015' - - -class Robot2015View(CustomTemplate): - template_name = 'robotics/2015-robot.html' - html_title = 'The Robot | SR2015' - - -class Code2015View(MarkdownView): - markdown = 'robotics/2015-code.md' - - -# 2014 -class Index2014View(CustomTemplate): - template_name = 'robotics/2014-index.html' - html_title = 'Student Robotics 2014' diff --git a/project/settings.py b/project/settings.py index 7035932..717f2c6 100644 --- a/project/settings.py +++ b/project/settings.py @@ -96,3 +96,7 @@ JOBS = { } WORDPRESS_URL = "realorangeone.wordpress.com" + +# Generate config data +from project.common.data import generate_config +DEFAULT_CONTEXT, PAGE_CONTEXT = generate_config(BASE_DIR) diff --git a/project/urls.py b/project/urls.py index 5415f68..47926c7 100644 --- a/project/urls.py +++ b/project/urls.py @@ -1,19 +1,7 @@ from django.conf.urls import include, url -from django_client_reverse import urls as reverse_urls -from project.pages.views.core import Custom404View, NoJavascriptView urlpatterns = [ - url(r'^reverse/$', include(reverse_urls, namespace='reverser')), - url(r'^404/', Custom404View.as_view(), name="404"), - url(r'^no-js/', NoJavascriptView.as_view(), name="no-js"), - url(r'^about/', include('project.pages.urls.about', namespace='about')), - url(r'^college/', include('project.pages.urls.college', namespace='college')), - 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')) + url(r'', include('project.pages.urls', namespace='pages')) ] - -# handler404 = Custom404View diff --git a/requirements.txt b/requirements.txt index 7c2b0b3..5396c24 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,10 +1,10 @@ +beautifulsoup4==4.3.2 coverage==4.0.3 colorama==0.3.6 Django==1.8.7 dj-database-url==0.3.0 django-db-queue==0.0.2 django-bootstrap-form==3.2 -git+https://github.com/RealOrangeOne/django-client-reverse django-flat-theme==1.1.3 djangorestframework==3.3.2 flake8==2.5.0 diff --git a/static/src/js/app.js b/static/src/js/app.js index 290a639..825a567 100644 --- a/static/src/js/app.js +++ b/static/src/js/app.js @@ -6,14 +6,10 @@ import './globals.js'; import 'whatwg-fetch'; import React from 'react'; -import ProjectImagesTypes from './components/index/project-images-types'; import NavBar from './components/navbar/navbar'; import Breadcrumbs from './components/breadcrumbs'; -if ($('body').hasClass('index')) { // Render components on index - React.render(, document.getElementById('project-images-types')); -} if ($('navbar').length > 0) { React.render(, $('navbar')[0]); @@ -22,3 +18,7 @@ if ($('navbar').length > 0) { if ($('#breadcrumbs').length > 0) { React.render(, $('#breadcrumbs')[0]); } + +if ($('.header h1').text()) { + $('.markdown-content h1').eq(0).hide(); +} diff --git a/templates/base.html b/templates/base.html index 9422f9a..89c9e83 100644 --- a/templates/base.html +++ b/templates/base.html @@ -57,7 +57,7 @@ {% if js_redirect %} {% endif %} diff --git a/templates/index.html b/templates/index.html index eb3894d..4e7ecfe 100644 --- a/templates/index.html +++ b/templates/index.html @@ -44,7 +44,7 @@

{{ project.title }}

- + Read More
@@ -53,7 +53,7 @@ {% endfor %}
diff --git a/templates/robotics/2014-index.html b/templates/robotics/2014/index.html similarity index 100% rename from templates/robotics/2014-index.html rename to templates/robotics/2014/index.html diff --git a/templates/robotics/2015-code.md b/templates/robotics/2015/code.md similarity index 100% rename from templates/robotics/2015-code.md rename to templates/robotics/2015/code.md diff --git a/templates/robotics/2015-index.html b/templates/robotics/2015/index.html similarity index 100% rename from templates/robotics/2015-index.html rename to templates/robotics/2015/index.html diff --git a/templates/robotics/2015-robot.html b/templates/robotics/2015/robot.html similarity index 100% rename from templates/robotics/2015-robot.html rename to templates/robotics/2015/robot.html diff --git a/templates/robotics/index.html b/templates/robotics/index.html index ef6301a..0ba3294 100644 --- a/templates/robotics/index.html +++ b/templates/robotics/index.html @@ -27,7 +27,7 @@

Student Robotics 2014

Robot Name: Lucy
(No, it doesn't stand for anything) -

More Info

+

More Info

@@ -39,7 +39,7 @@

Student Robotics 2015

Robot Name : A.L.I.C.E
(Autonomous Logistics and Inevitable Collision Engine)

-

More Info

+

More Info