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
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion myworld/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ services:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=123456
ports:
- '5446:5432'
- '5430:5432'
volumes:
- db:/var/lib/postgresql/data
volumes:
Expand Down
2 changes: 1 addition & 1 deletion myworld/dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN apk update && \
# Install requirements
RUN pip install --upgrade pip
RUN pip install Django psycopg2==2.9.3
# Create directories
# Create directories
RUN mkdir -p /root/workspace/src
COPY ./ /root/workspace/site
# Switch to project directory
Expand Down
Binary file modified myworld/members/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added myworld/members/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/members/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added myworld/members/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/members/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added myworld/members/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/members/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file modified myworld/members/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added myworld/members/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/members/__pycache__/views.cpython-310.pyc
Binary file not shown.
Binary file not shown.
8 changes: 6 additions & 2 deletions myworld/members/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from django.contrib import admin
from .models import Students
from .models import Employees

class DjStudentAdmin(admin.ModelAdmin):
list_display = ("first_name", "last_name", "address", "roll_number", "mobile", "branch")
list_filter = ("branch",)

class EmployeeAdmin(admin.ModelAdmin):
list_display = ("first_name", "last_name", "address", "emp_id" , "salary")

# Register your models here.
admin.site.register(Students, DjStudentAdmin)
admin.site.register(Students, DjStudentAdmin)
admin.site.register(Employees,EmployeeAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.1 on 2024-10-04 08:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('members', '0002_students_delete_members'),
]

operations = [
migrations.AddField(
model_name='students',
name='branch',
field=models.CharField(choices=[('BA', 'BA'), ('B.COM', 'B.COM'), ('MBA', 'MBA'), ('CA', 'CA')], max_length=10, null=True),
),
migrations.AlterField(
model_name='students',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
),
]
23 changes: 23 additions & 0 deletions myworld/members/migrations/0004_employees.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 5.1.1 on 2024-10-04 09:48

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('members', '0003_students_branch_alter_students_id'),
]

operations = [
migrations.CreateModel(
name='Employees',
fields=[
('first_name', models.CharField(max_length=200)),
('last_name', models.CharField(max_length=200)),
('address', models.CharField(max_length=200)),
('emp_id', models.IntegerField(max_length=10, primary_key=True, serialize=False)),
('salary', models.DecimalField(decimal_places=2, max_digits=10)),
],
),
]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified myworld/members/migrations/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
11 changes: 10 additions & 1 deletion myworld/members/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@ class Students(models.Model):
address = models.CharField(max_length=200)
roll_number = models.IntegerField()
mobile = models.CharField(max_length=10)
branch = models.CharField(max_length=10, choices=BRANCH_CHOICES)
branch = models.CharField(max_length=10, choices=BRANCH_CHOICES,null=True)

def __str__(self):
return self.first_name + " " + self.last_name
class Employees(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.IntegerField(max_length=10, primary_key= True)
salary = models.DecimalField(max_digits=10, decimal_places=2)

def __str__(self):
return self.first_name + " " + self.last_name
3 changes: 3 additions & 0 deletions myworld/members/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@
path('rest/student/<int:rolno>', views.StudentView.as_view()),
path('rest/student/', views.StudentView.as_view()),
path('rest/student/<str:branch>', views.StudentView.as_view()),
path('rest/employee/<int:emp_id>', views.EmployeeView.as_view()),
path('rest/employee/', views.EmployeeView.as_view()),

]
114 changes: 100 additions & 14 deletions myworld/members/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
from django.views import View
from .models import Students
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from .models import Students, Employees


# View to handle Students API
@method_decorator(csrf_exempt, name='dispatch')
class StudentView(View):

Expand All @@ -15,30 +16,115 @@ def get(self, request, rolno=None, branch=None):
student_model_list = Students.objects.filter(roll_number=rolno)
elif branch:
student_model_list = Students.objects.filter(branch=branch)
else:
student_model_list = Students.objects.all()
except Students.DoesNotExist:
return JsonResponse({'status': 'failed', "students": None}, status=400)

students = []
for student in student_model_list:
data = {
"first_name" : student.first_name,
"first_name": student.first_name,
"last_name": student.last_name,
"address": student.address,
"roll_number": student.roll_number,
"mobile": student.mobile,
"branch": student.branch
}
students.append(data)

return JsonResponse({'status': 'success', "students": students}, 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('roll_number') or not request.POST.get('mobile'):
return JsonResponse({'status': 'failed', "message" : "all fields required"}, status=500)

Students.objects.create(
first_name= request.POST.get('first_name'),
last_name= request.POST.get('last_name'),
address= request.POST.get('address'),
roll_number= request.POST.get('roll_number'),
mobile= request.POST.get('mobile'),
branch= request.POST.get('branch'))
return JsonResponse({'status': 'sucess'}, status=200)
try:
data = json.loads(request.body)
required_fields = ['first_name', 'last_name', 'address', 'roll_number', 'mobile']
for field in required_fields:
if not data.get(field):
return JsonResponse({'status': 'failed', "message": f"{field} is required"}, status=500)

Students.objects.create(
first_name=data.get('first_name'),
last_name=data.get('last_name'),
address=data.get('address'),
roll_number=data.get('roll_number'),
mobile=data.get('mobile'),
branch=data.get('branch')
)
return JsonResponse({'status': 'success', 'message': 'Student added successfully'}, status=200)

except json.JSONDecodeError:
return JsonResponse({'status': 'failed', "message": "Invalid JSON data"}, status=400)


# View to handle Employees API
@method_decorator(csrf_exempt, name='dispatch')
class EmployeeView(View):

def get(self, request, emp_id=None):
employee_list = []
try:
if emp_id:
employee_list = Employees.objects.filter(emp_id=emp_id)
else:
employee_list = Employees.objects.all()
except Employees.DoesNotExist:
return JsonResponse({'status': 'failed', "employees": None}, status=400)

employees = []
for employee in employee_list:
data = {
"first_name": employee.first_name,
"last_name": employee.last_name,
"address": employee.address,
"emp_id": employee.emp_id,
"salary": employee.salary
}
employees.append(data)

return JsonResponse({'status': 'success', "employees": employees}, status=200)

def post(self, request):
try:
data = json.loads(request.body)
required_fields = ['first_name', 'last_name', 'address', 'emp_id', 'salary']
for field in required_fields:
if not data.get(field):
return JsonResponse({'status': 'failed', "message": f"{field} is required"}, status=500)

Employees.objects.create(
first_name=data.get('first_name'),
last_name=data.get('last_name'),
address=data.get('address'),
emp_id=data.get('emp_id'),
salary=data.get('salary')
)
return JsonResponse({'status': 'success', 'message': 'Employee added successfully'}, status=200)

except json.JSONDecodeError:
return JsonResponse({'status': 'failed', "message": "Invalid JSON data"}, status=400)

def delete(self, request, emp_id):
try:
employee = Employees.objects.get(emp_id=emp_id)
employee.delete()
return JsonResponse({'status': 'success', 'message': 'Employee deleted successfully'}, status=200)
except Employees.DoesNotExist:
return JsonResponse({'status': 'failed', 'message': 'Employee not found'}, status=404)

def patch(self, request, emp_id):
try:
employee = Employees.objects.get(emp_id=emp_id)
data = json.loads(request.body)

# Update the salary if provided in the request
if 'salary' in data:
employee.salary = data['salary']
employee.save()
return JsonResponse({'status': 'success', 'message': 'Salary updated successfully'}, status=200)

return JsonResponse({'status': 'failed', 'message': 'No fields to update'}, status=400)
except Employees.DoesNotExist:
return JsonResponse({'status': 'failed', 'message': 'Employee not found'}, status=404)
except json.JSONDecodeError:
return JsonResponse({'status': 'failed', "message": "Invalid JSON data"}, status=400)
Binary file modified myworld/myworld/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added myworld/myworld/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/myworld/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file modified myworld/myworld/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added myworld/myworld/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified myworld/myworld/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
11 changes: 11 additions & 0 deletions myworld/myworld/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
"""

from pathlib import Path
import os

# settings.py

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
Expand Down Expand Up @@ -85,6 +90,12 @@
}
}

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join('BASE_DIR' , 'db.sqlite3'),
# }
# }
# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

Expand Down
22 changes: 22 additions & 0 deletions myworld/test1/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', 'test1.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 myworld/test1/test1/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions myworld/test1/test1/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for test1 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.1/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test1.settings')

application = get_asgi_application()
Loading