Background Image
Table of Contents Table of Contents
Previous Page  423 / 478 Next Page
Information
Show Menu
Previous Page 423 / 478 Next Page
Page Background

An Exercise for the Reader

Here’s an outline of the steps to take to get this new feature implemented:

1. We’ll need a new section in

list.html

, with, at first, a form with an input box for an

email address. That should get the FT one step further.

2. Next, we’ll need a view for the form to submit to. Start by defining the URL in the

template, maybe something like

lists/<list_id>/share

.

3. Then, our first unit test. It can be just enough to get a placeholder view in. We want

the view to respond to POST requests, and it should respond with a redirect back

to the list page, so the test could be called something like

ShareList

Test.test_post_redirects_to_lists_page

.

4. We build out our placeholder view, as just a two-liner that finds a list and redirects

to it.

5. We can then write a new unit test which creates a user and a list, does a POST with

their email address, and checks the user is added to

list_.shared_with.all()

(a

similar ORMusage to “My Lists”). That

shared_with

attribute won’t exist yet, we’re

going outside-in.

6. So before we can get this test to pass, we have to move down to the model layer.

The next test, in

test_models.py

, can check that a list has a

shared_with.add

method,

which can be called with a user’s email address and then check the lists’

shared_with.all()

queryset, which will subsequently contain that user.

7. You’ll then need a

ManyToManyField

. You’ll probably see an error message about a

clashing

related_name

, which you’ll find a solution to if you look around the

Django docs.

8. It will need a database migration.

9. That should get the model tests passing. Pop back up to fix the view test.

10. You may find the redirect view test fails, because it’s not sending a valid POST

request. You can either choose to ignore invalid inputs, or adjust the test to send a

valid POST.

11. Then back up to the template level; on the “My Lists” page we’ll want a

<ul>

with a

for loop of the lists shared with the user. On the lists page, we also want to show

who the list is shared with, as well as mention of who the list owner is. Look back

at the FT for the correct classes and IDs to use. You could have brief unit tests for

each of these if you like, as well.

12. You might find that spinning up the site with

runserver

will help you iron out any

bugs, as well as fine-tune the layout and aesthetics. If you use a private browser

session, you’ll be able to log multiple users in.

An Exercise for the Reader

|

395