Skip to content

Commit

Permalink
Added report and suggestion functions in argument viewset actions and…
Browse files Browse the repository at this point in the history
… added tests. Updated report model fields.
  • Loading branch information
purple-void committed Aug 18, 2024
1 parent b45121a commit b80f64b
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 16 deletions.
22 changes: 22 additions & 0 deletions backend/core/migrations/0013_report_options_alter_report_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 5.0.7 on 2024-08-17 20:11

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0012_report_alter_post_type"),
]

operations = [
migrations.AddField(
model_name="report",
name="options",
field=models.CharField(max_length=255, null=True),
),
migrations.AlterField(
model_name="report",
name="body",
field=models.CharField(max_length=255, null=True),
),
]
5 changes: 4 additions & 1 deletion backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
USERNAME_MAX_LEN = 255
AVATAR_MAX_LEN = 255
BIO_MAX_LEN = 255
OPTIONS_MAX_LEN = 255
REPORT_BODY_MAX_LEN = 255
ARGUMENT = "argument"
REBUTTAL = "rebuttal"
COMMENT = "comment"
Expand Down Expand Up @@ -43,8 +45,9 @@ class Post(models.Model):
class Report(models.Model):
ownerUserId: models.IntegerField = models.IntegerField(null=True)
parentId: models.IntegerField = models.IntegerField(null=True)
body: models.TextField = models.TextField()
body: models.CharField = models.CharField(max_length=REPORT_BODY_MAX_LEN, null=True)
created: models.DateTimeField = models.DateTimeField(auto_now_add=True)
options: models.CharField = models.CharField(max_length=OPTIONS_MAX_LEN, null=True)


class UserProfile(models.Model):
Expand Down
25 changes: 25 additions & 0 deletions backend/core/tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def setUp(self):

# Create sample post
self.sample_post = Post.objects.create(
id="76457211",
type="argument",
isPrivate=False,
created="2024-06-26 02:20:58.689998+00:00",
Expand Down Expand Up @@ -60,6 +61,18 @@ def setUp(self):
ownerUserId=1,
)

# Create sample suggestion
self.sample_suggestion = {
"id": "3245645456542324356",
"parentId": self.sample_post.id,
"type": "suggestion",
"isPrivate": False,
"created": "2024-06-26 02:20:58.689998+00:00",
"updated": "2024-06-26 02:20:58.689998+00:00",
"body": "<p>Sample suggestion content</p>",
"ownerUserId": 1,
}

def test_posts_api(self):
# Test the posts API endpoint
page_size = 5
Expand All @@ -86,3 +99,15 @@ def test_comments_api(self):
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_comment.type)

def test_suggestions_api(self):
# Test the suggestions API endpoint
response = self.client.post(
reverse("arguments-suggest-edit", kwargs={"pk": self.sample_post.id}),
data=self.sample_suggestion,
)
self.assertEqual(response.status_code, 200)
if not Post.objects.filter(
type="suggestion", id=self.sample_suggestion["id"]
).exists():
self.fail("Sample suggestion not found")
58 changes: 58 additions & 0 deletions backend/core/tests/test_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from django.test import Client, TestCase
from django.urls import reverse

from core.models import Post, Report


class ReportTests(TestCase):
def setUp(self):
# Create a client instance
self.client = Client()

# Create sample post
self.sample_post = Post.objects.create(
id="008643568",
type="argument",
isPrivate=False,
created="2024-06-26 02:20:58.689998+00:00",
updated="2024-06-26 02:20:58.689998+00:00",
body="<p>Sample post content</p>",
ownerUserId=1,
title="Sample Title",
)

# Create sample report dict
self.sample_report_dict = {
"id": "67659876",
"parentId": "008643568",
"created": "2024-06-26 02:20:58.689998+00:00",
"body": "<p>Sample report content</p>",
"options": "sample report options",
}

# Create sample report
self.sample_report = Report.objects.create(
id="65775545",
parentId="008643568",
created="2024-06-26 02:20:58.689998+00:00",
body="<p>Sample report content</p>",
options="sample report options",
)

def test_add_reports_api(self):
# Test the suggestions API endpoint
response = self.client.post(
reverse("arguments-add-reports", kwargs={"pk": self.sample_post.id}),
data=self.sample_report_dict,
)
self.assertEqual(response.status_code, 200)
if not Report.objects.filter(id=self.sample_report_dict["id"]).exists():
self.fail("Sample report not found")

def test_report_options_api(self):
# Test the report options API endpoint
response = self.client.get(
reverse("arguments-reports-options", kwargs={"pk": self.sample_post.id})
)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_report.options)
65 changes: 50 additions & 15 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)
from rest_framework.response import Response

from .models import Post, UserProfile, Vote
from .models import Post, Report, UserProfile, Vote
from .serializers import (
ArgumentSerializer,
CommentSerializer,
Expand Down Expand Up @@ -79,20 +79,55 @@ def get_permissions(self):
return [IsOwnerOrReadOnly()]
return [AllowAny()]

@action(detail=True, url_path="reports/add", methods=["post"])
def add_reports(self):
# put your code here
print("test")

@action(detail=True, url_path="reports/options", methods=["get"])
def reports_options(self):
# put your code here
print("test")

@action(detail=True, url_path="suggest-edit", methods=["post"])
def suggest_edit(self):
# put your code here
print("test")
@action(
detail=True,
url_path="reports/add",
methods=["post"],
serializer_class=ReportSerializer,
)
def add_reports(self, *args, **kwargs):
# creates a report
data = self.request.data
report = {}
for key in data:
report[key] = data[key]
Report.objects.create(**report)
if Report.objects.all().filter(id=report["id"]):
return HttpResponse("", status=200)
else:
return HttpResponse("", status=404)

@action(
detail=True,
url_path="reports/options",
methods=["get"],
serializer_class=ReportSerializer,
pagination_class=CursorPaginationViewSet,
)
def reports_options(self, *args, **kwargs):
# gets a report's options
argumentId = kwargs["pk"]
queryset = Report.objects.filter(parentId=argumentId)
options = queryset.first().options
return HttpResponse(options, status=200)

@action(
detail=True,
url_path="suggest-edit",
methods=["post"],
serializer_class=SuggestionSerializer,
)
def suggest_edit(self, *args, **kwargs):
# creates a suggestion
data = self.request.data
suggestion = {}
for key in data:
suggestion[key] = data[key]
Post.objects.create(**suggestion)
if Post.objects.all().filter(type="suggestion", id=suggestion["id"]):
return HttpResponse("", status=200)
else:
return HttpResponse("", status=404)


class RebuttalViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit b80f64b

Please sign in to comment.