Skip to content

Commit

Permalink
Merge pull request #464 from bounswe/BACKEND-440
Browse files Browse the repository at this point in the history
follow-unfollow functinonalities and admin panel to check them
  • Loading branch information
oktayozel authored Nov 3, 2024
2 parents 5ddf257 + fea0d10 commit 73676f6
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 2 deletions.
15 changes: 14 additions & 1 deletion backend/app/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
from django.contrib import admin
from .models import Profile

# Register your models here.
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ('user', 'get_following', 'get_followers')

# Custom method to display users this profile is following
def get_following(self, obj):
return ", ".join([str(profile.user.username) for profile in obj.following.all()])
get_following.short_description = 'Following'

# Custom method to display users following this profile
def get_followers(self, obj):
return ", ".join([str(profile.user.username) for profile in obj.followers.all()])
get_followers.short_description = 'Followers'
1 change: 1 addition & 0 deletions backend/app/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppConfig(AppConfig):

def ready(self):
post_migrate.connect(create_mockdata, sender=self)
import app.signals # Import the signals to ensure they are registered



Expand Down
18 changes: 18 additions & 0 deletions backend/app/migrations/0005_profile_following.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-11-03 14:21

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('app', '0004_post'),
]

operations = [
migrations.AddField(
model_name='profile',
name='following',
field=models.ManyToManyField(blank=True, related_name='followers', to='app.profile'),
),
]
2 changes: 2 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class Profile(models.Model):
bio = models.TextField(blank=True, null=True)
location = models.CharField(max_length=100, blank=True, null=True)
birth_date = models.DateField(null=True, blank=True)
following = models.ManyToManyField('self', symmetrical=False, related_name='followers', blank=True)

def __str__(self):
return self.user.username


class Tags(models.Model):
name = models.CharField(max_length=30, unique=True)

Expand Down
4 changes: 4 additions & 0 deletions backend/app/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.urls import path
from app.views import *
from app.views_directory.profileviews import view_profile, update_profile
from app.views_directory.follow_unfollow import follow_user, unfollow_user


urlpatterns = [
path('', index , name='index_page'),
Expand All @@ -14,4 +16,6 @@
path('signup/', RegisterView.as_view(), name='auth_register'),
path('login/', LoginView.as_view(), name='auth_login'),
path('logout/', LogoutView.as_view(), name='auth_logout'),
path('profile/follow/', follow_user, name='follow_user'),
path('profile/unfollow/', unfollow_user, name='unfollow_user'),
]
29 changes: 29 additions & 0 deletions backend/app/views_directory/follow_unfollow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.shortcuts import get_object_or_404
from django.http import JsonResponse
from app.models import Profile
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def follow_user(request):
current_user_profile = request.user.profile
user_id = request.data.get("user_id") # Get the user_id from the request body
user_to_follow = get_object_or_404(Profile, user__id=user_id)

if user_to_follow != current_user_profile:
current_user_profile.following.add(user_to_follow)
return JsonResponse({"message": "Followed successfully"}, status=200)
return JsonResponse({"error": "Cannot follow yourself"}, status=400)

@api_view(['POST'])
@permission_classes([IsAuthenticated])
def unfollow_user(request):
current_user_profile = request.user.profile
user_id = request.data.get("user_id") # Get the user_id from the request body
user_to_unfollow = get_object_or_404(Profile, user__id=user_id)

if user_to_unfollow != current_user_profile:
current_user_profile.following.remove(user_to_unfollow)
return JsonResponse({"message": "Unfollowed successfully"}, status=200)
return JsonResponse({"error": "Cannot unfollow yourself"}, status=400)
14 changes: 13 additions & 1 deletion backend/backend_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
Expand Down Expand Up @@ -159,3 +159,15 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[email protected]' # Replace with your Gmail address
EMAIL_HOST_PASSWORD = 'Oktay2001!' # Replace with your Gmail password
DEFAULT_FROM_EMAIL = EMAIL_HOST_USER


LOGIN_URL = '/login/'

0 comments on commit 73676f6

Please sign in to comment.