Files
kursy-mirror/kursy/forms.py

48 lines
1.6 KiB
Python

import os
import requests
from django import forms
from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
class SignUpForm(forms.Form):
first_name = forms.CharField(max_length=60, required=True, label="First Name")
last_name = forms.CharField(max_length=60, required=True, label="Last Name")
def signup(self, request: WSGIRequest, user):
user.first_name = self.cleaned_data["first_name"].strip().title()
user.last_name = self.cleaned_data["last_name"].strip().title()
user.save()
# create Gitea account
payload = {
"user_id": user.id,
"username": f"studio77-{user.id}",
"email": user.email,
"full_name": f"{user.first_name} {user.last_name}".strip(),
"password": request.POST.get("password1"),
"must_change_password": False,
"visibility": "private",
}
api_url = getattr(settings, "GITEA_URL", None)
if api_url:
url = f"{api_url}/admin/users"
try:
response = requests.post(
url,
json=payload,
timeout=5,
headers={"Authorization": f"token {os.getenv('GITEA_API_TOKEN')}"},
)
response.raise_for_status()
print(f"Successfully created Gitea account for {user.email}")
except Exception as e:
print(
f"Failed to create Gitea account for user {user.email}: {e}\n{response.text}"
)
raise e
return user