Coverage for lib/lib_airtable.py: 41%
82 statements
« prev ^ index » next coverage.py v7.9.1, created at 2026-01-26 23:58 +0100
« prev ^ index » next coverage.py v7.9.1, created at 2026-01-26 23:58 +0100
1__author__ = 'moilerat'
3# TODO quand on heritera refacto auth
4# from auth.lib_auth import LibUserDataExternalServices
6# VR TODO 7-5-23 : lib airtable in lib/3rd that is used by lib_stat_usage and lib_auth and lib_user_conf
7class LibAirTable():
9 def __init__(self,
10 api_key = "patNbuYIKnL3BPB9J.2d4ae96eea8d9461f11a399fdae01e219508ff92e15cd4468d82b671bb5101e9",
11 base_id = "appujAYNrC92tCuUj"):
13 # Base APIA
14 self.table_name_stat = "Query"
15 self.table_name_auth = "Users"
16 self.table_name_configuration = "ConfigurationTemplate"
18 # Base PI
19 self.table_name_otp_link = "OTP_SAFIA"
20 self.record_id = ""
22 try:
23 from pyairtable import Table
24 except Exception as e:
25 print("Padam")
26 self.api_key = api_key
27 self.base_id = base_id
29 # VR TODO : je en sais pas comment tester
30 try :
31 self.table_name_stat_at = Table(api_key, base_id, self.table_name_stat)
32 self.table_name_auth_at = Table(api_key, base_id, self.table_name_auth)
33 self.table_name_configuration_at = Table(api_key, base_id, self.table_name_configuration)
34 except Exception as e:
35 print(str(e))
36 print(" We may be using PI DB instead of APIA (where these table are !) ")
38 try :
39 self.table_otp_at = Table(api_key, base_id, self.table_name_otp_link)
40 except Exception as e:
41 print(str(e))
42 print(" We may be using APIA DB instead of PI (where the OTP link are !) ")
45 # VR 3-6-23 : je ne sais plus à quoi ca sert
46 self.hard_corded_configuration_id_no_preprompt = "recG0alExxv4ARmzD"
48 # TODO separate with user_config and not only auth
49 # is_mail_valid
50 def get_info_user_data_from_mail(self, from_mail):
51 from pyairtable.formulas import match
53 #table = Table("apikey", "base_id", "Contact")
54 formula = match({"Email" : from_mail})
56 conf_user = "DefaultConfiguration"
57 ret = self.table_name_auth_at.all(formula=formula)
58 if len(ret) == 0:
59 self.authentified_email = "victor@reutenauer.eu"
60 return False, conf_user, True, "", ""
61 elif len(ret) == 1:
62 self.id_email_auth = ret[0]["id"]
63 self.authentified_email = from_mail
64 fields = ret[0]["fields"]
65 privacy_from_at = False
66 defaut_github_issue = ""
67 github_token = ""
68 if 'ChoiceStrictPrivacyVSDataSharing' in fields and fields["ChoiceStrictPrivacyVSDataSharing"] == "StrictPrivacy":
69 privacy_from_at = True
70 if 'DefaultConfiguration' in fields:
71 conf_user = fields["DefaultConfiguration"]
72 if 'defaut_github_issue' in fields:
73 defaut_github_issue = fields["defaut_github_issue"]
74 if 'github_token' in fields:
75 github_token = fields["github_token"]
76 return True, conf_user, privacy_from_at, defaut_github_issue, github_token
77 else :
78 self.authentified_email = "victor@reutenauer.eu"
79 return False, conf_user, True, "", ""
83 def record_usage(self,
84 mail,
85 cost,
86 input = "",
87 output = "",
88 verbose = False):
90 if mail == self.authentified_email :
91 record = {
92 "EmailUser" : [self.id_email_auth],
93 "Cost" : cost,
94 "Input" : input,
95 "Output" : output,
96 "Configuration" : [self.hard_corded_configuration_id_no_preprompt]
97 }
98 ret = self.table_name_stat_at.create(record)
99 print(str(ret))
100 else :
101 print("Email not authentified")
103 def get_otp_info(self, otp = "", verbose = False):
104 from pyairtable.formulas import match
106 # , "Status" : "active" (or in setup ?)
107 formula = match({"Token" : otp})
109 print(" otp : " + str(otp))
110 if otp == "" or otp == None:
111 return {}
113 ret = self.table_otp_at.all(formula=formula)
115 if verbose :
116 print(" ret : " + str(ret))
118 if len(ret) != 1:
119 print(" Authentification error " + str(ret))
120 return {}
122 fields = ret[0]["fields"]
123 self.record_id = ret[0]["id"]
124 result = {"ExpiryDate" : fields["ExpiryDate"] if "ExpiryDate" in fields else "",
125 "Email_User" : fields["Email_User"] if "Email_User" in fields else "",
126 "OTP_LINK" : fields["OTP_LINK"] if "OTP_LINK" in fields else "",
127 "Description" : fields["Description"] if "Description" in fields else "",
128 "Status" : fields["Status"] if "Status" in fields else ""}
130 # ExpiryDate
131 # Email_User
132 # OTP_LINK
133 # fields
135 return result
139 def update_otp_link(self, otp = "", status = "active"):
140 print("TODO and use")
142 to_update = {"fields": {
143 "Status": "expired"
144 }}
146 print("VR TODO update token expired with https://airtable.com/developers/web/api/update-record !")
147# ret = self.table_otp_at.update(self.record_id, to_update)
148# print(str(ret))
157# HC link
158# BASE ID APIA : appujAYNrC92tCuUj
159# USERS TABLE :
160# QUERY TABLE : tblN9rxcydbo5ya3V
162# "victor@reutenauer.eu" : "recRjc4xmytOgWrNc"
163# "Auto Preprompt Query" : recG0alExxv4ARmzD
165def hello_world_airtable(): # pragma no coverage
167# api_key = os.environ['AIRTABLE_API_KEY']
168 api_key = "patNbuYIKnL3BPB9J.2d4ae96eea8d9461f11a399fdae01e219508ff92e15cd4468d82b671bb5101e9"
169 base_id = "appujAYNrC92tCuUj"
170 table_name ="Users"
171 table = Table(api_key, base_id, table_name)
172 ret = table.all()
174 print(str(ret))
176 return
180def hello_world_airtable_deprecated(): # pragma no coverage
181 from airtable import airtable
182 at = airtable.Airtable('BASE_ID', 'API_KEY')
183 att = at.get('TABLE_NAME')
185 ret = att.get_all()
187 print(str(ret))
189 return
191if __name__ == "__main__":
192 hello_world_airtable_deprecated()
193 hello_world_airtable()