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

[Backend] Report Endpoint Implementation #503

Merged
merged 1 commit into from
Dec 26, 2022
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
25 changes: 25 additions & 0 deletions app/backend/forum/migrations/0013_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.1.2 on 2022-12-25 19:16

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('forum', '0012_post_related_labels'),
]

operations = [
migrations.CreateModel(
name='Report',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content_id', models.IntegerField(default=0)),
('content_type', models.IntegerField(default=0)),
('reporter', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
9 changes: 8 additions & 1 deletion app/backend/forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,11 @@ class Comment(models.Model):

class CommentImages(models.Model):
image_url = models.CharField(max_length=100)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)

class Report(models.Model):
content_id = models.IntegerField(null=False, default=0)
content_type = models.IntegerField(null=False, default=0)
# content_type = 0 : Post
# content_type = 1 : Comment
reporter = models.ForeignKey(CustomUser, null=False, on_delete=models.CASCADE)
11 changes: 11 additions & 0 deletions app/backend/forum/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,15 @@ def test_bookmark_remove_post(self):
response2 = client.post(f'/forum/post/{post.id}/bookmark', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response2.status_code, 200)

def test_report_content(self):
#tests are not done.
client = Client()
user = backendModels.CustomUser.objects.create_user(email="joedoetest@gmail.com", password="testpassword",
type=2)
token = Token.objects.create(user=user)
post = models.Post.objects.create(title='test post title', body='test post body', author=user, date=datetime.now())
response = client.post(f'/forum/report_content', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response.status_code, 200)

1 change: 1 addition & 0 deletions app/backend/forum/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
path('post/<int:id>/comment', views.create_comment, name='create comment'),
path('categories', views.get_all_categories, name='get_all_categories'),
path('post/<int:id>/bookmark', views.bookmark_post, name='bookmark a post'),
path('report_content', views.report_content, name='report content'),
]
38 changes: 36 additions & 2 deletions app/backend/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime
from rest_framework.response import Response
from forum.serializers import PostSerializer, CommentSerializer, UpdatePostSerializer, CreateCommentSerializer, LabelSerializer, CategorySerializer
from forum.models import Post, Comment
from forum.models import Post, Comment, Report
from common.views import upload_to_s3, delete_from_s3
from rest_framework.pagination import PageNumberPagination
from django.shortcuts import get_object_or_404, render
Expand Down Expand Up @@ -814,4 +814,38 @@ def bookmark_post(request, id):
else:
user_info.bookmarked_posts.append(id)
user_info.save()
return Response({'response': 'Bookmark added successfully'}, status=200)
return Response({'response': 'Bookmark added successfully'}, status=200)


@api_view(['POST',])
@permission_classes([IsAuthenticated, ])
def report_content(request):
# content_type = 0 : Post
# content_type = 1 : Comment
try:

content_id = request.data['content_id']
content_type = request.data['content_type']

try:
if content_type == 0:
post = Post.objects.get(id=content_id)

elif content_type == 1:
comment = Comment.objects.get(id=content_id)
else:
return Response({'error': 'Wrong content_type, please use 0 or 1'}, status=400)

except:
return Response({'error': 'Wrong content_id'}, status=400)



reporter = request.user
report = Report(content_id=content_id, content_type=content_type, reporter=reporter)
report.save()
return Response({'response': 'Content is reported successfully'}, status=200)

except:

return Response({'error': 'Content not found'}, status=400)