Skip to content

Commit

Permalink
Model for list Items and associated migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisipi committed Mar 26, 2022
1 parent 2644a40 commit 3adfa47
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 11 deletions.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,40 @@ INSTALLED_APPS = [

Invoke Django test runner (to run unit tests):
`python manage.py test`
You can also add the App name:
`python manage.py test lists`


## Django Migration
When you change something in the Django model (for example changing or adding a field), the database tables that store these models need to be changed too. A Django migration propagates these changes in the model into the database schema.
When you change something in the Django model (for example change or add a field), the database tables that store these models need to be changed too. A Django migration propagates these changes in the model into the database schema.

```
python manage.py makemigrations
```
```

## Overview Files

├── db.sqlite3
├── functional_tests.py <<< Functional tests
├── geckodriver.log
├── lists <<< Directory of lists App
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations <<< Directory containing Django migrations
│ ├── models.py <<< Django models
│ ├── templates <<< HTML template directory
│ ├── tests.py <<< Unittests of lists App
│ └── views.py <<< Django views
├── manage.py <<< Django's management script
├── README.md
├── superlists <<< Django's main project directory
│ ├── __init__.py
│ ├── settings.py <<< register the lists App
│ ├── urls.py <<< URL patterns mapping URLs to views
│ └── wsgi.py
└── virtualenv
├── bin
├── lib
├── pyvenv.cfg
└── selenium
18 changes: 12 additions & 6 deletions functional_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ def test_online_todo_list(self):
inputbox.send_keys(Keys.ENTER)
time.sleep(1)

table = self.browser.find_element_by_id("id_list_table")
rows = table.find_elements_by_tag_name("tr")
self.assertTrue(
any(row.text == "1: Buy peacock feathers" for row in rows),
"New to-do item did not appear in table",
)
self.check_for_row_in_list_table("1: Buy peacock feathers")

# There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very methodical)
inputbox = self.browser.find_element_by_id("id_new_item")
inputbox.send_keys("Use peacock feathers to make a fly")
inputbox.send_keys(Keys.ENTER)
time.sleep(1)

# The page updates again, and now shows both items on her list
self.check_for_row_in_list_table("1: Buy peacock feathers")
self.check_for_row_in_list_table("2: Use peacock feathers to make a fly")

# Edith wonders whether the site will remember her list. Then she sees
# that the site has generated a unique URL for her -- there is some
Expand All @@ -59,6 +60,11 @@ def test_online_todo_list(self):

self.fail("Finish test!")

def check_for_row_in_list_table(self, row_text):
table = self.browser.find_element_by_id("id_list_table")
rows = table.find_elements_by_tag_name("tr")
self.assertIn(row_text, [row.text for row in rows])


if __name__ == "__main__":
# unittest.main(warnings="ignore")
Expand Down
22 changes: 22 additions & 0 deletions lists/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2022-03-25 18:33
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Item',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
20 changes: 20 additions & 0 deletions lists/migrations/0002_item_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2022-03-25 18:44
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('lists', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='item',
name='text',
field=models.TextField(default=''),
),
]
4 changes: 4 additions & 0 deletions lists/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from django.db import models

# Create your models here.


class Item(models.Model):
text = models.TextField(default="")
5 changes: 5 additions & 0 deletions lists/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ <h1>Your To-Do List</h1>
</form>

<table id="id_list_table">
<tr>
<td>1: {{ new_item_text }}</td>
</tr>

</table>

</body>

</html>
27 changes: 27 additions & 0 deletions lists/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

from lists.views import home_page

from lists.models import Item


# Create your tests here.


Expand Down Expand Up @@ -38,3 +41,27 @@ def test_home_page_returns_correct_html_using_client(self):
def test_uses_home_template(self):
response = self.client.get("/")
self.assertTemplateUsed(response, "home.html")

def test_can_save_a_POST_request(self):
response = self.client.post("/", data={"item_text": "A new list item"})
self.assertIn("A new list item", response.content.decode())
self.assertTemplateUsed(response, "home.html")


class ItemModelTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = "The first list item"
first_item.save()

second_item = Item()
second_item.text = "Item the second"
second_item.save()

saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)

first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, "The first list item")
self.assertEqual(second_saved_item.text, "Item the second")
9 changes: 7 additions & 2 deletions lists/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@


def home_page(request):
# return HttpResponse("<html><title>To-Do Lists</title></html>")
return render(request, "home.html")
return render(
request,
"home.html",
{
"new_item_text": request.POST.get("item_text", ""),
},
)
2 changes: 1 addition & 1 deletion superlists/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
urlpatterns = [
# url(r'^admin/', admin.site.urls),
#
# Map root URL to home_page
# Map root URL / to home_page
url(r"^$", views.home_page, name="home")
]

0 comments on commit 3adfa47

Please sign in to comment.