66 lines
No EOL
1.5 KiB
Python
66 lines
No EOL
1.5 KiB
Python
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import Self
|
|
|
|
import humanize
|
|
|
|
from taskflower.auth.permission import NamespacePermissionType
|
|
from taskflower.auth.permission.checks import assert_user_perms_on_task
|
|
from taskflower.db.model.task import Task
|
|
from taskflower.db.model.user import User
|
|
from taskflower.types.either import Either
|
|
|
|
def _due_str(due: datetime) -> str:
|
|
now = datetime.now()
|
|
delta = datetime.now() - due
|
|
if now > due:
|
|
return humanize.naturaldelta(
|
|
delta
|
|
) + ' ago'
|
|
else:
|
|
return 'in ' + humanize.naturaldelta(
|
|
delta
|
|
)
|
|
|
|
@dataclass(frozen=True)
|
|
class TaskForUser:
|
|
id: int
|
|
name: str
|
|
description: str
|
|
due: datetime
|
|
due_rel: str
|
|
created: datetime
|
|
complete: bool
|
|
namespace_id: int
|
|
|
|
@classmethod
|
|
def from_task(
|
|
cls,
|
|
tsk: Task,
|
|
usr: User
|
|
) -> Either[Exception, Self]:
|
|
''' Returns a user-sanitized version of the task.
|
|
|
|
This function performs authorization checks on ``usr`` and will
|
|
return ``Left(AuthorizationError)`` if the action is unauthorized.
|
|
However, it is the caller's responsibility to report the violation
|
|
if warranted (use ``taskflower.auth.violations.check_for_auth_error_and_report``
|
|
in an ``lside_effect()``)
|
|
'''
|
|
return assert_user_perms_on_task(
|
|
usr,
|
|
tsk,
|
|
NamespacePermissionType.READ,
|
|
'Retrieve task'
|
|
).map(
|
|
lambda val: cls(
|
|
tsk.id,
|
|
tsk.name,
|
|
tsk.description,
|
|
tsk.due,
|
|
_due_str(tsk.due),
|
|
tsk.created,
|
|
tsk.complete,
|
|
tsk.namespace
|
|
)
|
|
) |