From ffea80c0491295b1890814256156987f772de08b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Mart=C3=ADn?= Date: Tue, 28 May 2013 15:15:12 +0200 Subject: [PATCH] Added blog app: + BlogEntry model + Views for news lists and news detail + Migrations + Custom date time filter based on settings timezone --- minecraftcodex/blog/__init__.py | 0 .../blog/migrations/0001_initial.py | 38 +++++++++++++++++ minecraftcodex/blog/migrations/__init__.py | 0 minecraftcodex/blog/models.py | 34 +++++++++++++++ minecraftcodex/blog/templates/blog.html | 24 +++++++++++ minecraftcodex/blog/templates/blog_entry.html | 8 ++++ minecraftcodex/blog/tests.py | 16 +++++++ minecraftcodex/blog/views.py | 42 +++++++++++++++++++ minecraftcodex/herobrine/filters.py | 15 +++++++ minecraftcodex/herobrine/urls.py | 1 + 10 files changed, 178 insertions(+) create mode 100644 minecraftcodex/blog/__init__.py create mode 100644 minecraftcodex/blog/migrations/0001_initial.py create mode 100644 minecraftcodex/blog/migrations/__init__.py create mode 100644 minecraftcodex/blog/models.py create mode 100644 minecraftcodex/blog/templates/blog.html create mode 100644 minecraftcodex/blog/templates/blog_entry.html create mode 100644 minecraftcodex/blog/tests.py create mode 100644 minecraftcodex/blog/views.py create mode 100644 minecraftcodex/herobrine/filters.py diff --git a/minecraftcodex/blog/__init__.py b/minecraftcodex/blog/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/minecraftcodex/blog/migrations/0001_initial.py b/minecraftcodex/blog/migrations/0001_initial.py new file mode 100644 index 0000000..2d99f83 --- /dev/null +++ b/minecraftcodex/blog/migrations/0001_initial.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +import datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Adding model 'BlogEntry' + db.create_table(u'blog_blogentry', ( + (u'id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), + ('title', self.gf('django.db.models.fields.CharField')(max_length=128)), + ('date', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 5, 28, 0, 0))), + ('content', self.gf('django.db.models.fields.TextField')()), + ('slug', self.gf('django.db.models.fields.SlugField')(max_length=128)), + )) + db.send_create_signal('blog', ['BlogEntry']) + + + def backwards(self, orm): + # Deleting model 'BlogEntry' + db.delete_table(u'blog_blogentry') + + + models = { + 'blog.blogentry': { + 'Meta': {'ordering': "['date']", 'object_name': 'BlogEntry'}, + 'content': ('django.db.models.fields.TextField', [], {}), + 'date': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime(2013, 5, 28, 0, 0)'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '128'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '128'}) + } + } + + complete_apps = ['blog'] \ No newline at end of file diff --git a/minecraftcodex/blog/migrations/__init__.py b/minecraftcodex/blog/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/minecraftcodex/blog/models.py b/minecraftcodex/blog/models.py new file mode 100644 index 0000000..45e8d20 --- /dev/null +++ b/minecraftcodex/blog/models.py @@ -0,0 +1,34 @@ +from django.db import models +from django.contrib import admin +import datetime +from django.utils.timezone import utc + + +# Create your models here. +class BlogEntry(models.Model): + title = models.CharField(max_length=128) + date = models.DateTimeField(auto_now_add=True) + content = models.TextField() + slug = models.SlugField(max_length=128) + + class Meta: + app_label = 'blog' + ordering = ['-date'] + + +class BlogEntryAdmin(admin.ModelAdmin): + list_display = ('title', 'date', ) + list_display_links = ('title', ) + + list_filter = ('date', ) + search_fields = ('title', 'content', ) + + prepopulated_fields = {"slug": ("title",)} + + class Media: + css = { + "all": ("lib/redactor.css",) + } + js = ("lib/redactor.js",) + +admin.site.register(BlogEntry, BlogEntryAdmin) diff --git a/minecraftcodex/blog/templates/blog.html b/minecraftcodex/blog/templates/blog.html new file mode 100644 index 0000000..29e2e4f --- /dev/null +++ b/minecraftcodex/blog/templates/blog.html @@ -0,0 +1,24 @@ +{% extends "layout.html" %} + +{% block content %} +

Blog

+ {% for item in page.object_list %} +
+
+

{{ item.title }}

+
{{ item.date|dt('%B %e, %Y') }}
+

{{ item.content|nl2br }}

+
+ {% endfor %} + {% if paginator.num_pages > 1 %} + + {% endif %} +{% endblock %} diff --git a/minecraftcodex/blog/templates/blog_entry.html b/minecraftcodex/blog/templates/blog_entry.html new file mode 100644 index 0000000..98a66e6 --- /dev/null +++ b/minecraftcodex/blog/templates/blog_entry.html @@ -0,0 +1,8 @@ +{% extends "layout.html" %} + +{% block content %} +

Go back

+

{{ item.title }}

+
{{ item.date|dt('%B %e, %Y') }}
+

{{ item.content|nl2br }}

+{% endblock %} diff --git a/minecraftcodex/blog/tests.py b/minecraftcodex/blog/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/minecraftcodex/blog/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/minecraftcodex/blog/views.py b/minecraftcodex/blog/views.py new file mode 100644 index 0000000..3ad5b5f --- /dev/null +++ b/minecraftcodex/blog/views.py @@ -0,0 +1,42 @@ +from blog.models import BlogEntry +from django.core.paginator import Paginator +from django.shortcuts import render_to_response +from django.template import RequestContext +from datetime import datetime + + +def blog(request): + section = 'blog' + + items = BlogEntry.objects.all() + paginator = Paginator(items, 4) + page_number = 1 + + if 'page' in request.GET: + page_number = int(request.GET['page']) + + page = paginator.page(page_number) + + data = { + 'section': section, + 'page': page, + 'page_number': page_number, + 'paginator': paginator, + } + context = RequestContext(request, data) + return render_to_response('blog.html', context_instance=context) + + +def blog_item(request, year, month, day, slug): + item = BlogEntry.objects.get( + slug=slug, + date__year=int(year), + date__month=int(month), + date__day=int(day) + ) + + data = { + 'item': item + } + context = RequestContext(request, data) + return render_to_response('blog_entry.html', context_instance=context) diff --git a/minecraftcodex/herobrine/filters.py b/minecraftcodex/herobrine/filters.py new file mode 100644 index 0000000..d1cf7fb --- /dev/null +++ b/minecraftcodex/herobrine/filters.py @@ -0,0 +1,15 @@ +from jingo import register +from django.utils.translation import ugettext as _ +from django.utils.encoding import smart_unicode +from django.conf import settings +from pytz import timezone +import pytz + + +@register.filter +def dt(t, fmt=None): + """Call ``datetime.strftime`` with the given format string.""" + tz = timezone(settings.TIME_ZONE) + if fmt is None: + fmt = _('%B %e, %Y') + return smart_unicode(tz.normalize(t).strftime(fmt)) if t else u'' diff --git a/minecraftcodex/herobrine/urls.py b/minecraftcodex/herobrine/urls.py index 9ef7a94..ea6735d 100644 --- a/minecraftcodex/herobrine/urls.py +++ b/minecraftcodex/herobrine/urls.py @@ -1,5 +1,6 @@ from django.conf.urls import patterns, include, url from django.http import HttpResponse, HttpResponseRedirect +from herobrine import filters # Admin from django.contrib import admin