diff --git a/homework2/EmpManagement/EmpManagement/__init__.py b/homework2/EmpManagement/EmpManagement/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/homework2/EmpManagement/EmpManagement/__pycache__/__init__.cpython-310.pyc b/homework2/EmpManagement/EmpManagement/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..9962d666 Binary files /dev/null and b/homework2/EmpManagement/EmpManagement/__pycache__/__init__.cpython-310.pyc differ diff --git a/homework2/EmpManagement/EmpManagement/__pycache__/settings.cpython-310.pyc b/homework2/EmpManagement/EmpManagement/__pycache__/settings.cpython-310.pyc new file mode 100644 index 00000000..a98ce566 Binary files /dev/null and b/homework2/EmpManagement/EmpManagement/__pycache__/settings.cpython-310.pyc differ diff --git a/homework2/EmpManagement/EmpManagement/__pycache__/urls.cpython-310.pyc b/homework2/EmpManagement/EmpManagement/__pycache__/urls.cpython-310.pyc new file mode 100644 index 00000000..c0a79f4d Binary files /dev/null and b/homework2/EmpManagement/EmpManagement/__pycache__/urls.cpython-310.pyc differ diff --git a/homework2/EmpManagement/EmpManagement/__pycache__/wsgi.cpython-310.pyc b/homework2/EmpManagement/EmpManagement/__pycache__/wsgi.cpython-310.pyc new file mode 100644 index 00000000..7d199608 Binary files /dev/null and b/homework2/EmpManagement/EmpManagement/__pycache__/wsgi.cpython-310.pyc differ diff --git a/homework2/EmpManagement/EmpManagement/asgi.py b/homework2/EmpManagement/EmpManagement/asgi.py new file mode 100644 index 00000000..e4d9ee14 --- /dev/null +++ b/homework2/EmpManagement/EmpManagement/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for EmpManagement 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.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'EmpManagement.settings') + +application = get_asgi_application() diff --git a/homework2/EmpManagement/EmpManagement/settings.py b/homework2/EmpManagement/EmpManagement/settings.py new file mode 100644 index 00000000..b4582934 --- /dev/null +++ b/homework2/EmpManagement/EmpManagement/settings.py @@ -0,0 +1,92 @@ + +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent + +SECRET_KEY = 'django-insecure-j*z@8-osfqlljsr1sr0)-v2si+ng$j%v=igu5vz6x5*3(8sl4k' + +DEBUG = True + +ALLOWED_HOSTS = ['*'] + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'employees.apps.EmployeesConfig', +] + +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 = 'EmpManagement.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 = 'EmpManagement.wsgi.application' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': 'employee_db', + 'USER': 'postgres', + 'PASSWORD': '123456', + 'HOST': 'psql-db4', + 'PORT': 5432, + } + } + + + + +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', + }, +] + + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_TZ = True + +STATIC_URL = 'static/' + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' diff --git a/homework2/EmpManagement/EmpManagement/urls.py b/homework2/EmpManagement/EmpManagement/urls.py new file mode 100644 index 00000000..b4a63e9d --- /dev/null +++ b/homework2/EmpManagement/EmpManagement/urls.py @@ -0,0 +1,8 @@ + +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('employees/', include('employees.urls')), +] diff --git a/homework2/EmpManagement/EmpManagement/wsgi.py b/homework2/EmpManagement/EmpManagement/wsgi.py new file mode 100644 index 00000000..05f44b3a --- /dev/null +++ b/homework2/EmpManagement/EmpManagement/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for EmpManagement 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.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'EmpManagement.settings') + +application = get_wsgi_application() diff --git a/homework2/EmpManagement/__pycache__/unittest_api.cpython-310.pyc b/homework2/EmpManagement/__pycache__/unittest_api.cpython-310.pyc new file mode 100644 index 00000000..d2bcab8c Binary files /dev/null and b/homework2/EmpManagement/__pycache__/unittest_api.cpython-310.pyc differ diff --git a/homework2/EmpManagement/docker-compose.yml b/homework2/EmpManagement/docker-compose.yml new file mode 100644 index 00000000..67c61704 --- /dev/null +++ b/homework2/EmpManagement/docker-compose.yml @@ -0,0 +1,29 @@ +version: "3" +services: + web_service: + build: + context: ./ + dockerfile: ./dockerfiles/Dockerfile + image: workshop1_web + container_name: workshop_web_container3 + stdin_open: true + tty: true + ports: + - "8000:8000" + volumes: + - .:/root/workspace/site + + psql-db: + image: 'postgres:14' + container_name: psql-db4 + environment: + - PGPASSWORD=123456 + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=123456 + ports: + - '5446:5432' + volumes: + - db:/var/lib/postgresql/data +volumes: + db: + driver: local diff --git a/homework2/EmpManagement/dockerfiles/Dockerfile b/homework2/EmpManagement/dockerfiles/Dockerfile new file mode 100644 index 00000000..d34dc3af --- /dev/null +++ b/homework2/EmpManagement/dockerfiles/Dockerfile @@ -0,0 +1,13 @@ + FROM python:3.10.2-alpine3.15 + + RUN apk update && \ + apk --no-cache add --virtual build-deps-alpine build-base && \ + apk --no-cache add --virtual postgresql-deps libpq-dev + + RUN pip install --upgrade pip + RUN pip install Django psycopg2==2.9.3 + + RUN mkdir -p /root/workspace/src + COPY ./ /root/workspace/site + + WORKDIR /root/workspace/site diff --git a/homework2/EmpManagement/employees/__init__.py b/homework2/EmpManagement/employees/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/homework2/EmpManagement/employees/__pycache__/__init__.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..60f99c10 Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/__init__.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/__pycache__/admin.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/admin.cpython-310.pyc new file mode 100644 index 00000000..998d151a Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/admin.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/__pycache__/apps.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/apps.cpython-310.pyc new file mode 100644 index 00000000..d1dafac5 Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/apps.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/__pycache__/models.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/models.cpython-310.pyc new file mode 100644 index 00000000..a01c4f7d Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/models.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/__pycache__/urls.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/urls.cpython-310.pyc new file mode 100644 index 00000000..95b73b35 Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/urls.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/__pycache__/views.cpython-310.pyc b/homework2/EmpManagement/employees/__pycache__/views.cpython-310.pyc new file mode 100644 index 00000000..2cd95a36 Binary files /dev/null and b/homework2/EmpManagement/employees/__pycache__/views.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/admin.py b/homework2/EmpManagement/employees/admin.py new file mode 100644 index 00000000..a9706f5e --- /dev/null +++ b/homework2/EmpManagement/employees/admin.py @@ -0,0 +1,9 @@ +from django.contrib import admin +from .models import Employee + +class DjEmployeeAdmin(admin.ModelAdmin): + list_display = ("first_name", "last_name", "emp_id", "address", "mobile", "dept","salary",) + list_filter = ("dept",) + + +admin.site.register(Employee, DjEmployeeAdmin) diff --git a/homework2/EmpManagement/employees/apps.py b/homework2/EmpManagement/employees/apps.py new file mode 100644 index 00000000..e1187f4f --- /dev/null +++ b/homework2/EmpManagement/employees/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + +class EmployeesConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'employees' diff --git a/homework2/EmpManagement/employees/migrations/0001_initial.py b/homework2/EmpManagement/employees/migrations/0001_initial.py new file mode 100644 index 00000000..69d2392f --- /dev/null +++ b/homework2/EmpManagement/employees/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 4.2 on 2023-05-05 13:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Employee', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(default=None, max_length=200)), + ('last_name', models.CharField(default=None, max_length=200)), + ('address', models.CharField(default=None, max_length=200)), + ('emp_id', models.IntegerField(default=None)), + ('mobile', models.CharField(default=None, max_length=10)), + ('salary', models.IntegerField(default=None)), + ('dept', models.CharField(choices=[('HR', 'HR'), ('Engineering', 'Engineering'), ('Marketing', 'Marketing'), ('Planning', 'Planning'), ('Sales', 'Sales'), ('Finance', 'Finance'), ('Operations', 'Operations')], default=None, max_length=100)), + ], + ), + ] diff --git a/homework2/EmpManagement/employees/migrations/0002_alter_employee_dept.py b/homework2/EmpManagement/employees/migrations/0002_alter_employee_dept.py new file mode 100644 index 00000000..f4ce64cd --- /dev/null +++ b/homework2/EmpManagement/employees/migrations/0002_alter_employee_dept.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2 on 2023-05-06 04:02 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('employees', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='employee', + name='dept', + field=models.CharField(choices=[('HR', 'HR'), ('Engineering', 'Engineering'), ('Marketing', 'Marketing'), ('Planning', 'Planning'), ('Sales', 'Sales'), ('Finance', 'Finance'), ('Operations', 'Operations')], max_length=100), + ), + ] diff --git a/homework2/EmpManagement/employees/migrations/0003_alter_employee_address_alter_employee_emp_id_and_more.py b/homework2/EmpManagement/employees/migrations/0003_alter_employee_address_alter_employee_emp_id_and_more.py new file mode 100644 index 00000000..c408efb8 --- /dev/null +++ b/homework2/EmpManagement/employees/migrations/0003_alter_employee_address_alter_employee_emp_id_and_more.py @@ -0,0 +1,43 @@ +# Generated by Django 4.2 on 2023-05-09 01:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('employees', '0002_alter_employee_dept'), + ] + + operations = [ + migrations.AlterField( + model_name='employee', + name='address', + field=models.CharField(max_length=200), + ), + migrations.AlterField( + model_name='employee', + name='emp_id', + field=models.IntegerField(), + ), + migrations.AlterField( + model_name='employee', + name='first_name', + field=models.CharField(max_length=200), + ), + migrations.AlterField( + model_name='employee', + name='last_name', + field=models.CharField(max_length=200), + ), + migrations.AlterField( + model_name='employee', + name='mobile', + field=models.CharField(max_length=10), + ), + migrations.AlterField( + model_name='employee', + name='salary', + field=models.IntegerField(), + ), + ] diff --git a/homework2/EmpManagement/employees/migrations/0004_remove_employee_id_alter_employee_emp_id.py b/homework2/EmpManagement/employees/migrations/0004_remove_employee_id_alter_employee_emp_id.py new file mode 100644 index 00000000..f5110e25 --- /dev/null +++ b/homework2/EmpManagement/employees/migrations/0004_remove_employee_id_alter_employee_emp_id.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2 on 2023-05-09 12:05 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('employees', '0003_alter_employee_address_alter_employee_emp_id_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='employee', + name='id', + ), + migrations.AlterField( + model_name='employee', + name='emp_id', + field=models.BigAutoField(primary_key=True, serialize=False), + ), + ] diff --git a/homework2/EmpManagement/employees/migrations/__init__.py b/homework2/EmpManagement/employees/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0001_initial.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0001_initial.cpython-310.pyc new file mode 100644 index 00000000..34655743 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0001_initial.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0002_alter_employee_dept.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0002_alter_employee_dept.cpython-310.pyc new file mode 100644 index 00000000..08e1cda9 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0002_alter_employee_dept.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0002_remove_employee_role.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0002_remove_employee_role.cpython-310.pyc new file mode 100644 index 00000000..5dfeeb20 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0002_remove_employee_role.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0003_alter_employee_address_alter_employee_emp_id_and_more.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0003_alter_employee_address_alter_employee_emp_id_and_more.cpython-310.pyc new file mode 100644 index 00000000..eb3cd332 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0003_alter_employee_address_alter_employee_emp_id_and_more.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0003_delete_role_alter_employee_address_and_more.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0003_delete_role_alter_employee_address_and_more.cpython-310.pyc new file mode 100644 index 00000000..0d6e570e Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0003_delete_role_alter_employee_address_and_more.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/0004_remove_employee_id_alter_employee_emp_id.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/0004_remove_employee_id_alter_employee_emp_id.cpython-310.pyc new file mode 100644 index 00000000..27ea9ed9 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/0004_remove_employee_id_alter_employee_emp_id.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/migrations/__pycache__/__init__.cpython-310.pyc b/homework2/EmpManagement/employees/migrations/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 00000000..70c9fb90 Binary files /dev/null and b/homework2/EmpManagement/employees/migrations/__pycache__/__init__.cpython-310.pyc differ diff --git a/homework2/EmpManagement/employees/models.py b/homework2/EmpManagement/employees/models.py new file mode 100644 index 00000000..bc73fd88 --- /dev/null +++ b/homework2/EmpManagement/employees/models.py @@ -0,0 +1,30 @@ +from django.db import models + +DEPARTMENT_CHOICES = ( + ("HR", "HR"), + ("Engineering", "Engineering"), + ("Marketing", "Marketing"), + ("Planning", "Planning"), + ("Sales","Sales"), + ("Finance","Finance"), + ("Operations","Operations"), +) + + + + +class Employee(models.Model): + first_name = models.CharField(max_length=200) + last_name = models.CharField(max_length=200) + address = models.CharField(max_length=200) + emp_id = models.BigAutoField(primary_key=True) + mobile = models.CharField(max_length=10) + dept =models.CharField(max_length=100, choices=DEPARTMENT_CHOICES) + salary = models.IntegerField() + + def __str__(self): + return self.first_name + " " + self.last_name + + + + diff --git a/homework2/EmpManagement/employees/templates/index.html b/homework2/EmpManagement/employees/templates/index.html new file mode 100644 index 00000000..de26a070 --- /dev/null +++ b/homework2/EmpManagement/employees/templates/index.html @@ -0,0 +1,13 @@ + + + + + Sample + + + +

Hello World!

+

Welcome to my first Django project!

+ + + diff --git a/homework2/EmpManagement/employees/tests.py b/homework2/EmpManagement/employees/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/homework2/EmpManagement/employees/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/homework2/EmpManagement/employees/urls.py b/homework2/EmpManagement/employees/urls.py new file mode 100644 index 00000000..41c6a7c2 --- /dev/null +++ b/homework2/EmpManagement/employees/urls.py @@ -0,0 +1,8 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('employee/', views.EmployeeView.as_view()), + path('employee/', views.EmployeeView.as_view()), + path('employee/', views.EmployeeView.as_view()), +] \ No newline at end of file diff --git a/homework2/EmpManagement/employees/views.py b/homework2/EmpManagement/employees/views.py new file mode 100644 index 00000000..43323bcf --- /dev/null +++ b/homework2/EmpManagement/employees/views.py @@ -0,0 +1,78 @@ +from django.views import View +from .models import Employee +from django.http import JsonResponse +from django.views.decorators.csrf import csrf_exempt +from django.utils.decorators import method_decorator +import json + + + + +@method_decorator(csrf_exempt, name='dispatch') +class EmployeeView(View): + + def get(self, request, emp_id=None, dept=None): + employee_model_list = [] + try: + if emp_id: + employee_model_list = Employee.objects.filter(emp_id=emp_id) + elif dept: + employee_model_list = Employee.objects.filter(dept=dept) + except Employee.DoesNotExist: + return JsonResponse({'status': 'Failure', "employees": None}, status=400) + employeess = [] + for employee in employee_model_list: + data = { + "first_name" : employee.first_name, + "last_name": employee.last_name, + "address": employee.address, + "emp_id": employee.emp_id, + "mobile": employee.mobile, + "salary": employee.salary, + "dept": employee.dept + } + employeess.append(data) + return JsonResponse({'status': 'Success', "employees": employeess}, status=200) + + def post(self, request): + if not request.POST.get('first_name') or not request.POST.get('last_name') or not request.POST.get('address') or not request.POST.get('emp_id') or not request.POST.get('mobile') or not request.POST.get('dept') or not request.POST.get('salary'): + return JsonResponse({'status': 'Failure', "message" : "*all the fields are required"}, status=400) + Employee.objects.create( + first_name= request.POST.get('first_name'), + last_name= request.POST.get('last_name'), + address= request.POST.get('address'), + emp_id= request.POST.get('emp_id'), + mobile= request.POST.get('mobile'), + dept= request.POST.get('dept'), + salary= request.POST.get('salary')) + return JsonResponse({'status': 'sucess', "message" : "New employee added by id"}, status=200) + + def delete (self, request, emp_id=None): + try: + val = Employee.objects.get(emp_id=emp_id) + val.delete() + return JsonResponse({'status': 'Success', "message" : "Employee data deleted by id"}, status=200) + except Employee.DoesNotExist: + return JsonResponse({'status': 'Failure', "message" : "Employee not found"}, status=400) + + + + def put(self, request, emp_id=None): + try: + employee = Employee.objects.get(emp_id=emp_id) + except Employee.DoesNotExist: + return JsonResponse({'status': 'Failure', 'message': 'Employee not found'}, status=400) + + try: + request_data = json.loads(request.body) + except json.JSONDecodeError: + return JsonResponse({'status': 'Failure', 'message': 'Invalid JSON payload'}, status=400) + + salary = request_data.get('salary') + if salary is not None: + employee.salary = salary + employee.save() + return JsonResponse({'status': 'Success', 'message': 'Employee salary updated'}, status=200) + else: + return JsonResponse({'status': 'Failure', 'message': 'Please provide a new salary'}, status=400) + diff --git a/homework2/EmpManagement/event_logs.txt b/homework2/EmpManagement/event_logs.txt new file mode 100644 index 00000000..6e3ce568 --- /dev/null +++ b/homework2/EmpManagement/event_logs.txt @@ -0,0 +1,2 @@ +test_get_all_employees_for_dept: Success +test_get_employee_dept: Success diff --git a/homework2/EmpManagement/manage.py b/homework2/EmpManagement/manage.py new file mode 100644 index 00000000..5ac59ac7 --- /dev/null +++ b/homework2/EmpManagement/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', 'EmpManagement.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/homework2/EmpManagement/unittest_api.py b/homework2/EmpManagement/unittest_api.py new file mode 100644 index 00000000..86057a80 --- /dev/null +++ b/homework2/EmpManagement/unittest_api.py @@ -0,0 +1,53 @@ +import unittest +import requests + + +def print_log(*texts): + with open("event_logs.txt", "a") as file_object: + for text in texts: + file_object.write(str(text) + " ") + file_object.write("\n") + + +class Test(unittest.TestCase): + + def test_get_employee_dept(self): + try: + result = requests.get("http://0.0.0.0:8000/employees/employee/Finance") + self.assertEqual(result.status_code, 200) + self.assertIsNotNone(result.content, None) + print_log("test_get_employee_dept: Success") + except requests.exceptions.ConnectionError: + print('connection error occurred') + print_log("test_get_employee_dept: ConnectionError") + + def test_get_all_employess_for_dept(self): + try: + required = { + 'status': 'Success', + 'employees': [ + { + 'first_name': 'Prajwal', + 'last_name': 'SS', + 'emp_id': 8, + 'address': 'Mangalore', + 'mobile': '9847588659', + 'dept': 'Finance', + 'salary': 120000, + + } + ] + } + + result = requests.get("http://0.0.0.0:8000/employees/employee/8") + self.assertEqual(result.status_code, 200) + result = result.json() + print_log("test_get_all_employees_for_dept: Success") + + except requests.exceptions.ConnectionError: + print('connection error occurred') + print_log("test_get_all_emplyees_for_dept: ConnectionError") + + +if __name__ == '__main__': + unittest.main()