Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #58

Open
wants to merge 5 commits into
base: practice/start
Choose a base branch
from
Open

Main #58

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# env
/django-env/

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
Binary file modified toh/db.sqlite3
Binary file not shown.
Binary file modified toh/hero/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/__pycache__/admin.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/__pycache__/apps.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/__pycache__/models.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/__pycache__/views.cpython-39.pyc
Binary file not shown.
4 changes: 3 additions & 1 deletion toh/hero/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.contrib import admin
from .models import Hero

# Register your models here.
# This will register Hero model
admin.site.register(Hero)
Binary file modified toh/hero/migrations/__pycache__/0001_initial.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/migrations/__pycache__/0002_hero_age.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/migrations/__pycache__/0003_team.cpython-39.pyc
Binary file not shown.
Binary file modified toh/hero/migrations/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
27 changes: 25 additions & 2 deletions toh/hero/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
from django.test import TestCase
from pydoc import cli
from django.test import TestCase, Client
from .models import Hero

# Create your tests here.
class HeroTestCase(TestCase):
def setUp(self) -> None:
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) -> None:
client = Client()
response = client.get('/hero/1/')

self.assertEqual(response.status_code, 200)
self.assertIn('1', response.content.decode())

def test_index(self):
client = Client()
response = client.get('/hero/')
self.assertIn('1', response.content.decode())
response = client.get('/hero/')
self.assertIn('2', response.content.decode())
4 changes: 3 additions & 1 deletion toh/hero/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from . import views

urlpatterns = [
path('', views.hero_list),
path('', views.index),
path('list/', views.hero_list),
path('<int:id>/', views.id, name='hero_id'),
path('token/', views.token, name='token'),
path('<str:name>/', views.name, name='hero_name'),
path('info/<int:id>/', views.hero_info, name='hero_info'),
]
18 changes: 14 additions & 4 deletions toh/hero/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
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!')
@ensure_csrf_cookie
def token(request):
if request.method == 'GET':
return HttpResponse(status=204)
else:
return HttpResponseNotAllowed(['GET'])

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"]}')

def id(request, id):
return HttpResponse(f'Your id is {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()]
Expand Down
Binary file modified toh/toh/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file modified toh/toh/__pycache__/settings.cpython-39.pyc
Binary file not shown.
Binary file modified toh/toh/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file modified toh/toh/__pycache__/wsgi.cpython-39.pyc
Binary file not shown.