mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-24 23:39:37 +02:00
feat: Add country name to region page
This commit is contained in:
parent
597e56ea62
commit
bb1f2d92cf
8 changed files with 76 additions and 13 deletions
|
@ -44,8 +44,8 @@ class Adventure(models.Model):
|
|||
raise ValidationError('Adventures must be associated with trips owned by the same user. Trip owner: ' + self.trip.user_id.username + ' Adventure owner: ' + self.user_id.username)
|
||||
if self.type != self.trip.type:
|
||||
raise ValidationError('Adventure type must match trip type. Trip type: ' + self.trip.type + ' Adventure type: ' + self.type)
|
||||
if self.type == 'featured' and not self.is_public:
|
||||
raise ValidationError('Featured adventures must be public. Adventure: ' + self.name)
|
||||
if self.type == 'featured' and not self.is_public:
|
||||
raise ValidationError('Featured adventures must be public. Adventure: ' + self.name)
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
from django.urls import include, path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .views import AdventureViewSet, TripViewSet
|
||||
from .views import AdventureViewSet, TripViewSet, StatsViewSet
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'adventures', AdventureViewSet, basename='adventures')
|
||||
router.register(r'trips', TripViewSet, basename='trips')
|
||||
router.register(r'stats', StatsViewSet, basename='stats')
|
||||
|
||||
urlpatterns = [
|
||||
# Include the router under the 'api/' prefix
|
||||
|
|
|
@ -2,6 +2,7 @@ from rest_framework.decorators import action
|
|||
from rest_framework import viewsets
|
||||
from rest_framework.response import Response
|
||||
from .models import Adventure, Trip
|
||||
from worldtravel.models import VisitedRegion
|
||||
from .serializers import AdventureSerializer, TripSerializer
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.db.models import Q, Prefetch
|
||||
|
@ -72,4 +73,30 @@ class TripViewSet(viewsets.ModelViewSet):
|
|||
def featured(self, request):
|
||||
trips = self.get_queryset().filter(type='featured', is_public=True)
|
||||
serializer = self.get_serializer(trips, many=True)
|
||||
return Response(serializer.data)
|
||||
return Response(serializer.data)
|
||||
|
||||
class StatsViewSet(viewsets.ViewSet):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@action(detail=False, methods=['get'])
|
||||
def counts(self, request):
|
||||
visited_count = Adventure.objects.filter(
|
||||
type='visited', user_id=request.user.id).count()
|
||||
planned_count = Adventure.objects.filter(
|
||||
type='planned', user_id=request.user.id).count()
|
||||
featured_count = Adventure.objects.filter(
|
||||
type='featured', is_public=True).count()
|
||||
trips_count = Trip.objects.filter(
|
||||
user_id=request.user.id).count()
|
||||
region_count = VisitedRegion.objects.filter(
|
||||
user_id=request.user.id).count()
|
||||
country_count = VisitedRegion.objects.filter(
|
||||
user_id=request.user.id).values('region__country').distinct().count()
|
||||
return Response({
|
||||
'visited_count': visited_count,
|
||||
'planned_count': planned_count,
|
||||
'featured_count': featured_count,
|
||||
'trips_count': trips_count,
|
||||
'region_count': region_count,
|
||||
'country_count': country_count,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue