feat(forms.py): create gitea account on signup
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import os
|
||||
|
||||
import requests
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
@@ -12,4 +15,33 @@ class SignUpForm(forms.Form):
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user