Skip to content

Commit

Permalink
Merge pull request #16 from LikeLion-at-DGU/lsj
Browse files Browse the repository at this point in the history
✨ Feat : Add feature of chat app
  • Loading branch information
pump9918 authored Oct 2, 2023
2 parents e377b0d + 13f431b commit f8ecccb
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 171 deletions.
96 changes: 48 additions & 48 deletions booth/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
# Generated by Django 4.2.5 on 2023-09-30 05:33

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Booth',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=255)),
('type', models.CharField(choices=[('주간부스', '주간부스'), ('야간부스', '야간부스')], max_length=10)),
('operater', models.CharField(max_length=255)),
('location', models.CharField(choices=[('사회과학관', '사회과학관'), ('혜화관', '혜화관'), ('팔정도', '팔정도'), ('원흥관', '원흥관'), ('만해광장', '만해광장'), ('학생회관', '학생회관'), ('학림관', '학림관')], max_length=10)),
('description', models.CharField(max_length=255)),
('start_at', models.DateTimeField(blank=True, null=True)),
('end_at', models.DateTimeField(blank=True, null=True)),
],
),
migrations.CreateModel(
name='Booth_like',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(blank=True, editable=False, max_length=10)),
('booth', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='likes', to='booth.booth')),
],
),
migrations.CreateModel(
name='Booth_image',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to=core.models.image_upload_path)),
('booth', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='boothimages', to='booth.booth')),
],
options={
'abstract': False,
},
),
]
# Generated by Django 4.2.5 on 2023-10-01 13:47

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


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Booth',
fields=[
('id', models.IntegerField(primary_key=True, serialize=False)),
('name', models.CharField(max_length=255)),
('type', models.CharField(choices=[('주간부스', '주간부스'), ('야간부스', '야간부스')], max_length=10)),
('operater', models.CharField(max_length=255)),
('location', models.CharField(choices=[('사회과학관', '사회과학관'), ('혜화관', '혜화관'), ('팔정도', '팔정도'), ('원흥관', '원흥관'), ('만해광장', '만해광장'), ('학생회관', '학생회관'), ('학림관', '학림관')], max_length=10)),
('description', models.CharField(max_length=255)),
('start_at', models.DateTimeField(blank=True, null=True)),
('end_at', models.DateTimeField(blank=True, null=True)),
],
),
migrations.CreateModel(
name='Booth_like',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(blank=True, editable=False, max_length=10)),
('booth', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='likes', to='booth.booth')),
],
),
migrations.CreateModel(
name='Booth_image',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('image', models.ImageField(blank=True, null=True, upload_to=core.models.booth_image_upload_path)),
('booth', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='boothimages', to='booth.booth')),
],
options={
'abstract': False,
},
),
]
19 changes: 0 additions & 19 deletions booth/migrations/0002_alter_booth_image_image.py

This file was deleted.

19 changes: 0 additions & 19 deletions booth/migrations/0003_alter_booth_image_image.py

This file was deleted.

19 changes: 0 additions & 19 deletions booth/migrations/0004_alter_booth_image_image.py

This file was deleted.

2 changes: 2 additions & 0 deletions chat/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.contrib import admin
from .models import*

# Register your models here.
admin.site.register(Chat)
5 changes: 3 additions & 2 deletions chat/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-09-27 02:01
# Generated by Django 4.2.5 on 2023-10-01 13:47

from django.db import migrations, models

Expand All @@ -16,7 +16,8 @@ class Migration(migrations.Migration):
fields=[
('id', models.BigIntegerField(primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('content', models.CharField(max_length=100)),
('content', models.TextField()),
('icon', models.CharField(choices=[('cry', 'cry'), ('hip', 'hip'), ('festival', 'festival'), ('fire', 'fire'), ('heart', 'heart')], default='cry', max_length=10)),
('key', models.CharField(blank=True, editable=False, max_length=10)),
],
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.5 on 2023-09-27 17:15
# Generated by Django 4.2.5 on 2023-10-01 14:23

from django.db import migrations, models

Expand All @@ -12,7 +12,7 @@ class Migration(migrations.Migration):
operations = [
migrations.AlterField(
model_name='chat',
name='content',
field=models.TextField(),
name='id',
field=models.BigAutoField(primary_key=True, serialize=False),
),
]
16 changes: 14 additions & 2 deletions chat/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
# Create your models here.
# 방명록
class Chat(models.Model):
id = models.BigIntegerField(primary_key=True)
id = models.BigAutoField(primary_key=True)
created_at = models.DateTimeField(auto_now_add=True)
# min_length, max_length 10~100인데 프론트와 협의 후 변경
content = models.TextField()
ICON_CHOICES=(
('cry','cry'),
('hip','hip'),
('festival','festival'),
('fire','fire'),
('heart','heart'),
)
icon = models.CharField(
max_length=10,
choices=ICON_CHOICES,
default='cry',
)
key=models.CharField(
max_length=10,
blank=True,
editable=False
)

def __str__(self):
return f'{self.booth}/{self.key}'
return f'{self.chat}/{self.key}'
4 changes: 4 additions & 0 deletions chat/paginations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from rest_framework.pagination import PageNumberPagination

class ChatPagination(PageNumberPagination):
page_size = 10
12 changes: 12 additions & 0 deletions chat/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import serializers
from .models import Chat

class ChatSerializer(serializers.ModelSerializer):
is_abused = serializers.BooleanField(read_only=True, default=False)

# 욕설 필터링 기능 추가 예정
def get_is_abused(self, obj):
return obj.is_abused
class Meta:
model = Chat
fields = ['id', 'content', 'icon', 'is_abused', 'key']
13 changes: 13 additions & 0 deletions chat/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path, include
from rest_framework import routers

from .views import *

app_name = 'chat'

default_router = routers.SimpleRouter(trailing_slash=False)
default_router.register('chat', ChatViewSet, basename='chat')

urlpatterns = [
path('', include(default_router.urls)),
]
51 changes: 50 additions & 1 deletion chat/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,52 @@
from django.shortcuts import render

from rest_framework import mixins, viewsets, status
from rest_framework.decorators import action
from .models import *
from .serializers import ChatSerializer
from rest_framework.response import Response
from .paginations import ChatPagination
# Create your views here.

class ChatViewSet(viewsets.GenericViewSet, mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin):
# queryset 가져오는 함수 오버라이딩 (최신순으로 정렬)
def get_queryset(self):
queryset = Chat.objects.all().order_by('created_at')
return queryset
serializer_class = ChatSerializer
pagination_class = ChatPagination

def create(self, reqest):
serializer = self.get_serializer(data=reqest.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

# 방명록의 왼쪽 열에 대한 api
@action(detail=False, methods=['GET'], url_path='dataleft')
def dataleft(self, request):
# id가 홀수인 방명록만 가져오기
dataleft = self.get_queryset().extra(where=["id % 2 = 1"])
page = self.paginate_queryset(dataleft)

if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(dataleft, many=True)
return Response(serializer.data)


# 방명록의 오른쪽 열에 대한 api
@action(detail=False, methods=['GET'], url_path='dataright')
def dataright(self, request):
# id가 짝수인 방명록만 가져오기
dataright = self.get_queryset().extra(where=["id % 2 = 0"])
page = self.paginate_queryset(dataright)

if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)

serializer = self.get_serializer(dataright, many=True)
return Response(serializer.data)
Loading

0 comments on commit f8ecccb

Please sign in to comment.