Skip to content
Snippets Groups Projects
Commit 3e14132a authored by James2Tulloch's avatar James2Tulloch
Browse files

User account functionality

parent 769ba7a1
Branches
No related tags found
No related merge requests found
<!DOCTYPE html>
<html>
<head>
<title>Account</title>
</head>
<body>
<h1>Account Page</h1>
<p>Username: {{ user.username }}</p>
<p>Email: {{ user.email }}</p>
<p><a href="{% url 'delete_account' %}">Delete Account</a></p>
<p><a href="{% url 'logout' %}">Logout</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Delete Account</title>
</head>
<body>
<h1>Delete Account</h1>
<p>Are you sure you want to delete your account? This action cannot be undone.</p>
<form method="post">
{% csrf_token %}
<button type="submit">Yes, delete my account</button>
</form>
<p><a href="{% url 'account' %}">Cancel</a></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
{% if messages %}
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
{% endif %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Login</button>
</form>
<p>Don't have an account? <a href="{% url 'register' %}">Register here</a>.</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Logged Out</title>
<style>
body { font-family: Arial, sans-serif; margin: 2em; }
a { color: blue; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>You have been logged out.</h1>
<p>
Thank you for using our service.
<br>
<a href="{% url 'login' %}">Click here to log in again</a>.
</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
{% if messages %}
{% for message in messages %}
<p>{{ message }}</p>
{% endfor %}
{% endif %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
<p>Already have an account? <a href="{% url 'login' %}">Login here</a>.</p>
</body>
</html>
from django.shortcuts import render from django.shortcuts import render, redirect
from django.http import JsonResponse from django.http import JsonResponse
from django.conf import settings
import os import os
import rust_crud_api # This is the module we built import rust_crud_api # This is the module we built
...@@ -11,27 +12,103 @@ def init_db_view(request): ...@@ -11,27 +12,103 @@ def init_db_view(request):
except Exception as e: except Exception as e:
return JsonResponse({"error": str(e)}, status=500) return JsonResponse({"error": str(e)}, status=500)
def create_user_view(request): def register_view(request):
db_url = os.environ.get("DATABASE_URL") """
name = request.POST.get("name", "Alice") Register a new user using the Rust extension.
email = request.POST.get("email", "alice@example.com") Expects POST with 'name' and 'email'.
try: After registration, it simulates a login by storing the user's info in session.
rust_crud_api.create_user(db_url, name, email) """
return JsonResponse({"message": "User created"}) db_url = settings.DATABASE_URL
except Exception as e: if request.method == 'POST':
return JsonResponse({"error": str(e)}, status=500) name = request.POST.get('name')
email = request.POST.get('email')
if not name or not email:
return JsonResponse({'error': 'Name and email are required.'}, status=400)
try:
# Create user in the database via the Rust extension.
rust_crud_api.create_user(db_url, name, email)
# Since our extension does not return the new user’s id directly,
# we retrieve all users and find the one with the matching email.
users = rust_crud_api.get_all_users(db_url)
user = next((u for u in users if u.email == email), None)
if user is None:
return JsonResponse({'error': 'Registration failed.'}, status=500)
# Simulate login by storing user info in session.
request.session['user_id'] = user.id
request.session['user_email'] = user.email
request.session['user_name'] = user.name
return redirect('account')
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
return render(request, 'myapp/register.html')
def get_user_view(request, user_id):
db_url = os.environ.get("DATABASE_URL") def login_view(request):
"""
Login a user by looking up their email in the database.
(Since our Rust extension doesn't handle passwords, we simply check if the email exists.)
"""
db_url = settings.DATABASE_URL
if request.method == 'POST':
email = request.POST.get('email')
if not email:
return JsonResponse({'error': 'Email is required.'}, status=400)
try:
# Retrieve all users and try to find one with the given email.
users = rust_crud_api.get_all_users(db_url)
user = next((u for u in users if u.email == email), None)
if user is None:
return JsonResponse({'error': 'User not found.'}, status=404)
# Simulate login by storing user info in session.
request.session['user_id'] = user.id
request.session['user_email'] = user.email
request.session['user_name'] = user.name
return redirect('account')
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
return render(request, 'myapp/login.html')
def account_view(request):
"""
Display the account page for the logged-in user.
"""
user_id = request.session.get('user_id')
if not user_id:
return redirect('login')
db_url = settings.DATABASE_URL
try: try:
user = rust_crud_api.get_user(db_url, int(user_id)) user = rust_crud_api.get_user(db_url, int(user_id))
if user is None: if user is None:
return JsonResponse({"error": "User not found"}, status=404) return JsonResponse({'error': 'User not found.'}, status=404)
return JsonResponse({ return render(request, 'myapp/account.html', {'user': user})
"id": user.id,
"name": user.name,
"email": user.email
})
except Exception as e: except Exception as e:
return JsonResponse({"error": str(e)}, status=500) return JsonResponse({'error': str(e)}, status=500)
def delete_account_view(request):
"""
Delete the account of the logged-in user.
"""
user_id = request.session.get('user_id')
if not user_id:
return redirect('login')
db_url = settings.DATABASE_URL
if request.method == 'POST':
try:
success = rust_crud_api.delete_user(db_url, int(user_id))
if success:
request.session.flush() # Clear the session after deletion.
return redirect('register')
else:
return JsonResponse({'error': 'Account deletion failed.'}, status=500)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)
return render(request, 'myapp/delete_account.html')
def logout_view(request):
# Clear all session data to log out the user.
request.session.flush()
# Optionally, render a confirmation page or redirect to login.
return render(request, 'myapp/logout.html')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment