diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f6ddf86b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*__pycache__* + diff --git a/DJANGO_REST_LAB3/__init__.py b/DJANGO_REST_LAB3/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/DJANGO_REST_LAB3/asgi.py b/DJANGO_REST_LAB3/asgi.py new file mode 100644 index 00000000..7b15ebc2 --- /dev/null +++ b/DJANGO_REST_LAB3/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for DJANGO_REST_LAB3 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.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DJANGO_REST_LAB3.settings') + +application = get_asgi_application() diff --git a/DJANGO_REST_LAB3/settings.py b/DJANGO_REST_LAB3/settings.py new file mode 100644 index 00000000..15bd37d5 --- /dev/null +++ b/DJANGO_REST_LAB3/settings.py @@ -0,0 +1,125 @@ +""" +Django settings for DJANGO_REST_LAB3 project. + +Generated by 'django-admin startproject' using Django 4.0.6. + +For more information on this file, see +https://docs.djangoproject.com/en/4.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.0/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.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-pjj8i)g)9nkq@^#l533*2*ko6d-xi*(a-32-2zss#l0hu2$36)' + +# 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', + 'students', +] + +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 = 'DJANGO_REST_LAB3.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 = 'DJANGO_REST_LAB3.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/4.0/ref/settings/#databases + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.0/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.0/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.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/DJANGO_REST_LAB3/urls.py b/DJANGO_REST_LAB3/urls.py new file mode 100644 index 00000000..d35537da --- /dev/null +++ b/DJANGO_REST_LAB3/urls.py @@ -0,0 +1,22 @@ +"""DJANGO_REST_LAB3 URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.0/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 + +urlpatterns = [ + path('admin/', admin.site.urls), + path('students/',include('students.urls')) +] diff --git a/DJANGO_REST_LAB3/wsgi.py b/DJANGO_REST_LAB3/wsgi.py new file mode 100644 index 00000000..94fcaa52 --- /dev/null +++ b/DJANGO_REST_LAB3/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for DJANGO_REST_LAB3 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.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'DJANGO_REST_LAB3.settings') + +application = get_wsgi_application() diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 00000000..045141bd Binary files /dev/null and b/db.sqlite3 differ diff --git a/manage.py b/manage.py new file mode 100644 index 00000000..cecfb663 --- /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', 'DJANGO_REST_LAB3.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/students/__init__.py b/students/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/students/admin.py b/students/admin.py new file mode 100644 index 00000000..67592b91 --- /dev/null +++ b/students/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Students + +admin.site.register(Students) +# Register your models here. diff --git a/students/apps.py b/students/apps.py new file mode 100644 index 00000000..c242c4de --- /dev/null +++ b/students/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class StudentsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'students' diff --git a/students/migrations/0001_initial.py b/students/migrations/0001_initial.py new file mode 100644 index 00000000..43e52bfd --- /dev/null +++ b/students/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 4.0.6 on 2022-07-27 14:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Students', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=15)), + ('last_name', models.CharField(max_length=15)), + ('birth_date', models.DateField()), + ('GPA', models.FloatField()), + ], + ), + ] diff --git a/students/migrations/__init__.py b/students/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/students/models.py b/students/models.py new file mode 100644 index 00000000..a1b6ef4e --- /dev/null +++ b/students/models.py @@ -0,0 +1,10 @@ +from django.db import models + +class Students(models.Model): + first_name = models.CharField(max_length=15) + last_name = models.CharField(max_length=15) + birth_date = models.DateField() + GPA = models.FloatField() + + + diff --git a/students/serializer.py b/students/serializer.py new file mode 100644 index 00000000..58804d41 --- /dev/null +++ b/students/serializer.py @@ -0,0 +1,8 @@ +from rest_framework import serializers +from .models import Students + + +class StudentSerializer(serializers.ModelSerializer): + class Meta: + model = Students + fields = '__all__' diff --git a/students/test.py b/students/test.py new file mode 100644 index 00000000..962ede32 --- /dev/null +++ b/students/test.py @@ -0,0 +1,8 @@ +from datetime import date +from models import Students +first_name="mohmmad" +last_name="kalid" +birth_date=date.today() +GPA = 3.5 +newStudent= Students(first_name,last_name,birth_date,GPA) +print(newStudent) diff --git a/students/tests.py b/students/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/students/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/students/urls.py b/students/urls.py new file mode 100644 index 00000000..f0654951 --- /dev/null +++ b/students/urls.py @@ -0,0 +1,12 @@ +from django.urls import path +from . import views + +app_name = "students" + +urlpatterns = [ + path('create', views.add_Student, name="add_student"), + path('list', views.list_Student, name="list_student"), + path('update/', views.update_student, name='update_student'), + path('delete/', views.delete_student, name='delete_student'), + path('patch/', views.patch_student, name='patch_student'), +] diff --git a/students/views.py b/students/views.py new file mode 100644 index 00000000..cdde2faf --- /dev/null +++ b/students/views.py @@ -0,0 +1,80 @@ +from turtle import st +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework.request import Request +from .models import Students +from .serializer import StudentSerializer +from django.db.models.functions import Lower + + +@api_view(['POST']) # add new student to the databases +def add_Student(request: Request): + serializer = StudentSerializer(data=request.data) + if serializer.is_valid(): + serializer.save() + else: + return Response({ + "msg": "InValid input Successfully" + }) + + return Response({ + "msg": "Created student Successfully" + }) + + +@api_view(["GET"]) +def list_Student(request: Request): + all_Student = Students.objects.order_by(Lower("first_name")) + + serializer = StudentSerializer(all_Student, many=True) + return Response({ + "msg": "This is a list of All Students", + "student": serializer.data + }) + + +@api_view(['PUT']) +def update_student(request: Request, student_id): + + fName = request.data['first_name'] + lName = request.data['last_name'] + birth_Date = request.data['birth_date'] + GPA = request.data['GPA'] + + student = Students.objects.get(id=student_id) + + student.first_name = fName + student.last_name = lName + student.birth_date = birth_Date + student.GPA = GPA + + student.save() + + return Response({"msg": f"the student of the {student_id=} is updated !"}) + + +@api_view(["DELETE"]) +def delete_student(request: Request, student_id): + try: + student = Students.objects.get(id=student_id) + temp = student + student.delete() + except Exception as e: + return Response({"msg": "The student is not Found!"}) + + return Response({"msg": f"delete the following student {temp.first_name}"}) + + +@api_view(["patch"]) +def patch_student(request: Request, student_id): + student = Students.objects.get(id=student_id) + data = request.data + student.first_name = data.get('first_name', student.first_name) + student.last_name = data.get('last_name', student.last_name) + student.birth_date = data.get('birth_date', student.birth_date) + student.GPA = data.get('GPA', student.GPA) + + student.save() + + serializer = StudentSerializer(student) + return Response(serializer.data)