-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtenant_service.py
More file actions
130 lines (104 loc) · 4.53 KB
/
tenant_service.py
File metadata and controls
130 lines (104 loc) · 4.53 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import json
from utility import get_app_directory
class TenantService:
"""
Used for reading and saving the tenant_settings.json file.
"""
def __init__(self):
self.tenant_settings_filename = get_app_directory() / "tenant_settings.json"
def get_all_tenants(self, initial_load: bool = False) -> list[dict]:
""""
Loads and returns the tenant settings file from file.
Set initial_load to True when you load tenants the firs time at launch,
this ensures data integrity of the JSON.
"""
all_tenants: list[dict] = []
with open(self.tenant_settings_filename) as f:
all_tenants = json.load(f)
if initial_load:
self.add_missing_fetch_options(all_tenants)
return all_tenants
def get_tenant_by_id(self, tenant_id):
all_tenants: list[dict] = self.get_all_tenants(False)
tenant_index: int = self.get_tenant_index(all_tenants, tenant_id)
return all_tenants[tenant_index]
def add_missing_fetch_options(self, all_tenants: list[dict]) -> list[dict]:
"""
Checks if there are any tenants without "fetch options", and if so adds a default dictionary to each.
Is done because earlier CRMScript Fetcher version did not contain this object in JSON.
"""
fetch_options = {
"fetch_scripts": True,
"fetch_triggers": True,
"fetch_screens": True,
"fetch_screen_choosers": True,
"fetch_scheduled_tasks": True,
"fetch_extra_tables": True
}
for tenant in [t for t in all_tenants if t.get("fetch_options") is None]:
tenant["fetch_options"] = fetch_options
self.save(all_tenants)
return all_tenants
def save(self, all_tenants: list[dict]):
"""Saves entire JSON file. Must include all tenants!"""
with open(self.tenant_settings_filename, "w") as f:
f.write(json.dumps(all_tenants, indent=4))
@staticmethod
def get_next_id(all_tenants: list[dict]) -> int:
if not all_tenants: return 1
return max(t["id"] for t in all_tenants) + 1
def add_tenant(self, new_tenant: dict) -> dict:
"""
Adds a new tenant to json file and saves file.
Returns the tenant with new ID
"""
# To make sure frontend isn't sending empty objects
if not new_tenant.get("tenant_name"):
raise Exception("Tenant name is missing")
if not new_tenant.get("url"):
raise Exception("URL is missing")
with open(self.tenant_settings_filename, 'r+') as f:
tenants: list[dict] = json.load(f)
# Set the new tenant's ID before adding to list
new_tenant["id"] = self.get_next_id(tenants)
tenants.append(new_tenant)
f.seek(0)
json.dump(tenants, f, indent=4)
return new_tenant
@staticmethod
def get_tenant_index(all_tenants: list[dict], tenant_id: int) -> int:
"""
Returns the JSON array index of the tenant by its ID.
"""
for i, tenant in enumerate(all_tenants):
if tenant.get("id") == tenant_id:
return i
raise ValueError("Tenant ID not found in tenant list")
def update_tenant(self, tenant: dict):
"""
Updates an existing tenant in json file by its ID, and saves file.
"""
# Some basic validation to make sure Vue isn't sending faulty objects
if not tenant.get("id"):
raise ValueError("Tenant ID missing")
if not tenant.get("tenant_name"):
raise ValueError("Tenant must have a name")
# Find the index of the given tenant in the JSON array so we can replace it.
all_tenants: list[dict] = self.get_all_tenants()
tenant_index: int = self.get_tenant_index(all_tenants, tenant["id"])
if all_tenants[tenant_index] == tenant:
return # No changes, no need to save
# Replace tenant object and save file
all_tenants[tenant_index] = tenant
self.save(all_tenants)
def delete_tenant(self, tenant_id: int) -> None:
"""
Deletes a tenant from json file by its ID.
"""
if not tenant_id:
raise ValueError("Tenant ID missing")
# Find the index of the given tenant in the JSON array so we can delete it.
all_tenants: list[dict] = self.get_all_tenants()
tenant_index: int = self.get_tenant_index(all_tenants, tenant_id)
all_tenants.pop(tenant_index)
self.save(all_tenants)