-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from seanmorley15/development
Development
- Loading branch information
Showing
9 changed files
with
171 additions
and
18 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 |
---|---|---|
|
@@ -115,5 +115,4 @@ demo/react-spa/yarn.lock | |
|
||
*/media/* | ||
|
||
/static/* | ||
/staticfiles/* | ||
*/staticfiles/* |
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
37 changes: 37 additions & 0 deletions
37
backend/server/adventures/migrations/0005_remove_adventure_trip_id_trip_adventure_trip.py
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,37 @@ | ||
# Generated by Django 5.0.6 on 2024-07-09 16:49 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('adventures', '0004_adventure_latitude_adventure_longitude'), | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='adventure', | ||
name='trip_id', | ||
), | ||
migrations.CreateModel( | ||
name='Trip', | ||
fields=[ | ||
('id', models.AutoField(primary_key=True, serialize=False)), | ||
('name', models.CharField(max_length=200)), | ||
('type', models.CharField(max_length=100)), | ||
('location', models.CharField(blank=True, max_length=200, null=True)), | ||
('date', models.DateField(blank=True, null=True)), | ||
('is_public', models.BooleanField(default=False)), | ||
('user_id', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='adventure', | ||
name='trip', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='adventures.trip'), | ||
), | ||
] |
23 changes: 23 additions & 0 deletions
23
backend/server/adventures/migrations/0006_alter_adventure_type_alter_trip_type.py
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,23 @@ | ||
# Generated by Django 5.0.6 on 2024-07-09 16:58 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('adventures', '0005_remove_adventure_trip_id_trip_adventure_trip'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='adventure', | ||
name='type', | ||
field=models.CharField(choices=[('visited', 'Visited'), ('planned', 'Planned'), ('featured', 'Featured')], max_length=100), | ||
), | ||
migrations.AlterField( | ||
model_name='trip', | ||
name='type', | ||
field=models.CharField(choices=[('visited', 'Visited'), ('planned', 'Planned'), ('featured', 'Featured')], max_length=100), | ||
), | ||
] |
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,15 @@ | ||
from rest_framework import permissions | ||
|
||
class IsOwnerOrReadOnly(permissions.BasePermission): | ||
""" | ||
Custom permission to only allow owners of an object to edit it. | ||
""" | ||
|
||
def has_object_permission(self, request, view, obj): | ||
# Read permissions are allowed to any request, | ||
# so we'll always allow GET, HEAD or OPTIONS requests. | ||
if request.method in permissions.SAFE_METHODS: | ||
return True | ||
|
||
# Write permissions are only allowed to the owner of the object. | ||
return obj.user_id == request.user |
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
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,44 +1,74 @@ | ||
from rest_framework.decorators import action | ||
from rest_framework import viewsets | ||
from rest_framework.response import Response | ||
from .models import Adventure | ||
from .serializers import AdventureSerializer | ||
from .models import Adventure, Trip | ||
from .serializers import AdventureSerializer, TripSerializer | ||
from rest_framework.permissions import IsAuthenticated | ||
from django.db.models import Q | ||
|
||
# Create your views here. | ||
from .permissions import IsOwnerOrReadOnly | ||
|
||
class AdventureViewSet(viewsets.ModelViewSet): | ||
serializer_class = AdventureSerializer | ||
permission_classes = [IsAuthenticated] | ||
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly] | ||
|
||
def get_queryset(self): | ||
# Allow any user to see public adventures or their own adventures | ||
return Adventure.objects.filter( | ||
Q(is_public=True) | Q(user_id=self.request.user.id) | ||
) | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(user_id=self.request.user) | ||
|
||
# Custom actions to return visited and planned adventures | ||
@action(detail=False, methods=['get']) | ||
def visited(self, request): | ||
visited_adventures = Adventure.objects.filter( | ||
type='visited', user_id=request.user.id, trip_id=None) | ||
type='visited', user_id=request.user.id, trip=None) | ||
serializer = self.get_serializer(visited_adventures, many=True) | ||
return Response(serializer.data) | ||
|
||
@action(detail=False, methods=['get']) | ||
def planned(self, request): | ||
planned_adventures = Adventure.objects.filter( | ||
type='planned', user_id=request.user.id, trip_id=None) | ||
type='planned', user_id=request.user.id, trip=None) | ||
serializer = self.get_serializer(planned_adventures, many=True) | ||
return Response(serializer.data) | ||
|
||
@action(detail=False, methods=['get']) | ||
def featured(self, request): | ||
featured_adventures = Adventure.objects.filter( | ||
type='featured', is_public=True, trip_id=None) | ||
type='featured', is_public=True, trip=None) | ||
serializer = self.get_serializer(featured_adventures, many=True) | ||
return Response(serializer.data) | ||
|
||
class TripViewSet(viewsets.ModelViewSet): | ||
serializer_class = TripSerializer | ||
permission_classes = [IsAuthenticated, IsOwnerOrReadOnly] | ||
|
||
def get_queryset(self): | ||
return Trip.objects.filter( | ||
Q(is_public=True) | Q(user_id=self.request.user.id) | ||
) | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(user_id=self.request.user) | ||
|
||
@action(detail=False, methods=['get']) | ||
def visited(self, request): | ||
trips = Trip.objects.filter( | ||
type='visited', user_id=request.user.id) | ||
serializer = self.get_serializer(trips, many=True) | ||
return Response(serializer.data) | ||
|
||
@action(detail=False, methods=['get']) | ||
def planned(self, request): | ||
trips = Trip.objects.filter( | ||
type='planned', user_id=request.user.id) | ||
serializer = self.get_serializer(trips, many=True) | ||
return Response(serializer.data) | ||
|
||
@action(detail=False, methods=['get']) | ||
def featured(self, request): | ||
trips = Trip.objects.filter( | ||
type='featured', is_public=True) | ||
serializer = self.get_serializer(trips, many=True) | ||
return Response(serializer.data) |