about summary refs log tree commit diff
path: root/gn_auth/auth/authorisation/resources/base.py
blob: e4a12396fc4ba6a843b721d49019c701088b5b30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Base types for resources."""
import logging
import datetime
from uuid import UUID
from dataclasses import dataclass
from typing import Any, Sequence, Optional

import sqlite3

from gn_auth.auth.authentication.users import User


logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class ResourceCategory:
    """Class representing a resource category."""
    resource_category_id: UUID
    resource_category_key: str
    resource_category_description: str


@dataclass(frozen=True)
class Resource:
    """Class representing a resource."""
    resource_id: UUID
    resource_name: str
    resource_category: ResourceCategory
    public: bool
    resource_data: Sequence[dict[str, Any]] = tuple()
    created_by: Optional[User] = None
    created_at: datetime.datetime = datetime.datetime(1970, 1, 1, 0, 0, 0)

    @staticmethod
    def from_resource(# pylint: disable=[too-many-arguments, too-many-positional-arguments]
            resource,
            resource_id: Optional[UUID] = None,
            resource_name: Optional[str] = None,
            resource_category: Optional[ResourceCategory] = None,
            public: Optional[bool] = None,
            resource_data: Optional[Sequence[dict[str, Any]]] = None,
            created_by: Optional[User] = None,
            created_at: Optional[datetime.datetime] = None
    ):
        """Takes a Resource object `resource` and updates the attributes specified in `kwargs`."""
        return Resource(
            resource_id=resource_id or resource.resource_id,
            resource_name=resource_name or resource.resource_name,
            resource_category=resource_category or resource.resource_category,
            public=bool(public) or resource.public,
            resource_data=resource_data or resource.resource_data,
            created_by=created_by or resource.created_by,
            created_at=created_at or resource.created_at)


def resource_from_dbrow(row: sqlite3.Row):
    """Convert an SQLite3 resultset row into a resource."""
    try:
        created_at = datetime.datetime.fromtimestamp(row["created_at"])
    except IndexError as _ie:
        created_at = datetime.datetime(1970, 1, 1, 0, 0, 0)

    try:
        created_by = User.from_sqlite3_row({
            "user_id": row["creator_user_id"],
            "email": row["creator_email"],
            "name": row["creator_name"],
            "verified": row["creator_verified"],
            "created": row["creator_created"]
        })
    except IndexError as _ie:
        created_by = None

    return Resource(
        resource_id=UUID(row["resource_id"]),
        resource_name=row["resource_name"],
        resource_category=ResourceCategory(
            UUID(row["resource_category_id"]),
            row["resource_category_key"],
            row["resource_category_description"]),
        public=bool(int(row["public"])),
        created_by=created_by,
        created_at=created_at)