diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fa501c6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/__pycache__/ +db.sqlite3 \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100644 index 0000000..28c24cc --- /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', 'taskmanager_backend.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/taskmanager_backend/__init__.py b/taskmanager_backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/taskmanager_backend/asgi.py b/taskmanager_backend/asgi.py new file mode 100644 index 0000000..2811234 --- /dev/null +++ b/taskmanager_backend/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for taskmanager_backend 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/5.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'taskmanager_backend.settings') + +application = get_asgi_application() diff --git a/taskmanager_backend/settings.py b/taskmanager_backend/settings.py new file mode 100644 index 0000000..bbd16da --- /dev/null +++ b/taskmanager_backend/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for taskmanager_backend project. + +Generated by 'django-admin startproject' using Django 5.2.5. + +For more information on this file, see +https://docs.djangoproject.com/en/5.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.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/5.2/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-8wkk*b88=y*w=&nsxytxpo$)g0a_&138bc9=9hm@gmrx(*n68x" + +# 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", + "tasks", +] + +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 = "taskmanager_backend.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "taskmanager_backend.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.2/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.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/5.2/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/5.2/howto/static-files/ + +STATIC_URL = "static/" + +# Default primary key field type +# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/taskmanager_backend/urls.py b/taskmanager_backend/urls.py new file mode 100644 index 0000000..550d4d4 --- /dev/null +++ b/taskmanager_backend/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for taskmanager_backend project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.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 + +urlpatterns = [ + path('admin/', admin.site.urls), + path('tasks/', include('tasks.urls')) +] diff --git a/taskmanager_backend/wsgi.py b/taskmanager_backend/wsgi.py new file mode 100644 index 0000000..4171ce2 --- /dev/null +++ b/taskmanager_backend/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for taskmanager_backend 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/5.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'taskmanager_backend.settings') + +application = get_wsgi_application() diff --git a/tasks/__init__.py b/tasks/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tasks/admin.py b/tasks/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/tasks/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/tasks/apps.py b/tasks/apps.py new file mode 100644 index 0000000..3ff3ab3 --- /dev/null +++ b/tasks/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TasksConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'tasks' diff --git a/tasks/migrations/__init__.py b/tasks/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tasks/models.py b/tasks/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/tasks/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/tasks/tests.py b/tasks/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/tasks/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/tasks/urls.py b/tasks/urls.py new file mode 100644 index 0000000..5a57018 --- /dev/null +++ b/tasks/urls.py @@ -0,0 +1,25 @@ +""" +URL configuration for taskmanager_backend project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.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 +import tasks.views as views + +urlpatterns = [ + path("", views.tasks), + path("", views.single_task), + path("priority/", views.priority_task) +] diff --git a/tasks/utils.py b/tasks/utils.py new file mode 100644 index 0000000..39728d9 --- /dev/null +++ b/tasks/utils.py @@ -0,0 +1,4 @@ +def is_null_empty_space(val): + if val is None or val.strip() == "": + return True + return False \ No newline at end of file diff --git a/tasks/views.py b/tasks/views.py new file mode 100644 index 0000000..2646464 --- /dev/null +++ b/tasks/views.py @@ -0,0 +1,138 @@ +from rest_framework.response import Response +from rest_framework import status +from rest_framework.decorators import api_view +import json +import tasks.utils as utils +from datetime import datetime + +# reading in memory database stored in json format +dbpath = "./tasks_db.json" + + +# Create your views here. +@api_view(["GET", "POST"]) +def tasks(request): + + if request.method == "GET": + try: + with open(dbpath, "r+") as task_json: + task_list = json.load(task_json) + sortedTaskList = sorted(task_list["tasks"], key=lambda x: datetime.strptime(x["creation_date"], "%d/%m/%Y")) + if utils.is_null_empty_space(request.query_params.get("completed")): + return Response({"tasks": sortedTaskList}, status=status.HTTP_200_OK) + else: + comTasks = [] + for task in sortedTaskList: + if str(task["completed"]) == request.query_params.get("completed"): + comTasks.append(task) + return Response( + {"completed_tasks": comTasks}, status=status.HTTP_200_OK + ) + except Exception as e: + return Response( + {"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + + if request.method == "POST": + data = request.data + if ( + utils.is_null_empty_space(data["title"]) + or utils.is_null_empty_space(data["description"]) + or not isinstance(data["completed"], bool) + ): + return Response( + {"message": "invalid input"}, status=status.HTTP_400_BAD_REQUEST + ) + try: + with open(dbpath, "r+") as task_json: + task_list = json.load(task_json) + task_json.seek(0) + task_list["tasks"].append(data) + json.dump(task_list, task_json, indent=4) + task_json.truncate() + return Response({"tasks": task_list}, status=status.HTTP_201_CREATED) + except Exception as e: + return Response( + {"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + + +@api_view(["GET", "DELETE", "PUT"]) +def single_task(request, pk): + + if request.method == "GET": + with open(dbpath, "r+") as task_json: + task_list = json.load(task_json) + for task in task_list["tasks"]: + if task["id"] == pk: + return Response(task, status=status.HTTP_302_FOUND) + return Response("Not Found", status=status.HTTP_404_NOT_FOUND) + + if request.method == "PUT": + data = request.data + if ( + utils.is_null_empty_space(data["title"]) + or utils.is_null_empty_space(data["description"]) + or not isinstance(data["completed"], bool) + ): + return Response( + {"message": "invalid input"}, status=status.HTTP_400_BAD_REQUEST + ) + try: + with open(dbpath, "r+") as task_json: + task_list = json.load(task_json) + for task in task_list["tasks"]: + if task["id"] == pk: + task["title"] = data["title"] + task["description"] = data["description"] + task["completed"] = data["completed"] + task["creation_date"] = data["creation_date"] + task["priority"] = data["priority"] + task_json.seek(0) + json.dump(task_list, task_json, indent=4) + task_json.truncate() + task_json.close() + return Response(task, status=status.HTTP_200_OK) + return Response("Not Found", status=status.HTTP_404_NOT_FOUND) + except Exception as e: + return Response( + {"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + + if request.method == "DELETE": + try: + with open(dbpath, "r+") as task_json: + task_list = json.load(task_json) + for task in task_list["tasks"]: + if task["id"] == pk: + temp_task = task + task_list["tasks"].remove(task) + task_json.seek(0) + json.dump(task_list, task_json, indent=4) + task_json.truncate() + return Response(temp_task, status=status.HTTP_200_OK) + return Response("Not Found", status=status.HTTP_404_NOT_FOUND) + except Exception as e: + return Response( + {"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + +@api_view(["GET"]) +def priority_task(request,level): + + if request.method == "GET": + + try: + with open(dbpath, "r") as task_json: + task_list = json.load(task_json) + tasks = [] + for task in task_list["tasks"]: + if task["priority"] == level: + tasks.append(task) + return Response( + {"tasks": tasks}, status=status.HTTP_200_OK + ) + except Exception as e: + return Response( + {"message": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) \ No newline at end of file diff --git a/tasks_db.json b/tasks_db.json new file mode 100644 index 0000000..2dd1ead --- /dev/null +++ b/tasks_db.json @@ -0,0 +1,156 @@ +{ + "tasks": [ + { + "id": 1, + "title": "Set up environment", + "description": "Install Node.js, npm, and git", + "completed": true, + "creation_date": "01/01/2025", + "priority": 1 + }, + { + "id": 2, + "title": "Install Express", + "description": "Install Express", + "completed": true, + "creation_date": "02/01/2025", + "priority": 2 + }, + { + "id": 3, + "title": "Install nodemon", + "description": "Install nodemon as a development dependency", + "completed": true, + "creation_date": "12/01/2025", + "priority": 1 + }, + { + "id": 4, + "title": "Install Express", + "description": "Install Express", + "completed": false, + "creation_date": "05/01/2025", + "priority": 1 + }, + { + "id": 5, + "title": "Install Mongoose", + "description": "Install Mongoose", + "completed": false, + "creation_date": "20/01/2025", + "priority": 1 + }, + { + "id": 6, + "title": "Install Morgan", + "description": "Install Morgan", + "completed": false, + "creation_date": "07/01/2025", + "priority": 1 + }, + { + "id": 7, + "title": "Install body-parser", + "description": "Install body-parser", + "completed": false, + "creation_date": "10/01/2025", + "priority": 1 + }, + { + "id": 8, + "title": "Install cors", + "description": "Install cors", + "completed": false, + "creation_date": "31/01/2025", + "priority": 1 + }, + { + "id": 9, + "title": "Install passport", + "description": "Install passport", + "completed": false, + "creation_date": "25/01/2025", + "priority": 1 + }, + { + "id": 10, + "title": "Install passport-local", + "description": "Install passport-local", + "completed": false, + "creation_date": "25/01/2025", + "priority": 1 + }, + { + "id": 11, + "title": "Install passport-local-mongoose", + "description": "Install passport-local-mongoose", + "completed": false, + "creation_date": "16/01/2025", + "priority": 1 + }, + { + "id": 12, + "title": "Install express-session", + "description": "Install express-session", + "completed": false, + "creation_date": "08/01/2025", + "priority": 1 + }, + { + "id": 13, + "title": "Install connect-mongo", + "description": "Install connect-mongo", + "completed": false, + "creation_date": "09/01/2025", + "priority": 1 + }, + { + "id": 14, + "title": "Install dotenv", + "description": "Install dotenv", + "completed": false, + "creation_date": "13/01/2025", + "priority": 1 + }, + { + "id": 15, + "title": "Install jsonwebtoken", + "description": "Install jsonwebtoken", + "completed": false, + "creation_date": "27/01/2025", + "priority": 1 + }, + { + "id": 16, + "title": "Install Express", + "description": "Install Express", + "completed": false, + "creation_date": "15/01/2025", + "priority": 1 + }, + { + "id": 17, + "title": "Install vs code", + "description": "Install Express", + "completed": false, + "creation_date": "23/11/2025", + "priority": 6 + }, + { + "id": 17, + "title": "Install vs code", + "description": "Install Express", + "completed": false, + "creation_date": "10/01/2025", + "priority": 1 + }, + { + "id": 17, + "title": "Install vs code", + "description": "Install Express", + "completed": false, + "creation_date": "23/11/2025", + "priority": 5 + } + ] +} \ No newline at end of file