diff --git a/toh/db.sqlite3 b/toh/db.sqlite3 index 69d2e6a..76c724d 100644 Binary files a/toh/db.sqlite3 and b/toh/db.sqlite3 differ diff --git a/toh/hero/admin.py b/toh/hero/admin.py index 8c38f3f..95703e3 100644 --- a/toh/hero/admin.py +++ b/toh/hero/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import Hero # Register your models here. +admin.site.register(Hero) \ No newline at end of file diff --git a/toh/hero/tests.py b/toh/hero/tests.py index 7ce503c..0954d11 100644 --- a/toh/hero/tests.py +++ b/toh/hero/tests.py @@ -1,3 +1,26 @@ -from django.test import TestCase +from django.test import TestCase, Client +from .models import Hero # Create your tests here. +class HeroTestCase(TestCase): + def setUp(self): + Hero.objects.create(name="Superman") + Hero.objects.create(name="Batman") + Hero.objects.create(name="Ironman") + + def test_hero_count(self): + self.assertEqual(Hero.objects.all().count(), 3) + + def test_hero_id(self): + client = Client() + response = client.get('/hero/10/') + + self.assertEqual(response.status_code, 200) + self.assertIn('10', response.content.decode()) + + def test_hero_session(self): + client = Client() + response = client.get('/hero/') + self.assertIn('1', response.content.decode()) + response = client.get('/hero/') + self.assertIn('2', response.content.decode()) diff --git a/toh/hero/urls.py b/toh/hero/urls.py index 7384e1e..5b91a28 100644 --- a/toh/hero/urls.py +++ b/toh/hero/urls.py @@ -2,8 +2,10 @@ from . import views urlpatterns = [ - path('', views.hero_list), + path('list/', views.hero_list), + path('', views.index), path('/', views.id, name='hero_id'), - path('/', views.name, name='hero_name'), + # path('/', views.name, name='hero_name'), path('info//', views.hero_info, name='hero_info'), + path('token/', views.token, name='token') ] \ No newline at end of file diff --git a/toh/hero/views.py b/toh/hero/views.py index d86c570..0abc926 100644 --- a/toh/hero/views.py +++ b/toh/hero/views.py @@ -1,11 +1,15 @@ from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, JsonResponse -from django.views.decorators.csrf import csrf_exempt +from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie import json from json.decoder import JSONDecodeError from .models import Hero -# def index(request): -# return HttpResponse('Hello, world!') +def index(request): + if 'visit_count' not in request.session: + request.session['visit_count'] = 1 + else: + request.session['visit_count'] += 1 + return HttpResponse(f'Hello, world! You visited {request.session["visit_count"]} times.\n') def id(request, id): return HttpResponse(f'Your id is {id}!') @@ -13,7 +17,6 @@ def id(request, id): def name(request, name): return HttpResponse(f'Your name is {name}!') -@csrf_exempt def hero_list(request): if request.method == 'GET': hero_all_list = [hero for hero in Hero.objects.all().values()] @@ -50,4 +53,11 @@ def hero_info(request, id): response_dict = {"id": hero.id, "name": hero.name, "age": hero.age} return JsonResponse(response_dict, status=200) else: - return HttpResponseNotAllowed(['GET', 'PUT']) \ No newline at end of file + return HttpResponseNotAllowed(['GET', 'PUT']) + +@ensure_csrf_cookie +def token(request): + if request.method == "GET": + return HttpResponse(status=204) + else: + return HttpResponseNotAllowed(['GET']) \ No newline at end of file