mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 19:56:19 -04:00
Added Register functionality
This commit is contained in:
22
backend/api/migrations/0005_file_created_at.py
Normal file
22
backend/api/migrations/0005_file_created_at.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# Generated by Django 4.2.11 on 2024-03-26 23:14
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.utils.timezone
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("api", "0004_course_number_alter_course_name"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name="file",
|
||||||
|
name="created_at",
|
||||||
|
field=models.DateTimeField(
|
||||||
|
auto_now_add=True, default=django.utils.timezone.now
|
||||||
|
),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -56,6 +56,7 @@ class File(models.Model):
|
|||||||
course = models.ForeignKey(
|
course = models.ForeignKey(
|
||||||
Course, on_delete=models.SET_NULL, null=True, related_name="files"
|
Course, on_delete=models.SET_NULL, null=True, related_name="files"
|
||||||
)
|
)
|
||||||
|
created_at = models.DateTimeField(auto_now_add=True)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.filename
|
return self.filename
|
||||||
|
|||||||
@@ -55,4 +55,5 @@ urlpatterns = [
|
|||||||
),
|
),
|
||||||
path("api/files/", views.FileListCreateAPIView.as_view(), name="file-list"),
|
path("api/files/", views.FileListCreateAPIView.as_view(), name="file-list"),
|
||||||
path("api/files/<int:pk>/", views.FileDetailAPIView.as_view(), name="file-detail"),
|
path("api/files/<int:pk>/", views.FileDetailAPIView.as_view(), name="file-detail"),
|
||||||
|
path("api/register/", views.RegisterFile.as_view(), name="file-register"),
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -6,15 +6,7 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.authentication import TokenAuthentication
|
from rest_framework.authentication import TokenAuthentication
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from api.utils.get_client_ip import get_client_ip
|
||||||
|
|
||||||
def get_client_ip(request):
|
|
||||||
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
|
||||||
if x_forwarded_for:
|
|
||||||
ip = x_forwarded_for.split(",")[0]
|
|
||||||
else:
|
|
||||||
ip = request.META.get("REMOTE_ADDR")
|
|
||||||
return ip
|
|
||||||
|
|
||||||
|
|
||||||
class LoginView(APIView):
|
class LoginView(APIView):
|
||||||
|
|||||||
0
backend/api/utils/__init__.py
Normal file
0
backend/api/utils/__init__.py
Normal file
14
backend/api/utils/get_client_ip.py
Normal file
14
backend/api/utils/get_client_ip.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
def get_client_ip(request):
|
||||||
|
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
|
||||||
|
if x_forwarded_for:
|
||||||
|
ip = x_forwarded_for.split(",")[0]
|
||||||
|
else:
|
||||||
|
ip = request.META.get("REMOTE_ADDR")
|
||||||
|
return ip
|
||||||
|
|
||||||
|
|
||||||
|
def update_user_ip(request):
|
||||||
|
user = request.user
|
||||||
|
ip_address = get_client_ip(request)
|
||||||
|
user.ip_address = ip_address
|
||||||
|
user.save()
|
||||||
@@ -14,6 +14,8 @@ from rest_framework.permissions import IsAuthenticated
|
|||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework import filters
|
from rest_framework import filters
|
||||||
|
from rest_framework.exceptions import ValidationError, NotFound
|
||||||
|
from api.utils.get_client_ip import update_user_ip
|
||||||
|
|
||||||
|
|
||||||
class TopicListCreateAPIView(generics.ListCreateAPIView):
|
class TopicListCreateAPIView(generics.ListCreateAPIView):
|
||||||
@@ -99,3 +101,65 @@ class FileDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
|
|||||||
serializer_class = FileSerializer
|
serializer_class = FileSerializer
|
||||||
authentication_classes = [TokenAuthentication]
|
authentication_classes = [TokenAuthentication]
|
||||||
permission_classes = [IsAuthenticated]
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
|
||||||
|
class RegisterFile(APIView):
|
||||||
|
authentication_classes = [TokenAuthentication]
|
||||||
|
permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
user = request.user
|
||||||
|
update_user_ip(request)
|
||||||
|
data = request.data
|
||||||
|
try:
|
||||||
|
required_fields = set(
|
||||||
|
["filename", "topic", "semester", "professor", "course"]
|
||||||
|
)
|
||||||
|
provided_fields = set(request.data.keys())
|
||||||
|
missing_fields = required_fields - provided_fields
|
||||||
|
|
||||||
|
if any(["filename", "topic"]) in missing_fields:
|
||||||
|
raise ValidationError(
|
||||||
|
"Missing one or more of the required field(s): filename, topic"
|
||||||
|
)
|
||||||
|
|
||||||
|
file = File.objects.create(filename=data["filename"], original_author=user)
|
||||||
|
file.peer_users.add(user)
|
||||||
|
|
||||||
|
file.course = (
|
||||||
|
Course.objects.get(id=data.get("course"))
|
||||||
|
if data.get("course")
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
file.professor = (
|
||||||
|
Professor.objects.get(id=data.get("professor"))
|
||||||
|
if data.get("professor")
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
file.semester = (
|
||||||
|
Semester.objects.get(id=data.get("semester"))
|
||||||
|
if data.get("semester")
|
||||||
|
else None
|
||||||
|
)
|
||||||
|
|
||||||
|
topic_id = data["topic"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
topic = Topic.objects.get(id=topic_id)
|
||||||
|
file.topic = topic
|
||||||
|
except Exception as e:
|
||||||
|
topic_serializer = TopicSerializer(data=topic_id)
|
||||||
|
if topic_serializer.is_valid():
|
||||||
|
topic = topic_serializer.save()
|
||||||
|
file.topic = topic
|
||||||
|
|
||||||
|
file.save()
|
||||||
|
return Response(
|
||||||
|
{"id": file.id, "filename": file.filename, "topic": file.topic.name},
|
||||||
|
status=status.HTTP_201_CREATED,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print("Error", str(e))
|
||||||
|
return Response(
|
||||||
|
{"error(s)": "Something went wrong"}, status=status.HTTP_400_BAD_REQUEST
|
||||||
|
)
|
||||||
|
|||||||
0
backend/api/views/filters.py
Normal file
0
backend/api/views/filters.py
Normal file
Reference in New Issue
Block a user