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

added user profile #85

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Generated by Django 5.0.4 on 2024-05-28 23:21

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


class Migration(migrations.Migration):
dependencies = [
("core", "0001_initial"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.AlterField(
model_name="posts",
name="answerCount",
field=models.IntegerField(default=0, null=True),
),
migrations.AlterField(
model_name="posts",
name="commentCount",
field=models.IntegerField(default=0, null=True),
),
migrations.AlterField(
model_name="posts",
name="favoriteCount",
field=models.IntegerField(default=0, null=True),
),
migrations.AlterField(
model_name="posts",
name="score",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="posts",
name="tags",
field=models.ManyToManyField(related_name="tags", to="core.tags"),
),
migrations.AlterField(
model_name="posts",
name="viewcount",
field=models.IntegerField(default=0, null=True),
),
migrations.AlterField(
model_name="tags",
name="count",
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name="tags",
name="isModeratorOnly",
field=models.BooleanField(default=False),
),
migrations.AlterField(
model_name="tags",
name="isRequired",
field=models.BooleanField(default=False),
),
migrations.CreateModel(
name="UserProfile",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("username", models.CharField(max_length=255, null=True)),
("avatar", models.CharField(max_length=255, null=True)),
("bio", models.TextField(max_length=255, null=True)),
("reputation", models.IntegerField(default=0, null=True)),
("joinDate", models.DateField()),
("upVotes", models.IntegerField(default=0, null=True)),
("downVotes", models.IntegerField(default=0, null=True)),
(
"edits",
models.ManyToManyField(related_name="edits", to="core.posts"),
),
(
"posts",
models.ManyToManyField(related_name="posts", to="core.posts"),
),
(
"privateRebuttals",
models.ManyToManyField(
related_name="private_rebuttals", to="core.posts"
),
),
(
"savedPosts",
models.ManyToManyField(related_name="saved_posts", to="core.posts"),
),
(
"user",
models.OneToOneField(
null=True,
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
]
30 changes: 29 additions & 1 deletion backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
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):
Expand Down Expand Up @@ -36,9 +39,34 @@ class Posts(models.Model):
lastEditDate: models.DateField = models.DateField(null=True)
lastActivityDate: models.DateField = models.DateField()
title: models.CharField = models.CharField(max_length=TITLE_MAX_LEN)
tags: models.ManyToManyField = models.ManyToManyField(Tags, related_name="posts")
tags: models.ManyToManyField = models.ManyToManyField(Tags, related_name="tags")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good catch

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)


class UserProfile(models.Model):
# Public
user = None # type: ignore
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
username: models.CharField = models.CharField(
max_length=USERNAME_MAX_LEN, null=True
)
avatar: models.CharField = models.CharField(max_length=AVATAR_MAX_LEN, null=True)
bio: models.TextField = models.TextField(max_length=BIO_MAX_LEN, null=True)
reputation: models.IntegerField = models.IntegerField(default=0, null=True)
joinDate: models.DateField = models.DateField()
posts: models.ManyToManyField = models.ManyToManyField(Posts, related_name="posts")
edits: models.ManyToManyField = models.ManyToManyField(Posts, related_name="edits")
upVotes: models.IntegerField = models.IntegerField(default=0, null=True)
downVotes: models.IntegerField = models.IntegerField(default=0, null=True)

# Private
savedPosts: models.ManyToManyField = models.ManyToManyField(
Posts, related_name="saved_posts"
)
privateRebuttals: models.ManyToManyField = models.ManyToManyField(
Posts, related_name="private_rebuttals"
seporterfield marked this conversation as resolved.
Show resolved Hide resolved
)
12 changes: 9 additions & 3 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from rest_framework import serializers

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


class TagSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = "__all__"


class PostSerializer(serializers.ModelSerializer):
Expand All @@ -9,7 +15,7 @@ class Meta:
fields = "__all__"


class TagSerializer(serializers.ModelSerializer):
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
model = UserProfile
fields = "__all__"
56 changes: 41 additions & 15 deletions backend/core/tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
# 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
from .models import Posts, Tags, UserProfile


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

# Create sample posts and tags
# 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,
Expand All @@ -33,30 +44,45 @@ def setUp(self):
closedDate=None,
communityOwnedDate=None,
)
# Correctly assign the tag to the post
self.sample_post.tags.set([self.sample_tag])

self.sample_tag = Tags.objects.create(
tagName="tag1",
count=1,
excerptPostId=1,
wikiPostId=1,
isModeratorOnly=False,
isRequired=True,
# 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.privateRebuttals.set([self.sample_post])

# Correctly assign the tag to the post
self.sample_post.tags.set([self.sample_tag])
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_tags_api(self):
# Test the tags API endpoint
response = self.client.get(reverse("tags-list"))
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_tag.tagName)
self.assertContains(response, self.sample_user_profile.username)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing 👍👍👍👍👍👍
Everything runs for me

def test_alivecheck_smoketest(self):
response = self.client.get(reverse("alive-list"))
Expand Down
16 changes: 10 additions & 6 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.template.loader import render_to_string
from django.utils.decorators import method_decorator
from django.utils.encoding import force_bytes, force_str
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
from rest_framework import viewsets
from rest_framework.response import Response

from .forms import UserRegisterForm
from .models import Posts, Tags
from .serializers import PostSerializer, TagSerializer
from .models import Posts, Tags, UserProfile
from .serializers import PostSerializer, TagSerializer, UserProfileSerializer
from .token import account_activation_token


class TagViewSet(viewsets.ModelViewSet):
queryset = Tags.objects.all()
serializer_class = TagSerializer


class PostViewSet(viewsets.ModelViewSet):
queryset = Posts.objects.all()
serializer_class = PostSerializer


class TagViewSet(viewsets.ModelViewSet):
queryset = Tags.objects.all()
serializer_class = TagSerializer
class UserProfileViewSet(viewsets.ModelViewSet):
queryset = UserProfile.objects.all()
serializer_class = UserProfileSerializer


class StatusViewSet(viewsets.ViewSet):
Expand Down
14 changes: 13 additions & 1 deletion backend/rebutify/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"corsheaders",
"core",
"rest_framework",
"rebutify",
Expand All @@ -56,6 +57,8 @@
]

MIDDLEWARE = [
# CorsMiddleware should be placed as high as possible
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand All @@ -65,6 +68,16 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

# Domains that can access the API
CORS_ALLOWED_ORIGINS = [
"https://rebutify.org",
"http://localhost:3000",
"http://0.0.0.0:3000",
]

# Normalizing URLs for front-end (https://docs.djangoproject.com/en/4.0/ref/middleware/#django.middleware.common.CommonMiddleware)
APPEND_SLASH = False
seporterfield marked this conversation as resolved.
Show resolved Hide resolved

REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.BasicAuthentication",
Expand Down Expand Up @@ -92,7 +105,6 @@
},
]

CRISPY_TEMPLATE_PACK = "bootstrap4"

WSGI_APPLICATION = "rebutify.wsgi.application"

Expand Down
3 changes: 2 additions & 1 deletion backend/rebutify/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

router = routers.DefaultRouter()
router.register(r"status/alive", views.StatusViewSet, basename="alive")
router.register(r"posts", views.PostViewSet, basename="posts")
router.register(r"tags", views.TagViewSet, basename="tags")
router.register(r"posts", views.PostViewSet, basename="posts")
router.register(r"user-profile", views.UserProfileViewSet, basename="user-profile")


urlpatterns = [
Expand Down