Format and lint web directory (#67)

This commit is contained in:
Michael Plunkett
2025-03-10 12:45:19 -05:00
committed by GitHub
parent 1ca0ae2fb2
commit b50ca91d89
20 changed files with 761 additions and 309 deletions

View File

@@ -1,4 +1,3 @@
from datetime import datetime
from typing import Dict, Set
@@ -21,14 +20,15 @@ class UserState:
def __init__(self, db: Session, email: str):
self.db = db
self.email = email.lower()
self._permissions = {}
@property
def permissions(self) -> Dict[str, GroupInfo]:
"""
Returns a dict of all group permissions and a special {"all": read/archive_url/archive_sheet} key
Returns a dict of all group permissions and a special
{"all": read/archive_url/archive_sheet} key
"""
if not hasattr(self, '_permissions'):
self._permissions = {}
if not self._permissions:
self._permissions["all"] = GroupInfo(
read=self.read,
read_public=self.read_public,
@@ -38,23 +38,33 @@ class UserState:
max_archive_lifespan_months=self.max_archive_lifespan_months,
max_monthly_urls=self.max_monthly_urls,
max_monthly_mbs=self.max_monthly_mbs,
priority=self.priority
priority=self.priority,
)
for group in self.user_groups:
if not group.permissions: continue
self._permissions[group.id] = GroupInfo(**group.permissions, description=group.description, service_account_email=group.service_account_email)
if not group.permissions:
continue
self._permissions[group.id] = GroupInfo(
**group.permissions,
description=group.description,
service_account_email=group.service_account_email,
)
return self._permissions
@property
def user_groups_names(self):
if not hasattr(self, '_user_groups_names'):
self._user_groups_names = crud.get_user_group_names(self.db, self.email) + ["default"]
if not hasattr(self, "_user_groups_names"):
# TODO: Define hidden properties in __init__ method
self._user_groups_names = crud.get_user_group_names(
self.db, self.email
) + ["default"]
return self._user_groups_names
@property
def user_groups(self):
if not hasattr(self, '_user_groups'):
self._user_groups = crud.get_user_groups_by_name(self.db, self.user_groups_names)
if not hasattr(self, "_user_groups"):
self._user_groups = crud.get_user_groups_by_name(
self.db, self.user_groups_names
)
return self._user_groups
@property
@@ -62,10 +72,11 @@ class UserState:
"""
Read can be a list of group names or True, if all can be read.
"""
if not hasattr(self, '_read'):
if not hasattr(self, "_read"):
self._read = set()
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
group_read_permissions = group.permissions.get("read", [])
if "all" in group_read_permissions:
self._read = True
@@ -79,10 +90,11 @@ class UserState:
"""
Read public permission
"""
if not hasattr(self, '_read_public'):
if not hasattr(self, "_read_public"):
self._read_public = False
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
if group.permissions.get("read_public", False):
self._read_public = True
return self._read_public
@@ -93,10 +105,11 @@ class UserState:
"""
Archive URL permission
"""
if not hasattr(self, '_archive_url'):
if not hasattr(self, "_archive_url"):
self._archive_url = False
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
if group.permissions.get("archive_url", False):
self._archive_url = True
return self._archive_url
@@ -107,10 +120,11 @@ class UserState:
"""
Archive sheet permission
"""
if not hasattr(self, '_archive_sheet'):
if not hasattr(self, "_archive_sheet"):
self._archive_sheet = False
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
if group.permissions.get("archive_sheet", False):
self._archive_sheet = True
return self._archive_sheet
@@ -118,37 +132,53 @@ class UserState:
@property
def sheet_frequency(self):
if not hasattr(self, '_sheet_frequency'):
if not hasattr(self, "_sheet_frequency"):
self._sheet_frequency = set()
for group in self.user_groups:
if not group.permissions: continue
self._sheet_frequency.update(group.permissions.get("sheet_frequency", None))
if not group.permissions:
continue
self._sheet_frequency.update(
group.permissions.get("sheet_frequency", None)
)
return self._sheet_frequency
@property
def max_archive_lifespan_months(self) -> int:
if not hasattr(self, '_max_archive_lifespan_months'):
self._max_archive_lifespan_months = self._helper_for_grouping_max_numerical_permissions("max_archive_lifespan_months")
if not hasattr(self, "_max_archive_lifespan_months"):
self._max_archive_lifespan_months = (
self._helper_for_grouping_max_numerical_permissions(
"max_archive_lifespan_months"
)
)
return self._max_archive_lifespan_months
@property
def max_monthly_urls(self) -> int:
if not hasattr(self, '_max_monthly_urls'):
self._max_monthly_urls = self._helper_for_grouping_max_numerical_permissions("max_monthly_urls")
if not hasattr(self, "_max_monthly_urls"):
self._max_monthly_urls = (
self._helper_for_grouping_max_numerical_permissions(
"max_monthly_urls"
)
)
return self._max_monthly_urls
@property
def max_monthly_mbs(self) -> int:
if not hasattr(self, '_max_monthly_mbs'):
self._max_monthly_mbs = self._helper_for_grouping_max_numerical_permissions("max_monthly_mbs")
if not hasattr(self, "_max_monthly_mbs"):
self._max_monthly_mbs = (
self._helper_for_grouping_max_numerical_permissions(
"max_monthly_mbs"
)
)
return self._max_monthly_mbs
@property
def priority(self) -> str:
if not hasattr(self, '_priority'):
if not hasattr(self, "_priority"):
self._priority = "low"
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
if group.permissions.get("priority", self._priority) == "high":
self._priority = "high"
break
@@ -159,18 +189,28 @@ class UserState:
"""
A user is active if they can read/archive anything
"""
if not hasattr(self, '_active'):
self._active = bool(self.read or self.read_public or self.archive_url or self.archive_sheet)
if not hasattr(self, "_active"):
self._active = bool(
self.read
or self.read_public
or self.archive_url
or self.archive_sheet
)
return self._active
def _helper_for_grouping_max_numerical_permissions(self, permission_name: str) -> int:
def _helper_for_grouping_max_numerical_permissions(
self, permission_name: str
) -> int:
"""
Iterates one of the numerical permissions where -1 means no restrictions and returns either -1 or the maximum value, defaults according to GroupPermissions
Iterates one of the numerical permissions where -1 means no restrictions
and returns either -1 or the maximum value, defaults according to
GroupPermissions
"""
default = GroupPermissions.model_fields[permission_name].default
max_value = default
for group in self.user_groups:
if not group.permissions: continue
if not group.permissions:
continue
group_value = group.permissions.get(permission_name, default)
if group_value == -1:
max_value = -1
@@ -181,43 +221,65 @@ class UserState:
def in_group(self, group_id: str) -> bool:
return group_id in self.user_groups_names
def usage(self) -> Dict:
def usage(self) -> UsageResponse:
"""
returns the monthly quotas for the URLs/MBs and the totals for Sheets
Returns the monthly quotas for the URLs/MBs and the totals for Sheets
"""
current_month = datetime.now().month
current_year = datetime.now().year
# find and sum all user sheets over this month
user_sheets = self.db.query(
models.Sheet.group_id,
func.count(models.Sheet.id).label('sheet_count')
).filter(models.Sheet.author_id == self.email).group_by(models.Sheet.group_id).all()
user_sheets = (
self.db.query(
models.Sheet.group_id,
func.count(models.Sheet.id).label("sheet_count"),
)
.filter(models.Sheet.author_id == self.email)
.group_by(models.Sheet.group_id)
.all()
)
sheets_by_group = {sheet.group_id: sheet.sheet_count for sheet in user_sheets}
sheets_by_group = {
sheet.group_id: sheet.sheet_count for sheet in user_sheets
}
# find and sum all user urls over this month
urls_by_group = self.db.query(
models.Archive.group_id,
func.count(models.Archive.id).label('url_count'),
func.coalesce(func.sum(
urls_by_group = (
self.db.query(
models.Archive.group_id,
func.count(models.Archive.id).label("url_count"),
func.coalesce(
func.cast(
func.json_extract(models.Archive.result, '$.metadata.total_bytes'),
sqlalchemy.Integer
), 0
)
), 0).label('total_bytes')
).filter(
models.Archive.author_id == self.email,
func.extract('month', models.Archive.created_at) == current_month,
func.extract('year', models.Archive.created_at) == current_year
).group_by(models.Archive.group_id).all()
func.sum(
func.coalesce(
func.cast(
func.json_extract(
models.Archive.result,
"$.metadata.total_bytes",
),
sqlalchemy.Integer,
),
0,
)
),
0,
).label("total_bytes"),
)
.filter(
models.Archive.author_id == self.email,
func.extract("month", models.Archive.created_at)
== current_month,
func.extract("year", models.Archive.created_at) == current_year,
)
.group_by(models.Archive.group_id)
.all()
)
# merge the two queries
usage_by_group: Dict[str, Usage] = {
(url.group_id or ""):
Usage(monthly_urls=url.url_count, monthly_mbs=int(url.total_bytes / 1024 / 1024))
(url.group_id or ""): Usage(
monthly_urls=url.url_count,
monthly_mbs=int(url.total_bytes / 1024 / 1024),
)
for url in urls_by_group
}
for group_id, sheet_count in sheets_by_group.items():
@@ -236,7 +298,7 @@ class UserState:
monthly_urls=total_urls,
monthly_mbs=int(total_bytes / 1024 / 1024),
total_sheets=total_sheets,
groups=usage_by_group
groups=usage_by_group,
)
def has_quota_monthly_sheets(self, group_id: str) -> bool:
@@ -246,7 +308,14 @@ class UserState:
if group_id not in self.permissions:
return False
user_sheets = self.db.query(models.Sheet).filter(models.Sheet.author_id == self.email, models.Sheet.group_id == group_id).count()
user_sheets = (
self.db.query(models.Sheet)
.filter(
models.Sheet.author_id == self.email,
models.Sheet.group_id == group_id,
)
.count()
)
sheet_quota = self.permissions[group_id].max_sheets
if sheet_quota == -1:
@@ -255,13 +324,15 @@ class UserState:
def has_quota_max_monthly_urls(self, group_id: str) -> bool:
"""
checks if a user has reached their monthly url quota for a group, if global then group should be empty string
Checks if a user has reached their monthly url quota for a group, if
global then group should be empty string
"""
quota = 0
if not group_id:
quota = self.max_monthly_urls
else:
if group_id not in self.permissions: return False
if group_id not in self.permissions:
return False
quota = self.permissions[group_id].max_monthly_urls
if quota == -1:
@@ -269,24 +340,31 @@ class UserState:
current_month = datetime.now().month
current_year = datetime.now().year
user_urls = self.db.query(models.Archive).filter(
models.Archive.author_id == self.email,
models.Archive.group_id == group_id,
func.extract('month', models.Archive.created_at) == current_month,
func.extract('year', models.Archive.created_at) == current_year
).count()
user_urls = (
self.db.query(models.Archive)
.filter(
models.Archive.author_id == self.email,
models.Archive.group_id == group_id,
func.extract("month", models.Archive.created_at)
== current_month,
func.extract("year", models.Archive.created_at) == current_year,
)
.count()
)
return user_urls < quota
def has_quota_max_monthly_mbs(self, group_id: str) -> bool:
"""
checks if a user has reached their monthly MBs quota for a group, if global then group should be empty string
Checks if a user has reached their monthly MBs quota for a group, if
global then group should be empty string
"""
quota = 0
if not group_id:
quota = self.max_monthly_mbs
else:
if group_id not in self.permissions: return False
if group_id not in self.permissions:
return False
quota = self.permissions[group_id].max_monthly_mbs
if quota == -1:
@@ -296,19 +374,34 @@ class UserState:
current_year = datetime.now().year
# find and sum all user bytes over this month
user_bytes = self.db.query(models.Archive).filter(
models.Archive.author_id == self.email,
models.Archive.group_id == group_id,
func.extract('month', models.Archive.created_at) == current_month,
func.extract('year', models.Archive.created_at) == current_year
).with_entities(func.coalesce(func.sum(
func.coalesce(
func.cast(
func.json_extract(models.Archive.result, '$.metadata.total_bytes'),
sqlalchemy.Integer
), 0
user_bytes = (
self.db.query(models.Archive)
.filter(
models.Archive.author_id == self.email,
models.Archive.group_id == group_id,
func.extract("month", models.Archive.created_at)
== current_month,
func.extract("year", models.Archive.created_at) == current_year,
)
), 0).label('total')).scalar()
.with_entities(
func.coalesce(
func.sum(
func.coalesce(
func.cast(
func.json_extract(
models.Archive.result,
"$.metadata.total_bytes",
),
sqlalchemy.Integer,
),
0,
)
),
0,
).label("total")
)
.scalar()
)
# convert bytes to mb
user_mbs = int(user_bytes / 1024 / 1024)
@@ -316,7 +409,7 @@ class UserState:
def can_manually_trigger(self, group_id: str) -> bool:
"""
checks if a user is allowed to manually trigger a sheet
Checks if a user is allowed to manually trigger a sheet
"""
if group_id not in self.permissions:
return False
@@ -325,18 +418,21 @@ class UserState:
def is_sheet_frequency_allowed(self, group_id: str, frequency: str) -> bool:
"""
checks if a user is allowed to create a sheet with this frequency for this group
Checks if a user is allowed to create a sheet with this frequency for
this group
"""
if group_id not in self.permissions:
return False
return frequency in self.permissions[group_id].sheet_frequency
def priority_group(self, group_id: str) -> str:
def priority_group(self, group_id: str) -> dict:
priority = "low"
for group in self.user_groups:
if group.id != group_id: continue
if not group.permissions: continue
if group.id != group_id:
continue
if not group.permissions:
continue
priority = group.permissions.get("priority", priority)
break
return convert_priority_to_queue_dict(priority)