From 9deb084422d3b98c0d4ec5066edf1d6038f4ba8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandro=20S=C3=A1nchez=20Bach?= Date: Mon, 16 Jun 2014 18:52:36 +0200 Subject: [PATCH] Implemented blog_pages and blog_entry views --- .gitignore | 7 +++- deploy/linux.sh | 3 +- website.pyproj | 4 +- website/blog/admin.py | 5 +++ website/blog/models.py | 29 +++++++++++++- .../{index.html => blog_article.html} | 2 +- website/blog/templates/blog_index.html | 39 +++++++++++++++++++ website/blog/urls.py | 6 ++- website/blog/views.py | 33 +++++++++++++++- website/compat/models.py | 2 +- website/templates/base.html | 2 +- 11 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 website/blog/admin.py rename website/blog/templates/{index.html => blog_article.html} (81%) create mode 100644 website/blog/templates/blog_index.html diff --git a/.gitignore b/.gitignore index 624461e..49a72cc 100644 --- a/.gitignore +++ b/.gitignore @@ -30,9 +30,12 @@ pip-delete-this-directory.txt *.log *.pot +# SQLite3 +sqlite3.exe + # Python Tools for Visual Studio *.suo -# Secret stuff: +# Irrelevant files settings_secret.py -*.sqlite3 +db.sqlite3 diff --git a/deploy/linux.sh b/deploy/linux.sh index 6ce947d..2dc9be5 100644 --- a/deploy/linux.sh +++ b/deploy/linux.sh @@ -5,8 +5,9 @@ apt-get install python apt-get install python-dev apt-get install python-pip -# Django +# Modules pip install django +pip install markdown # Initialize DB python ../manage.py syncdb diff --git a/website.pyproj b/website.pyproj index 9e9f7a5..093eab0 100644 --- a/website.pyproj +++ b/website.pyproj @@ -36,6 +36,7 @@ + @@ -84,7 +85,8 @@ - + + diff --git a/website/blog/admin.py b/website/blog/admin.py new file mode 100644 index 0000000..56d05b2 --- /dev/null +++ b/website/blog/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from website.blog.models import Article, Tag + +admin.site.register(Article) +admin.site.register(Tag) diff --git a/website/blog/models.py b/website/blog/models.py index 71a8362..6a828b2 100644 --- a/website/blog/models.py +++ b/website/blog/models.py @@ -1,3 +1,30 @@ from django.db import models -# Create your models here. +import markdown +from django.template.defaultfilters import slugify + + +class Tag(models.Model): + tag = models.CharField(max_length=31) + def __unicode__(self): + return self.tag + + +class Article(models.Model): + title = models.CharField(max_length=255) + content = models.TextField() + date = models.DateTimeField() + visible = models.BooleanField() + tags = models.ManyToManyField(Tag) + slug = models.SlugField() + + def __unicode__(self): + return self.title + + def save(self): + if not self.id: # Newly created object, so set slug + self.slug = slugify(self.title) + super(Article, self).save(*args, **kwargs) + + def get_formatted_content(self): + return markdown.markdown(self.content) diff --git a/website/blog/templates/index.html b/website/blog/templates/blog_article.html similarity index 81% rename from website/blog/templates/index.html rename to website/blog/templates/blog_article.html index a0cf4bf..c54efcf 100644 --- a/website/blog/templates/index.html +++ b/website/blog/templates/blog_article.html @@ -3,7 +3,7 @@ -{{ content }} +Hello World! diff --git a/website/blog/templates/blog_index.html b/website/blog/templates/blog_index.html new file mode 100644 index 0000000..77192c5 --- /dev/null +++ b/website/blog/templates/blog_index.html @@ -0,0 +1,39 @@ +{% extends 'base.html' %} + +{% block content %} +{% for entry in articles %} +
+
+

+ + {{ entry.title }} + +

+

+ Written by + + on + {{ entry.date|date:"F j, Y" }} + / + {% for tag in entry.tags.all %} + {{ tag.tag }} + {% endfor %} + / + Short link +

+
+
+ {{ entry.get_formatted_content|safe }} +
+
+
+{% endfor %} + +
+
    + {% for num in pages %} +
  • {{ num }}
  • + {% endfor %} +
+
+{% endblock %} diff --git a/website/blog/urls.py b/website/blog/urls.py index f77cdf0..d44e4eb 100644 --- a/website/blog/urls.py +++ b/website/blog/urls.py @@ -1,5 +1,9 @@ from django.conf.urls import patterns, url urlpatterns = patterns('website.blog.views', - url(r'^$', 'blog_index', name='blog-index'), + url(r'^$', 'blog_pages', name='blog-index'), + url(r'^page/(?P[0-9]+)/$', 'blog_pages'), + url(r'^tags/$', 'blog_pages', name='blog-entry'), + url(r'^tags/(?P[-\w]+)/$', 'blog_pages', name='blog-entry'), + url(r'^(?P\d{4})/(?P\d{2})/(?P\d{2})/(?P[-\w]+)/$', 'blog_entry', name='blog-entry'), ) diff --git a/website/blog/views.py b/website/blog/views.py index 60f00ef..82c4fc5 100644 --- a/website/blog/views.py +++ b/website/blog/views.py @@ -1 +1,32 @@ -# Create your views here. +from django.shortcuts import render + +from website.blog.models import Article + +def blog_pages(request, num="1"): + count = 10 # Articles per page + num = max(1, int(num)) + articles = Article.objects.order_by('-date')[count*(num-1):count*num] + objects = { + 'articles' : articles, + 'pages' : range(1, 1 + Article.objects.all().count()/count), + } + return render(request, 'blog_index.html', objects) + +def blog_entry(request, year='', month='', day='', slug=''): + articles = Article.objects.all() + if year: + articles = articles.filter(date__year=year) + if month: + articles = articles.filter(date__month=month) + if day: + articles = articles.filter(date__day=day) + if title: + # Specific article + article = articles.filter(slug=slug)[0] + return render(request, 'blog_article.html', {'article' : article}) + else: + # List of articles + objects = { + 'articles' : articles, + } + return render(request, 'blog_index.html', objects) diff --git a/website/compat/models.py b/website/compat/models.py index 288b158..1414bd3 100644 --- a/website/compat/models.py +++ b/website/compat/models.py @@ -4,7 +4,7 @@ from website.constants import * class Game(models.Model): # Static - titleid = models.CharField(max_length=9) + titleid = models.CharField(max_length=9, primary_key=True) name = models.CharField(max_length=64) publisher = models.CharField(max_length=64) developer = models.CharField(max_length=64) diff --git a/website/templates/base.html b/website/templates/base.html index 57cd5f4..0d3a2ed 100644 --- a/website/templates/base.html +++ b/website/templates/base.html @@ -28,7 +28,7 @@
  • Downloads
  • Compatibility
  • FAQ
  • -
  • Blog
  • +
  • Blog
  • Forum
  • Contact