From 88907772377583b46a107a24f2179f4a7156ca93 Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Tue, 11 Mar 2025 14:40:54 +0330 Subject: [PATCH] current avatar url --- .../migrations/0004_application_avatar.py | 18 ++++++++++++++++++ apps/gooyal_oauth2/models.py | 2 ++ apps/gooyal_oauth2/views/apis.py | 15 ++++++++++++++- apps/users/views.py | 14 +++++++------- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 apps/gooyal_oauth2/migrations/0004_application_avatar.py diff --git a/apps/gooyal_oauth2/migrations/0004_application_avatar.py b/apps/gooyal_oauth2/migrations/0004_application_avatar.py new file mode 100644 index 0000000..e578f98 --- /dev/null +++ b/apps/gooyal_oauth2/migrations/0004_application_avatar.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.4 on 2025-03-11 11:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('gooyal_oauth2', '0003_accesstoken_token_checksum_refreshtoken_token_family_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='application', + name='avatar', + field=models.ImageField(blank=True, null=True, upload_to='avatars'), + ), + ] diff --git a/apps/gooyal_oauth2/models.py b/apps/gooyal_oauth2/models.py index 113ad30..82b9793 100644 --- a/apps/gooyal_oauth2/models.py +++ b/apps/gooyal_oauth2/models.py @@ -51,6 +51,8 @@ class Application(AbstractApplication): related_name='application' ) + avatar = models.ImageField(upload_to='avatars', null=True, blank=True) + @property def allowed_scopes(self): """ diff --git a/apps/gooyal_oauth2/views/apis.py b/apps/gooyal_oauth2/views/apis.py index a3d07e6..b42440e 100644 --- a/apps/gooyal_oauth2/views/apis.py +++ b/apps/gooyal_oauth2/views/apis.py @@ -1,5 +1,9 @@ +from django.http import Http404 +from django.shortcuts import redirect from oauth2_provider.contrib.rest_framework import TokenHasScope from rest_framework import generics +from rest_framework.decorators import action +from rest_framework.permissions import AllowAny from rest_framework.viewsets import ReadOnlyModelViewSet from ..models import Application @@ -9,7 +13,7 @@ from ..serializers import ApplicationSerializer class ApplicationViewSet(ReadOnlyModelViewSet): serializer_class = ApplicationSerializer - lookup_field = 'uuid' + # lookup_field = 'uuid' permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements] required_alternate_scopes = { "GET": [['accounts.applications:list_or_retrieve']], @@ -17,3 +21,12 @@ class ApplicationViewSet(ReadOnlyModelViewSet): def get_queryset(self): user = self.request.user return Application.objects.filter(user=user).all() + + @action(detail=True, permission_classes=[AllowAny]) + def avatar(self, request, pk=None): + app: Application = self.get_object() + if app.avatar: + return redirect(app.avatar.url) + else: + raise Http404() + diff --git a/apps/users/views.py b/apps/users/views.py index 0b4b4a1..926b0a8 100644 --- a/apps/users/views.py +++ b/apps/users/views.py @@ -1,6 +1,7 @@ from django.contrib.auth import get_user_model from django.contrib.auth.decorators import login_required from django.contrib.auth.views import LoginView +from django.http import Http404 from django.shortcuts import render, redirect from django.urls import reverse from django.utils import timezone @@ -8,6 +9,7 @@ from django.utils.decorators import method_decorator from django.views.generic import DetailView, UpdateView from oauth2_provider.contrib.rest_framework import IsAuthenticatedOrTokenHasScope from rest_framework import generics, status, permissions +from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.views import APIView @@ -40,18 +42,16 @@ class UserDetailView(generics.RetrieveAPIView): } class UserCurrentAvatarUrlView(generics.RetrieveAPIView): - permission_classes = [IsAuthenticatedOrTokenMatchesOASRequirements] + permission_classes = [AllowAny] queryset = User.objects.all() - # serializer_class = PublicUserSerializer lookup_field = 'uuid' - required_alternate_scopes = { - "GET": [["accounts.profile:retrieve"]], - } def retrieve(self, request, *args, **kwargs): user: User = self.get_object() - return redirect(user.avatar.url) - # Response(serializer.data) + if user.avatar: + return redirect(user.avatar.url) + else: + raise Http404() class AccountView(generics.RetrieveUpdateAPIView):