Skip to content

Commit

Permalink
Merge pull request #49 from dudtlstm/feature/filtering
Browse files Browse the repository at this point in the history
✨ feat: 댓글 작성 시 욕설 필터링
  • Loading branch information
dudtlstm authored May 27, 2024
2 parents 56f8abb + 3609c46 commit f2b0a4d
Show file tree
Hide file tree
Showing 2 changed files with 642 additions and 0 deletions.
36 changes: 36 additions & 0 deletions booth/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import secrets
import os
import re

from decouple import config

Expand All @@ -9,6 +11,7 @@
from rest_framework import viewsets, mixins, status
from rest_framework.decorators import action
from rest_framework.response import Response
from django.contrib.auth.hashers import make_password

from .models import Booth, BoothLike, TYPE_CHOICES, Comment

Expand All @@ -18,6 +21,19 @@

DEPLOY = config('DJANGO_DEPLOY', default=False, cast=bool)

def get_fword_list():
with open(os.path.join('static', 'fword_list.txt'), 'r', encoding='utf-8') as file:
fword_list = file.read().splitlines()
return fword_list

def censor_content(content):
fword_list = get_fword_list()
pattern_list = [re.escape(word).replace(r'\ ', r'\s*') for word in fword_list]
pattern = re.compile('|'.join(pattern_list), re.IGNORECASE)
censored_content = pattern.sub(lambda x: '*' * len(x.group()), content)
return censored_content


class BoothFilter(filters.FilterSet):
type = filters.MultipleChoiceFilter(field_name='type', choices=TYPE_CHOICES)
date = filters.CharFilter(method='filter_by_date')
Expand Down Expand Up @@ -128,6 +144,25 @@ def get_queryset(self, *args, **kwargs):
booth__id=self.kwargs.get("id")
)
return queryset

def create(self, request, *args, **kwargs):
#사용자가 작성한 댓글 내용 중 욕설 있는지 필터링
content = request.data.get('content')
censored_content = censor_content(content)

password = request.data.get('password')
hashed_password = make_password(password)

data = request.data.copy()
data['content'] = censored_content
data['password'] = hashed_password

serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)

headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

def destroy(self, request, *args, **kwargs):
instance = self.get_object()
Expand All @@ -136,3 +171,4 @@ def destroy(self, request, *args, **kwargs):
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(status=400)

Loading

0 comments on commit f2b0a4d

Please sign in to comment.