fix(forms.py): fix error when gitea account already exists
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import logging as lg
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -5,6 +6,8 @@ from django import forms
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.handlers.wsgi import WSGIRequest
|
from django.core.handlers.wsgi import WSGIRequest
|
||||||
|
|
||||||
|
logger = lg.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create_gitea_account(user, password):
|
def create_gitea_account(user, password):
|
||||||
payload = {
|
payload = {
|
||||||
@@ -27,9 +30,20 @@ def create_gitea_account(user, password):
|
|||||||
headers={"Authorization": f"token {os.getenv('GITEA_API_TOKEN')}"},
|
headers={"Authorization": f"token {os.getenv('GITEA_API_TOKEN')}"},
|
||||||
)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
print(f"Successfully created Gitea account for {user.email}")
|
logger.info(f"Successfully created Gitea account for {user.email}")
|
||||||
|
except requests.exceptions.HTTPError as http_err:
|
||||||
|
status_code = http_err.response.status_code
|
||||||
|
if status_code == 422:
|
||||||
|
logger.warning(
|
||||||
|
f"Gitea account for user {user.email} already exists. Skipping creation."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
f"HTTP error occurred while creating Gitea account for user {user.email}: {http_err}\n{response.text}"
|
||||||
|
)
|
||||||
|
raise http_err
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(
|
logger.error(
|
||||||
f"Failed to create Gitea account for user {user.email}: {e}\n{response.text}"
|
f"Failed to create Gitea account for user {user.email}: {e}\n{response.text}"
|
||||||
)
|
)
|
||||||
raise e
|
raise e
|
||||||
|
|||||||
Reference in New Issue
Block a user