diff --git a/djtut/Pipfile b/djtut/Pipfile index 017ca0a..daa0ff2 100644 --- a/djtut/Pipfile +++ b/djtut/Pipfile @@ -3,9 +3,17 @@ name = "pypi" url = "https://pypi.org/simple" verify_ssl = true +[dev-packages] +black = "*" +pytest = "*" +pytest-django = "*" + [packages] Django = "<2.0" psycopg2 = "<2.8" [requires] python_version = "3.7" + +[pipenv] +allow_prereleases = true diff --git a/djtut/Pipfile.lock b/djtut/Pipfile.lock index f1f1e9d..281a429 100644 --- a/djtut/Pipfile.lock +++ b/djtut/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "091688c1bd388439552d0e26d333666867899615bbcafabf614f67f1466fd51d" + "sha256": "b34be6918d1424822217ca7656f0c09eae51b584a40c8a902cc0d589db10a1b8" }, "pipfile-spec": 6, "requires": { @@ -68,5 +68,181 @@ "version": "==2019.3" } }, - "develop": {} + "develop": { + "appdirs": { + "hashes": [ + "sha256:9e5896d1372858f8dd3344faf4e5014d21849c756c8d5701f78f8a103b372d92", + "sha256:d8b24664561d0d34ddfaec54636d502d7cea6e29c3eaf68f3df6180863e2166e" + ], + "version": "==1.4.3" + }, + "attrs": { + "hashes": [ + "sha256:08a96c641c3a74e44eb59afb61a24f2cb9f4d7188748e76ba4bb5edfa3cb7d1c", + "sha256:f7b7ce16570fe9965acd6d30101a28f62fb4a7f9e926b3bbc9b61f8b04247e72" + ], + "version": "==19.3.0" + }, + "black": { + "hashes": [ + "sha256:1b30e59be925fafc1ee4565e5e08abef6b03fe455102883820fe5ee2e4734e0b", + "sha256:c2edb73a08e9e0e6f65a0e6af18b059b8b1cdd5bef997d7a0b181df93dc81539" + ], + "index": "pypi", + "version": "==19.10b0" + }, + "click": { + "hashes": [ + "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc", + "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a" + ], + "version": "==7.1.1" + }, + "importlib-metadata": { + "hashes": [ + "sha256:06f5b3a99029c7134207dd882428a66992a9de2bef7c2b699b5641f9886c3302", + "sha256:b97607a1a18a5100839aec1dc26a1ea17ee0d93b20b0f008d80a5a050afb200b" + ], + "markers": "python_version < '3.8'", + "version": "==1.5.0" + }, + "more-itertools": { + "hashes": [ + "sha256:5dd8bcf33e5f9513ffa06d5ad33d78f31e1931ac9a18f33d37e77a180d393a7c", + "sha256:b1ddb932186d8a6ac451e1d95844b382f55e12686d51ca0c68b6f61f2ab7a507" + ], + "version": "==8.2.0" + }, + "packaging": { + "hashes": [ + "sha256:3c292b474fda1671ec57d46d739d072bfd495a4f51ad01a055121d81e952b7a3", + "sha256:82f77b9bee21c1bafbf35a84905d604d5d1223801d639cf3ed140bd651c08752" + ], + "version": "==20.3" + }, + "pathspec": { + "hashes": [ + "sha256:163b0632d4e31cef212976cf57b43d9fd6b0bac6e67c26015d611a647d5e7424", + "sha256:562aa70af2e0d434367d9790ad37aed893de47f1693e4201fd1d3dca15d19b96" + ], + "version": "==0.7.0" + }, + "pluggy": { + "hashes": [ + "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0", + "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d" + ], + "version": "==0.13.1" + }, + "py": { + "hashes": [ + "sha256:5e27081401262157467ad6e7f851b7aa402c5852dbcb3dae06768434de5752aa", + "sha256:c20fdd83a5dbc0af9efd622bee9a5564e278f6380fffcacc43ba6f43db2813b0" + ], + "version": "==1.8.1" + }, + "pyparsing": { + "hashes": [ + "sha256:4c830582a84fb022400b85429791bc551f1f4871c33f23e44f353119e92f969f", + "sha256:c342dccb5250c08d45fd6f8b4a559613ca603b57498511740e65cd11a2e7dcec" + ], + "version": "==2.4.6" + }, + "pytest": { + "hashes": [ + "sha256:0e5b30f5cb04e887b91b1ee519fa3d89049595f428c1db76e73bd7f17b09b172", + "sha256:84dde37075b8805f3d1f392cc47e38a0e59518fb46a431cfdaf7cf1ce805f970" + ], + "index": "pypi", + "version": "==5.4.1" + }, + "pytest-django": { + "hashes": [ + "sha256:456fa6854d04ee625d6bbb8b38ca2259e7040a6f93333bfe8bc8159b7e987203", + "sha256:489b904f695f9fb880ce591cf5a4979880afb467763b1f180c07574554bdfd26" + ], + "index": "pypi", + "version": "==3.8.0" + }, + "regex": { + "hashes": [ + "sha256:01b2d70cbaed11f72e57c1cfbaca71b02e3b98f739ce33f5f26f71859ad90431", + "sha256:046e83a8b160aff37e7034139a336b660b01dbfe58706f9d73f5cdc6b3460242", + "sha256:113309e819634f499d0006f6200700c8209a2a8bf6bd1bdc863a4d9d6776a5d1", + "sha256:200539b5124bc4721247a823a47d116a7a23e62cc6695744e3eb5454a8888e6d", + "sha256:25f4ce26b68425b80a233ce7b6218743c71cf7297dbe02feab1d711a2bf90045", + "sha256:269f0c5ff23639316b29f31df199f401e4cb87529eafff0c76828071635d417b", + "sha256:5de40649d4f88a15c9489ed37f88f053c15400257eeb18425ac7ed0a4e119400", + "sha256:7f78f963e62a61e294adb6ff5db901b629ef78cb2a1cfce3cf4eeba80c1c67aa", + "sha256:82469a0c1330a4beb3d42568f82dffa32226ced006e0b063719468dcd40ffdf0", + "sha256:8c2b7fa4d72781577ac45ab658da44c7518e6d96e2a50d04ecb0fd8f28b21d69", + "sha256:974535648f31c2b712a6b2595969f8ab370834080e00ab24e5dbb9d19b8bfb74", + "sha256:99272d6b6a68c7ae4391908fc15f6b8c9a6c345a46b632d7fdb7ef6c883a2bbb", + "sha256:9b64a4cc825ec4df262050c17e18f60252cdd94742b4ba1286bcfe481f1c0f26", + "sha256:9e9624440d754733eddbcd4614378c18713d2d9d0dc647cf9c72f64e39671be5", + "sha256:9ff16d994309b26a1cdf666a6309c1ef51ad4f72f99d3392bcd7b7139577a1f2", + "sha256:b33ebcd0222c1d77e61dbcd04a9fd139359bded86803063d3d2d197b796c63ce", + "sha256:bba52d72e16a554d1894a0cc74041da50eea99a8483e591a9edf1025a66843ab", + "sha256:bed7986547ce54d230fd8721aba6fd19459cdc6d315497b98686d0416efaff4e", + "sha256:c7f58a0e0e13fb44623b65b01052dae8e820ed9b8b654bb6296bc9c41f571b70", + "sha256:d58a4fa7910102500722defbde6e2816b0372a4fcc85c7e239323767c74f5cbc", + "sha256:f1ac2dc65105a53c1c2d72b1d3e98c2464a133b4067a51a3d2477b28449709a0" + ], + "version": "==2020.2.20" + }, + "six": { + "hashes": [ + "sha256:236bdbdce46e6e6a3d61a337c0f8b763ca1e8717c03b369e87a7ec7ce1319c0a", + "sha256:8f3cd2e254d8f793e7f3d6d9df77b92252b52637291d0f0da013c76ea2724b6c" + ], + "version": "==1.14.0" + }, + "toml": { + "hashes": [ + "sha256:229f81c57791a41d65e399fc06bf0848bab550a9dfd5ed66df18ce5f05e73d5c", + "sha256:235682dd292d5899d361a811df37e04a8828a5b1da3115886b73cf81ebc9100e" + ], + "version": "==0.10.0" + }, + "typed-ast": { + "hashes": [ + "sha256:0666aa36131496aed8f7be0410ff974562ab7eeac11ef351def9ea6fa28f6355", + "sha256:0c2c07682d61a629b68433afb159376e24e5b2fd4641d35424e462169c0a7919", + "sha256:249862707802d40f7f29f6e1aad8d84b5aa9e44552d2cc17384b209f091276aa", + "sha256:24995c843eb0ad11a4527b026b4dde3da70e1f2d8806c99b7b4a7cf491612652", + "sha256:269151951236b0f9a6f04015a9004084a5ab0d5f19b57de779f908621e7d8b75", + "sha256:4083861b0aa07990b619bd7ddc365eb7fa4b817e99cf5f8d9cf21a42780f6e01", + "sha256:498b0f36cc7054c1fead3d7fc59d2150f4d5c6c56ba7fb150c013fbc683a8d2d", + "sha256:4e3e5da80ccbebfff202a67bf900d081906c358ccc3d5e3c8aea42fdfdfd51c1", + "sha256:6daac9731f172c2a22ade6ed0c00197ee7cc1221aa84cfdf9c31defeb059a907", + "sha256:715ff2f2df46121071622063fc7543d9b1fd19ebfc4f5c8895af64a77a8c852c", + "sha256:73d785a950fc82dd2a25897d525d003f6378d1cb23ab305578394694202a58c3", + "sha256:8c8aaad94455178e3187ab22c8b01a3837f8ee50e09cf31f1ba129eb293ec30b", + "sha256:8ce678dbaf790dbdb3eba24056d5364fb45944f33553dd5869b7580cdbb83614", + "sha256:aaee9905aee35ba5905cfb3c62f3e83b3bec7b39413f0a7f19be4e547ea01ebb", + "sha256:bcd3b13b56ea479b3650b82cabd6b5343a625b0ced5429e4ccad28a8973f301b", + "sha256:c9e348e02e4d2b4a8b2eedb48210430658df6951fa484e59de33ff773fbd4b41", + "sha256:d205b1b46085271b4e15f670058ce182bd1199e56b317bf2ec004b6a44f911f6", + "sha256:d43943ef777f9a1c42bf4e552ba23ac77a6351de620aa9acf64ad54933ad4d34", + "sha256:d5d33e9e7af3b34a40dc05f498939f0ebf187f07c385fd58d591c533ad8562fe", + "sha256:fc0fea399acb12edbf8a628ba8d2312f583bdbdb3335635db062fa98cf71fca4", + "sha256:fe460b922ec15dd205595c9b5b99e2f056fd98ae8f9f56b888e7a17dc2b757e7" + ], + "version": "==1.4.1" + }, + "wcwidth": { + "hashes": [ + "sha256:8fd29383f539be45b20bd4df0dc29c20ba48654a41e661925e612311e9f3c603", + "sha256:f28b3e8a6483e5d49e7f8949ac1a78314e740333ae305b4ba5defd3e74fb37a8" + ], + "version": "==0.1.8" + }, + "zipp": { + "hashes": [ + "sha256:aa36550ff0c0b7ef7fa639055d797116ee891440eac1a56f378e2d3179e0320b", + "sha256:c599e4d75c98f6798c509911d08a22e6c021d074469042177c8c86fb92eefd96" + ], + "version": "==3.1.0" + } + } } diff --git a/djtut/mysite/mysite/settings.py b/djtut/mysite/mysite/settings.py index 3eb36fe..e27eb68 100644 --- a/djtut/mysite/mysite/settings.py +++ b/djtut/mysite/mysite/settings.py @@ -47,7 +47,8 @@ DATABASES = { "HOST": "localhost", "PORT": "5432", "PASSWORD": "notsecurelol", - } + }, + "test": {"ENGINE": "django.db.backends.sqlite3", "NAME": "test-mysite.sqlite3",}, } AUTH_PASSWORD_VALIDATORS = [ { diff --git a/djtut/mysite/polls/models.py b/djtut/mysite/polls/models.py index 380716f..af049dc 100644 --- a/djtut/mysite/polls/models.py +++ b/djtut/mysite/polls/models.py @@ -1,3 +1,6 @@ +import datetime + +from django.utils import timezone from django.db import models @@ -5,6 +8,10 @@ class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField("date published") + def was_published_recently(self): + now = timezone.now() + return now - datetime.timedelta(days=1) <= self.pub_date <= now + class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) diff --git a/djtut/mysite/polls/tests.py b/djtut/mysite/polls/tests.py index 7ce503c..d8bf075 100644 --- a/djtut/mysite/polls/tests.py +++ b/djtut/mysite/polls/tests.py @@ -1,3 +1,11 @@ -from django.test import TestCase +import datetime -# Create your tests here. +from django.utils import timezone + +from .models import Question + + +def test_was_published_recently_with_future_question(): + time = timezone.now() + datetime.timedelta(days=30) + future_question = Question(pub_date=time) + assert not future_question.was_published_recently() diff --git a/djtut/mysite/polls/urls.py b/djtut/mysite/polls/urls.py index 54c3961..95418cf 100644 --- a/djtut/mysite/polls/urls.py +++ b/djtut/mysite/polls/urls.py @@ -4,8 +4,8 @@ from . import views app_name = "polls" urlpatterns = [ - url("^$", views.index, name="index"), - url("^(?P[0-9]+)/$", views.detail, name="detail"), - url("^(?P[0-9]+)/results/$", views.results, name="results"), + url("^$", views.IndexView.as_view(), name="index"), + url("^(?P[0-9]+)/$", views.DetailView.as_view(), name="detail"), + url("^(?P[0-9]+)/results/$", views.ResultsView.as_view(), name="results",), url("^(?P[0-9]+)/vote/$", views.vote, name="vote"), ] diff --git a/djtut/mysite/polls/views.py b/djtut/mysite/polls/views.py index 4e7d5d6..a2b8535 100644 --- a/djtut/mysite/polls/views.py +++ b/djtut/mysite/polls/views.py @@ -1,26 +1,27 @@ -from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect, HttpResponse +from django.shortcuts import render, get_object_or_404 from django.urls import reverse +from django.views import generic -from .models import Question +from .models import Choice, Question -def index(request): - latest_question_list = Question.objects.order_by("-pub_date")[:5] - context = { - "latest_question_list": latest_question_list, - } - return render(request, "polls/index.html", context) +class IndexView(generic.ListView): + template_name = "polls/index.html" + context_object_name = "latest_question_list" + def get_queryset(self): + return Question.objects.order_by("-pub_date")[:5] -def detail(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, "polls/detail.html", {"question": question}) +class DetailView(generic.DetailView): + model = Question + template_name = "polls/detail.html" -def results(request, question_id): - question = get_object_or_404(Question, pk=question_id) - return render(request, "polls/results.html", {"question": question}) + +class ResultsView(generic.DetailView): + model = Question + template_name = "polls/results.html" def vote(request, question_id): diff --git a/djtut/mysite/pytest.ini b/djtut/mysite/pytest.ini new file mode 100644 index 0000000..c5f935f --- /dev/null +++ b/djtut/mysite/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +DJANGO_SETTINGS_MODULE = mysite.settings