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 search bar feature and updated styling #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions socbackend/accounts/custom_auth.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
from rest_framework_simplejwt.authentication import JWTAuthentication
import logging
logger = logging.getLogger(__name__)

logger = logging.getLogger(__name__)

class CookieJWTAuthentication(JWTAuthentication):
def authenticate(self, request):
# Attempt header authentication first
header = None
try:
header = super().authenticate(request)
except Exception as e:
logger.debug(f"Header login failed: {e}")
logger.debug(f"Header authentication failed: {e}")


# Fallback to cookie-based authentication
if header is None:
# Attempt to get token from the cookie
token = request.COOKIES.get("auth")
token = request.COOKIES.get("auth") # Fetch JWT from cookies
if token:
return self.get_user(self.get_validated_token(token)), None
try:
validated_token = self.get_validated_token(token)
return self.get_user(validated_token), None
except Exception as e:
logger.error(f"Cookie token validation failed: {e}")
return None

return header

return header, None
6 changes: 3 additions & 3 deletions socbackend/projects/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from django.urls import path

from . import views

app_name = "projects"

urlpatterns = [
path("", views.BasicProjectListView.as_view(), name="project_list"),
path("<int:pk>/", views.ProjectDetailView.as_view(), name="project_detail"),
path("wishlist/", views.ProjectWishlist.as_view(), name="wishlist"),
path("preference/", views.ProjectPreference.as_view(), name="prefenrence"),

path("preference/", views.ProjectPreference.as_view(), name="preference"),
path("upload-projects/", views.UploadProjectsView.as_view(), name="upload_projects"), # New endpoint
# path("add/", views.ProjectAddView.as_view(), name="project_add"),
]
205 changes: 111 additions & 94 deletions socbackend/projects/views.py
Original file line number Diff line number Diff line change
@@ -1,126 +1,143 @@
from rest_framework import generics

from .models import Project
from .serializers import ProjectSerializer, BasicProjectSerializer, MenteePreferenceSerializer, MenteePreferenceSaveSerializer

# from projects.models import Season
from accounts.custom_auth import CookieJWTAuthentication
from rest_framework import generics, views
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import Mentee, Project, MenteePreference, MenteeWishlist
from rest_framework.permissions import IsAuthenticated
from rest_framework.permissions import IsAuthenticated, AllowAny
from accounts.custom_auth import CookieJWTAuthentication
from accounts.models import UserProfile
from rest_framework.permissions import AllowAny
import logging
from .models import Mentee, Project, MenteePreference, MenteeWishlist
from .serializers import (
ProjectSerializer,
BasicProjectSerializer,
MenteePreferenceSerializer,
MenteePreferenceSaveSerializer,
)
from projects.management.commands.upload_projects import upload_projects
import logging

logger = logging.getLogger(__name__)
# from .serializers import (
# ProjectAdditionSerializer,
# )

class ProjectDetailView(APIView):
permission_classes = []
queryset = Project.objects.all()
permission_classes = [AllowAny]
serializer_class = ProjectSerializer

def get(self, request, pk):
project = Project.objects.get(pk=pk)
serializer = self.serializer_class(project)
return Response(serializer.data)

try:
project = Project.objects.get(pk=pk)
serializer = self.serializer_class(project)
return Response(serializer.data)
except Project.DoesNotExist:
return Response({"error": "Project not found"}, status=404)


class ProjectWishlist(APIView):
authentication_classes = [CookieJWTAuthentication]
authentication_classes = [CookieJWTAuthentication]
permission_classes = [IsAuthenticated]
permission_classes = [AllowAny] # Allow any user to access the post request

def get(self, request):
user_profile = UserProfile.objects.get(user=request.user)

# logger.error('\n \n Error 1 \n \n ')
mentee = Mentee.objects.get(user=user_profile)
# logger.error('\n \n Error 2 \n \n ')
preferences = MenteeWishlist.objects.filter(mentee=mentee)
# logger.error('\n \n Error 3 \n \n ')
project_objects = [preference.project for preference in preferences]
# logger.error('\n \n Error 4 \n \n ')
serializer = BasicProjectSerializer(project_objects, many=True)
# logger.error('\n \n Error 5 \n \n ')
return Response(serializer.data)

try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
preferences = MenteeWishlist.objects.filter(mentee=mentee)
project_objects = [preference.project for preference in preferences]
serializer = BasicProjectSerializer(project_objects, many=True)
return Response(serializer.data)
except Exception as e:
logger.error(f"Error fetching wishlist: {e}")
return Response({"error": "Failed to fetch wishlist"}, status=500)

def post(self, request):
# logger.error('\n \n Error 6 \n \n ')
# print("HI")
user_profile = UserProfile.objects.get(user=request.user)
# logger.error('\n \n Error 7 \n \n ')
mentee = Mentee.objects.get(user=user_profile)
# logger.error('\n \n Error 8 \n \n ')
project_id = request.data["project_id"]
# logger.error('\n \n Error 9 \n \n ')
project = Project.objects.get(pk=project_id)
# logger.error('\n \n Error 10 \n \n ')
preference = MenteeWishlist(mentee=mentee, project=project)
# logger.error('\n \n Error 11 \n \n ')
preference.save()
# logger.error('\n \n Error 12 \n \n ')
return Response({"message": "Project added to wishlist."})

try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.data["project_id"]
project = Project.objects.get(pk=project_id)
preference = MenteeWishlist(mentee=mentee, project=project)
preference.save()
return Response({"message": "Project added to wishlist."})
except Exception as e:
logger.error(f"Error adding to wishlist: {e}")
return Response({"error": "Failed to add project to wishlist"}, status=500)

def delete(self, request):
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.GET['project_id']
project = Project.objects.get(pk=project_id)
preference = MenteeWishlist.objects.get(mentee=mentee, project=project)
preference.delete()
return Response({"message": "Project removed from wishlist."})

try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.GET.get("project_id")
project = Project.objects.get(pk=project_id)
preference = MenteeWishlist.objects.get(mentee=mentee, project=project)
preference.delete()
return Response({"message": "Project removed from wishlist."})
except Exception as e:
logger.error(f"Error removing from wishlist: {e}")
return Response({"error": "Failed to remove project from wishlist"}, status=500)


class ProjectPreference(APIView):
permission_classes = [IsAuthenticated]

def get(self, request):
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
preferences = MenteePreference.objects.filter(mentee=mentee)
serializer = MenteePreferenceSerializer(preferences, many=True)
return Response(serializer.data)

try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
preferences = MenteePreference.objects.filter(mentee=mentee)
serializer = MenteePreferenceSerializer(preferences, many=True)
return Response(serializer.data)
except Exception as e:
logger.error(f"Error fetching preferences: {e}")
return Response({"error": "Failed to fetch preferences"}, status=500)

def post(self, request):
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.data["project"]
preference = request.data["preference"]
sop = request.data["sop"]
project = Project.objects.get(pk=project_id)
serializer = MenteePreferenceSaveSerializer(data={"mentee": mentee.id, "project": project.id, "preference": preference, "sop": sop})
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)

try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.data["project"]
preference = request.data["preference"]
sop = request.data["sop"]
project = Project.objects.get(pk=project_id)
serializer = MenteePreferenceSaveSerializer(
data={
"mentee": mentee.id,
"project": project.id,
"preference": preference,
"sop": sop,
}
)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=201)
return Response(serializer.errors, status=400)
except Exception as e:
logger.error(f"Error adding preference: {e}")
return Response({"error": "Failed to add preference"}, status=500)

def delete(self, request):
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.data["project_id"]
project = Project.objects.get(pk=project_id)
preference = MenteePreference.objects.get(mentee=mentee, project=project)
preference.delete()
return Response({"message": "Project removed from preferences."})
try:
user_profile = UserProfile.objects.get(user=request.user)
mentee = Mentee.objects.get(user=user_profile)
project_id = request.data["project_id"]
project = Project.objects.get(pk=project_id)
preference = MenteePreference.objects.get(mentee=mentee, project=project)
preference.delete()
return Response({"message": "Project removed from preferences."})
except Exception as e:
logger.error(f"Error removing preference: {e}")
return Response({"error": "Failed to remove preference"}, status=500)


class BasicProjectListView(generics.ListAPIView):
permission_classes = []
if True:
upload_projects("./projects.csv")
permission_classes = [AllowAny]
queryset = Project.objects.all()
serializer_class = BasicProjectSerializer


# class ProjectAddView(generics.CreateAPIView):
# queryset = Project.objects.all()
# serializer_class = ProjectAdditionSerializer
class UploadProjectsView(APIView):
permission_classes = [AllowAny]

# def get_serializer_context(self):
# context = super().get_serializer_context()
# context["request"] = self.request
# return context
def post(self, request):
try:
csv_path = request.data.get("csv_path", "./projects.csv")
upload_projects(csv_path)
return Response({"message": "Projects uploaded successfully."})
except Exception as e:
logger.error(f"Error uploading projects: {e}")
return Response({"error": str(e)}, status=500)
31 changes: 13 additions & 18 deletions socbackend/socbackend/urls.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
"""
URL configuration for backend project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from django.http import HttpResponse
from django.shortcuts import redirect

# Optional: Define a root view with a welcome message
def root_view(request):
return HttpResponse(
"<h1>Welcome to the Backend</h1>"
"<p>Available endpoints:</p>"
"<ul>"
"<li><a href='/api/accounts/'>Accounts API</a></li>"
"<li><a href='/api/projects/'>Projects API</a></li>"
"</ul>"
)

urlpatterns = [
path("admin/", admin.site.urls),
path("api/accounts/", include("accounts.urls")),
path("api/projects/", include("projects.urls")),
path("", root_view, name="root"), # Add this line for the root URL
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Loading