Skip to content

Commit b4c7e07

Browse files
committed
adding starting point for the blog app. All homework tasks completed.
1 parent 78a5090 commit b4c7e07

File tree

20 files changed

+558
-0
lines changed

20 files changed

+558
-0
lines changed

resources/session09/mysite/manage.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env python
2+
import os
3+
import sys
4+
5+
if __name__ == "__main__":
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
7+
8+
from django.core.management import execute_from_command_line
9+
10+
execute_from_command_line(sys.argv)

resources/session09/mysite/myblog/__init__.py

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import datetime
2+
from django.contrib import admin
3+
from django.core.urlresolvers import reverse
4+
from myblog.models import Post, Category
5+
6+
7+
class CategorizationInline(admin.TabularInline):
8+
model = Category.posts.through
9+
10+
11+
def make_published(modeladmin, request, queryset):
12+
now = datetime.datetime.now()
13+
queryset.update(published_date=now)
14+
make_published.short_description = "Set publication date for selected posts"
15+
16+
17+
class PostAdmin(admin.ModelAdmin):
18+
inlines = [
19+
CategorizationInline,
20+
]
21+
list_display = (
22+
'__unicode__', 'author_for_admin', 'created_date', 'modified_date', 'published_date'
23+
)
24+
readonly_fields = (
25+
'created_date', 'modified_date',
26+
)
27+
actions = [make_published, ]
28+
29+
def author_for_admin(self, obj):
30+
author = obj.author
31+
url = reverse('admin:auth_user_change', args=(author.pk,))
32+
name = author.get_full_name() or author.username
33+
link = '<a href="{}">{}</a>'.format(url, name)
34+
return link
35+
author_for_admin.short_description = 'Author'
36+
author_for_admin.allow_tags = True
37+
38+
39+
class CategoryAdmin(admin.ModelAdmin):
40+
exclude = ('posts', )
41+
42+
43+
admin.site.register(Post, PostAdmin)
44+
admin.site.register(Category, CategoryAdmin)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"pk": 1,
4+
"model": "auth.user",
5+
"fields": {
6+
"username": "admin",
7+
"first_name": "Mr.",
8+
"last_name": "Administrator",
9+
"is_active": true,
10+
"is_superuser": true,
11+
"is_staff": true,
12+
"last_login": "2013-05-24T05:35:58.628Z",
13+
"groups": [],
14+
"user_permissions": [],
15+
"password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
16+
"email": "[email protected]",
17+
"date_joined": "2013-05-24T05:35:58.628Z"
18+
}
19+
},
20+
{
21+
"pk": 2,
22+
"model": "auth.user",
23+
"fields": {
24+
"username": "noname",
25+
"first_name": "",
26+
"last_name": "",
27+
"is_active": true,
28+
"is_superuser": true,
29+
"is_staff": true,
30+
"last_login": "2013-05-24T05:35:58.628Z",
31+
"groups": [],
32+
"user_permissions": [],
33+
"password": "pbkdf2_sha256$10000$1rQazFNdOfFt$6aw/uIrv2uASkZ7moXMTajSN+ySYuowBnbP6ILNQntE=",
34+
"email": "[email protected]",
35+
"date_joined": "2013-05-24T05:35:58.628Z"
36+
}
37+
}
38+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
from django.conf import settings
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
]
13+
14+
operations = [
15+
migrations.CreateModel(
16+
name='Post',
17+
fields=[
18+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
19+
('title', models.CharField(max_length=128)),
20+
('text', models.TextField(blank=True)),
21+
('created_date', models.DateTimeField(auto_now_add=True)),
22+
('modified_date', models.DateTimeField(auto_now=True)),
23+
('published_date', models.DateTimeField(null=True, blank=True)),
24+
('author', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
25+
],
26+
options={
27+
},
28+
bases=(models.Model,),
29+
),
30+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('myblog', '0001_initial'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Category',
16+
fields=[
17+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
18+
('name', models.CharField(max_length=128)),
19+
('description', models.TextField(blank=True)),
20+
('posts', models.ManyToManyField(related_name='categories', null=True, to='myblog.Post', blank=True)),
21+
],
22+
options={
23+
},
24+
bases=(models.Model,),
25+
),
26+
]

resources/session09/mysite/myblog/migrations/__init__.py

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.db import models
2+
from django.contrib.auth.models import User
3+
4+
5+
class Post(models.Model):
6+
title = models.CharField(max_length=128)
7+
text = models.TextField(blank=True)
8+
author = models.ForeignKey(User)
9+
created_date = models.DateTimeField(auto_now_add=True)
10+
modified_date = models.DateTimeField(auto_now=True)
11+
published_date = models.DateTimeField(blank=True, null=True)
12+
13+
def __unicode__(self):
14+
return self.title
15+
16+
17+
class Category(models.Model):
18+
name = models.CharField(max_length=128)
19+
description = models.TextField(blank=True)
20+
posts = models.ManyToManyField(Post, blank=True, null=True,
21+
related_name='categories')
22+
23+
class Meta:
24+
verbose_name_plural = 'Categories'
25+
26+
def __unicode__(self):
27+
return self.name
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
body {
2+
background-color: #eee;
3+
color: #111;
4+
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
5+
margin:0;
6+
padding:0;
7+
}
8+
#container {
9+
margin:0;
10+
padding:0;
11+
margin-top: 0px;
12+
}
13+
#header {
14+
background-color: #333;
15+
border-botton: 1px solid #111;
16+
margin:0;
17+
padding:0;
18+
}
19+
#control-bar {
20+
margin: 0em 0em 1em;
21+
list-style: none;
22+
list-style-type: none;
23+
text-align: right;
24+
color: #eee;
25+
font-size: 80%;
26+
padding-bottom: 0.4em;
27+
}
28+
#control-bar li {
29+
display: inline-block;
30+
}
31+
#control-bar li a {
32+
color: #eee;
33+
padding: 0.5em;
34+
text-decoration: none;
35+
}
36+
#control-bar li a:hover {
37+
color: #cce;
38+
}
39+
#content {
40+
margin: 0em 1em 1em;
41+
}
42+
43+
ul#entries {
44+
list-style: none;
45+
list-style-type: none;
46+
}
47+
div.entry {
48+
margin-right: 2em;
49+
margin-top: 1em;
50+
border-top: 1px solid #cecece;
51+
}
52+
ul#entries li:first-child div.entry {
53+
border-top: none;
54+
margin-top: 0em;
55+
}
56+
div.entry-body {
57+
margin-left: 2em;
58+
}
59+
.notification {
60+
float: right;
61+
text-align: center;
62+
width: 25%;
63+
padding: 1em;
64+
}
65+
.info {
66+
background-color: #aae;
67+
}
68+
ul.categories {
69+
list-style: none;
70+
list-style-type: none;
71+
}
72+
ul.categories li {
73+
display: inline;
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<a class="backlink" href="/">Home</a>
5+
<h1>{{ post }}</h1>
6+
<p class="byline">
7+
Posted by {{ post.author_name }} &mdash; {{ post.published_date }}
8+
</p>
9+
<div class="post-body">
10+
{{ post.text }}
11+
</div>
12+
<ul class="categories">
13+
{% for category in post.categories.all %}
14+
<li>{{ category }}</li>
15+
{% endfor %}
16+
</ul>
17+
{% endblock %}

0 commit comments

Comments
 (0)