diff --git a/README.md b/README.md index 96c140ff8..fc030c239 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,24 @@ -# Django_REST_LAB_2 - -## Using Django REST Framework , create a new porject and develop those two API endpoints (urls) - -### API path: my_app/date ['GET'] -- This returns the current date for today. -Example response : - { "date" : "Today is 2022-06-06 !" } - - - -### API path : my_app/random ['POST'] -- This api needs a min , max JSON object , and based on it, it will generate a random number between the minimum and maximum value . if the minimum value is less than 0 , then return a response that it is not supported. else return the random number . - -Example Request JSON: - {"min" : 5, "max" : 200} - - -Example Response JSON: - {"random" : 26} - -Example Response if min number less than 0 : - {"msg", "Not Allowed. Please provide a min that is bigger than 0"} - +# Django_REST_LAB_2 + +## Using Django REST Framework , create a new porject and develop those two API endpoints (urls) + +### API path: my_app/date ['GET'] +- This returns the current date for today. +Example response : + { "date" : "Today is 2022-06-06 !" } + + + +### API path : my_app/random ['POST'] +- This api needs a min , max JSON object , and based on it, it will generate a random number between the minimum and maximum value . if the minimum value is less than 0 , then return a response that it is not supported. else return the random number . + +Example Request JSON: + {"min" : 5, "max" : 200} + + +Example Response JSON: + {"random" : 26} + +Example Response if min number less than 0 : + {"msg", "Not Allowed. Please provide a min that is bigger than 0"} + diff --git a/db.sqlite3 b/db.sqlite3 new file mode 100644 index 000000000..e69de29bb diff --git a/manage.py b/manage.py new file mode 100644 index 000000000..7fb685543 --- /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', 'my_project.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/my_app/__init__.py b/my_app/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/my_app/__pycache__/__init__.cpython-310.pyc b/my_app/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 000000000..feb78d41c Binary files /dev/null and b/my_app/__pycache__/__init__.cpython-310.pyc differ diff --git a/my_app/__pycache__/admin.cpython-310.pyc b/my_app/__pycache__/admin.cpython-310.pyc new file mode 100644 index 000000000..2ba384a49 Binary files /dev/null and b/my_app/__pycache__/admin.cpython-310.pyc differ diff --git a/my_app/__pycache__/apps.cpython-310.pyc b/my_app/__pycache__/apps.cpython-310.pyc new file mode 100644 index 000000000..11228df5c Binary files /dev/null and b/my_app/__pycache__/apps.cpython-310.pyc differ diff --git a/my_app/__pycache__/models.cpython-310.pyc b/my_app/__pycache__/models.cpython-310.pyc new file mode 100644 index 000000000..fbd5c2c14 Binary files /dev/null and b/my_app/__pycache__/models.cpython-310.pyc differ diff --git a/my_app/__pycache__/urls.cpython-310.pyc b/my_app/__pycache__/urls.cpython-310.pyc new file mode 100644 index 000000000..fb572926b Binary files /dev/null and b/my_app/__pycache__/urls.cpython-310.pyc differ diff --git a/my_app/__pycache__/views.cpython-310.pyc b/my_app/__pycache__/views.cpython-310.pyc new file mode 100644 index 000000000..c725bf1cc Binary files /dev/null and b/my_app/__pycache__/views.cpython-310.pyc differ diff --git a/my_app/admin.py b/my_app/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/my_app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/my_app/apps.py b/my_app/apps.py new file mode 100644 index 000000000..e360eca64 --- /dev/null +++ b/my_app/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MyAppConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'my_app' diff --git a/my_app/migrations/__init__.py b/my_app/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/my_app/migrations/__pycache__/__init__.cpython-310.pyc b/my_app/migrations/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 000000000..c092308d5 Binary files /dev/null and b/my_app/migrations/__pycache__/__init__.cpython-310.pyc differ diff --git a/my_app/models.py b/my_app/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/my_app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/my_app/tests.py b/my_app/tests.py new file mode 100644 index 000000000..7ce503c2d --- /dev/null +++ b/my_app/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/my_app/urls.py b/my_app/urls.py new file mode 100644 index 000000000..c1ea0e572 --- /dev/null +++ b/my_app/urls.py @@ -0,0 +1,11 @@ +from django.urls import path +from . import views + +app_name = "my_app" + +urlpatterns = [ + #get + path("date/", views.date, name="date"), + #post + path("random/", views.random, name="random") +] diff --git a/my_app/views.py b/my_app/views.py new file mode 100644 index 000000000..419855d16 --- /dev/null +++ b/my_app/views.py @@ -0,0 +1,37 @@ +from django.shortcuts import render +from rest_framework.decorators import api_view +from rest_framework.response import Response +from rest_framework.request import Request +import random as rn +import datetime + +# Create your views here. + + +@api_view(['GET']) +def date(request: Request): + # return current date + current_date = datetime.date.today() + response_body = { + "date": f'Today is {current_date}!' + } + return Response(response_body) + + +@api_view(['POST']) +def random(request: Request): + #random + min = request.data['min'] + max = request.data['max'] + + if min < 0: + # Msg + response_body = { + "msg": "Not Allowed. Please provide a min that is bigger than 0" + } + return Response(response_body) + + response_body = { + "random": rn.randint(min, max) + } + return Response(response_body) diff --git a/my_project/__init__.py b/my_project/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/my_project/__pycache__/__init__.cpython-310.pyc b/my_project/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 000000000..7cb7aba07 Binary files /dev/null and b/my_project/__pycache__/__init__.cpython-310.pyc differ diff --git a/my_project/__pycache__/settings.cpython-310.pyc b/my_project/__pycache__/settings.cpython-310.pyc new file mode 100644 index 000000000..234b0d49d Binary files /dev/null and b/my_project/__pycache__/settings.cpython-310.pyc differ diff --git a/my_project/__pycache__/urls.cpython-310.pyc b/my_project/__pycache__/urls.cpython-310.pyc new file mode 100644 index 000000000..7c9f8ab25 Binary files /dev/null and b/my_project/__pycache__/urls.cpython-310.pyc differ diff --git a/my_project/__pycache__/wsgi.cpython-310.pyc b/my_project/__pycache__/wsgi.cpython-310.pyc new file mode 100644 index 000000000..45f6c4435 Binary files /dev/null and b/my_project/__pycache__/wsgi.cpython-310.pyc differ diff --git a/my_project/asgi.py b/my_project/asgi.py new file mode 100644 index 000000000..d0b85ea49 --- /dev/null +++ b/my_project/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for my_project 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', 'my_project.settings') + +application = get_asgi_application() diff --git a/my_project/settings.py b/my_project/settings.py new file mode 100644 index 000000000..da726a79a --- /dev/null +++ b/my_project/settings.py @@ -0,0 +1,112 @@ + +from pathlib import Path + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-y=axgr&p-o$&l(sk9%8krvwo7a(v)5uon&al*2(--2*sp2(!)3' + +# 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', + 'my_app' +] + +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 = 'my_project.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 = 'my_project.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/my_project/urls.py b/my_project/urls.py new file mode 100644 index 000000000..ccf5c7698 --- /dev/null +++ b/my_project/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('my_app/', include("my_app.urls")) +] diff --git a/my_project/wsgi.py b/my_project/wsgi.py new file mode 100644 index 000000000..6d32adf99 --- /dev/null +++ b/my_project/wsgi.py @@ -0,0 +1,8 @@ + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings') + +application = get_wsgi_application()