Skip to content

Commit e6e38d8

Browse files
committed
stage 2 app
1 parent 6c5833b commit e6e38d8

File tree

17 files changed

+374
-0
lines changed

17 files changed

+374
-0
lines changed
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/session08/mysite_stage_2/myblog/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
from myblog.models import Post, Category
3+
4+
5+
admin.site.register(Post)
6+
admin.site.register(Category)
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/session08/mysite_stage_2/myblog/migrations/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
def __unicode__(self):
24+
return self.name
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{% extends "base.html" %}
2+
3+
{% block content %}
4+
<h1>Recent Posts</h1>
5+
6+
{% comment %} here is where the query happens {% endcomment %}
7+
{% for post in posts %}
8+
<div class="post">
9+
<h2>{{ post }}</h2>
10+
<p class="byline">
11+
Posted by {{ post.author_name }} &mdash; {{ post.published_date }}
12+
</p>
13+
<div class="post-body">
14+
{{ post.text }}
15+
</div>
16+
<ul class="categories">
17+
{% for category in post.categories.all %}
18+
<li>{{ category }}</li>
19+
{% endfor %}
20+
</ul>
21+
</div>
22+
{% endfor %}
23+
{% endblock %}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import datetime
2+
from django.test import TestCase
3+
from django.contrib.auth.models import User
4+
from django.utils.timezone import utc
5+
from myblog.models import Post, Category
6+
7+
8+
class PostTestCase(TestCase):
9+
fixtures = ['myblog_test_fixture.json', ]
10+
11+
def setUp(self):
12+
self.user = User.objects.get(pk=1)
13+
14+
def test_unicode(self):
15+
expected = u"This is a title"
16+
p1 = Post(title=expected)
17+
actual = unicode(p1)
18+
self.assertEqual(expected, actual)
19+
20+
21+
class CategoryTestCase(TestCase):
22+
23+
def test_unicode(self):
24+
expected = "A Category"
25+
c1 = Category(name=expected)
26+
actual = unicode(c1)
27+
self.assertEqual(expected, actual)
28+
29+
30+
class FrontEndTestCase(TestCase):
31+
"""test views provided in the front-end"""
32+
fixtures = ['myblog_test_fixture.json', ]
33+
34+
def setUp(self):
35+
self.now = datetime.datetime.utcnow().replace(tzinfo=utc)
36+
self.timedelta = datetime.timedelta(15)
37+
author = User.objects.get(pk=1)
38+
for count in range(1, 11):
39+
post = Post(title="Post %d Title" % count,
40+
text="foo",
41+
author=author)
42+
if count < 6:
43+
# publish the first five posts
44+
pubdate = self.now - self.timedelta * count
45+
post.published_date = pubdate
46+
post.save()
47+
48+
def test_list_only_published(self):
49+
resp = self.client.get('/')
50+
self.assertTrue("Recent Posts" in resp.content)
51+
for count in range(1,11):
52+
title = "Post %d Title" % count
53+
if count < 6:
54+
self.assertContains(resp, title, count=1)
55+
else:
56+
self.assertNotContains(resp, title)

0 commit comments

Comments
 (0)