Skip to content

Commit

Permalink
add andreas's first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
K02D committed Jan 29, 2024
1 parent 1fcaf7a commit 43a55fb
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 0 deletions.
Empty file added friends/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions friends/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions friends/apps.py
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'
8 changes: 8 additions & 0 deletions friends/models.py
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)
37 changes: 37 additions & 0 deletions friends/tests.py
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()))
7 changes: 7 additions & 0 deletions friends/urls.py
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"),
]
33 changes: 33 additions & 0 deletions friends/views.py
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)
24 changes: 24 additions & 0 deletions migrations/0001_initial.py
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 added migrations/__init__.py
Empty file.
1 change: 1 addition & 0 deletions semesterly/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ def get_secret(key):
"student",
"timetable",
"ckeditor",
"friends",
)

REST_FRAMEWORK = {"UNICODE_JSON": False}
Expand Down
1 change: 1 addition & 0 deletions semesterly/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
re_path("", include("analytics.urls")),
re_path("", include("agreement.urls")),
re_path("", include("notifications.urls")),
re_path("", include("friends.urls")),
re_path(r"admin/?", admin.site.urls),
# Automatic deployment endpoint
re_path(r"deploy_staging/?", semesterly.views.deploy_staging),
Expand Down

0 comments on commit 43a55fb

Please sign in to comment.