Added draft and user attributes to BlogEntry.

This commit is contained in:
Felipe Martín 2013-05-29 17:54:01 +02:00
parent 24c54e50e5
commit 680de44cdf
5 changed files with 124 additions and 10 deletions

View File

@ -0,0 +1,85 @@
# -*- 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 field 'BlogEntry.draft'
db.add_column(u'blog_blogentry', 'draft',
self.gf('django.db.models.fields.BooleanField')(default=True),
keep_default=False)
# Adding field 'BlogEntry.user'
db.add_column(u'blog_blogentry', 'user',
self.gf('django.db.models.fields.related.ForeignKey')(default=1, to=orm['auth.User']),
keep_default=False)
# Changing field 'BlogEntry.date'
db.alter_column(u'blog_blogentry', 'date', self.gf('django.db.models.fields.DateTimeField')(auto_now_add=True))
def backwards(self, orm):
# Deleting field 'BlogEntry.draft'
db.delete_column(u'blog_blogentry', 'draft')
# Deleting field 'BlogEntry.user'
db.delete_column(u'blog_blogentry', 'user_id')
# Changing field 'BlogEntry.date'
db.alter_column(u'blog_blogentry', 'date', self.gf('django.db.models.fields.DateTimeField')())
models = {
u'auth.group': {
'Meta': {'object_name': 'Group'},
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
},
u'auth.permission': {
'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'},
'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
},
u'auth.user': {
'Meta': {'object_name': 'User'},
'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
},
'blog.blogentry': {
'Meta': {'ordering': "['-date']", 'object_name': 'BlogEntry'},
'content': ('django.db.models.fields.TextField', [], {}),
'date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
'draft': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
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'}),
'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['auth.User']"})
},
u'contenttypes.contenttype': {
'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
}
}
complete_apps = ['blog']

View File

@ -3,6 +3,7 @@ from django.contrib import admin
import datetime
from django.utils.timezone import utc
from django import forms
from django.contrib.auth.models import User
# Create your models here.
@ -11,6 +12,8 @@ class BlogEntry(models.Model):
date = models.DateTimeField(auto_now_add=True)
content = models.TextField()
slug = models.SlugField(max_length=128)
draft = models.BooleanField(default=True)
user = models.ForeignKey(User)
class Meta:
app_label = 'blog'

View File

@ -6,7 +6,16 @@
<hr />
<article class="blog-entry">
<h2><a href="/blog/{{ item.date.year }}/{{ item.date.month }}/{{ item.date.day }}/{{ item.slug }}">{{ item.title }}</a></h2>
<div><i class="icon-calendar"></i> {{ item.date|dt('%B %e, %Y') }}</div>
<div>
<i class="icon-calendar"></i> {{ item.date|dt('%B %e, %Y') }}
&nbsp;
<i class="icon-user"></i> {{ item.user.first_name }}
{% if item.draft %}
<span class="badge badge-warning">
This post is a draft
</span>
{% endif %}
</div>
<div class="content">{{ item.content|safe }}</div>
</article>
{% endfor %}

View File

@ -4,7 +4,11 @@
<p><a href="{{ url('blog_list') }}"><i class="icon-arrow-left"></i> Go back</a></p>
<article class="blog-entry">
<h1>{{ item.title }}</h1>
<div><i class="icon-calendar"></i> {{ item.date|dt('%B %e, %Y') }}</div>
<div>
<i class="icon-calendar"></i> {{ item.date|dt('%B %e, %Y') }}
&nbsp;
<i class="icon-user"></i> {{ item.user.first_name }}
</div>
<div class="content">
{{ item.content|safe }}
</div>

View File

@ -2,13 +2,16 @@ 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()
if request.user.is_authenticated():
items = BlogEntry.objects.all()
else:
items = BlogEntry.objects.filter(draft=False)
paginator = Paginator(items, 4)
page_number = 1
@ -28,12 +31,22 @@ def blog(request):
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)
)
if request.user.is_authenticated():
item = BlogEntry.objects.get(
slug=slug,
date__year=int(year),
date__month=int(month),
date__day=int(day)
)
else:
item = BlogEntry.objects.get(
slug=slug,
date__year=int(year),
date__month=int(month),
date__day=int(day),
draft=False
)
data = {
'item': item