Skip to content

Commit e7713df

Browse files
Delete and Update actions + Refactor (#6)
* Refactor code for improved readability and consistency in Record, Column, and Base classes * Fix record API calls * Fix column test --------- Co-authored-by: infeeeee <[email protected]>
1 parent 11d42bf commit e7713df

File tree

6 files changed

+158
-93
lines changed

6 files changed

+158
-93
lines changed

.github/workflows/unittests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: Unit tests
33
on:
44
# push:
55
# branches: ["main"]
6-
pull_request:
7-
branches: ["main"]
6+
pull_request_target:
7+
types: [labeled]
88
workflow_dispatch:
99

1010
permissions:
@@ -13,6 +13,7 @@ permissions:
1313
jobs:
1414
tests:
1515
name: Unit tests on Cloud
16+
if: contains(github.event.pull_request.labels.*.name, 'safe to test')
1617
strategy:
1718
max-parallel: 1
1819
matrix:

nocodb/Base.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22
from typing import TYPE_CHECKING
3+
34
if TYPE_CHECKING:
45
from nocodb import NocoDB
56

@@ -8,45 +9,48 @@
89
from nocodb.Table import Table
910

1011
import logging
12+
1113
_logger = logging.getLogger(__name__)
1214
_logger.addHandler(logging.NullHandler())
1315

1416

1517
class Base:
16-
def __init__(self, noco_db: "NocoDB",
17-
**kwargs) -> None:
18+
def __init__(self, noco_db: "NocoDB", **kwargs) -> None:
1819

1920
self.noco_db = noco_db
2021
self.base_id = kwargs["id"]
2122
self.title = kwargs["title"]
2223
self.metadata = kwargs
2324

24-
def duplicate(self,
25-
exclude_data: bool = True,
26-
exclude_views: bool = True,
27-
exclude_hooks: bool = True
28-
) -> "Base":
29-
30-
r = self.noco_db.call_noco(path=f"meta/duplicate/{self.base_id}",
31-
method="POST",
32-
json={
33-
"excludeData": exclude_data,
34-
"excludeViews": exclude_views,
35-
"excludeHooks": exclude_hooks})
25+
def duplicate(
26+
self,
27+
exclude_data: bool = True,
28+
exclude_views: bool = True,
29+
exclude_hooks: bool = True,
30+
) -> "Base":
31+
32+
r = self.noco_db.call_noco(
33+
path=f"meta/duplicate/{self.base_id}",
34+
method="POST",
35+
json={
36+
"excludeData": exclude_data,
37+
"excludeViews": exclude_views,
38+
"excludeHooks": exclude_hooks,
39+
},
40+
)
3641
_logger.info(f"Base {self.title} duplicated")
3742

3843
return self.noco_db.get_base(base_id=r.json()["base_id"])
3944

4045
def delete(self) -> bool:
41-
r = self.noco_db.call_noco(path=f"meta/bases/{self.base_id}",
42-
method="DELETE")
46+
r = self.noco_db.call_noco(path=f"meta/bases/{self.base_id}", method="DELETE")
4347
_logger.info(f"Base {self.title} deleted")
4448
return r.json()
4549

4650
def update(self, **kwargs) -> None:
47-
self.noco_db.call_noco(path=f"meta/bases/{self.base_id}",
48-
method="PATCH",
49-
json=kwargs)
51+
self.noco_db.call_noco(
52+
path=f"meta/bases/{self.base_id}", method="PATCH", json=kwargs
53+
)
5054

5155
def get_base_info(self) -> dict:
5256
r = self.noco_db.call_noco(path=f"meta/bases/{self.base_id}/info")
@@ -55,13 +59,11 @@ def get_base_info(self) -> dict:
5559
def get_tables(self) -> list[Table]:
5660
r = self.noco_db.call_noco(path=f"meta/bases/{self.base_id}/tables")
5761
tables = [Table(noco_db=self.noco_db, **t) for t in r.json()["list"]]
58-
_logger.debug(f"Tables in base {self.title}: "
59-
+ str([t.title for t in tables]))
62+
_logger.debug(f"Tables in base {self.title}: " + str([t.title for t in tables]))
6063
return tables
6164

6265
def get_table(self, table_id: str) -> Table:
63-
r = self.noco_db.call_noco(
64-
path=f"meta/tables/{table_id}")
66+
r = self.noco_db.call_noco(path=f"meta/tables/{table_id}")
6567
return Table(noco_db=self.noco_db, **r.json())
6668

6769
def get_table_by_title(self, title: str) -> Table:
@@ -70,9 +72,13 @@ def get_table_by_title(self, title: str) -> Table:
7072
except StopIteration:
7173
raise Exception(f"Table with name {title} not found!")
7274

73-
def create_table(self, table_name: str,
74-
columns: list[dict] | None = None, add_default_columns: bool = True,
75-
**kwargs) -> Table:
75+
def create_table(
76+
self,
77+
table_name: str,
78+
columns: list[dict] | None = None,
79+
add_default_columns: bool = True,
80+
**kwargs,
81+
) -> Table:
7682
kwargs["table_name"] = table_name
7783

7884
if not columns:
@@ -83,7 +89,7 @@ def create_table(self, table_name: str,
8389
else:
8490
kwargs["columns"] = columns
8591

86-
r = self.noco_db.call_noco(path=f"meta/bases/{self.base_id}/tables",
87-
method="POST",
88-
json=kwargs)
92+
r = self.noco_db.call_noco(
93+
path=f"meta/bases/{self.base_id}/tables", method="POST", json=kwargs
94+
)
8995
return self.get_table(table_id=r.json()["id"])

nocodb/Column.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
from typing import TYPE_CHECKING
5+
56
if TYPE_CHECKING:
67
from nocodb.Table import Table
78
from nocodb import NocoDB
@@ -17,7 +18,7 @@ def __str__(self) -> str:
1718

1819

1920
class Column:
20-
def __init__(self, noco_db: "NocoDB",**kwargs) -> None:
21+
def __init__(self, noco_db: "NocoDB", **kwargs) -> None:
2122
self.noco_db = noco_db
2223
self.title = kwargs["title"]
2324
self.column_id = kwargs["id"]
@@ -31,7 +32,7 @@ def __init__(self, noco_db: "NocoDB",**kwargs) -> None:
3132

3233
if "colOptions" in kwargs and "fk_related_model_id" in kwargs["colOptions"]:
3334
self.linked_table_id = kwargs["colOptions"]["fk_related_model_id"]
34-
35+
3536
def get_linked_table(self) -> Table:
3637
if hasattr(self, "linked_table_id"):
3738
return self.noco_db.get_table(self.linked_table_id)
@@ -41,25 +42,46 @@ def get_linked_table(self) -> Table:
4142
@staticmethod
4243
def get_id_metadata() -> list[dict]:
4344
return [
44-
{'title': 'Id', 'column_name': 'id', 'uidt': str(Column.DataType.ID),
45-
'dt': 'int4', 'np': '11', 'ns': '0', 'clen': None,
46-
'pk': True, 'pv': None, 'rqd': True, 'ct': 'int(11)', 'ai': True,
47-
'dtx': 'integer', 'dtxp': '11', },
48-
{'title': 'Title', 'column_name': 'title', 'uidt': str(Column.DataType.SingleLineText),
49-
'dt': 'character varying', 'np': None, 'ns': None, 'clen': '45',
50-
'pk': False, 'pv': True, 'rqd': False, 'ct': 'varchar(45)', 'ai': False,
51-
'dtx': 'specificType', 'dtxp': '45', }
45+
{
46+
"title": "Id",
47+
"column_name": "id",
48+
"uidt": str(Column.DataType.ID),
49+
"dt": "int4",
50+
"np": "11",
51+
"ns": "0",
52+
"clen": None,
53+
"pk": True,
54+
"pv": None,
55+
"rqd": True,
56+
"ct": "int(11)",
57+
"ai": True,
58+
"dtx": "integer",
59+
"dtxp": "11",
60+
},
61+
{
62+
"title": "Title",
63+
"column_name": "title",
64+
"uidt": str(Column.DataType.SingleLineText),
65+
"dt": "character varying",
66+
"np": None,
67+
"ns": None,
68+
"clen": "45",
69+
"pk": False,
70+
"pv": True,
71+
"rqd": False,
72+
"ct": "varchar(45)",
73+
"ai": False,
74+
"dtx": "specificType",
75+
"dtxp": "45",
76+
},
5277
]
5378

5479
class DataType:
5580
Formula = DataType("Formula")
56-
5781
LinkToAnotherRecord = DataType("LinkToAnotherRecord")
5882
Links = DataType("Links")
59-
6083
Lookup = DataType("Lookup")
6184
Rollup = DataType("Rollup")
62-
6385
Attachment = DataType("Attachment")
6486
AutoNumber = DataType("AutoNumber")
6587
Barcode = DataType("Barcode")
@@ -85,6 +107,7 @@ class DataType:
85107
LongText = DataType("LongText")
86108
MultiSelect = DataType("MultiSelect")
87109
Number = DataType("Number")
110+
Order = DataType("Order")
88111
Percent = DataType("Percent")
89112
PhoneNumber = DataType("PhoneNumber")
90113
QrCode = DataType("QrCode")
@@ -103,5 +126,3 @@ def get_data_type(cls, uidt: str) -> DataType:
103126
return getattr(cls, uidt)
104127
else:
105128
raise Exception(f"Invalid datatype {uidt}")
106-
107-

nocodb/Record.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import TYPE_CHECKING, Any
33

44
from nocodb.Column import Column
5+
56
if TYPE_CHECKING:
67
from nocodb.Table import Table
78

@@ -15,24 +16,32 @@ def __init__(self, table: "Table", **kwargs) -> None:
1516
self.metadata = kwargs
1617

1718
def link_record(self, column: Column, link_record: "Record") -> bool:
18-
path = (f"tables/{self.table.table_id}/links/" +
19-
f"{column.column_id}/records/{self.record_id}")
20-
r = self.noco_db.call_noco(path=path,
21-
method="POST", json={"Id": link_record.record_id})
19+
path = (
20+
f"tables/{self.table.table_id}/links/"
21+
+ f"{column.column_id}/records/{self.record_id}"
22+
)
23+
r = self.noco_db.call_noco(
24+
path=path, method="POST", json={"Id": link_record.record_id}
25+
)
2226

2327
return r.json()
2428

2529
def link_records(self, column: Column, link_records: list["Record"]) -> bool:
26-
path = (f"tables/{self.table.table_id}/links/" +
27-
f"{column.column_id}/records/{self.record_id}")
28-
r = self.noco_db.call_noco(path=path,
29-
method="POST", json=[{"Id": l.record_id} for l in link_records])
30+
path = (
31+
f"tables/{self.table.table_id}/links/"
32+
+ f"{column.column_id}/records/{self.record_id}"
33+
)
34+
r = self.noco_db.call_noco(
35+
path=path, method="POST", json=[{"Id": l.record_id} for l in link_records]
36+
)
3037

3138
return r.json()
3239

3340
def get_linked_records(self, column: Column) -> list[Record]:
34-
path = (f"tables/{self.table.table_id}/links/" +
35-
f"{column.column_id}/records/{self.record_id}")
41+
path = (
42+
f"tables/{self.table.table_id}/links/"
43+
+ f"{column.column_id}/records/{self.record_id}"
44+
)
3645
r = self.noco_db.call_noco(path=path)
3746

3847
if "list" in r.json():
@@ -64,5 +73,7 @@ def get_attachments(self, field: str, encoding: str = "utf-8") -> list[str]:
6473
if not isinstance(value_list, list):
6574
raise Exception("Invalid field value")
6675

67-
return [self.noco_db.get_file(p["signedPath"], encoding=encoding)
68-
for p in value_list]
76+
return [
77+
self.noco_db.get_file(p["signedPath"], encoding=encoding)
78+
for p in value_list
79+
]

0 commit comments

Comments
 (0)