Skip to content

Commit

Permalink
Tweaking tests and task descriptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Holderness committed Jan 29, 2020
1 parent 96ab4d3 commit 8575b40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
44 changes: 22 additions & 22 deletions module4/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,6 @@ def check_model_file(self):
z.value.func.value.attr == 'name' and
z.value.func.attr == 'lower'):
self.clean_assign_found = True

elif x.name == 'BlogPost':
for y in x.body:
if (isinstance(y, ast.Assign) and
y.targets[0].id == 'tags' and
y.value.func.value.id == 'models' and
y.value.func.attr == 'ManyToManyField'):
self.tags_field_found = True
if (len(y.value.keywords) > 0 and
y.value.keywords[0].arg == 'related_name' and
getattr(y.value.keywords[0].value, self.value) == 'posts' and
len(y.value.args) > 0 and
getattr(y.value.args[0], self.value) == 'Tag'):
self.tag_many_to_many_found = True
# Task 9
if (isinstance(y, ast.FunctionDef) and
y.name == 'get_absolute_url'):
Expand All @@ -98,14 +84,28 @@ def check_model_file(self):
self.get_absolute_url_return_found = True
if y.body[0].value.func.id == 'reverse':
self.get_absolute_url_reverse_found = True
if getattr(y.body[0].value.args[0], self.value) == 'post':
if getattr(y.body[0].value.args[0], self.value) == 'tag_posts':
self.get_absolute_url_post_found = True
if (y.body[0].value.keywords[0].arg == 'args' and
y.body[0].value.keywords[0].value.elts[0].func.id == 'str' and
y.body[0].value.keywords[0].value.elts[0].args[0].value.id == 'self' and
y.body[0].value.keywords[0].value.elts[0].args[0].attr == 'id'):
y.body[0].value.keywords[0].value.elts[0].args[0].attr == 'name'):
self.get_absolute_url_args_correct = True


elif x.name == 'BlogPost':
for y in x.body:
if (isinstance(y, ast.Assign) and
y.targets[0].id == 'tags' and
y.value.func.value.id == 'models' and
y.value.func.attr == 'ManyToManyField'):
self.tags_field_found = True
if (len(y.value.keywords) > 0 and
y.value.keywords[0].arg == 'related_name' and
getattr(y.value.keywords[0].value, self.value) == 'posts' and
len(y.value.args) > 0 and
getattr(y.value.args[0], self.value) == 'Tag'):
self.tag_many_to_many_found = True
except Exception as e:
print(e)

Expand All @@ -126,12 +126,12 @@ def test_task2_clean_method_exists(self):
"""Add clean method to sanitize input."""
self.check_model_file()

self.assertTrue(self.clean_method_found, msg="Did you implement the `clean` method in the `tag` model class?")
self.assertTrue(self.clean_method_found, msg="Did you implement the `clean` method in the `Tag` model class?")
self.assertTrue(self.clean_assign_found, msg="Did you assign to `self.name` in the `clean` method?")

def test_task3_str_exists(self):
self.check_model_file()
self.assertTrue(self.str_method_found, msg="Did you implement the `__str__` method in the `tag` model class?")
self.assertTrue(self.str_method_found, msg="Did you implement the `__str__` method in the `Tag` model class?")

def test_task4_many_to_many_exists(self):
self.check_model_file()
Expand Down Expand Up @@ -240,8 +240,8 @@ def test_task9_url_reverser(self):
self.assertTrue(self.get_absolute_url_found, msg="The method `get_absolute_url()` does not exist in the BlogPost model.")
self.assertTrue(self.get_absolute_url_return_found, msg="The method `get_absolute_url()` does not return anything.")
self.assertTrue(self.get_absolute_url_reverse_found, msg="The method `get_absolute_url()` does not call `reverse()`.")
self.assertTrue(self.get_absolute_url_post_found, msg="In `get_absolute_url()` the first argument in `reverse()` should be `'post'`.")
self.assertTrue(self.get_absolute_url_args_correct, msg="In `get_absolute_url()` the second argument in `reverse()` should be `args=[str(self.id)]`.")
self.assertTrue(self.get_absolute_url_post_found, msg="In `get_absolute_url()` the first argument in `reverse()` should be `'tag_posts'`.")
self.assertTrue(self.get_absolute_url_args_correct, msg="In `get_absolute_url()` the second argument in `reverse()` should be `args=[str(self.name)]`.")

def test_task10_uncomment_templates(self):
# Remove `{% comment %}` and {% endcomment %} tags from
Expand Down Expand Up @@ -277,7 +277,7 @@ def test_task11_admin_register_tag(self):
pass

self.assertTrue(import_Tag_found, msg="Did you import `Tag`?")
self.assertTrue(admin_site_register_found, msg="Did you register the Tag model to the admin site?")
self.assertTrue(admin_site_register_found, msg="Did you register the `Tag` model to the admin site?")

def test_task12_make_migrations(self):
msg = "Did you use `manage.py makemigrations` to create the `Tag` migrations file? Don't forget to `add` it to the git repo."
Expand All @@ -286,4 +286,4 @@ def test_task12_make_migrations(self):





6 changes: 3 additions & 3 deletions tasks_module4.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def tag_posts(request, name):

# Pass title to template

Continuing in the `tag_posts` method, we want to pass a title for `Tag` page to the template. First, clean the incoming `name` parameter by setting it equal to a lowercase version of itself (e.g. `name.lower()` ). Then, create a `title` variable and set it equal to `"Posts about {}".format(name)`. To pass the `title` to the template, add a dictionary as another argument to the end of the `render()` call. In the dictionary, set `'title'` to the new `title` variable.
Continuing in the `tag_posts` method, we want to pass a title for the `Tag` page to the template. Before the `return` line, clean the incoming `name` parameter by setting it equal to a lowercase version of itself (e.g. `name.lower()` ). Then, also before the `return`, create a `title` variable and set it equal to `"Posts about {}".format(name)`. To pass the `title` to the template, add a dictionary as another argument to the end of the `render()` call. In the dictionary, set `'title'` to the new `title` variable.

```python
def tag_posts(request, name):
Expand All @@ -56,7 +56,7 @@ def tag_posts(request, name):

# Find posts and pass to template

Since we want to display all of the posts with the name tag, let's first find the Tag object. Call `get_object_or_404(Tag, name=name)` and set that equal to a variable named `tag`. Then, get all of the posts with that tag by calling `BlogPost.objects.filter(tags=tag)` and setting the result equal to a variable named `posts`. Finally, pass the `posts` list to the template by adding `'posts':posts` to the dict in the `render()` call, `{'posts':posts, 'title':title}`.
Since we want to display all of the posts with the name tag, let's first find the Tag object. Right after the `title` line, call `get_object_or_404(Tag, name=name)` and set that equal to a variable named `tag`. Then, get all of the posts with that tag by calling `BlogPost.objects.filter(tags=tag)` and setting the result equal to a variable named `posts`. Finally, pass the `posts` list to the template by adding `'posts':posts` to the dict in the `render()` call, `{'posts':posts, 'title':title}`.

```python
def tag_posts(request, name):
Expand Down Expand Up @@ -87,7 +87,7 @@ To generate a url for the specified tag page, we can use `django.urls` `reverse(

# Uncomment tag section of index and post templates

Remove `{% comment %}` and `{% endcomment %}` tags from both `templates/mainapp/snippet_post_list.html` and `templates/mainapp/post.html`.
Remove `{% comment %}` and `{% endcomment %}` tags from both `mainapp/templates/mainapp/snippet_post_list.html` and `mainapp/templates/mainapp/post.html`.

# Add tags to the admin site

Expand Down

0 comments on commit 8575b40

Please sign in to comment.