From ac06653f9471a7162b55b505005efbb6685c8f7d Mon Sep 17 00:00:00 2001 From: jaewooMaeng Date: Mon, 10 Oct 2022 22:52:46 +0900 Subject: [PATCH 1/2] start --- toh/README.md | 17 --- toh/hero/__init__.py | 0 toh/hero/admin.py | 3 - toh/hero/apps.py | 6 - toh/hero/migrations/0001_initial.py | 21 ---- toh/hero/migrations/0002_hero_age.py | 18 --- toh/hero/migrations/0003_remove_hero_age.py | 17 --- toh/hero/migrations/0004_hero_age.py | 18 --- toh/hero/migrations/0005_team.py | 23 ---- toh/hero/migrations/0006_hero_score.py | 18 --- toh/hero/migrations/__init__.py | 0 toh/hero/models.py | 27 ----- toh/hero/tests.py | 3 - toh/hero/urls.py | 11 -- toh/hero/views.py | 58 --------- toh/manage.py | 22 ---- toh/toh/__init__.py | 0 toh/toh/asgi.py | 16 --- toh/toh/settings.py | 126 -------------------- toh/toh/urls.py | 22 ---- toh/toh/wsgi.py | 16 --- 21 files changed, 442 deletions(-) delete mode 100644 toh/README.md delete mode 100644 toh/hero/__init__.py delete mode 100644 toh/hero/admin.py delete mode 100644 toh/hero/apps.py delete mode 100644 toh/hero/migrations/0001_initial.py delete mode 100644 toh/hero/migrations/0002_hero_age.py delete mode 100644 toh/hero/migrations/0003_remove_hero_age.py delete mode 100644 toh/hero/migrations/0004_hero_age.py delete mode 100644 toh/hero/migrations/0005_team.py delete mode 100644 toh/hero/migrations/0006_hero_score.py delete mode 100644 toh/hero/migrations/__init__.py delete mode 100644 toh/hero/models.py delete mode 100644 toh/hero/tests.py delete mode 100644 toh/hero/urls.py delete mode 100644 toh/hero/views.py delete mode 100755 toh/manage.py delete mode 100644 toh/toh/__init__.py delete mode 100644 toh/toh/asgi.py delete mode 100644 toh/toh/settings.py delete mode 100644 toh/toh/urls.py delete mode 100644 toh/toh/wsgi.py diff --git a/toh/README.md b/toh/README.md deleted file mode 100644 index 6e3b7b7..0000000 --- a/toh/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# SWPP Practice Session #6 - -**Due: 10/3 (Thur), 21:00** - -### Today's Task -1. Fork this repo. -2. Implement today's tasks (total 3). -3. Send PR to this repo's main branch. - -Note that you need to start from creating a new Django project. (No skeleton code) -Run `django-admin startproject toh` at the root of this repo. - -``` -git clone https://github.com/{YOUR_GITHUB_ID}/swpp-p6-django-tutorial.git -cd swpp-p6-django-tutorial -django-admin startproject toh -``` diff --git a/toh/hero/__init__.py b/toh/hero/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/toh/hero/admin.py b/toh/hero/admin.py deleted file mode 100644 index 8c38f3f..0000000 --- a/toh/hero/admin.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.contrib import admin - -# Register your models here. diff --git a/toh/hero/apps.py b/toh/hero/apps.py deleted file mode 100644 index d673eaa..0000000 --- a/toh/hero/apps.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.apps import AppConfig - - -class HeroConfig(AppConfig): - default_auto_field = 'django.db.models.BigAutoField' - name = 'hero' diff --git a/toh/hero/migrations/0001_initial.py b/toh/hero/migrations/0001_initial.py deleted file mode 100644 index 707c62f..0000000 --- a/toh/hero/migrations/0001_initial.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-03 13:51 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - initial = True - - dependencies = [ - ] - - operations = [ - migrations.CreateModel( - name='Hero', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=120)), - ], - ), - ] diff --git a/toh/hero/migrations/0002_hero_age.py b/toh/hero/migrations/0002_hero_age.py deleted file mode 100644 index 0f78298..0000000 --- a/toh/hero/migrations/0002_hero_age.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 14:36 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0001_initial'), - ] - - operations = [ - migrations.AddField( - model_name='hero', - name='age', - field=models.IntegerField(blank=True, null=True), - ), - ] diff --git a/toh/hero/migrations/0003_remove_hero_age.py b/toh/hero/migrations/0003_remove_hero_age.py deleted file mode 100644 index 2602050..0000000 --- a/toh/hero/migrations/0003_remove_hero_age.py +++ /dev/null @@ -1,17 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 14:49 - -from django.db import migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0002_hero_age'), - ] - - operations = [ - migrations.RemoveField( - model_name='hero', - name='age', - ), - ] diff --git a/toh/hero/migrations/0004_hero_age.py b/toh/hero/migrations/0004_hero_age.py deleted file mode 100644 index 0fa9f9d..0000000 --- a/toh/hero/migrations/0004_hero_age.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 14:49 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0003_remove_hero_age'), - ] - - operations = [ - migrations.AddField( - model_name='hero', - name='age', - field=models.IntegerField(blank=True, null=True), - ), - ] diff --git a/toh/hero/migrations/0005_team.py b/toh/hero/migrations/0005_team.py deleted file mode 100644 index da724b3..0000000 --- a/toh/hero/migrations/0005_team.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 17:06 - -from django.db import migrations, models -import django.db.models.deletion - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0004_hero_age'), - ] - - operations = [ - migrations.CreateModel( - name='Team', - fields=[ - ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('name', models.CharField(max_length=120)), - ('leader', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='leader_set', to='hero.hero')), - ('members', models.ManyToManyField(related_name='teams', to='hero.Hero')), - ], - ), - ] diff --git a/toh/hero/migrations/0006_hero_score.py b/toh/hero/migrations/0006_hero_score.py deleted file mode 100644 index 6644fd8..0000000 --- a/toh/hero/migrations/0006_hero_score.py +++ /dev/null @@ -1,18 +0,0 @@ -# Generated by Django 3.2.6 on 2021-10-04 17:15 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('hero', '0005_team'), - ] - - operations = [ - migrations.AddField( - model_name='hero', - name='score', - field=models.IntegerField(default=0), - ), - ] diff --git a/toh/hero/migrations/__init__.py b/toh/hero/migrations/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/toh/hero/models.py b/toh/hero/models.py deleted file mode 100644 index 03e678a..0000000 --- a/toh/hero/models.py +++ /dev/null @@ -1,27 +0,0 @@ -from django.db import models - -class Hero(models.Model): - name = models.CharField(max_length=120) - age = models.IntegerField(blank=True, null=True) - score = models.IntegerField(default=0) - - def __str__(self): - return self.name - - def introduce(self): - print(f'Hello, my name is {self.name} and my score is {self.score}') - -class Team(models.Model): - name = models.CharField(max_length=120) - leader = models.ForeignKey( - Hero, - on_delete=models.CASCADE, - related_name='leader_set', - ) - members = models.ManyToManyField( - Hero, - related_name='teams', - ) - - def __str__(self): - return self.name \ No newline at end of file diff --git a/toh/hero/tests.py b/toh/hero/tests.py deleted file mode 100644 index 7ce503c..0000000 --- a/toh/hero/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.test import TestCase - -# Create your tests here. diff --git a/toh/hero/urls.py b/toh/hero/urls.py deleted file mode 100644 index 0563bad..0000000 --- a/toh/hero/urls.py +++ /dev/null @@ -1,11 +0,0 @@ -from django.urls import path - -from . import views - -urlpatterns = [ - path('', views.index, name='index'), - path('/', views.id, name='hero_id'), - path('/', views.name, name='hero_name'), - path('info/', views.hero_list, name='hero_list'), - path('info//', views.hero_info, name='hero_info') -] \ No newline at end of file diff --git a/toh/hero/views.py b/toh/hero/views.py deleted file mode 100644 index 38df04b..0000000 --- a/toh/hero/views.py +++ /dev/null @@ -1,58 +0,0 @@ -from django.http import HttpResponse, JsonResponse, HttpResponseBadRequest -from django.http.response import HttpResponseNotAllowed -from django.views.decorators.csrf import csrf_exempt - -import json -from json.decoder import JSONDecodeError - -from .models import Hero - -def index(request): - return HttpResponse('Hello, world!\n') - -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_info(request, id): - if request.method == 'GET': - hero = Hero.objects.get(id=id) - return JsonResponse({"id": hero.id, "name": hero.name, "age": hero.age}) - - elif request.method == 'PUT': - try: - body = request.body.decode() - hero_name = json.loads(body)['name'] - hero_age = json.loads(body)['age'] - except (KeyError, JSONDecodeError) as e: - return HttpResponseBadRequest() - hero = Hero.objects.get(id=id) - hero.name = hero_name - hero.age = hero_age - hero.save() - response_dict = {"id": hero.id, "name": hero.name, "age": hero.age} - return JsonResponse(response_dict, status=200) - else: - return HttpResponseNotAllowed(['GET', 'PUT']) - -@csrf_exempt -def hero_list(request): - if request.method == "GET": - hero_all_list = [hero for hero in Hero.objects.all().values()] - return JsonResponse(hero_all_list, safe=False) - elif request.method == "POST": - try: - body = request.body.decode() - hero_name = json.loads(body)['name'] - hero_age = json.loads(body)['age'] - except (KeyError, JSONDecodeError) as e: - return HttpResponseBadRequest() - hero = Hero(name=hero_name, age=hero_age) - hero.save() - response_dict = {'id': hero.id, 'name': hero.name, 'age': hero.age} - return JsonResponse(response_dict, status=201) - else: - return HttpResponseNotAllowed(["GET", "POST"]) \ No newline at end of file diff --git a/toh/manage.py b/toh/manage.py deleted file mode 100755 index 04f198e..0000000 --- a/toh/manage.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -"""Django's command-line utility for administrative tasks.""" -import os -import sys - - -def main(): - """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') - try: - from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) from exc - execute_from_command_line(sys.argv) - - -if __name__ == '__main__': - main() diff --git a/toh/toh/__init__.py b/toh/toh/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/toh/toh/asgi.py b/toh/toh/asgi.py deleted file mode 100644 index db671d6..0000000 --- a/toh/toh/asgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -ASGI config for toh project. - -It exposes the ASGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ -""" - -import os - -from django.core.asgi import get_asgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') - -application = get_asgi_application() diff --git a/toh/toh/settings.py b/toh/toh/settings.py deleted file mode 100644 index 8e283cd..0000000 --- a/toh/toh/settings.py +++ /dev/null @@ -1,126 +0,0 @@ -""" -Django settings for toh project. - -Generated by 'django-admin startproject' using Django 3.2.6. - -For more information on this file, see -https://docs.djangoproject.com/en/3.2/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/3.2/ref/settings/ -""" - -from pathlib import Path - -# Build paths inside the project like this: BASE_DIR / 'subdir'. -BASE_DIR = Path(__file__).resolve().parent.parent - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'django-insecure-_7mcsvpz575w%1&4#(u=x(@xoi*33hj^yzqbs&dn3a3bb8x5ri' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'hero.apps.HeroConfig', - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'toh.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], - }, - }, -] - -WSGI_APPLICATION = 'toh.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/3.2/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - } -} - - -# Password validation -# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/3.2/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/3.2/howto/static-files/ - -STATIC_URL = '/static/' - -# Default primary key field type -# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field - -DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/toh/toh/urls.py b/toh/toh/urls.py deleted file mode 100644 index edd5052..0000000 --- a/toh/toh/urls.py +++ /dev/null @@ -1,22 +0,0 @@ -"""toh URL Configuration - -The `urlpatterns` list routes URLs to views. For more information please see: - https://docs.djangoproject.com/en/3.2/topics/http/urls/ -Examples: -Function views - 1. Add an import: from my_app import views - 2. Add a URL to urlpatterns: path('', views.home, name='home') -Class-based views - 1. Add an import: from other_app.views import Home - 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') -Including another URLconf - 1. Import the include() function: from django.urls import include, path - 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) -""" -from django.contrib import admin -from django.urls import include, path - -urlpatterns = [ - path('hero/', include('hero.urls')), - path('admin/', admin.site.urls), -] diff --git a/toh/toh/wsgi.py b/toh/toh/wsgi.py deleted file mode 100644 index e54327b..0000000 --- a/toh/toh/wsgi.py +++ /dev/null @@ -1,16 +0,0 @@ -""" -WSGI config for toh project. - -It exposes the WSGI callable as a module-level variable named ``application``. - -For more information on this file, see -https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ -""" - -import os - -from django.core.wsgi import get_wsgi_application - -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') - -application = get_wsgi_application() From 252a48cbb5559c658a8d7aea3059f42ae9c7bb20 Mon Sep 17 00:00:00 2001 From: pacifiic Date: Thu, 13 Oct 2022 18:40:45 +0900 Subject: [PATCH 2/2] ad --- .idea/.gitignore | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 12 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/swpp-p6-django-tutorial.iml | 8 ++ .idea/vcs.xml | 6 + toh/hero/__init__.py | 0 toh/hero/admin.py | 3 + toh/hero/apps.py | 6 + toh/hero/migrations/0001_initial.py | 21 +++ toh/hero/migrations/0002_hero_age.py | 18 +++ toh/hero/migrations/0003_team.py | 23 ++++ toh/hero/migrations/0004_hero_score.py | 18 +++ toh/hero/migrations/__init__.py | 0 toh/hero/models.py | 27 ++++ toh/hero/tests.py | 3 + toh/hero/urls.py | 9 ++ toh/hero/views.py | 49 +++++++ toh/manage.py | 22 ++++ toh/toh/__init__.py | 0 toh/toh/asgi.py | 16 +++ toh/toh/settings.py | 124 ++++++++++++++++++ toh/toh/urls.py | 22 ++++ toh/toh/wsgi.py | 16 +++ 25 files changed, 429 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/swpp-p6-django-tutorial.iml create mode 100644 .idea/vcs.xml create mode 100644 toh/hero/__init__.py create mode 100644 toh/hero/admin.py create mode 100644 toh/hero/apps.py create mode 100644 toh/hero/migrations/0001_initial.py create mode 100644 toh/hero/migrations/0002_hero_age.py create mode 100644 toh/hero/migrations/0003_team.py create mode 100644 toh/hero/migrations/0004_hero_score.py create mode 100644 toh/hero/migrations/__init__.py create mode 100644 toh/hero/models.py create mode 100644 toh/hero/tests.py create mode 100644 toh/hero/urls.py create mode 100644 toh/hero/views.py create mode 100755 toh/manage.py create mode 100644 toh/toh/__init__.py create mode 100644 toh/toh/asgi.py create mode 100644 toh/toh/settings.py create mode 100644 toh/toh/urls.py create mode 100644 toh/toh/wsgi.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..cd4192d --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4d5a890 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..97319a1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/swpp-p6-django-tutorial.iml b/.idea/swpp-p6-django-tutorial.iml new file mode 100644 index 0000000..2e28675 --- /dev/null +++ b/.idea/swpp-p6-django-tutorial.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/toh/hero/__init__.py b/toh/hero/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/toh/hero/admin.py b/toh/hero/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/toh/hero/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/toh/hero/apps.py b/toh/hero/apps.py new file mode 100644 index 0000000..d673eaa --- /dev/null +++ b/toh/hero/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class HeroConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'hero' diff --git a/toh/hero/migrations/0001_initial.py b/toh/hero/migrations/0001_initial.py new file mode 100644 index 0000000..e9c983b --- /dev/null +++ b/toh/hero/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 4.1.2 on 2022-10-10 15:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Hero', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=120)), + ], + ), + ] diff --git a/toh/hero/migrations/0002_hero_age.py b/toh/hero/migrations/0002_hero_age.py new file mode 100644 index 0000000..e5ef436 --- /dev/null +++ b/toh/hero/migrations/0002_hero_age.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2022-10-10 18:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hero', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='hero', + name='age', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/toh/hero/migrations/0003_team.py b/toh/hero/migrations/0003_team.py new file mode 100644 index 0000000..3551922 --- /dev/null +++ b/toh/hero/migrations/0003_team.py @@ -0,0 +1,23 @@ +# Generated by Django 4.1.2 on 2022-10-10 18:25 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('hero', '0002_hero_age'), + ] + + operations = [ + migrations.CreateModel( + name='Team', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=120)), + ('leader', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='leader_set', to='hero.hero')), + ('members', models.ManyToManyField(related_name='teams', to='hero.hero')), + ], + ), + ] diff --git a/toh/hero/migrations/0004_hero_score.py b/toh/hero/migrations/0004_hero_score.py new file mode 100644 index 0000000..5577e28 --- /dev/null +++ b/toh/hero/migrations/0004_hero_score.py @@ -0,0 +1,18 @@ +# Generated by Django 4.1.2 on 2022-10-10 18:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('hero', '0003_team'), + ] + + operations = [ + migrations.AddField( + model_name='hero', + name='score', + field=models.IntegerField(default=0), + ), + ] diff --git a/toh/hero/migrations/__init__.py b/toh/hero/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/toh/hero/models.py b/toh/hero/models.py new file mode 100644 index 0000000..b10832f --- /dev/null +++ b/toh/hero/models.py @@ -0,0 +1,27 @@ +from django.db import models + +class Hero(models.Model): + name = models.CharField(max_length=120) + age = models.IntegerField(blank=True, null=True) + score = models.IntegerField(default=0) + + def __str__(self): + return self.name + + def introduce(self): + print(f'Hello, my name is {self.name} and my score is {self.score}') + +class Team(models.Model): + name = models.CharField(max_length=120) + leader = models.ForeignKey( + Hero, + on_delete=models.CASCADE, + related_name='leader_set', + ) + members = models.ManyToManyField( + Hero, + related_name='teams', + ) + + def __str__(self): + return self.name \ No newline at end of file diff --git a/toh/hero/tests.py b/toh/hero/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/toh/hero/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/toh/hero/urls.py b/toh/hero/urls.py new file mode 100644 index 0000000..7384e1e --- /dev/null +++ b/toh/hero/urls.py @@ -0,0 +1,9 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.hero_list), + path('/', views.id, name='hero_id'), + path('/', views.name, name='hero_name'), + path('info//', views.hero_info, name='hero_info'), +] \ No newline at end of file diff --git a/toh/hero/views.py b/toh/hero/views.py new file mode 100644 index 0000000..e0cea11 --- /dev/null +++ b/toh/hero/views.py @@ -0,0 +1,49 @@ +from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, JsonResponse +from django.views.decorators.csrf import csrf_exempt +import json +from json.decoder import JSONDecodeError +from .models import Hero +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()] + return JsonResponse(hero_all_list, safe=False) + elif request.method == 'POST': + try: + body = request.body.decode() + hero_name = json.loads(body)['name'] + except (KeyError, JSONDecodeError) as e: + return HttpResponseBadRequest() + hero = Hero(name=hero_name) + hero.save() + response_dict = {'id': hero.id, 'name': hero.name} + return JsonResponse(response_dict, status=201) + else: + return HttpResponseNotAllowed(['GET', 'POST']) + +@csrf_exempt +def hero_info(request, id): + if request.method == 'GET': + hero = Hero.objects.get(id=id) + return JsonResponse({"id": hero.id, "name": hero.name, "age": hero.age}) + elif request.method == 'PUT': + try: + body = request.body.decode() + hero_name = json.loads(body)['name'] + hero_age = json.loads(body)['age'] + except (KeyError, JSONDecodeError) as e: + return HttpResponseBadRequest() + hero = Hero.objects.get(id=id) + hero.name = hero_name + hero.age = hero_age + hero.save() + 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 diff --git a/toh/manage.py b/toh/manage.py new file mode 100755 index 0000000..04f198e --- /dev/null +++ b/toh/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == '__main__': + main() diff --git a/toh/toh/__init__.py b/toh/toh/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/toh/toh/asgi.py b/toh/toh/asgi.py new file mode 100644 index 0000000..940b9f8 --- /dev/null +++ b/toh/toh/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for toh project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') + +application = get_asgi_application() diff --git a/toh/toh/settings.py b/toh/toh/settings.py new file mode 100644 index 0000000..731ead0 --- /dev/null +++ b/toh/toh/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for toh project. + +Generated by 'django-admin startproject' using Django 4.1.2. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-x*cal+4dr^5wqe&0modde7$72#ck2zxe-&e=uhfvdal6(22!o)' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = ['127.0.0.1','0.0.0.0', 'localhost'] + + +# Application definition + +INSTALLED_APPS = [ + 'hero.apps.HeroConfig', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'toh.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'toh.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/toh/toh/urls.py b/toh/toh/urls.py new file mode 100644 index 0000000..8c57ad4 --- /dev/null +++ b/toh/toh/urls.py @@ -0,0 +1,22 @@ +"""toh URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import include, path + +urlpatterns = [ + path('hero/', include('hero.urls')), + path('admin/', admin.site.urls), +] diff --git a/toh/toh/wsgi.py b/toh/toh/wsgi.py new file mode 100644 index 0000000..97b4d40 --- /dev/null +++ b/toh/toh/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for toh project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'toh.settings') + +application = get_wsgi_application()