Skip to content

Commit

Permalink
Merge pull request #15 from Likelion-at-SMWU-10th/feat/#14-Balance
Browse files Browse the repository at this point in the history
Feat/#14 balance
  • Loading branch information
summit45 authored Aug 14, 2022
2 parents a3e96e6 + 55c22e7 commit 46bdf07
Show file tree
Hide file tree
Showing 21 changed files with 337 additions and 4 deletions.
Empty file added rememB/balanceapp/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions rememB/balanceapp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin

from .models import Answer, Balance, Question

# Register your models here.
admin.site.register(Question)
admin.site.register(Answer)
admin.site.register(Balance)
6 changes: 6 additions & 0 deletions rememB/balanceapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BalanceappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'balanceapp'
39 changes: 39 additions & 0 deletions rememB/balanceapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.0.6 on 2022-08-11 13:12

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


class Migration(migrations.Migration):

initial = True

dependencies = [
('userapp', '0004_alter_user_refreshtoken'),
]

operations = [
migrations.CreateModel(
name='Answer',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('answer_content', models.CharField(max_length=300, null=True)),
],
),
migrations.CreateModel(
name='Question',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('question_content', models.CharField(max_length=300, null=True)),
],
),
migrations.CreateModel(
name='Balance',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('answer_id', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='balanceapp.answer')),
('question_id', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='balanceapp.question')),
('user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='userapp.user')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.6 on 2022-08-11 13:25

from django.db import migrations, models


class Migration(migrations.Migration):

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

operations = [
migrations.AlterField(
model_name='answer',
name='answer_content',
field=models.CharField(max_length=300),
),
migrations.AlterField(
model_name='question',
name='question_content',
field=models.CharField(max_length=300),
),
]
Empty file.
23 changes: 23 additions & 0 deletions rememB/balanceapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import models

from userapp.models import User

# Create your models here.
class Question(models.Model):
question_content=models.CharField(max_length=300, null=False, blank=False,)

class Answer(models.Model):
answer_content=models.CharField(max_length=300, null=False, blank=False,)

class Balance(models.Model):
user=models.ForeignKey(User, on_delete=models.CASCADE, null=True,)
answer_id=models.ForeignKey(Answer, on_delete=models.CASCADE, null=True,)
question_id=models.ForeignKey(Question, on_delete=models.CASCADE, null=True,)

# class QAContent(models.Model):
# q_id=models.IntegerField()
# a1_id=models.IntegerField()
# a2_id=models.IntegerField()
# q_content=models.CharField()
# a1_content=models.CharField()
# a2_content=models.CharField()
72 changes: 72 additions & 0 deletions rememB/balanceapp/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from rest_framework import serializers
from .models import *
from datetime import datetime
from userapp.models import User

class QuestionSerializer(serializers.ModelSerializer):
class Meta:
model=Question
fields='__all__'

class AnswerSerializer(serializers.ModelSerializer):
class Meta:
model=Answer
fields='__all__'

class BalanceSerializer(serializers.ModelSerializer):
qcontent=serializers.SerializerMethodField()
acontent=serializers.SerializerMethodField()

class Meta:
model=Balance
fields = (
'user',
'question_id',
'answer_id',
'qcontent',
'acontent'
)

#유효하지 않은 값이 들어왔을 때 오류처리해야함
def get_qcontent(self, obj):
return Question.objects.get(id=obj.question_id.id).question_content

def get_acontent(self, obj):
return Answer.objects.get(id=obj.answer_id.id).answer_content


class BAQSerializer(serializers.ModelSerializer):
a1id=serializers.SerializerMethodField()
a1content=serializers.SerializerMethodField()
a2id=serializers.SerializerMethodField()
a2content=serializers.SerializerMethodField()

class Meta:
model=Question
fields = (
'id',
'question_content',
'a1id',
'a1content',
'a2id',
'a2content',
)

#유효하지 않은 값이 들어왔을 때 오류처리해야함

def get_a1id(self, obj):
return Answer.objects.get(id=(obj.id*2-1)).id

def get_a1content(self, obj):
return Answer.objects.get(id=(obj.id*2-1)).answer_content

def get_a2id(self, obj):
return Answer.objects.get(id=(obj.id*2)).id

def get_a2content(self, obj):
return Answer.objects.get(id=(obj.id*2)).answer_content





3 changes: 3 additions & 0 deletions rememB/balanceapp/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.
11 changes: 11 additions & 0 deletions rememB/balanceapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import path
from . import views


urlpatterns = [
path('api/question/', views.QuestionList.as_view(), name="api_question"),
path('api/answer/', views.AnswerList.as_view(), name="api_answer"),
path('api/alllist/<int:pk>/', views.BalanceList.as_view(), name="api_balance_by_id"),
path('mylist/<int:pk>/', views.myBalanceList.as_view(), name="mylist"),
path('game/<int:pk>/', views.myBalanceGame.as_view(), name="game"),
]
123 changes: 123 additions & 0 deletions rememB/balanceapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from datetime import datetime
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView

from .models import Answer, Balance, Question
from .serializers import AnswerSerializer, BAQSerializer, QuestionSerializer, BalanceSerializer
from userapp.models import User

from balanceapp import serializers

def getDayBefore(mybirthday):
mybirthdayList = mybirthday.split("-")
byear = int(mybirthdayList[0])
bmonth = int(mybirthdayList[1])
bday = int(mybirthdayList[2])

nowList = str(datetime.now().date()).split("-")
nmonth = int(nowList[1])
nday= int(nowList[2])

if(bmonth<nmonth | ((bmonth==nmonth) & (bday<nday))): #이미 생일이 지난경우
dday = datetime(2023, bmonth, bday).date()
now = datetime.now().date()
print(str(dday-now).split(",")[0].split(" ")[0])
return int(str(dday-now).split(",")[0].split(" ")[0])
else:
dday = datetime(2022, bmonth, bday).date()
print(dday)
now = datetime.now().date()
diff = str(dday-now).split(",")[0].split(" ")[0]
print(diff)
return int(diff)


class QuestionList(APIView):
def get(self, request): #작성한 모든 질문 보기
questions=Question.objects.all()
serializers=QuestionSerializer(questions, many=True)
return Response(serializers.data, status=status.HTTP_200_OK)

def post(self, request): #질문 작성해서 DB에 저장
serializer = QuestionSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class AnswerList(APIView):
def get(self, request): #작성한 모든 답 보기
answers=Answer.objects.all()
serializers=AnswerSerializer(answers, many=True)
return Response(serializers.data)

def post(self, request): #답 작성해서 DB에 저장
serializer = AnswerSerializer(data = request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class BalanceList(APIView): #user(pk)의 질문&대답 목록
def get(self, request, pk): #한 유저의 전체 질문&대답 fk 전해주기
balances=Balance.objects.filter(id=pk)
serializers=BalanceSerializer(balances,many=True)
return Response(serializers.data, status=status.HTTP_200_OK)

# balance/myist/<int:pk>/에서
# 1. 한 유저의 전체적인 질문 & 대답

# balance/game/<int:pk>/
# 1. 유저의 pk로 해당 유저를 찾고 생일을 알아낸다음 오늘 날짜랑 비교한다.
# 2. 7일 이내라면 해당 질문과 답을 보여준다. / 밀린 게 있을 수도 있으니 다수 가능
# 3. 해당 답을 받아온다.

#실제 프론트와 전달할 api
class myBalanceList(APIView): #user(pk)의 질문&대답 목록
def get(self, request, pk):
user=User.objects.get(id=pk)
balances=Balance.objects.filter(user=user)
serializers=BalanceSerializer(balances,many=True)
return Response(serializers.data, status=status.HTTP_200_OK)

class myBalanceGame(APIView):
def post(self, request, pk): #밸런스게임 질문-답 선택(질문 형식으로)
user=User.objects.get(id=pk)
leftDay=getDayBefore(str(user.birth))
print(request.data)
if(leftDay <=7):
request.data['user'] = user.id
serializer = BalanceSerializer(data=request.data)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response("아직 응답 못함", status=status.HTTP_200_OK)


def get(self, request, pk): #밸런스게임 질문-답 선택할수있도록
user=User.objects.get(id=pk)
leftDay=getDayBefore(str(user.birth))

if(leftDay <= 7): #생일 비교해보고 7일 이내라면
questions=Question.objects.filter(id__gt=(leftDay-1)) #쿼리셋
print(questions)
serializer = BAQSerializer(questions, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
# if serializer.is_valid():
# return Response(serializer.data, status=status.HTTP_200_OK) #질문이랑 데이터가 전달됨
# return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)




else: #7일 이내가 아니라면 아직 답을 확인할 수 없음
print("not yet")
return Response("not yet", status=status.HTTP_200_OK)


Empty file added rememB/partyroomapp/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions rememB/partyroomapp/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.
6 changes: 6 additions & 0 deletions rememB/partyroomapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PartyroomappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'partyroomapp'
Empty file.
3 changes: 3 additions & 0 deletions rememB/partyroomapp/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.
3 changes: 3 additions & 0 deletions rememB/partyroomapp/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.
6 changes: 6 additions & 0 deletions rememB/partyroomapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.urls import path
from . import views


urlpatterns = [
]
4 changes: 4 additions & 0 deletions rememB/partyroomapp/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django.shortcuts import render

from userapp.models import User

4 changes: 2 additions & 2 deletions rememB/rememB/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#'mainapp',
'partyroomapp',
'letterapp',
#'balanceapp',
'balanceapp',
'userapp',

#restframework
Expand Down
4 changes: 2 additions & 2 deletions rememB/rememB/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

urlpatterns = [
path('admin/', admin.site.urls),
#path('balance/',include('balanceapp.urls')),
path('balance/',include('balanceapp.urls')),
path('letter/',include('letterapp.urls')),
#path('main/',include('mainapp.urls')),
path('partyroom/',include('partyroomapp.urls')),
path('user/',include('userapp.urls')),
]

0 comments on commit 46bdf07

Please sign in to comment.