Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE_users profile text data - editation #39

Merged
merged 5 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions backend/authentication/migrations/0004_user_image.py
Original file line number Diff line number Diff line change
@@ -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/'),
),
]
4 changes: 2 additions & 2 deletions backend/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

return self.username
2 changes: 1 addition & 1 deletion backend/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
9 changes: 8 additions & 1 deletion backend/authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static

from . import views

urlpatterns = [
# 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('update-user/', views.update_user, name='update_user')
]

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
29 changes: 29 additions & 0 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
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
from django.conf import settings
from rest_framework.parsers import MultiPartParser, FormParser

# ---------------------------------
# TODO: Finish this register method
Expand Down Expand Up @@ -53,3 +56,29 @@ def get_csrf_token(request):
csrf_token = get_token(request)
return JsonResponse({'csrfToken': csrf_token})

@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)
9 changes: 8 additions & 1 deletion backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,11 @@
HOCKEY_API_KEY = os.environ.get('HOCKEY_API_KEY')

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
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
]
2 changes: 1 addition & 1 deletion backend/backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
path('api/auth/', include('authentication.urls')),
path('api/games/', include('matches.urls')),
path('api/best_of/', include('best_of.urls'))
]
]
Binary file modified backend/db.sqlite3
Binary file not shown.