├── .gitattributes ├── api ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-39.pyc │ ├── admin.cpython-39.pyc │ ├── apps.cpython-39.pyc │ ├── models.cpython-39.pyc │ ├── serializers.cpython-39.pyc │ ├── urls.cpython-39.pyc │ └── views.cpython-39.pyc ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_employee.py │ ├── __init__.py │ └── __pycache__ │ │ ├── 0001_initial.cpython-39.pyc │ │ ├── 0002_employee.cpython-39.pyc │ │ └── __init__.cpython-39.pyc ├── models.py ├── serializers.py ├── tests.py ├── urls.py └── views.py ├── companyapi ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-39.pyc │ ├── settings.cpython-39.pyc │ ├── urls.cpython-39.pyc │ ├── views.cpython-39.pyc │ └── wsgi.cpython-39.pyc ├── asgi.py ├── settings.py ├── urls.py ├── views.py └── wsgi.py ├── db.sqlite3 └── manage.py /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /api/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__init__.py -------------------------------------------------------------------------------- /api/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/admin.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/admin.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/apps.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/apps.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/models.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/models.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/serializers.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/serializers.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/urls.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/urls.cpython-39.pyc -------------------------------------------------------------------------------- /api/__pycache__/views.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/__pycache__/views.cpython-39.pyc -------------------------------------------------------------------------------- /api/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from api.models import Company,Employee 3 | # Register your models here.. 4 | 5 | class CompanyAdmin(admin.ModelAdmin): 6 | list_display=('name','location','type') 7 | search_fields=('name',) 8 | 9 | class EmployeeAdmin(admin.ModelAdmin): 10 | list_display=('name','email','company') 11 | list_filter=('company',) 12 | 13 | admin.site.register(Company,CompanyAdmin) 14 | admin.site.register(Employee,EmployeeAdmin) 15 | 16 | -------------------------------------------------------------------------------- /api/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class ApiConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'api' 7 | -------------------------------------------------------------------------------- /api/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.13 on 2022-08-30 10:22 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [ 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Company', 16 | fields=[ 17 | ('company_id', models.AutoField(primary_key=True, serialize=False)), 18 | ('name', models.CharField(max_length=50)), 19 | ('location', models.CharField(max_length=50)), 20 | ('about', models.TextField()), 21 | ('type', models.CharField(choices=[('IT', 'IT'), ('Non IT', 'Non IT'), ('Mobiles Phones', 'Mobile Phones')], max_length=100)), 22 | ('added_date', models.DateTimeField(auto_now=True)), 23 | ('active', models.BooleanField(default=True)), 24 | ], 25 | ), 26 | ] 27 | -------------------------------------------------------------------------------- /api/migrations/0002_employee.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 3.2.13 on 2022-08-30 11:12 2 | 3 | from django.db import migrations, models 4 | import django.db.models.deletion 5 | 6 | 7 | class Migration(migrations.Migration): 8 | 9 | dependencies = [ 10 | ('api', '0001_initial'), 11 | ] 12 | 13 | operations = [ 14 | migrations.CreateModel( 15 | name='Employee', 16 | fields=[ 17 | ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), 18 | ('name', models.CharField(max_length=100)), 19 | ('email', models.CharField(max_length=50)), 20 | ('address', models.CharField(max_length=200)), 21 | ('phone', models.CharField(max_length=10)), 22 | ('about', models.TextField()), 23 | ('position', models.CharField(choices=[('Manager', 'manager'), ('Software Developer', 'sd'), ('Project Leader', 'pl')], max_length=50)), 24 | ('company', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.company')), 25 | ], 26 | ), 27 | ] 28 | -------------------------------------------------------------------------------- /api/migrations/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/migrations/__init__.py -------------------------------------------------------------------------------- /api/migrations/__pycache__/0001_initial.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/migrations/__pycache__/0001_initial.cpython-39.pyc -------------------------------------------------------------------------------- /api/migrations/__pycache__/0002_employee.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/migrations/__pycache__/0002_employee.cpython-39.pyc -------------------------------------------------------------------------------- /api/migrations/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/api/migrations/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /api/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | 5 | #Creating Company Model 6 | 7 | class Company(models.Model): 8 | company_id=models.AutoField(primary_key=True) 9 | name= models.CharField(max_length=50) 10 | location=models.CharField(max_length=50) 11 | about=models.TextField() 12 | type=models.CharField(max_length=100,choices= 13 | (('IT','IT'), 14 | ('Non IT','Non IT'), 15 | ("Mobiles Phones",'Mobile Phones') 16 | )) 17 | added_date=models.DateTimeField(auto_now=True) 18 | active=models.BooleanField(default=True) 19 | 20 | def __str__(self): 21 | return self.name +'--'+ self.location 22 | 23 | 24 | 25 | #Employee Model 26 | class Employee(models.Model): 27 | name=models.CharField(max_length=100) 28 | email=models.CharField(max_length=50) 29 | address=models.CharField(max_length=200) 30 | phone=models.CharField(max_length=10) 31 | about=models.TextField() 32 | position=models.CharField(max_length=50,choices=( 33 | ('Manager','manager'), 34 | ('Software Developer','sd'), 35 | ('Project Leader','pl') 36 | )) 37 | 38 | company=models.ForeignKey(Company, on_delete=models.CASCADE) 39 | 40 | -------------------------------------------------------------------------------- /api/serializers.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | from api.models import Company,Employee 3 | 4 | 5 | #create serializers here 6 | class CompanySerializer(serializers.HyperlinkedModelSerializer): 7 | company_id=serializers.ReadOnlyField() 8 | class Meta: 9 | model=Company 10 | fields="__all__" 11 | 12 | 13 | 14 | class EmployeeSerializer(serializers.HyperlinkedModelSerializer): 15 | id=serializers.ReadOnlyField() 16 | class Meta: 17 | model=Employee 18 | fields="__all__" -------------------------------------------------------------------------------- /api/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /api/urls.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | from django.urls import path,include 3 | from api.views import CompanyViewSet,EmployeeViewSet 4 | from rest_framework import routers 5 | 6 | 7 | router= routers.DefaultRouter() 8 | router.register(r'companies', CompanyViewSet) 9 | router.register(r'employees', EmployeeViewSet) 10 | 11 | urlpatterns = [ 12 | path('',include(router.urls)) 13 | 14 | ] 15 | 16 | 17 | #companies/{companyId}/employees -------------------------------------------------------------------------------- /api/views.py: -------------------------------------------------------------------------------- 1 | from django.shortcuts import render 2 | from rest_framework import viewsets 3 | from api.models import Company,Employee 4 | from api.serializers import CompanySerializer,EmployeeSerializer 5 | from rest_framework.decorators import action 6 | from rest_framework.response import Response 7 | # Create your views here. 8 | class CompanyViewSet(viewsets.ModelViewSet): 9 | queryset= Company.objects.all() 10 | serializer_class=CompanySerializer 11 | 12 | #companies/{companyId}/emplyees 13 | @action(detail=True,methods=['get']) 14 | def employees(self,request,pk=None): 15 | try: 16 | company=Company.objects.get(pk=pk) 17 | emps=Employee.objects.filter(company=company) 18 | emps_serializer=EmployeeSerializer(emps,many=True,context={'request':request}) 19 | return Response(emps_serializer.data) 20 | except Exception as e: 21 | print(e) 22 | return Response({ 23 | 'message':'Company might not exists !! Error' 24 | }) 25 | 26 | 27 | class EmployeeViewSet(viewsets.ModelViewSet): 28 | queryset=Employee.objects.all() 29 | serializer_class=EmployeeSerializer -------------------------------------------------------------------------------- /companyapi/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__init__.py -------------------------------------------------------------------------------- /companyapi/__pycache__/__init__.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__pycache__/__init__.cpython-39.pyc -------------------------------------------------------------------------------- /companyapi/__pycache__/settings.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__pycache__/settings.cpython-39.pyc -------------------------------------------------------------------------------- /companyapi/__pycache__/urls.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__pycache__/urls.cpython-39.pyc -------------------------------------------------------------------------------- /companyapi/__pycache__/views.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__pycache__/views.cpython-39.pyc -------------------------------------------------------------------------------- /companyapi/__pycache__/wsgi.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/companyapi/__pycache__/wsgi.cpython-39.pyc -------------------------------------------------------------------------------- /companyapi/asgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | ASGI config for companyapi project. 3 | 4 | It exposes the ASGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/asgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.asgi import get_asgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'companyapi.settings') 15 | 16 | application = get_asgi_application() 17 | -------------------------------------------------------------------------------- /companyapi/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for companyapi project. 3 | 4 | Generated by 'django-admin startproject' using Django 3.2.13. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/3.2/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | 15 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 16 | BASE_DIR = Path(__file__).resolve().parent.parent 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = 'django-insecure-5ufwtem@%!7hubt#=x98a9-elo2(-za6qylys25ee(1z_zgn9a' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'rest_framework', 41 | 'api' 42 | ] 43 | 44 | MIDDLEWARE = [ 45 | 'django.middleware.security.SecurityMiddleware', 46 | 'django.contrib.sessions.middleware.SessionMiddleware', 47 | 'django.middleware.common.CommonMiddleware', 48 | 'django.middleware.csrf.CsrfViewMiddleware', 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 50 | 'django.contrib.messages.middleware.MessageMiddleware', 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 52 | ] 53 | 54 | ROOT_URLCONF = 'companyapi.urls' 55 | 56 | TEMPLATES = [ 57 | { 58 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 59 | 'DIRS': [], 60 | 'APP_DIRS': True, 61 | 'OPTIONS': { 62 | 'context_processors': [ 63 | 'django.template.context_processors.debug', 64 | 'django.template.context_processors.request', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'companyapi.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/3.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': BASE_DIR / 'db.sqlite3', 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/3.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/3.2/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | 124 | # Default primary key field type 125 | # https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field 126 | 127 | DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' 128 | 129 | REST_FRAMEWORK = { 130 | # Use Django's standard `django.contrib.auth` permissions, 131 | # or allow read-only access for unauthenticated users. 132 | 'DEFAULT_PERMISSION_CLASSES': [ 133 | 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' 134 | ], 135 | 'DEFAULT_RENDERER_CLASSES': ( 136 | 'rest_framework.renderers.JSONRenderer', 137 | ) 138 | } 139 | -------------------------------------------------------------------------------- /companyapi/urls.py: -------------------------------------------------------------------------------- 1 | """companyapi URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/3.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.contrib import admin 17 | from django.urls import path,include 18 | from .views import home_page 19 | urlpatterns = [ 20 | path('admin/', admin.site.urls), 21 | path("home/",home_page), 22 | path("api/v1/",include('api.urls')) 23 | ] 24 | -------------------------------------------------------------------------------- /companyapi/views.py: -------------------------------------------------------------------------------- 1 | 2 | from django.http import HttpResponse,JsonResponse 3 | 4 | def home_page(request): 5 | print("home page requested") 6 | friends=[ 7 | 'ankit', 8 | 'ravi', 9 | 'uttam' 10 | ] 11 | return JsonResponse(friends,safe=False) -------------------------------------------------------------------------------- /companyapi/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for companyapi project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'companyapi.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /db.sqlite3: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/companyapi/913325c8d006f18a5a35dd74adbcc9657c14119e/db.sqlite3 -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'companyapi.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | --------------------------------------------------------------------------------