-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ApiConfig(AppConfig): | ||
name = 'api' |
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)), | ||
], | ||
), | ||
] |
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
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') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import path | ||
from .views import RoomView, CreateRoomView | ||
|
||
urlpatterns = [ | ||
path('room', RoomView.as_view()), | ||
path('create-room', CreateRoomView.as_view()) | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.shortcuts import render | ||
from rest_framework import generics, status | ||
from .serializers import RoomSerializer, CreateRoomSerializer | ||
from .models import Room | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
|
||
|
||
# Create your views here. | ||
|
||
|
||
class RoomView(generics.ListAPIView): | ||
queryset = Room.objects.all() | ||
serializer_class = RoomSerializer | ||
|
||
|
||
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']) | ||
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() | ||
return Response(RoomSerializer(room).data, status=status.HTTP_201_CREATED) | ||
|
||
return Response({'Bad Request': 'Invalid data...'}, status=status.HTTP_400_BAD_REQUEST) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FrontendConfig(AppConfig): | ||
name = 'frontend' |
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"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.