From 89e58dd426a614753246aec75d6cb8f2420433d8 Mon Sep 17 00:00:00 2001 From: aprikryl Date: Tue, 5 Nov 2024 21:43:57 +0100 Subject: [PATCH 1/5] First attempt of the new route for uploading profile image --- backend/authentication/models.py | 4 ++-- backend/authentication/serializers.py | 2 +- backend/authentication/urls.py | 3 ++- backend/authentication/views.py | 15 +++++++++++++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/backend/authentication/models.py b/backend/authentication/models.py index f0f6104..bab6d3d 100644 --- a/backend/authentication/models.py +++ b/backend/authentication/models.py @@ -4,7 +4,7 @@ class User(AbstractUser): email = models.EmailField(unique=True) password = models.CharField(max_length=128) + image = models.ImageField(upload_to='profile_images/', null=True, blank=True) def __str__(self): - return self.username - \ No newline at end of file + return self.username \ No newline at end of file diff --git a/backend/authentication/serializers.py b/backend/authentication/serializers.py index 7431ae9..52c7c31 100644 --- a/backend/authentication/serializers.py +++ b/backend/authentication/serializers.py @@ -4,5 +4,5 @@ class UserSerializer(serializers.ModelSerializer): class Meta: model = User - fields = ('id', 'username', 'email', 'password') + fields = ('id', 'username', 'email', 'password', 'image') extra_kwargs = {'password': {'write_only': True}} diff --git a/backend/authentication/urls.py b/backend/authentication/urls.py index 6310919..ec0d78d 100644 --- a/backend/authentication/urls.py +++ b/backend/authentication/urls.py @@ -5,5 +5,6 @@ # path('register/', UserRegisterView.as_view(), name='register'), path('login/', views.user_login, name='login'), path('logout/', views.log_out, name='logout'), - path('csrf-token/', views.get_csrf_token, name='csrf_token') + path('csrf-token/', views.get_csrf_token, name='csrf_token'), + path('upload-profile-image/', views.user_add_profile_image, name='upload-profile-image') ] diff --git a/backend/authentication/views.py b/backend/authentication/views.py index 4159e5d..9f0274e 100644 --- a/backend/authentication/views.py +++ b/backend/authentication/views.py @@ -3,6 +3,7 @@ from rest_framework.decorators import api_view from django.views.decorators.csrf import ensure_csrf_cookie from django.middleware.csrf import get_token +from .models import User # --------------------------------- # TODO: Finish this register method @@ -53,3 +54,17 @@ def get_csrf_token(request): csrf_token = get_token(request) return JsonResponse({'csrfToken': csrf_token}) +@api_view(['POST']) +def user_add_profile_image(request): + if request.method == 'POST': + try: + user = User.objects.get(id=request.user.id) + user.image = request.FILES['image'] + user.save() + return JsonResponse({"message": "Profile image uploaded successfully"}, status=200) + except User.DoesNotExist: + return JsonResponse({"error": "User not found"}, status=404) + except Exception as e: + return JsonResponse({"error": str(e)}, status=400) + + From 41785267d2a7664028c41e2efe368437e3d40d59 Mon Sep 17 00:00:00 2001 From: aprikryl Date: Tue, 5 Nov 2024 21:45:32 +0100 Subject: [PATCH 2/5] New migration for the uploaded image --- .../migrations/0004_user_image.py | 18 ++++++++++++++++++ backend/db.sqlite3 | Bin 188416 -> 188416 bytes 2 files changed, 18 insertions(+) create mode 100644 backend/authentication/migrations/0004_user_image.py diff --git a/backend/authentication/migrations/0004_user_image.py b/backend/authentication/migrations/0004_user_image.py new file mode 100644 index 0000000..1955095 --- /dev/null +++ b/backend/authentication/migrations/0004_user_image.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-11-05 20:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('authentication', '0003_rename_customuser_user'), + ] + + operations = [ + migrations.AddField( + model_name='user', + name='image', + field=models.ImageField(blank=True, null=True, upload_to='profile_images/'), + ), + ] diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index dc4cffaf5f926b88278cf33d7e5a68bf7bfa19f8..ea97779225b5004e11b618db385125429aeb4834 100644 GIT binary patch delta 274 zcmZoTz};|wdxEs!Zw3YiJs^gG@`*aetiKub$~rfuEZ}FB=J+(3UBHxy z(yR=c+K!2(B^jxCC7H>IC7Jno1_lNu@ukJ7Me&)riRr0E21X{jhK9NZrV2&|Rwkxa zMuvJu76zuqW}6Kp7m6`TH(%19eo3EEUza6_fuBi^fnSC15+56{HTNs7zg+&DlQ^>2 zr?98QFd`-W5%}X=^N%SN^CDNVO-BB zqobe%wq8l0EU_p#Be6)s(7-@b!7tRuM{_#238Om;0|Vowjg99R*^EWl#N8FA3z{&> Pw7Z!xZg(?d+O-k@^(jh9 delta 175 zcmZoTz};|wdxEs!F9rq%Js^gGvWYsztiKra@^)=ZS-{UM#c42^UBGl>;}?$229gWK z7^Rvo=}*6; p)WOff&A_k1cZrXU*P8nk*IzDw&Pg0u>{Hm@vPH6~umrJiGaq3} zV$x(RnAjM$y~TiW7URYVZyBd^n=ra_HT#RQiyIp=wz*E Date: Thu, 7 Nov 2024 20:12:13 +0100 Subject: [PATCH 3/5] First attempt of the image route --- backend/authentication/urls.py | 6 ++++++ backend/authentication/views.py | 25 +++++++++++++++---------- backend/backend/settings.py | 9 ++++++++- backend/backend/urls.py | 2 +- backend/db.sqlite3 | Bin 188416 -> 188416 bytes 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/backend/authentication/urls.py b/backend/authentication/urls.py index ec0d78d..ff84596 100644 --- a/backend/authentication/urls.py +++ b/backend/authentication/urls.py @@ -1,4 +1,7 @@ from django.urls import path +from django.conf import settings +from django.conf.urls.static import static + from . import views urlpatterns = [ @@ -8,3 +11,6 @@ path('csrf-token/', views.get_csrf_token, name='csrf_token'), path('upload-profile-image/', views.user_add_profile_image, name='upload-profile-image') ] + +if settings.DEBUG: + urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/backend/authentication/views.py b/backend/authentication/views.py index 9f0274e..ff4d773 100644 --- a/backend/authentication/views.py +++ b/backend/authentication/views.py @@ -4,6 +4,8 @@ from django.views.decorators.csrf import ensure_csrf_cookie from django.middleware.csrf import get_token from .models import User +from django.conf import settings +from rest_framework.parsers import MultiPartParser, FormParser # --------------------------------- # TODO: Finish this register method @@ -56,15 +58,18 @@ def get_csrf_token(request): @api_view(['POST']) def user_add_profile_image(request): - if request.method == 'POST': - try: - user = User.objects.get(id=request.user.id) - user.image = request.FILES['image'] - user.save() - return JsonResponse({"message": "Profile image uploaded successfully"}, status=200) - except User.DoesNotExist: - return JsonResponse({"error": "User not found"}, status=404) - except Exception as e: - return JsonResponse({"error": str(e)}, status=400) + parser_classes = (MultiPartParser, FormParser) + try: + user = User.objects.get(id=request.user.id) + user.image = request.FILES['image'] + user.save() + + # Return the image URL in the response + image_url = f"{settings.MEDIA_URL}{user.image.name}" + return JsonResponse({"message": "Profile image uploaded successfully", "image_url": image_url}, status=200) + except User.DoesNotExist: + return JsonResponse({"error": "User not found"}, status=404) + except Exception as e: + return JsonResponse({"error": str(e)}, status=400) diff --git a/backend/backend/settings.py b/backend/backend/settings.py index dc4c7d4..24775f5 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -141,4 +141,11 @@ HOCKEY_API_KEY = os.environ.get('HOCKEY_API_KEY') CORS_ALLOW_ALL_ORIGINS = True -CORS_ALLOW_CREDENTIALS = True \ No newline at end of file +CORS_ALLOW_CREDENTIALS = True + +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') + +CORS_ALLOWED_ORIGINS = [ + "http://localhost:3000", # Replace with your React frontend URL +] \ No newline at end of file diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 04c1421..7d040bc 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -6,4 +6,4 @@ path('api/auth/', include('authentication.urls')), path('api/games/', include('matches.urls')), path('api/best_of/', include('best_of.urls')) -] +] \ No newline at end of file diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index ea97779225b5004e11b618db385125429aeb4834..6f2f7e7f35757ee869b616f3701c9444b788f6e5 100644 GIT binary patch delta 599 zcmaiwO>fcw0ETJtBf!Qo%`S_PnGlH~UHU~^Fdho6P$dP;5?Y@Vtfzp=}n$I?_w8O>>}@vLEzzsN7}Wp}!k#Z*1W*{oAq0V7@&utWDJp{?r&x-hVC2(k!g3@E-z3h@ zYEscRU$OYmb zULBg-LlgM*V=oat3q-%~9UpuQpKYV(U?u!49xMZxLl7=y4kn)3o#+&SPD$882_3h| zsp@sT#Glbxz6MXWUq|5UqhKOZSl?fOWiV3MEPsY$kApPu*9Jvy*$^m6;)!kl9r&49 z-`Rn&%~c4vyn2$jTV9Fu|G1}Gao!xUY;`uIMOuJ71+Mm$CT7B_<8t)WZj2;xEGT-L zcP!YfXo{!$1DjDLo0k-E><{{Sc~VFzDWTSwnl6PGEVp!VDG~gn($EO5il!w*k;lD} ztGErmFB@D|fxN-Im^JI1NT!NTXEd*~ii&bZ2bU1G;S4J!+3)9SOnHXa&9lKi(&7Zo(ZHd2G9d9^|_aJE}MrIafMw6TKTs@7<41hqz-_zYS$|S8a#MeKs&^tNE z!r80H)k&qo!^J5hJd|P;=_$O@_NMQ2gnm9q7MTOayaeBlhCWY Date: Sun, 10 Nov 2024 20:13:17 +0100 Subject: [PATCH 4/5] Added route for edit user's data --- backend/authentication/urls.py | 4 +++- backend/authentication/views.py | 25 +++++++++++++++++++++++++ backend/db.sqlite3 | Bin 188416 -> 192512 bytes 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/authentication/urls.py b/backend/authentication/urls.py index ff84596..ecb5f30 100644 --- a/backend/authentication/urls.py +++ b/backend/authentication/urls.py @@ -9,7 +9,9 @@ path('login/', views.user_login, name='login'), path('logout/', views.log_out, name='logout'), path('csrf-token/', views.get_csrf_token, name='csrf_token'), - path('upload-profile-image/', views.user_add_profile_image, name='upload-profile-image') + path('upload-profile-image/', views.user_add_profile_image, name='upload-profile-image'), + path('update-user/', views.update_user, name='update_user'), + ] if settings.DEBUG: diff --git a/backend/authentication/views.py b/backend/authentication/views.py index ff4d773..6709c92 100644 --- a/backend/authentication/views.py +++ b/backend/authentication/views.py @@ -72,4 +72,29 @@ def user_add_profile_image(request): except Exception as e: return JsonResponse({"error": str(e)}, status=400) +@api_view(['POST']) +def update_user(request): + # Ensure the user is authenticated + if not request.user.is_authenticated: + return JsonResponse({"error": "User not authenticated"}, status=401) + # Retrieve the authenticated user + try: + user = User.objects.get(id=request.user.id) + except User.DoesNotExist: + return JsonResponse({"error": "User not found"}, status=404) + + # Update user fields if provided in the request data + if 'username' in request.data: + user.username = request.data['username'] + if 'email' in request.data: + user.email = request.data['email'] + + # Save changes to the database + user.save() + + return JsonResponse({ + "message": "User profile updated successfully", + "username": user.username, + "email": user.email + }, status=200) diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index 6f2f7e7f35757ee869b616f3701c9444b788f6e5..b22fce2e68f7bb8fb6cbd181217938121b0efb74 100644 GIT binary patch delta 702 zcmZoTz}@hGdxA8p90LP`{zL_PM!Ag%3*)(gJSKi2CVoGDp^c4f{F8<9Z6X?V8Tl9- zIpsxpIXR8h8*O=w42(>44Gnb-4HOJ5tqd%!OpWy{OpT1p%!^A4Qj1E9Q;Qr^5_7@I z42*RROcjg_tboYCs3f(x#34O5F*8RmxoYyRyok;J@|h=a^FIaJdXa(uJ^#hcf(1wU zr(fh}6p;qHg$sy*K!91D6GdyA0OK|RCKd-iX8w;L1^WEI`M>dh+$`Aeh+jod9VntJ z1tfHMK?Z1WSu*f#;gRB>#PfsCmFp9?4VUF~i3BD+Ca$>cZV5~uIsH_aeHkrE^DT@l zj7!S1jWZ1^j4~@Kip|RMDl;t-t5VW(3Jo(~2`n zb1G9alZ=zHOVmM%rbk?2QrNyciz!x=S%X`1I(q|?KNGj+#>R_W(>=PGcnwUD!p+Rk z(A-Evj@gkB7OX}PX;Ui$b3H>tGc!Z8?P=XidtJ6GA7J8UoUV6~iA{l!JEn(RWZLBA z=;G#Ly6#lFFz lX~BVkk-k2qCW+=)nGxj~A?U7wBr|Yu0RV67#>@Z! delta 478 zcmZp8z};|wdxA8pFaraF-b4j^M&XSK3*))@{TP_|A{h80_=Pq$`tWHtnlth-IC9F1 z@^W$-TQ}M=7#SFu=o%X88kj2>T3DG{SQ#4X8Jn6}7#L2j%vTUkOi9euD=5m$E~?Bi zcSz4o%*@eC&d;5^EI(qhP`(Wd(6&tz8`n*mz_OX8;BP-Skk7<_k%9jy|HaLM1xNX( zU*u;Lk(Oe%=EPxAn*ifB0VWm)4krF@K+Oz#8w)!5Cr(I_*OdbDba=Udq&im|1K$=N zDgH@ZaXdfxT)94R+f0{8VA9*}k-+qkbK(SbO%-NeM&lAQvoeePjJ(RE)S#hpuhI^8qql Date: Sun, 10 Nov 2024 21:28:04 +0100 Subject: [PATCH 5/5] Remove image handling for now --- backend/authentication/urls.py | 4 +--- backend/authentication/views.py | 16 ---------------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/backend/authentication/urls.py b/backend/authentication/urls.py index ecb5f30..9fcf60b 100644 --- a/backend/authentication/urls.py +++ b/backend/authentication/urls.py @@ -9,9 +9,7 @@ path('login/', views.user_login, name='login'), path('logout/', views.log_out, name='logout'), path('csrf-token/', views.get_csrf_token, name='csrf_token'), - path('upload-profile-image/', views.user_add_profile_image, name='upload-profile-image'), - path('update-user/', views.update_user, name='update_user'), - + path('update-user/', views.update_user, name='update_user') ] if settings.DEBUG: diff --git a/backend/authentication/views.py b/backend/authentication/views.py index 6709c92..e99271f 100644 --- a/backend/authentication/views.py +++ b/backend/authentication/views.py @@ -56,22 +56,6 @@ def get_csrf_token(request): csrf_token = get_token(request) return JsonResponse({'csrfToken': csrf_token}) -@api_view(['POST']) -def user_add_profile_image(request): - parser_classes = (MultiPartParser, FormParser) - try: - user = User.objects.get(id=request.user.id) - user.image = request.FILES['image'] - user.save() - - # Return the image URL in the response - image_url = f"{settings.MEDIA_URL}{user.image.name}" - return JsonResponse({"message": "Profile image uploaded successfully", "image_url": image_url}, status=200) - except User.DoesNotExist: - return JsonResponse({"error": "User not found"}, status=404) - except Exception as e: - return JsonResponse({"error": str(e)}, status=400) - @api_view(['POST']) def update_user(request): # Ensure the user is authenticated