Skip to content
Open
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
48 changes: 24 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"}
Empty file added db.sqlite3
Empty file.
22 changes: 22 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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()
Empty file added my_app/__init__.py
Empty file.
Binary file added my_app/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added my_app/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added my_app/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added my_app/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file added my_app/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added my_app/__pycache__/views.cpython-310.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions my_app/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions my_app/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class MyAppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'my_app'
Empty file added my_app/migrations/__init__.py
Empty file.
Binary file not shown.
3 changes: 3 additions & 0 deletions my_app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions my_app/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
11 changes: 11 additions & 0 deletions my_app/urls.py
Original file line number Diff line number Diff line change
@@ -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")
]
37 changes: 37 additions & 0 deletions my_app/views.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file added my_project/__init__.py
Empty file.
Binary file added my_project/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added my_project/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file added my_project/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added my_project/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
16 changes: 16 additions & 0 deletions my_project/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
112 changes: 112 additions & 0 deletions my_project/settings.py
Original file line number Diff line number Diff line change
@@ -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'
7 changes: 7 additions & 0 deletions my_project/urls.py
Original file line number Diff line number Diff line change
@@ -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"))
]
8 changes: 8 additions & 0 deletions my_project/wsgi.py
Original file line number Diff line number Diff line change
@@ -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()