-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement creation of subrabbit feature on backend and frontend
- Loading branch information
1 parent
d621a9b
commit 2f07cbc
Showing
27 changed files
with
572 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ db.sqlite3 | |
.idea/ | ||
|
||
# Dependency directories | ||
lib/ | ||
# lib/ | ||
lib64/ | ||
bin/ | ||
include/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 5.0.2 on 2024-03-12 12:24 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='subrabbit', | ||
name='name', | ||
field=models.CharField(max_length=255, unique=True, validators=[django.core.validators.MinLengthValidator(3)]), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 5.0.2 on 2024-03-12 15:41 | ||
|
||
import django.core.validators | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('core', '0002_alter_subrabbit_name'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='subrabbit', | ||
name='name', | ||
field=models.CharField(max_length=21, unique=True, validators=[django.core.validators.MinLengthValidator(3), django.core.validators.MaxLengthValidator(21)]), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from rest_framework import permissions | ||
|
||
class IsAuthenticatedOrReadOnly(permissions.BasePermission): | ||
def has_permission(self, request, view): | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
else: | ||
return request.user and request.user.is_authenticated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,69 @@ | ||
from rest_framework import generics | ||
from accounts.authenticate import CustomAuthentication | ||
from rest_framework import generics, status | ||
from rest_framework.permissions import SAFE_METHODS | ||
from rest_framework.response import Response | ||
from rest_framework.serializers import ValidationError | ||
|
||
from .models import Subrabbit | ||
from .serializers import SubrabbitSerializer | ||
from .permissions import IsAuthenticatedOrReadOnly | ||
from .serializers import SubrabbitSerializer, SubrabbitSerializer_detailed | ||
|
||
# from rest_framework.response import Response | ||
# from rest_framework import serializers | ||
|
||
|
||
class SubrabbitListCreateView(generics.ListCreateAPIView): | ||
serializer_class = SubrabbitSerializer | ||
permission_classes = [IsAuthenticatedOrReadOnly] | ||
authentication_classes = [CustomAuthentication] | ||
queryset = Subrabbit.objects.all().order_by('-created_at') | ||
# permission_classes = [IsAuthenticatedOrReadOnly] | ||
|
||
# Apply authentication to unsafe HTTP methods | ||
def get_authenticators(self): | ||
if self.request.method in SAFE_METHODS: | ||
return [] | ||
return super().get_authenticators() | ||
|
||
# Dynamically select serializer based on HTTP method | ||
def get_serializer_class(self): | ||
if self.request.method == 'GET': | ||
return SubrabbitSerializer_detailed | ||
elif self.request.method == 'POST': | ||
return SubrabbitSerializer | ||
|
||
# Override perform_create to add additional fields | ||
def perform_create(self, serializer): | ||
serializer.save( | ||
creator=self.request.user, | ||
subscribers=[self.request.user], | ||
moderators=[self.request.user] | ||
) | ||
|
||
# Override create to handle validation errors | ||
def create(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(data=request.data) | ||
|
||
try: | ||
serializer.is_valid(raise_exception=True) | ||
self.perform_create(serializer) | ||
|
||
except ValidationError as e: | ||
return self.handle_validation_error(e) | ||
|
||
except Exception as e: | ||
return Response({"detail": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
||
headers = self.get_success_headers(serializer.data) | ||
return Response(serializer.data['name'], status=status.HTTP_201_CREATED, headers=headers) | ||
|
||
# Method to handle validation errors | ||
def handle_validation_error(self, e): | ||
if 'unique' in e.detail['name'][0].code: | ||
error_message = { | ||
"title": "A subrabbit with this name already exists.", | ||
"detail": "Please choose a different subrabbit name" | ||
} | ||
return Response({"message": [error_message]}, status=status.HTTP_409_CONFLICT) | ||
elif 'min_length' in e.detail['name'][0].code: | ||
return Response(e.detail, status=status.HTTP_422_UNPROCESSABLE_ENTITY) | ||
else: | ||
return Response(e.detail, status=status.HTTP_400_BAD_REQUEST) |
Binary file not shown.
Oops, something went wrong.