Files
offline-db/patch_user_profile_validation.py
8d52f8c77a Add user bio field and validate profile updates (#322)
- Add `bio` (TextField) to `User` model.
- Extend `UserProfileAPIView` `patch` method to handle profile updates.
- Sanitize `bio` input using `bleach.clean` to prevent XSS.
- Validate `username` to allow only alphanumeric characters, underscores, and hyphens.
- Add comprehensive test cases in `test_profile_view.py`.
- Apply migrations.
- Mark relevant items as completed in `.jules/development-plan.md`.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Co-authored-by: Senpai-YoloBot <41898282+github-actions[bot]@users.noreply.github.com>
2026-03-22 12:25:48 +00:00

56 lines
1.6 KiB
Python

import re
with open('users/views.py', 'r') as f:
content = f.read()
new_content = content.replace(
''' def patch(self, request):
user = request.user
data = request.data
# We will handle validation in step 3.
# This will be refined.
if 'bio' in data:
user.bio = data['bio']
if 'username' in data:
user.username = data['username']
user.save()
return Response({'status': 'success'})''',
''' def patch(self, request):
import bleach
import re
from django.db import IntegrityError
user = request.user
data = request.data
if 'bio' in data:
bio_text = data['bio']
# Sanitize HTML input using bleach
user.bio = bleach.clean(bio_text, tags=[], strip=True)
if 'username' in data:
username = data['username']
# Only allow alphanumeric + _-
if not re.match(r'^[a-zA-Z0-9_-]+$', username):
return Response(
{"error": "Username can only contain alphanumeric characters, underscores, and hyphens."},
status=status.HTTP_400_BAD_REQUEST
)
user.username = username
try:
user.save()
return Response({'status': 'success'})
except IntegrityError:
return Response(
{"error": "Username is already taken."},
status=status.HTTP_400_BAD_REQUEST
)'''
)
with open('users/views.py', 'w') as f:
f.write(new_content)