Skip to content

Commit

Permalink
added tutorial 13
Browse files Browse the repository at this point in the history
  • Loading branch information
techwithtim committed Dec 19, 2020
1 parent daf920b commit 05314c8
Show file tree
Hide file tree
Showing 25,446 changed files with 1,385,692 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
Empty file added Tutorial 13/api/__init__.py
Empty file.
Binary file added Tutorial 13/api/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added Tutorial 13/api/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added Tutorial 13/api/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file added Tutorial 13/api/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file added Tutorial 13/api/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added Tutorial 13/api/__pycache__/views.cpython-38.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions Tutorial 13/api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions Tutorial 13/api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
name = 'api'
25 changes: 25 additions & 0 deletions Tutorial 13/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.1.1 on 2020-11-05 02:22

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Room',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('code', models.CharField(default='', max_length=8, unique=True)),
('host', models.CharField(max_length=50, unique=True)),
('guest_can_pause', models.BooleanField(default=False)),
('votes_to_skip', models.IntegerField(default=1)),
('created_at', models.DateTimeField(auto_now_add=True)),
],
),
]
19 changes: 19 additions & 0 deletions Tutorial 13/api/migrations/0002_auto_20201218_1626.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 3.1.2 on 2020-12-18 21:26

import api.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='room',
name='code',
field=models.CharField(default=api.models.generate_unique_code, max_length=8, unique=True),
),
]
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
23 changes: 23 additions & 0 deletions Tutorial 13/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import models
import string
import random


def generate_unique_code():
length = 6

while True:
code = ''.join(random.choices(string.ascii_uppercase, k=length))
if Room.objects.filter(code=code).count() == 0:
break

return code


class Room(models.Model):
code = models.CharField(
max_length=8, default=generate_unique_code, unique=True)
host = models.CharField(max_length=50, unique=True)
guest_can_pause = models.BooleanField(null=False, default=False)
votes_to_skip = models.IntegerField(null=False, default=1)
created_at = models.DateTimeField(auto_now_add=True)
23 changes: 23 additions & 0 deletions Tutorial 13/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import serializers
from .models import Room


class RoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = ('id', 'code', 'host', 'guest_can_pause',
'votes_to_skip', 'created_at')


class CreateRoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = ('guest_can_pause', 'votes_to_skip')


class UpdateRoomSerializer(serializers.ModelSerializer):
code = serializers.CharField(validators=[])

class Meta:
model = Room
fields = ('guest_can_pause', 'votes_to_skip', 'code')
3 changes: 3 additions & 0 deletions Tutorial 13/api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
12 changes: 12 additions & 0 deletions Tutorial 13/api/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.urls import path
from .views import RoomView, CreateRoomView, GetRoom, JoinRoom, UserInRoom, LeaveRoom, UpdateRoom

urlpatterns = [
path('room', RoomView.as_view()),
path('create-room', CreateRoomView.as_view()),
path('get-room', GetRoom.as_view()),
path('join-room', JoinRoom.as_view()),
path('user-in-room', UserInRoom.as_view()),
path('leave-room', LeaveRoom.as_view()),
path('update-room', UpdateRoom.as_view())
]
135 changes: 135 additions & 0 deletions Tutorial 13/api/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from django.shortcuts import render
from rest_framework import generics, status
from .serializers import RoomSerializer, CreateRoomSerializer, UpdateRoomSerializer
from .models import Room
from rest_framework.views import APIView
from rest_framework.response import Response
from django.http import JsonResponse

# Create your views here.


class RoomView(generics.ListAPIView):
queryset = Room.objects.all()
serializer_class = RoomSerializer


class GetRoom(APIView):
serializer_class = RoomSerializer
lookup_url_kwarg = 'code'

def get(self, request, format=None):
code = request.GET.get(self.lookup_url_kwarg)
if code != None:
room = Room.objects.filter(code=code)
if len(room) > 0:
data = RoomSerializer(room[0]).data
data['is_host'] = self.request.session.session_key == room[0].host
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)


class JoinRoom(APIView):
lookup_url_kwarg = 'code'

def post(self, request, format=None):
if not self.request.session.exists(self.request.session.session_key):
self.request.session.create()

code = request.data.get(self.lookup_url_kwarg)
if code != None:
room_result = Room.objects.filter(code=code)
if len(room_result) > 0:
room = room_result[0]
self.request.session['room_code'] = code
return Response({'message': 'Room Joined!'}, status=status.HTTP_200_OK)

return Response({'Bad Request': 'Invalid Room Code'}, status=status.HTTP_400_BAD_REQUEST)

return Response({'Bad Request': 'Invalid post data, did not find a code key'}, status=status.HTTP_400_BAD_REQUEST)


class CreateRoomView(APIView):
serializer_class = CreateRoomSerializer

def post(self, request, format=None):
if not self.request.session.exists(self.request.session.session_key):
self.request.session.create()

serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
guest_can_pause = serializer.data.get('guest_can_pause')
votes_to_skip = serializer.data.get('votes_to_skip')
host = self.request.session.session_key
queryset = Room.objects.filter(host=host)
if queryset.exists():
room = queryset[0]
room.guest_can_pause = guest_can_pause
room.votes_to_skip = votes_to_skip
room.save(update_fields=['guest_can_pause', 'votes_to_skip'])
self.request.session['room_code'] = room.code
return Response(RoomSerializer(room).data, status=status.HTTP_200_OK)
else:
room = Room(host=host, guest_can_pause=guest_can_pause,
votes_to_skip=votes_to_skip)
room.save()
self.request.session['room_code'] = room.code
return Response(RoomSerializer(room).data, status=status.HTTP_201_CREATED)

return Response({'Bad Request': 'Invalid data...'}, status=status.HTTP_400_BAD_REQUEST)


class UserInRoom(APIView):
def get(self, request, format=None):
if not self.request.session.exists(self.request.session.session_key):
self.request.session.create()

data = {
'code': self.request.session.get('room_code')
}
return JsonResponse(data, status=status.HTTP_200_OK)


class LeaveRoom(APIView):
def post(self, request, format=None):
if 'room_code' in self.request.session:
self.request.session.pop('room_code')
host_id = self.request.session.session_key
room_results = Room.objects.filter(host=host_id)
if len(room_results) > 0:
room = room_results[0]
room.delete()

return Response({'Message': 'Success'}, status=status.HTTP_200_OK)


class UpdateRoom(APIView):
serializer_class = UpdateRoomSerializer

def patch(self, request, format=None):
if not self.request.session.exists(self.request.session.session_key):
self.request.session.create()

serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
guest_can_pause = serializer.data.get('guest_can_pause')
votes_to_skip = serializer.data.get('votes_to_skip')
code = serializer.data.get('code')

queryset = Room.objects.filter(code=code)
if not queryset.exists():
return Response({'msg': 'Room not found.'}, status=status.HTTP_404_NOT_FOUND)

room = queryset[0]
user_id = self.request.session.session_key
if room.host != user_id:
return Response({'msg': 'You are not the host of this room.'}, status=status.HTTP_403_FORBIDDEN)

room.guest_can_pause = guest_can_pause
room.votes_to_skip = votes_to_skip
room.save(update_fields=['guest_can_pause', 'votes_to_skip'])
return Response(RoomSerializer(room).data, status=status.HTTP_200_OK)

return Response({'Bad Request': "Invalid Data..."}, status=status.HTTP_400_BAD_REQUEST)
Binary file added Tutorial 13/db.sqlite3
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file added Tutorial 13/frontend/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file not shown.
Binary file added Tutorial 13/frontend/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions Tutorial 13/frontend/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions Tutorial 13/frontend/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class FrontendConfig(AppConfig):
name = 'frontend'
14 changes: 14 additions & 0 deletions Tutorial 13/frontend/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "10"
}
}
],
"@babel/preset-react"
],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
Empty file.
Binary file not shown.
3 changes: 3 additions & 0 deletions Tutorial 13/frontend/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
15 changes: 15 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/acorn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/acorn.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/acorn.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/browserslist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/browserslist.cmd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Tutorial 13/frontend/node_modules/.bin/browserslist.ps1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 05314c8

Please sign in to comment.