From 8575b40572df968d7c9a8f862beb1b6efe18fd6b Mon Sep 17 00:00:00 2001 From: Will Holderness Date: Tue, 28 Jan 2020 23:18:58 -0500 Subject: [PATCH] Tweaking tests and task descriptions. --- module4/tests.py | 44 ++++++++++++++++++++++---------------------- tasks_module4.md | 6 +++--- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/module4/tests.py b/module4/tests.py index 3236681..39ae681 100644 --- a/module4/tests.py +++ b/module4/tests.py @@ -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'): @@ -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) @@ -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() @@ -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 @@ -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." @@ -286,4 +286,4 @@ def test_task12_make_migrations(self): - \ No newline at end of file + diff --git a/tasks_module4.md b/tasks_module4.md index 2a664b4..dd7c126 100644 --- a/tasks_module4.md +++ b/tasks_module4.md @@ -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): @@ -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): @@ -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