forked from noahpresler/semesterly
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class FriendsConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'friends' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from django.db import models | ||
from student.models import Student | ||
|
||
# Create your models here. | ||
|
||
class FriendRequest(models.Model): | ||
from_friend = models.ForeignKey(Student, related_name="from_friend", on_delete=models.CASCADE) | ||
to_friend = models.ForeignKey(Student, related_name="to_friend", on_delete=models.CASCADE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.test import TestCase | ||
from django.urls import reverse | ||
from .models import FriendRequest | ||
from .models import Student | ||
from helpers.test.utils import ( | ||
create_user, | ||
create_student, | ||
get_response, | ||
get_auth_response, | ||
) | ||
from rest_framework.test import APITestCase | ||
from friends.views import send_friend_request, accept_friend_request, reject_friend_request | ||
# Create your tests here. | ||
|
||
|
||
class FriendRequestTest(APITestCase): | ||
def setUp(self): | ||
self.user1 = create_user(username="Alice", password="security") | ||
self.student1 = create_student(user=self.user1) | ||
self.user2 = create_user(username="Bob", password="security2") | ||
self.student2 = create_student(user=self.user2) | ||
|
||
def test_send_request(self): | ||
self.client.force_login(self.user1) | ||
response = self.client.post(reverse("send friend request", args=[self.student1.id])) | ||
self.assertEquals(response.status_code, 200) | ||
self.assertEqual(1, len(FriendRequest.objects.all())) | ||
print(response) | ||
|
||
|
||
# def test_accept_request(self): | ||
# send = self.client.post(f"accept_friend_request/{self.student1.id}/", send_friend_request(request=self.student1, UserId=self.student2.id), name="send friend request") | ||
# response = self.client.get(f"accept_friend_request/{self.student1.id}/", accept_friend_request(request=self.student2, requestId=self.student1.id), name="accept friend request") | ||
# self.assertEquals(response.status_code, 200) | ||
|
||
# # self.assertEquals(response, self.student1.id) | ||
# self.assertEqual(0, len(FriendRequest.objects.all())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.urls import path | ||
from friends.views import send_friend_request, accept_friend_request, reject_friend_request | ||
urlpatterns = [ | ||
path('send_friend_request/<int:userId>/', send_friend_request, name="send friend request"), | ||
path('accept_friend_request/<int:userId>/', accept_friend_request, name="accept friend request"), | ||
path('reject_friend_request/<int:userId>/', reject_friend_request, name="reject friend request"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.shortcuts import render | ||
from student.models import Student | ||
from friends.models import FriendRequest | ||
from django.http import HttpResponse | ||
# Create your views here. | ||
def send_friend_request(request, userId): | ||
from_user = request.user | ||
from_student = Student.objects.get_or_create(user=from_user) | ||
to_student = Student.objects.get(id=userId) | ||
friend_request , created = FriendRequest.objects.get_or_create(from_friend=from_student, to_friend=to_student) | ||
|
||
if created: | ||
return HttpResponse('Friend request created', 200) | ||
else: | ||
return HttpResponse('Friend request failed', 400) | ||
|
||
def accept_friend_request(request, requestId): | ||
friend_request = FriendRequest.objects.get(id=requestId) | ||
if friend_request == request: | ||
friend_request.to_user.friends.add(friend_request.from_user) | ||
friend_request.from_user.friends.add(friend_request.to_user) | ||
friend_request.delete() | ||
return HttpResponse('Friend request accepted', 200) | ||
else: | ||
return HttpResponse('Friend request rejected', 400) | ||
|
||
def reject_friend_request(request, requestId): | ||
friend_request = FriendRequest.objects.get(id=requestId) | ||
if friend_request.user == request.user: | ||
friend_request.delete() | ||
return HttpResponse('Friend request rejected', 200) | ||
else: | ||
return HttpResponse('Friend request failed to reject', 400) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Generated by Django 3.2.18 on 2023-10-02 00:50 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('student', '0046_delete_registrationtoken'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='FriendRequest', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('from_friend', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='from_friend', to='student.student')), | ||
('to_friend', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='to_friend', to='student.student')), | ||
], | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters