Skip to content

Commit

Permalink
Merge pull request #217 from Monstarrrr/feat/4_add-arguments
Browse files Browse the repository at this point in the history
#4 Create new arguments
  • Loading branch information
seporterfield authored Jun 26, 2024
2 parents 9572e25 + e058bcb commit 6b7140b
Show file tree
Hide file tree
Showing 28 changed files with 479 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Generated by Django 5.0.4 on 2024-06-24 17:31

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0002_alter_posts_answercount_alter_posts_commentcount_and_more"),
]

operations = [
migrations.RemoveField(
model_name="posts",
name="acceptedAnswerId",
),
migrations.RemoveField(
model_name="posts",
name="answerCount",
),
migrations.RemoveField(
model_name="posts",
name="closedDate",
),
migrations.RemoveField(
model_name="posts",
name="commentCount",
),
migrations.RemoveField(
model_name="posts",
name="communityOwnedDate",
),
migrations.RemoveField(
model_name="posts",
name="creationDate",
),
migrations.RemoveField(
model_name="posts",
name="deletionDate",
),
migrations.RemoveField(
model_name="posts",
name="favoriteCount",
),
migrations.RemoveField(
model_name="posts",
name="lastActivityDate",
),
migrations.RemoveField(
model_name="posts",
name="lastEditDate",
),
migrations.RemoveField(
model_name="posts",
name="lastEditorDisplayName",
),
migrations.RemoveField(
model_name="posts",
name="lastEditorUserId",
),
migrations.RemoveField(
model_name="posts",
name="ownerDisplayName",
),
migrations.RemoveField(
model_name="posts",
name="parentId",
),
migrations.RemoveField(
model_name="posts",
name="postTypeId",
),
migrations.RemoveField(
model_name="posts",
name="score",
),
migrations.RemoveField(
model_name="posts",
name="tags",
),
migrations.RemoveField(
model_name="posts",
name="viewcount",
),
migrations.AddField(
model_name="posts",
name="type",
field=models.CharField(
choices=[
("argument", "argument"),
("rebuttal", "rebuttal"),
("comment", "comment"),
],
default="argument",
max_length=10,
),
),
]
26 changes: 26 additions & 0 deletions backend/core/migrations/0004_posts_createdat_posts_updatedat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.4 on 2024-06-25 14:53

import django.utils.timezone
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0003_remove_posts_acceptedanswerid_and_more"),
]

operations = [
migrations.AddField(
model_name="posts",
name="createdAt",
field=models.DateTimeField(
auto_now_add=True, default=django.utils.timezone.now
),
preserve_default=False,
),
migrations.AddField(
model_name="posts",
name="updatedAt",
field=models.DateTimeField(auto_now=True),
),
]
15 changes: 15 additions & 0 deletions backend/core/migrations/0005_delete_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Django 5.0.4 on 2024-06-26 02:24

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("core", "0004_posts_createdat_posts_updatedat"),
]

operations = [
migrations.DeleteModel(
name="Tags",
),
]
58 changes: 15 additions & 43 deletions backend/core/models.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,33 @@
from django.contrib.auth.models import User
from django.db import models

TAG_NAME_MAX_LEN = 50
TITLE_MAX_LEN = 255
LAST_EDITOR_MAX_LEN = 255
OWNER_MAX_LEN = 255
USERNAME_MAX_LEN = 255
AVATAR_MAX_LEN = 255
BIO_MAX_LEN = 255


class Tags(models.Model):
tagName: models.CharField = models.CharField(max_length=TAG_NAME_MAX_LEN)
count: models.IntegerField = models.IntegerField(default=0)
excerptPostId: models.IntegerField = models.IntegerField(null=True)
wikiPostId: models.IntegerField = models.IntegerField(null=True)
isModeratorOnly: models.BooleanField = models.BooleanField(default=False)
isRequired: models.BooleanField = models.BooleanField(default=False)
ARGUMENT = "argument"
REBUTTAL = "rebuttal"
COMMENT = "comment"
POSTS_TYPES = [
(ARGUMENT, "argument"),
(REBUTTAL, "rebuttal"),
(COMMENT, "comment"),
]


class Posts(models.Model):
"""Posts database model
Posts include the following types, marked
by their postTypeId
"""Posts database model"""

Arguments (1)
Rebuttals (2)
Comments (3)
Tag wikis (4)
"""

postTypeId: models.IntegerField = models.IntegerField()
acceptedAnswerId: models.IntegerField = models.IntegerField(null=True)
parentId: models.IntegerField = models.IntegerField(null=True)
creationDate: models.DateField = models.DateField()
deletionDate: models.DateField = models.DateField(null=True)
score: models.IntegerField = models.IntegerField(default=0)
viewcount: models.IntegerField = models.IntegerField(default=0, null=True)
body: models.TextField = models.TextField() # render as HTML
ownerUserId: models.IntegerField = models.IntegerField(null=True)
ownerDisplayName: models.CharField = models.CharField(
max_length=OWNER_MAX_LEN, null=True
)
lastEditorUserId: models.IntegerField = models.IntegerField(null=True)
lastEditorDisplayName: models.CharField = models.CharField(
max_length=LAST_EDITOR_MAX_LEN, null=True
type: models.CharField = models.CharField(
max_length=10, choices=POSTS_TYPES, default=ARGUMENT
)
lastEditDate: models.DateField = models.DateField(null=True)
lastActivityDate: models.DateField = models.DateField()
body: models.TextField = models.TextField() # render as HTML
title: models.CharField = models.CharField(max_length=TITLE_MAX_LEN)
tags: models.ManyToManyField = models.ManyToManyField(Tags, related_name="tags")
answerCount: models.IntegerField = models.IntegerField(default=0, null=True)
commentCount: models.IntegerField = models.IntegerField(default=0, null=True)
favoriteCount: models.IntegerField = models.IntegerField(default=0, null=True)
closedDate: models.DateField = models.DateField(null=True)
communityOwnedDate: models.DateField = models.DateField(null=True)
ownerUserId: models.IntegerField = models.IntegerField(null=True)
createdAt: models.DateTimeField = models.DateTimeField(auto_now_add=True)
updatedAt: models.DateTimeField = models.DateTimeField(auto_now=True)


class UserProfile(models.Model):
Expand Down
11 changes: 7 additions & 4 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from rest_framework import serializers

from .models import Posts, Tags, UserProfile
from .models import Posts, UserProfile


class TagSerializer(serializers.ModelSerializer):
class ArgumentSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = "__all__"
model = Posts
fields = ["id", "body", "title", "ownerUserId", "createdAt", "updatedAt"]
read_only_fields = [
"ownerUserId",
]


class PostSerializer(serializers.ModelSerializer):
Expand Down
73 changes: 5 additions & 68 deletions backend/core/tests/test_posts.py
Original file line number Diff line number Diff line change
@@ -1,89 +1,26 @@
# Create your tests here.
from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse

from ..models import Posts, Tags, UserProfile
from core.models import Posts


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

# Create sample tag
self.sample_tag = Tags.objects.create(
tagName="tag1",
count=1,
excerptPostId=1,
wikiPostId=1,
isModeratorOnly=False,
isRequired=True,
)

# Create sample post
self.sample_post = Posts.objects.create(
postTypeId=1,
acceptedAnswerId=1,
parentId=1,
creationDate="2024-01-01",
deletionDate=None,
score=10,
viewcount=100,
type="argument",
createdAt="2024-06-26 02:20:58.689998+00:00",
updatedAt="2024-06-26 02:20:58.689998+00:00",
body="<p>Sample post content</p>",
ownerUserId=1,
ownerDisplayName="John Doe",
lastEditorUserId=1,
lastEditorDisplayName="Jane Doe",
lastEditDate="2024-01-02",
lastActivityDate="2024-01-03",
title="Sample Title",
answerCount=0,
commentCount=0,
favoriteCount=0,
closedDate=None,
communityOwnedDate=None,
)
# Correctly assign the tag to the post
self.sample_post.tags.set([self.sample_tag])

# Create sample user
user = User.objects.create_superuser("username")
# Create sample user profile
self.sample_user_profile = UserProfile.objects.create(
user=user,
username="username",
avatar="avatar",
bio="bio",
reputation=1,
joinDate="2024-01-01",
upVotes=1,
downVotes=1,
)
# Correctly assign the post to the user profile
self.sample_user_profile.posts.set([self.sample_post])
self.sample_user_profile.edits.set([self.sample_post])
self.sample_user_profile.savedPosts.set([self.sample_post])
self.sample_user_profile.private_post.set([self.sample_post])

def test_tags_api(self):
# Test the tags API endpoint
response = self.client.get(reverse("tags-list"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_tag.tagName)

def test_posts_api(self):
# Test the posts API endpoint
response = self.client.get(reverse("posts-list"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_post.title)

def test_user_profile_api(self):
# Test the user profile API endpoint
response = self.client.get(reverse("user-profile-list"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_user_profile.username)

def test_alivecheck_smoketest(self):
response = self.client.get(reverse("alive-list"))
self.assertEqual(response.status_code, 200)
43 changes: 43 additions & 0 deletions backend/core/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.contrib.auth.models import User
from django.test import Client, TestCase
from django.urls import reverse

from core.models import Posts, UserProfile


class UserTests(TestCase):
def setUp(self):
self.client = Client()
user = User.objects.create_superuser("username")

self.sample_post = Posts.objects.create(
type="argument",
createdAt="2024-06-26 02:20:58.689998+00:00",
updatedAt="2024-06-26 02:20:58.689998+00:00",
body="<p>Sample post content</p>",
ownerUserId=1,
title="Sample Title",
)

# Create sample user profile
self.sample_user_profile = UserProfile.objects.create(
user=user,
username="username",
avatar="avatar",
bio="bio",
reputation=1,
joinDate="2024-01-01",
upVotes=1,
downVotes=1,
)
# Correctly assign the post to the user profile
self.sample_user_profile.posts.set([self.sample_post])
self.sample_user_profile.edits.set([self.sample_post])
self.sample_user_profile.savedPosts.set([self.sample_post])
self.sample_user_profile.private_post.set([self.sample_post])

def test_user_profile_api(self):
# Test the user profile API endpoint
response = self.client.get(reverse("user-profile-list"))
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.sample_user_profile.username)
Loading

0 comments on commit 6b7140b

Please sign in to comment.