diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..923e1bd6 Binary files /dev/null and b/.DS_Store differ diff --git a/MyFirstProject/__init__.py b/MyFirstProject/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/MyFirstProject/__pycache__/__init__.cpython-37.pyc b/MyFirstProject/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 00000000..f8e22be6 Binary files /dev/null and b/MyFirstProject/__pycache__/__init__.cpython-37.pyc differ diff --git a/MyFirstProject/__pycache__/settings.cpython-37.pyc b/MyFirstProject/__pycache__/settings.cpython-37.pyc new file mode 100644 index 00000000..292e3f41 Binary files /dev/null and b/MyFirstProject/__pycache__/settings.cpython-37.pyc differ diff --git a/MyFirstProject/__pycache__/urls.cpython-37.pyc b/MyFirstProject/__pycache__/urls.cpython-37.pyc new file mode 100644 index 00000000..fa6c88d6 Binary files /dev/null and b/MyFirstProject/__pycache__/urls.cpython-37.pyc differ diff --git a/MyFirstProject/__pycache__/wsgi.cpython-37.pyc b/MyFirstProject/__pycache__/wsgi.cpython-37.pyc new file mode 100644 index 00000000..871a8d3a Binary files /dev/null and b/MyFirstProject/__pycache__/wsgi.cpython-37.pyc differ diff --git a/MyFirstProject/asgi.py b/MyFirstProject/asgi.py new file mode 100644 index 00000000..beead572 --- /dev/null +++ b/MyFirstProject/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for MyFirstProject 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', 'MyFirstProject.settings') + +application = get_asgi_application() diff --git a/MyFirstProject/settings.py b/MyFirstProject/settings.py new file mode 100644 index 00000000..f66cd0c0 --- /dev/null +++ b/MyFirstProject/settings.py @@ -0,0 +1,127 @@ +""" +Django settings for MyFirstProject project. + +Generated by 'django-admin startproject' using Django 3.2.14. + +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-hgb!*b$2q2)7#@qc)b=dg)=te)je-04_m05ij3%6*^q62+h&ka' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'student', +] + +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 = 'MyFirstProject.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 = 'MyFirstProject.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/MyFirstProject/urls.py b/MyFirstProject/urls.py new file mode 100644 index 00000000..8bb01c4c --- /dev/null +++ b/MyFirstProject/urls.py @@ -0,0 +1,22 @@ +"""MyFirstProject 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 path, include +import my_first_app.views +urlpatterns = [ + path('admin/', admin.site.urls), + path('student/', include('student.urls')), +] diff --git a/MyFirstProject/wsgi.py b/MyFirstProject/wsgi.py new file mode 100644 index 00000000..629f9a94 --- /dev/null +++ b/MyFirstProject/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for MyFirstProject 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', 'MyFirstProject.settings') + +application = get_wsgi_application() diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 00000000..f3782568 Binary files /dev/null and b/db.sqlite3 differ diff --git a/manage.py b/manage.py new file mode 100755 index 00000000..df9a312e --- /dev/null +++ b/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', 'MyFirstProject.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/student/__init__.py b/student/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/student/__pycache__/__init__.cpython-37.pyc b/student/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 00000000..793e2042 Binary files /dev/null and b/student/__pycache__/__init__.cpython-37.pyc differ diff --git a/student/__pycache__/admin.cpython-37.pyc b/student/__pycache__/admin.cpython-37.pyc new file mode 100644 index 00000000..3b9007e2 Binary files /dev/null and b/student/__pycache__/admin.cpython-37.pyc differ diff --git a/student/__pycache__/apps.cpython-37.pyc b/student/__pycache__/apps.cpython-37.pyc new file mode 100644 index 00000000..1b915735 Binary files /dev/null and b/student/__pycache__/apps.cpython-37.pyc differ diff --git a/student/__pycache__/models.cpython-37.pyc b/student/__pycache__/models.cpython-37.pyc new file mode 100644 index 00000000..bc9f6f07 Binary files /dev/null and b/student/__pycache__/models.cpython-37.pyc differ diff --git a/student/__pycache__/urls.cpython-37.pyc b/student/__pycache__/urls.cpython-37.pyc new file mode 100644 index 00000000..6d56e9b2 Binary files /dev/null and b/student/__pycache__/urls.cpython-37.pyc differ diff --git a/student/__pycache__/views.cpython-37.pyc b/student/__pycache__/views.cpython-37.pyc new file mode 100644 index 00000000..91128598 Binary files /dev/null and b/student/__pycache__/views.cpython-37.pyc differ diff --git a/student/admin.py b/student/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/student/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/student/apps.py b/student/apps.py new file mode 100644 index 00000000..4cae5ca0 --- /dev/null +++ b/student/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class StudentConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'student' diff --git a/student/migrations/0001_initial.py b/student/migrations/0001_initial.py new file mode 100644 index 00000000..b0703488 --- /dev/null +++ b/student/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 3.2.14 on 2022-07-27 14:28 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Student', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=512)), + ('last_name', models.CharField(max_length=512)), + ('birth_date', models.DateField()), + ('GPA', models.FloatField()), + ], + ), + ] diff --git a/student/migrations/__init__.py b/student/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/student/migrations/__pycache__/0001_initial.cpython-37.pyc b/student/migrations/__pycache__/0001_initial.cpython-37.pyc new file mode 100644 index 00000000..fb9da694 Binary files /dev/null and b/student/migrations/__pycache__/0001_initial.cpython-37.pyc differ diff --git a/student/migrations/__pycache__/__init__.cpython-37.pyc b/student/migrations/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 00000000..3405a024 Binary files /dev/null and b/student/migrations/__pycache__/__init__.cpython-37.pyc differ diff --git a/student/models.py b/student/models.py new file mode 100644 index 00000000..161c1f41 --- /dev/null +++ b/student/models.py @@ -0,0 +1,8 @@ +from django.db import models + +# Create your models here. +class Student(models.Model): + first_name = models.CharField(max_length = 512) + last_name = models.CharField(max_length = 512) + birth_date = models.DateField() + GPA = models.FloatField() \ No newline at end of file diff --git a/student/tests.py b/student/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/student/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/student/urls.py b/student/urls.py new file mode 100644 index 00000000..bdc7545c --- /dev/null +++ b/student/urls.py @@ -0,0 +1,11 @@ +from django.urls import path +from . import views + +app_name = "student" + +urlpatterns = [ + path("add", views.add_student, name="add_student"), + path("list", views.list_students, name="list_student"), + path("update", views.update_info, name="update_info"), + path("delete/", views.delete_student, name="delete_student") +] \ No newline at end of file diff --git a/student/views.py b/student/views.py new file mode 100644 index 00000000..be346ef5 --- /dev/null +++ b/student/views.py @@ -0,0 +1,51 @@ +from django.shortcuts import render + +# Create your views here. +from rest_framework.decorators import api_view +from rest_framework.request import Request +from rest_framework.response import Response + +from .models import Student +import json + +@api_view(['POST']) +def add_student(request: Request): + new_student = Student(first_name = request.data["first_name"], last_name = request.data["last_name"], birth_date = request.data["birth_date"], GPA = request.data["GPA"]) + new_student.save() + response_date = { + "msg": "A new record has been created" + } + return Response(response_date) + + +@api_view(['GET']) +def list_students(request: Request): + students = Student.objects.all() + student_list = [{"id": student.id, "first_name": student.first_name, "last_name": student.last_name, + "birth_date": student.birth_date, "GPA": student.GPA} for student in students] + + response_data = { + "students": student_list + } + return Response(response_data) + +@api_view(['PUT']) +def update_info(request : Request): + student = Student.objects.get(id=request.data["student_id"]) + + student.first_name = request.data["first_name"] + student.last_name = request.data["last_name"] + student.birth_date = request.data["birth_date"] + student.GPA = request.data["GPA"] + student.save() + + return Response({"msg" : "Your student information is updated!"}) + +@api_view(["DELETE"]) +def delete_student(request : Request, student_id): + try: + student = Student.objects.get(id=student_id) + student.delete() + except Exception as e: + return Response({"msg" : "The student is not Found!"}) + return Response({"msg" : f"delete the following student {student.first_name}"}) \ No newline at end of file