|
1 | 1 | from __future__ import annotations |
| 2 | + |
| 3 | + |
| 4 | +from typing import TYPE_CHECKING |
| 5 | +if TYPE_CHECKING: |
| 6 | + from nocodb.Table import Table |
| 7 | + from nocodb import NocoDB |
| 8 | + |
| 9 | + |
| 10 | +class DataType: |
| 11 | + |
| 12 | + def __init__(self, uidt: str) -> None: |
| 13 | + self.name = uidt |
| 14 | + |
| 15 | + def __str__(self) -> str: |
| 16 | + return self.name |
| 17 | + |
| 18 | + |
2 | 19 | class Column: |
3 | | - def __init__(self, **kwargs) -> None: |
| 20 | + def __init__(self, noco_db: "NocoDB",**kwargs) -> None: |
| 21 | + self.noco_db = noco_db |
4 | 22 | self.title = kwargs["title"] |
5 | 23 | self.column_id = kwargs["id"] |
| 24 | + self.table_id = kwargs["fk_model_id"] |
| 25 | + |
6 | 26 | self.system = bool(kwargs["system"]) |
7 | 27 | self.primary_key = bool(kwargs["pk"]) |
| 28 | + |
| 29 | + self.data_type = Column.DataType.get_data_type(kwargs["uidt"]) |
8 | 30 | self.metadata = kwargs |
9 | 31 |
|
| 32 | + if "colOptions" in kwargs and "fk_related_model_id" in kwargs["colOptions"]: |
| 33 | + self.linked_table_id = kwargs["colOptions"]["fk_related_model_id"] |
| 34 | + |
| 35 | + def get_linked_table(self) -> Table: |
| 36 | + if hasattr(self, "linked_table_id"): |
| 37 | + return self.noco_db.get_table(self.linked_table_id) |
| 38 | + else: |
| 39 | + raise Exception("Not linked column!") |
| 40 | + |
10 | 41 | @staticmethod |
11 | 42 | def get_id_metadata() -> list[dict]: |
12 | 43 | return [ |
13 | | - {'title': 'Id', 'column_name': 'id', 'uidt': 'ID', |
| 44 | + {'title': 'Id', 'column_name': 'id', 'uidt': str(Column.DataType.ID), |
14 | 45 | 'dt': 'int4', 'np': '11', 'ns': '0', 'clen': None, |
15 | 46 | 'pk': True, 'pv': None, 'rqd': True, 'ct': 'int(11)', 'ai': True, |
16 | 47 | 'dtx': 'integer', 'dtxp': '11', }, |
17 | | - {'title': 'Title', 'column_name': 'title', 'uidt': 'SingleLineText', |
| 48 | + {'title': 'Title', 'column_name': 'title', 'uidt': str(Column.DataType.SingleLineText), |
18 | 49 | 'dt': 'character varying', 'np': None, 'ns': None, 'clen': '45', |
19 | 50 | 'pk': False, 'pv': True, 'rqd': False, 'ct': 'varchar(45)', 'ai': False, |
20 | 51 | 'dtx': 'specificType', 'dtxp': '45', } |
21 | 52 | ] |
| 53 | + |
| 54 | + class DataType: |
| 55 | + Formula = DataType("Formula") |
| 56 | + |
| 57 | + LinkToAnotherRecord = DataType("LinkToAnotherRecord") |
| 58 | + Links = DataType("Links") |
| 59 | + |
| 60 | + Lookup = DataType("Lookup") |
| 61 | + Rollup = DataType("Rollup") |
| 62 | + |
| 63 | + Attachment = DataType("Attachment") |
| 64 | + AutoNumber = DataType("AutoNumber") |
| 65 | + Barcode = DataType("Barcode") |
| 66 | + Button = DataType("Button") |
| 67 | + Checkbox = DataType("Checkbox") |
| 68 | + Collaborator = DataType("Collaborator") |
| 69 | + Count = DataType("Count") |
| 70 | + CreatedBy = DataType("CreatedBy") |
| 71 | + CreatedTime = DataType("CreatedTime") |
| 72 | + Currency = DataType("Currency") |
| 73 | + Date = DataType("Date") |
| 74 | + DateTime = DataType("DateTime") |
| 75 | + Decimal = DataType("Decimal") |
| 76 | + Duration = DataType("Duration") |
| 77 | + Email = DataType("Email") |
| 78 | + ForeignKey = DataType("ForeignKey") |
| 79 | + GeoData = DataType("GeoData") |
| 80 | + Geometry = DataType("Geometry") |
| 81 | + ID = DataType("ID") |
| 82 | + JSON = DataType("JSON") |
| 83 | + LastModifiedBy = DataType("LastModifiedBy") |
| 84 | + LastModifiedTime = DataType("LastModifiedTime") |
| 85 | + LongText = DataType("LongText") |
| 86 | + MultiSelect = DataType("MultiSelect") |
| 87 | + Number = DataType("Number") |
| 88 | + Percent = DataType("Percent") |
| 89 | + PhoneNumber = DataType("PhoneNumber") |
| 90 | + QrCode = DataType("QrCode") |
| 91 | + Rating = DataType("Rating") |
| 92 | + SingleLineText = DataType("SingleLineText") |
| 93 | + SingleSelect = DataType("SingleSelect") |
| 94 | + SpecificDBType = DataType("SpecificDBType") |
| 95 | + Time = DataType("Time") |
| 96 | + URL = DataType("URL") |
| 97 | + User = DataType("User") |
| 98 | + Year = DataType("Year") |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def get_data_type(cls, uidt: str) -> DataType: |
| 102 | + if hasattr(cls, uidt): |
| 103 | + return getattr(cls, uidt) |
| 104 | + else: |
| 105 | + raise Exception(f"Invalid datatype {uidt}") |
| 106 | + |
| 107 | + |
0 commit comments