Skip to content

Commit 0010f06

Browse files
committed
Maybe all done!
1 parent 6ee8940 commit 0010f06

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

chapter_22_outside_in.asciidoc

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ Let's write a test for that:
736736
[source,python]
737737
----
738738
from django.contrib.auth import get_user_model
739+
[...]
739740
740741
User = get_user_model()
741742
[...]
@@ -757,7 +758,7 @@ And that gives us a new unit test failure:
757758
----
758759
mylist = List.objects.create(owner=user)
759760
[...]
760-
TypeError: 'owner' is an invalid keyword argument for this function
761+
TypeError: List() got unexpected keyword arguments: 'owner'
761762
----
762763

763764
The naive implementation would be this:
@@ -790,7 +791,7 @@ that too:
790791
The correct implementation is this:
791792

792793
[role="sourcecode"]
793-
.src/lists/models.py (ch22l021)
794+
.src/lists/models.py (ch22l021)
794795
====
795796
[source,python]
796797
----
@@ -806,13 +807,13 @@ class List(models.Model):
806807
return reverse("view_list", args=[self.id])
807808
----
808809
====
809-
//21
810810

811811
Now running the tests gives the usual database error:
812812

813813
----
814-
return Database.Cursor.execute(self, query, params)
815-
django.db.utils.OperationalError: no such column: lists_list.owner_id
814+
return super().execute(query, params)
815+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
816+
django.db.utils.OperationalError: table lists_list has no column named owner_id
816817
----
817818

818819

@@ -822,36 +823,39 @@ Because we need to make some migrations:
822823
----
823824
$ pass:quotes[*python src/manage.py makemigrations*]
824825
Migrations for 'lists':
825-
lists/migrations/0006_list_owner.py
826+
src/lists/migrations/0006_list_owner.py
826827
- Add field owner to list
827828
----
828829
//22
829830

830831
We're almost there; a couple more failures:
831832

832833
----
833-
ERROR: test_redirects_after_POST (lists.tests.test_views.NewListTest)
834+
ERROR: test_can_save_a_POST_request
834835
[...]
835836
ValueError: Cannot assign "<SimpleLazyObject:
836-
<django.contrib.auth.models.AnonymousUser object at 0x7f364795ef90>>":
837-
"List.owner" must be a "User" instance.
838-
ERROR: test_can_save_a_POST_request (lists.tests.test_views.NewListTest)
837+
<django.contrib.auth.models.AnonymousUser object at 0x1069852e>>": "List.owner" must
838+
be a "User" instance.
839+
[...]
839840
841+
ERROR: test_redirects_after_POST
840842
[...]
841843
ValueError: Cannot assign "<SimpleLazyObject:
842-
<django.contrib.auth.models.AnonymousUser object at 0x7f364795ef90>>":
843-
"List.owner" must be a "User" instance.
844+
<django.contrib.auth.models.AnonymousUser object at 0x106a1b440>>": "List.owner" must
845+
be a "User" instance.
844846
----
845847

846848

847849

848-
We're moving back up to the views layer now, just doing a little
849-
tidying up. Notice that these are in the old test for the `new_list` view, when
850-
we haven't got a logged-in user. We should only save the list owner when the
851-
user is actually logged in. The `.is_authenticated` attribute we defined in
852-
<<chapter_18_spiking_custom_auth>> comes in useful now (when they're not logged in,
853-
Django represents users using a class called `AnonymousUser`, whose
854-
`.is_authenticated` is always `False`):
850+
We're moving back up to the views layer now, just doing a little tidying up.
851+
Notice that these are in the old test for the `new_list` view,
852+
when we haven't got a logged-in user.
853+
We should only save the list owner when the user is actually logged in.
854+
The `.is_authenticated` attribute we defined in <<chapter_18_spiking_custom_auth>>
855+
comes in useful now
856+
(when they're not logged in,
857+
Django represents users using a class called `AnonymousUser`,
858+
whose `.is_authenticated` is always `False`):
855859

856860

857861
[role="sourcecode"]
@@ -876,9 +880,8 @@ And that gets us passing!
876880
----
877881
$ pass:quotes[*python src/manage.py test lists*]
878882
[...]
879-
.......................................
880-
---------------------------------------------------------------------
881-
Ran 39 tests in 0.237s
883+
884+
Ran 37 tests in 0.237s
882885
883886
OK
884887
----
@@ -887,14 +890,13 @@ This is a good time for a commit:
887890

888891
[subs="specialcharacters,quotes"]
889892
----
890-
$ *git add lists*
893+
$ *git add src/lists*
891894
$ *git commit -m "lists can have owners, which are saved on creation."*
892895
----
893896

894897

895898

896-
Final Step: Feeding Through the .name API from the Template
897-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
899+
=== Final Step: Feeding Through the .name API from the Template
898900

899901
The last thing our outside-in design wanted came from the templates,
900902
which wanted to be able to access a list "name" based on the text of
@@ -907,9 +909,9 @@ its first item:
907909
----
908910
def test_list_name_is_first_item_text(self):
909911
list_ = List.objects.create()
910-
Item.objects.create(list=list_, text='first item')
911-
Item.objects.create(list=list_, text='second item')
912-
self.assertEqual(list_.name, 'first item')
912+
Item.objects.create(list=list_, text="first item")
913+
Item.objects.create(list=list_, text="second item")
914+
self.assertEqual(list_.name, "first item")
913915
----
914916
====
915917

0 commit comments

Comments
 (0)