Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reformer api #179

Merged
merged 3 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions market/migrations/0004_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 5.1.1 on 2025-01-11 07:01

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


class Migration(migrations.Migration):

dependencies = [
("market", "0003_service_suspended"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Report",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("reason", models.CharField(max_length=255)),
("details", models.TextField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"reported_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="reports_received",
to=settings.AUTH_USER_MODEL,
),
),
(
"reporter_user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="reports_made",
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
17 changes: 17 additions & 0 deletions market/migrations/0005_alter_report_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.1 on 2025-01-11 07:03

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("market", "0004_report"),
]

operations = [
migrations.AlterModelTable(
name="report",
table="report",
),
]
17 changes: 17 additions & 0 deletions market/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import uuid

from django.conf import settings
from django.contrib.auth.models import User
from django.db import models

from core.models import TimeStampedModel
Expand Down Expand Up @@ -154,3 +156,18 @@ class ServiceOptionImage(TimeStampedModel):

class Meta:
db_table = "market_service_option_image"


class Report(models.Model):
reported_user = models.ForeignKey(
"users.User", related_name="reports_received", on_delete=models.CASCADE
)
reporter_user = models.ForeignKey(
"users.User", related_name="reports_made", on_delete=models.CASCADE
)
reason = models.CharField(max_length=255)
details = models.TextField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)

class Meta:
db_table = "report"
Empty file.
9 changes: 9 additions & 0 deletions market/serializers/report_serializer/reporter_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from market.models import Report


class ReportSerializer(serializers.ModelSerializer):
class Meta:
model = Report
fields = ["reported_user", "reporter_user", "reason", "details"]
16 changes: 15 additions & 1 deletion market/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ def test_delete_market_info(self, mock_boto3_client: MagicMock):
# 마켓 정보가 삭제 되었는지 확인
self.assertEqual(Market.objects.filter(market_uuid=market_uuid).count(), 0)

def test_report_market(self):
# 마켓 신고가 정상적으로 작동하는 것
response = self.client.post(
path="/api/market/report",
data={
"reported_user_id": self.test_user.id,
"reason": "허위 정보 게시",
"details": "잘못된 정보를 포함한 마켓 설명",
},
)
self.assertEqual(response.data["reported_user"], self.test_user.id)
self.assertEqual(response.data["reason"], "허위 정보 게시")

def test_get_service_list(self):
# 서비스 리스트 가져오기 테스트

Expand All @@ -297,6 +310,7 @@ def test_get_service_list(self):
format="json",
)
self.assertEqual(response.status_code, 201)

market_uuid = response.data.get("market_uuid", None)
self.assertIsNotNone(market_uuid, None)

Expand Down Expand Up @@ -347,7 +361,7 @@ def test_service_update_suspend_field(self):
service_uuid = response.data.get("service_uuid", None)
self.assertNotEqual(service_uuid, None)

service: Suspended = Service.objects.filter(service_uuid=service_uuid).first()
service = Service.objects.filter(service_uuid=service_uuid).first()
self.assertEqual(service.suspended, False)

# 3. 서비스 suspended 업데이트 시도
Expand Down
2 changes: 2 additions & 0 deletions market/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
)
from market.views.market_view.market_create_list_view import MarketCreateListView
from market.views.market_view.market_crud_view import MarketCrudView
from market.views.report_views import ReportUserView
from market.views.service_view.service_create_list_view import (
GetAllServiceView,
MarketServiceCreateListView,
Expand Down Expand Up @@ -83,4 +84,5 @@
ServiceOptionImageUploadView.as_view(),
name="market_service_option_image_upload",
),
path("/report", ReportUserView.as_view(), name="report_user"),
]
47 changes: 47 additions & 0 deletions market/views/report_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# market/views/report_views.py

from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView

from market.models import Report
from market.serializers.report_serializer.reporter_serializer import ReportSerializer
from users.models.user import User


class ReportUserView(APIView):
def post(self, request):
data = request.data
try:
reported_user = User.objects.get(id=data["reported_user_id"])
reporter_user = User.objects.get(id=request.user.id)

report: Report = Report.objects.create(
reported_user=reported_user,
reporter_user=reporter_user,
reason=data["reason"],
details=data.get("details", ""),
)
serializer = ReportSerializer(instance=report)

report_count = Report.objects.filter(reported_user=reported_user).count()
serializer.data["report_count"] = report_count

if report_count >= 5:
reported_user.is_active = False
reported_user.save()

return Response(data=serializer.data, status=status.HTTP_200_OK)

return Response(data=serializer.data, status=status.HTTP_201_CREATED)

except User.DoesNotExist:
return Response(
{"status": "error", "message": "사용자를 찾을 수 없습니다."},
status=status.HTTP_404_NOT_FOUND,
)
except Exception as e:
return Response(
data={"status": "error", "details": str(e)},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
Loading