Created basic api serialization

This commit is contained in:
Praneeth Bhandaru 2020-12-12 16:18:10 -05:00
parent 9ba3c8b98e
commit 926e2b9bd0
8 changed files with 128 additions and 4 deletions

View File

@ -8,6 +8,8 @@ django = "*"
djangorestframework = "*"
django-cockroachdb = "*"
psycopg2 = "*"
pillow = "*"
django-cors-headers = "*"
[dev-packages]

44
site/Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "1a0c4ca347340e98b3dd8bad3d296f6e9b6b6692d4324a552d79f5580ce192fe"
"sha256": "96ba9fb628e24892849db03d49b9fab6a938477ef11fb3c5bb861a03d08e6ca1"
},
"pipfile-spec": 6,
"requires": {
@ -40,6 +40,14 @@
"index": "pypi",
"version": "==3.1.2"
},
"django-cors-headers": {
"hashes": [
"sha256:9322255c296d5f75089571f29e520c83ff9693df17aa3cf9f6a4bea7c6740169",
"sha256:db82b2840f667d47872ae3e4a4e0a0d72fbecb42779b8aa233fa8bb965f7836a"
],
"index": "pypi",
"version": "==3.5.0"
},
"djangorestframework": {
"hashes": [
"sha256:0209bafcb7b5010fdfec784034f059d512256424de2a0f084cb82b096d6dd6a7"
@ -47,6 +55,40 @@
"index": "pypi",
"version": "==3.12.2"
},
"pillow": {
"hashes": [
"sha256:006de60d7580d81f4a1a7e9f0173dc90a932e3905cc4d47ea909bc946302311a",
"sha256:0a2e8d03787ec7ad71dc18aec9367c946ef8ef50e1e78c71f743bc3a770f9fae",
"sha256:0eeeae397e5a79dc088d8297a4c2c6f901f8fb30db47795113a4a605d0f1e5ce",
"sha256:11c5c6e9b02c9dac08af04f093eb5a2f84857df70a7d4a6a6ad461aca803fb9e",
"sha256:2fb113757a369a6cdb189f8df3226e995acfed0a8919a72416626af1a0a71140",
"sha256:4b0ef2470c4979e345e4e0cc1bbac65fda11d0d7b789dbac035e4c6ce3f98adb",
"sha256:59e903ca800c8cfd1ebe482349ec7c35687b95e98cefae213e271c8c7fffa021",
"sha256:5abd653a23c35d980b332bc0431d39663b1709d64142e3652890df4c9b6970f6",
"sha256:5f9403af9c790cc18411ea398a6950ee2def2a830ad0cfe6dc9122e6d528b302",
"sha256:6b4a8fd632b4ebee28282a9fef4c341835a1aa8671e2770b6f89adc8e8c2703c",
"sha256:6c1aca8231625115104a06e4389fcd9ec88f0c9befbabd80dc206c35561be271",
"sha256:795e91a60f291e75de2e20e6bdd67770f793c8605b553cb6e4387ce0cb302e09",
"sha256:7ba0ba61252ab23052e642abdb17fd08fdcfdbbf3b74c969a30c58ac1ade7cd3",
"sha256:7c9401e68730d6c4245b8e361d3d13e1035cbc94db86b49dc7da8bec235d0015",
"sha256:81f812d8f5e8a09b246515fac141e9d10113229bc33ea073fec11403b016bcf3",
"sha256:895d54c0ddc78a478c80f9c438579ac15f3e27bf442c2a9aa74d41d0e4d12544",
"sha256:8de332053707c80963b589b22f8e0229f1be1f3ca862a932c1bcd48dafb18dd8",
"sha256:92c882b70a40c79de9f5294dc99390671e07fc0b0113d472cbea3fde15db1792",
"sha256:95edb1ed513e68bddc2aee3de66ceaf743590bf16c023fb9977adc4be15bd3f0",
"sha256:b63d4ff734263ae4ce6593798bcfee6dbfb00523c82753a3a03cbc05555a9cc3",
"sha256:bd7bf289e05470b1bc74889d1466d9ad4a56d201f24397557b6f65c24a6844b8",
"sha256:cc3ea6b23954da84dbee8025c616040d9aa5eaf34ea6895a0a762ee9d3e12e11",
"sha256:cc9ec588c6ef3a1325fa032ec14d97b7309db493782ea8c304666fb10c3bd9a7",
"sha256:d3d07c86d4efa1facdf32aa878bd508c0dc4f87c48125cc16b937baa4e5b5e11",
"sha256:d8a96747df78cda35980905bf26e72960cba6d355ace4780d4bdde3b217cdf1e",
"sha256:e38d58d9138ef972fceb7aeec4be02e3f01d383723965bfcef14d174c8ccd039",
"sha256:eb472586374dc66b31e36e14720747595c2b265ae962987261f044e5cce644b5",
"sha256:fbd922f702582cb0d71ef94442bfca57624352622d75e3be7a1e7e9360b07e72"
],
"index": "pypi",
"version": "==8.0.1"
},
"psycopg2": {
"hashes": [
"sha256:00195b5f6832dbf2876b8bf77f12bdce648224c89c880719c745b90515233301",

View File

@ -1,7 +1,36 @@
from django.db import models
from django.contrib.auth.models import AbstractUser
from PIL import Image
# Create your models here.
class User(AbstractUser):
pass
pass
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_pic = models.ImageField(default='default.jpg', upload_to='profile_pics')
def __str__(self):
return f"{self.user.username}'s Profile"
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.profile_pic.path)
if img.height > 300 or img.width > 300:
size = (300, 300)
img.thumbnail(size)
img.save(self.profile_pic.path)
class Classroom(models.Model):
student = models.ForeignKey(Profile, on_delete=models.CASCADE)
name = models.CharField(max_length=30)
teacher = models.CharField(max_length=30)
period = models.CharField(models.PositiveIntegerField, blank=True, unique=True, max_length=1)
def __str__(self):
return f"{self.student.user.username}'s Class: {self.name}"

20
site/api/serializers.py Normal file
View File

@ -0,0 +1,20 @@
from rest_framework import serializers
from .models import *
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = '__all__'
class ClassroomSerializer(serializers.ModelSerializer):
class Meta:
model = Classroom
fields = '__all__'

View File

@ -19,4 +19,5 @@ from .views import *
urlpatterns = [
path('ping/', PingView.as_view()),
path('', GetProfile.as_view())
]

View File

@ -3,6 +3,10 @@ from django.views.generic import View
from django.db import Error, OperationalError
from psycopg2 import errorcodes
from functools import wraps
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import generics, status
from .serializers import *
import time
@ -29,4 +33,23 @@ def retry_on_exception(view, num_retries=3, on_failure=HttpResponse(status=500),
class PingView(View):
def get(self, request, *args, **kwargs):
return HttpResponse("python/django", status=200)
return HttpResponse("python/django", status=200)
class GetProfile(APIView):
serializer_class = ProfileSerializer
lookup_url_kwarg = 'pk'
def get(self, request):
pk = request.GET.get(self.lookup_url_kwarg)
if pk:
profile = Profile.objects.filter(pk=pk)
if profile.exists():
user_profile = profile[0]
data = self.serializer_class(user_profile).data
return Response(data, status=status.HTTP_200_OK)
return Response({'Room Not Found': 'Invalid Room Code.'}, status=status.HTTP_404_NOT_FOUND)
return Response({'Bad Request': 'Code paramater not found in request'}, status=status.HTTP_400_BAD_REQUEST)

View File

@ -41,9 +41,11 @@ INSTALLED_APPS = [
'rest_framework',
'frontend',
'api',
'corsheaders',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
@ -138,3 +140,8 @@ STATIC_URL = '/static/'
## Custom
AUTH_USER_MODEL = 'api.User'
CORS_ORIGIN_ALLOW_ALL = True
# CORS_ORIGIN_WHITELIST = (
# 'http//localhost:8000',
# )

View File

@ -20,6 +20,6 @@ from frontend import urls as frontend_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api', include(api_urls)),
path('api/', include(api_urls)),
path('', include(frontend_urls))
]