mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-22 22:39:36 +02:00
Add public profile to user model
This commit is contained in:
parent
c83ef1e904
commit
711b974add
4 changed files with 69 additions and 3 deletions
|
@ -46,8 +46,9 @@ from users.models import CustomUser
|
||||||
class CustomUserAdmin(UserAdmin):
|
class CustomUserAdmin(UserAdmin):
|
||||||
model = CustomUser
|
model = CustomUser
|
||||||
list_display = ['username', 'email', 'is_staff', 'is_active', 'image_display']
|
list_display = ['username', 'email', 'is_staff', 'is_active', 'image_display']
|
||||||
|
readonly_fields = ('uuid',)
|
||||||
fieldsets = UserAdmin.fieldsets + (
|
fieldsets = UserAdmin.fieldsets + (
|
||||||
(None, {'fields': ('profile_pic',)}),
|
(None, {'fields': ('profile_pic', 'uuid', 'public_profile')}),
|
||||||
)
|
)
|
||||||
def image_display(self, obj):
|
def image_display(self, obj):
|
||||||
if obj.profile_pic:
|
if obj.profile_pic:
|
||||||
|
|
|
@ -10,11 +10,14 @@ from django.core.exceptions import PermissionDenied
|
||||||
from worldtravel.models import VisitedRegion, Region, Country
|
from worldtravel.models import VisitedRegion, Region, Country
|
||||||
from .serializers import AdventureImageSerializer, AdventureSerializer, CollectionSerializer, NoteSerializer, TransportationSerializer, ChecklistSerializer
|
from .serializers import AdventureImageSerializer, AdventureSerializer, CollectionSerializer, NoteSerializer, TransportationSerializer, ChecklistSerializer
|
||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from django.db.models import Q, Prefetch
|
from django.db.models import Q
|
||||||
from .permissions import CollectionShared, IsOwnerOrReadOnly, IsOwnerOrSharedWithFullAccess, IsPublicOrOwnerOrSharedWithFullAccess, IsPublicReadOnly
|
from .permissions import CollectionShared, IsOwnerOrSharedWithFullAccess, IsPublicOrOwnerOrSharedWithFullAccess
|
||||||
from rest_framework.pagination import PageNumberPagination
|
from rest_framework.pagination import PageNumberPagination
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
class StandardResultsSetPagination(PageNumberPagination):
|
class StandardResultsSetPagination(PageNumberPagination):
|
||||||
page_size = 25
|
page_size = 25
|
||||||
|
@ -398,6 +401,49 @@ class CollectionViewSet(viewsets.ModelViewSet):
|
||||||
instance._prefetched_objects_cache = {}
|
instance._prefetched_objects_cache = {}
|
||||||
|
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
|
# Adds a new user to the shared_with field of an adventure
|
||||||
|
@action(detail=True, methods=['post'], url_path='share/(?P<uuid>[^/.]+)')
|
||||||
|
def share(self, request, pk=None, uuid=None):
|
||||||
|
collection = self.get_object()
|
||||||
|
if not uuid:
|
||||||
|
return Response({"error": "User UUID is required"}, status=400)
|
||||||
|
try:
|
||||||
|
user = User.objects.get(uuid=uuid, public_profile=True)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
return Response({"error": "User not found"}, status=404)
|
||||||
|
|
||||||
|
if user == request.user:
|
||||||
|
return Response({"error": "Cannot share with yourself"}, status=400)
|
||||||
|
|
||||||
|
if collection.shared_with.filter(id=user.id).exists():
|
||||||
|
return Response({"error": "Adventure is already shared with this user"}, status=400)
|
||||||
|
|
||||||
|
collection.shared_with.add(user)
|
||||||
|
collection.save()
|
||||||
|
return Response({"success": f"Shared with {user.username}"})
|
||||||
|
|
||||||
|
@action(detail=True, methods=['post'], url_path='unshare/(?P<uuid>[^/.]+)')
|
||||||
|
def unshare(self, request, pk=None, uuid=None):
|
||||||
|
if not request.user.is_authenticated:
|
||||||
|
return Response({"error": "User is not authenticated"}, status=400)
|
||||||
|
collection = self.get_object()
|
||||||
|
if not uuid:
|
||||||
|
return Response({"error": "User UUID is required"}, status=400)
|
||||||
|
try:
|
||||||
|
user = User.objects.get(uuid=uuid, public_profile=True)
|
||||||
|
except User.DoesNotExist:
|
||||||
|
return Response({"error": "User not found"}, status=404)
|
||||||
|
|
||||||
|
if user == request.user:
|
||||||
|
return Response({"error": "Cannot unshare with yourself"}, status=400)
|
||||||
|
|
||||||
|
if not collection.shared_with.filter(id=user.id).exists():
|
||||||
|
return Response({"error": "Collection is not shared with this user"}, status=400)
|
||||||
|
|
||||||
|
collection.shared_with.remove(user)
|
||||||
|
collection.save()
|
||||||
|
return Response({"success": f"Unshared with {user.username}"})
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
if self.action == 'destroy':
|
if self.action == 'destroy':
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 5.0.8 on 2024-09-06 23:46
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('users', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='customuser',
|
||||||
|
name='public_profile',
|
||||||
|
field=models.BooleanField(default=False),
|
||||||
|
),
|
||||||
|
]
|
|
@ -6,6 +6,7 @@ from django_resized import ResizedImageField
|
||||||
class CustomUser(AbstractUser):
|
class CustomUser(AbstractUser):
|
||||||
profile_pic = ResizedImageField(force_format="WEBP", quality=75, null=True, blank=True, upload_to='profile-pics/')
|
profile_pic = ResizedImageField(force_format="WEBP", quality=75, null=True, blank=True, upload_to='profile-pics/')
|
||||||
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
||||||
|
public_profile = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.username
|
return self.username
|
Loading…
Add table
Add a link
Reference in a new issue