Skip to content

Commit

Permalink
add apis
Browse files Browse the repository at this point in the history
  • Loading branch information
purush34 committed Nov 21, 2022
1 parent 0cbdd36 commit 9f1bdb4
Show file tree
Hide file tree
Showing 20 changed files with 148 additions and 6 deletions.
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django>=3.0,<=3.1
psycopg2
Django==4.1.3
psycopg2==2.9.5
djangorestframework==3.14.0
Binary file modified ssrBackend/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file modified ssrBackend/__pycache__/urls.cpython-310.pyc
Binary file not shown.
24 changes: 20 additions & 4 deletions ssrBackend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@
# Application definition

INSTALLED_APPS = [
'ssrData',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -73,11 +75,25 @@
# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

# Database
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases

# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'ssr',
'USER': 'ssrAdmin',
'PASSWORD': '$$r@2o22',
'HOST': 'ssr.postgres.database.azure.com',
'PORT': '5432',
'sslmode': 'require',
}
}


Expand Down
4 changes: 4 additions & 0 deletions ssrBackend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include


urlpatterns = [
path('admin/', admin.site.urls),
path('', include('ssrData.urls')),
path('api-auth/', include('rest_framework.urls')),
]
Binary file added ssrData/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/admin.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/apps.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/models.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/serializers.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added ssrData/__pycache__/views.cpython-310.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions ssrData/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from django.contrib import admin

# Register your models here.
from .models import Project

admin.site.register(Project)


26 changes: 26 additions & 0 deletions ssrData/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.1 on 2022-11-21 01:15

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Project',
fields=[
('projectId', models.CharField(max_length=20, primary_key=True, serialize=False)),
('name', models.CharField(max_length=20)),
('description', models.TextField(max_length=250)),
('year', models.IntegerField(validators=[django.core.validators.MinValueValidator(1900), django.core.validators.MaxValueValidator(2022)])),
('mentor', models.CharField(max_length=50)),
('category', models.CharField(max_length=20)),
],
),
]
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions ssrData/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
from django.db import models
import datetime
from django.core.validators import MinValueValidator,MaxValueValidator


# Create your models here.
class Project(models.Model):

projectId = models.CharField(max_length=20,primary_key=True)
name = models.CharField(max_length=20,blank=False)
description = models.TextField(max_length=250,blank=False)
year = models.IntegerField(blank=False,validators=[MinValueValidator(1900),MaxValueValidator(datetime.datetime.now().year)])
mentor = models.CharField(max_length=50,blank=False)
category = models.CharField(max_length=20,blank=False)



def __str__(self):
return self.name

8 changes: 8 additions & 0 deletions ssrData/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from rest_framework import serializers
from .models import Project


class projectSerializer(serializers.ModelSerializer):
class Meta:
model = Project
fields = ["projectId", "name", "description", "year", "mentor","category"]
11 changes: 11 additions & 0 deletions ssrData/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# from django.conf.urls import url
from django.urls import path, include
from ssrData import views

urlpatterns = [
path('projects/', views.ssrApiView),
path('projects/year/<int:year>/', views.ssrYear),
path('projects/category/<str:category>/', views.ssrCategory),
path('projects/year/', views.getYears),
path('projects/category/', views.getCatories),
]
54 changes: 54 additions & 0 deletions ssrData/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
from django.shortcuts import render
from django.http import JsonResponse
from .models import Project
from .serializers import projectSerializer
from rest_framework.decorators import api_view
from rest_framework.response import Response

# Create your views here.

# class ssrApiView(APIView):
# # add permission to check if user is authenticated
# permission_classes = [permissions.IsAuthenticated]
@api_view(['GET'])
def ssrApiView(request):
if request.method == 'GET':
projects = Project.objects.all()
serializer = projectSerializer(projects, many=True)
return JsonResponse({"Projects":serializer.data})
elif request.method == 'POST':
serializer = projectSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return JsonResponse({"Message":"Project posted successfully"})
return JsonResponse({"Error":serializer.errors})

def ssrYear(request, year):
projects = Project.objects.filter(year=year)
serializer = projectSerializer(projects, many=True)
return JsonResponse({"Projects":serializer.data})

def ssrCategory(request, category):
projects = Project.objects.filter(category=category)
serializer = projectSerializer(projects, many=True)
return JsonResponse({"Projects":serializer.data})

def getCatories(request):
try:
categories = Project.objects.values_list('category', flat=True).distinct()
return JsonResponse({"Categories":list(categories)})
except:
return JsonResponse({"Error":"No projects found"})

def getYears(request):
try:
years = Project.objects.values_list('year', flat=True).distinct()
return JsonResponse({"Years":list(years)})
except:
return JsonResponse({"Error":"No projects found"})


def ssrProjectId(request, projectId):
try :
project = Project.objects.get(projectId=projectId)
except Project.DoesNotExist:
return JsonResponse({"Error":"Project not found"})
serializer = projectSerializer(project)
return JsonResponse({"Project":serializer.data})

0 comments on commit 9f1bdb4

Please sign in to comment.