refactor(forms.py): move gitea account creation login to separate function
This commit is contained in:
@@ -6,6 +6,35 @@ from django.conf import settings
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
|
||||
|
||||
def create_gitea_account(user, password):
|
||||
payload = {
|
||||
"user_id": user.id,
|
||||
"username": f"studio77-{user.id}",
|
||||
"email": user.email,
|
||||
"full_name": f"{user.first_name} {user.last_name}".strip(),
|
||||
"password": password,
|
||||
"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
|
||||
|
||||
|
||||
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")
|
||||
@@ -15,33 +44,6 @@ 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
|
||||
# gitea account creation
|
||||
password = request.POST.get("password1")
|
||||
create_gitea_account(user, password)
|
||||
|
||||
Reference in New Issue
Block a user