feature: implements user profile quotas update

This commit is contained in:
ievgenii vdovenko 2025-11-14 17:06:25 +01:00
parent a960c9f99c
commit 0e08a98987
8 changed files with 139 additions and 4 deletions

View file

@ -715,3 +715,11 @@ def UserQuotasSettingsForm():
rhodecode_global_user_repo_quotas = v.Int(strip=True, required=True)
return _UserQuotasSettingsForm
def UserQuotasUpdateForm():
class _UserQuotasUpdateForm(formencode.Schema):
allow_extra_fields = True
user_repo_quotas = v.Int(strip=True, required=True)
return _UserQuotasUpdateForm

View file

@ -10,7 +10,7 @@ log = logging.getLogger(__name__)
class UserQuotaModel:
UNLIMITED = float("inf")
UNLIMITED = -1
DEFAULT_REPO_QUOTA = 5
DEFAULT_QUOTAS_ENABLED = False
@ -44,18 +44,37 @@ class UserQuotaModel:
max_repository_count_allowance=repo_count_allowance,
)
def user_repositories_count(self):
@property
def user_repositories_count(self) -> int:
return Session().scalar(
select(func.count()).select_from(Repository).where(Repository.user_id == self.user.user_id)
)
@property
def user_max_repository_count_allowance(self) -> int:
quotas = self.user.get_quotas()
return quotas.get("max_repository_count_allowance")
@user_max_repository_count_allowance.setter
def user_max_repository_count_allowance(self, new_value: int):
self.user.update_quotas(max_repository_count_allowance=new_value)
def formatted_user_max_repository_count_allowance(self) -> str:
max_repos = self.user_max_repository_count_allowance
if max_repos is self.UNLIMITED:
return "Unlimited"
return str(max_repos)
def is_repo_creation_allowed(self):
if not self.enabled:
return True
if self._user_quotas_valid():
u_quotas = self.user.get_quotas()
return u_quotas.get("max_repository_count_allowance") > self.user_repositories_count()
if self.user_max_repository_count_allowance is self.UNLIMITED:
# since DB can't save float('inf'), we need to use a special case
return True
return self.user_max_repository_count_allowance > self.user_repositories_count
log.warning("User quotas not present, re-initialize quotas.")
self.init_quotas()

View file

@ -412,6 +412,13 @@ class UserModel(BaseModel):
self.sa.add(new_user)
# TODO: fix tests and uncomment this, because tests launching pyramid app, this part fails during
# fixture setup, in normal app this works as intended
# user quotas will be lazily initialized anyway, but this is workaround
# if new_user.is_new_user:
# qm = UserQuotaModel(new_user)
# qm.init_quotas()
if not edit and create_repo_group:
RepoGroupModel().create_personal_repo_group(new_user, commit_early=False)