Commit d6a47991 authored by Erlend Ydse's avatar Erlend Ydse
Browse files

Delete unused friends app

parent 12b2055a
from django.contrib import admin
from friends.models import FriendList, FriendRequest
class FriendListAdmin(admin.ModelAdmin):
list_filter = ['user']
list_display = ['user']
search_fields = ['user']
readonly_fields = ['user']
class Meta:
model = FriendList
admin.site.register(FriendList, FriendListAdmin)
class FriendRequestAdmin(admin.ModelAdmin):
list_filter = ['sender', 'receiver']
list_display = ['sender', 'receiver']
search_fields = ['sender__username', 'receiver__username']
class Meta:
model = FriendRequest
admin.site.register(FriendRequest, FriendRequestAdmin)
from django.apps import AppConfig
class FriendsConfig(AppConfig):
name = 'friends'
# Generated by Django 3.1 on 2021-03-05 17:40
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='FriendRequest',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('is_active', models.BooleanField(blank=True, default=True)),
('timestamp', models.DateTimeField(auto_now_add=True)),
('receiver', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='receiver', to=settings.AUTH_USER_MODEL)),
('sender', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sender', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='FriendList',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('friends', models.ManyToManyField(blank=True, related_name='friends', to=settings.AUTH_USER_MODEL)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='user', to=settings.AUTH_USER_MODEL)),
],
),
]
# Generated by Django 3.1 on 2021-03-12 10:35
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('friends', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='friendrequest',
options={'ordering': ['-timestamp']},
),
]
from django.db import models
from django.contrib.auth import get_user_model
class FriendList(models.Model):
user = models.OneToOneField(get_user_model(), on_delete=models.CASCADE, related_name="user")
friends = models.ManyToManyField(get_user_model(), blank=True, related_name="friends")
def __str__(self):
return self.user.username
def add(self, other_user):
"""
Add a new friend to the list of user's friends
:param other_user: The user that should be added as a friend
"""
if other_user not in self.friends.all():
self.friends.add(other_user)
self.save()
def remove(self, other_user):
"""
Remove a friend from the list of user's friends
:param other_user: The user which should be removed as a friend
"""
if other_user in self.friends.all():
self.friends.remove(other_user)
self.save()
def is_friend(self, other_user):
"""
Check if user is a mutual friend of self
:param other_user: User to check friendship with
:return: True if friends, False if not friends
"""
return other_user in self.friends.all()
def unfriend(self, other_user):
"""
Unfriend a friend of the user
:param self: The user terminating the friendship
:param other_user: The friend who is being removed
:return:
"""
self.remove(other_user)
removee_friend_list = FriendList.objects.get(user=other_user)
removee_friend_list.remove(self.user)
class FriendRequest(models.Model):
sender = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name="sender")
receiver = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, related_name="receiver")
is_active = models.BooleanField(blank=True, null=False, default=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ["-timestamp"]
def __str__(self):
return self.sender.username + " " + self.sender.username
def accept(self):
"""
The request is accepted. Updates both the friend lists of both the sender and receiver fields.
"""
receiver_friend_list = FriendList.objects.get(user=self.receiver)
sender_friend_list = FriendList.objects.get(user=self.sender)
try:
try:
sender_friend_list.add(self.receiver)
except Exception:
raise Exception("Could not add receiver to sender's friend list")
try:
receiver_friend_list.add(self.sender)
except Exception:
raise Exception("Could not add sender to receiver's friend list")
finally:
self.is_active = False
self.save()
def decline(self):
"""
The request is declined and the 'is_active' field is set to false.
"""
self.is_active = False
self.save()
def cancel(self):
"""
The request is cancelled on behalf of the sender.
"""
self.is_active = False
self.save()
from rest_framework import serializers
from .models import FriendList, FriendRequest
class FriendListSerializer(serializers.HyperlinkedModelSerializer):
user = serializers.ReadOnlyField(source="user")
class Meta:
model = FriendList
fields = [
"url",
"id",
"user",
"friends",
]
class FriendRequestSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name="friends:friendrequest-detail")
class Meta:
model = FriendRequest
fields = [
"url",
"id",
"sender",
"receiver",
"is_active",
"timestamp",
]
from django.test import TestCase
# Create your tests here.
from django.urls import path
from rest_framework import routers
from .views import friend_list, friend_detail, FriendRequestViewSet
app_name = "friends"
router = routers.SimpleRouter()
router.register(r'friend-requests', FriendRequestViewSet)
urlpatterns = [
path('friend_list/', friend_list, name='friend-list'),
path('friend_detail/<int:pk>/', friend_detail, name='friend-detail'),
]
urlpatterns += router.urls
from django.db.models.query_utils import Q
from rest_framework import viewsets
from rest_framework.decorators import api_view, action
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.response import Response
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_401_UNAUTHORIZED, HTTP_201_CREATED
from users.models import User
from friends.models import FriendRequest, FriendList
from friends.serializers import FriendListSerializer, FriendRequestSerializer
from users.serializers import UserSerializer
@api_view(['GET'])
def friend_list(request):
friends = FriendList.objects.all()
serializer = FriendListSerializer(friends, many=True)
return Response(serializer.data)
@api_view(['GET'])
def friend_detail(request, pk):
friends = FriendList.objects.get(id=pk)
serializer = FriendListSerializer(friends, many=False)
return Response(serializer.data)
"""
@api_view(['GET'])
def is_friend(request, pk):
"""
class FriendRequestViewSet(viewsets.ModelViewSet):
queryset = FriendRequest.objects.all()
serializer_class = FriendRequestSerializer
lookup_field_options = ["sender", "receiver"]
def create(self, request, *args, **kwargs):
data = {"receiver": request.data["receiver"]}
serialized_sender = UserSerializer(request.user, context={'request': request})
data['sender'] = serialized_sender.data['url']
serializer = self.serializer_class(data=data, context={"request": request})
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=HTTP_201_CREATED)
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
def get_object(self):
req_receiver = None
req_sender = None
if "receiver" in self.request.data:
req_receiver = self.request.data["receiver"].split("/")[-2]
if "sender" in self.request.data:
req_sender = self.request.data["sender"].split("/")[-2]
qs = FriendRequest.objects.filter(
(Q(sender=self.request.user) & Q(receiver=req_receiver))
| (Q(receiver=self.request.user) & Q(sender=req_sender))
)
return qs.first()
@action(detail=True, methods=['patch'])
def accept_request(self, request):
friend_request = self.get_object()
data = {"sender": request.data["sender"]}
serialized_receiver = UserSerializer(request.user, context={'request': request})
data['receiver'] = serialized_receiver.data['url']
serializer = self.serializer_class(data=data, context={"request": request})
if serializer.is_valid(raise_exception=True):
if request.user.is_authenticated and request.user == friend_request.receiver:
friend_request.accept()
friend_request.save()
return Response({'status': 'Friend request was accepted.'})
else:
Response(AuthenticationFailed.default_detail, status=HTTP_401_UNAUTHORIZED)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
@action(detail=True, methods=['patch'])
def decline_request(self, request, pk=None):
friend_request = self.get_object()
serializer = FriendListSerializer(data=request.data)
if serializer.is_valid():
if request.user.is_authenticated and request.user == friend_request.receiver:
friend_request.decline()
friend_request.save()
return Response({'status': 'Friend request was declined.'})
else:
Response(AuthenticationFailed.default_detail, status=HTTP_401_UNAUTHORIZED)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
@action(detail=True, methods=['delete'])
def cancel_request(self, request, pk=None):
friend_request = self.get_object()
serializer = FriendListSerializer(data=request.data)
if serializer.is_valid():
if request.user.is_authenticated and request.user == friend_request.sender:
friend_request.cancel()
friend_request.save()
return Response({'status': 'Friend request was cancelled.'})
else:
Response(AuthenticationFailed.default_detail, status=HTTP_401_UNAUTHORIZED)
else:
return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)
......@@ -58,7 +58,6 @@ INSTALLED_APPS = [
"django.contrib.staticfiles",
"rest_framework",
"comments.apps.CommentsConfig",
"friends.apps.FriendsConfig",
"users.apps.UsersConfig",
"workouts.apps.WorkoutsConfig",
"corsheaders",
......
......@@ -20,8 +20,7 @@ from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("workouts.urls")),
path("friends/", include("friends.urls", namespace="friends"))
path("", include("workouts.urls"))
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
......
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save
from django.dispatch import receiver
def profile_image_directory_path(instance, filename):
......@@ -33,17 +31,6 @@ class User(AbstractUser):
symmetric_friends = models.ManyToManyField("self", blank=True)
# Needs to be imported after UserModel to avoid circular dependency
from friends.models import FriendList
# Instantiate User instance with a FriendList
@receiver(post_save, sender=get_user_model())
def create_user_friend_list(sender, instance, created, **kwargs):
if created:
FriendList.objects.create(user=instance)
def athlete_directory_path(instance, filename):
"""
Return the path for an athlete's file
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment