1616TEXT_COLOR = '#FFFFFF'
1717DATE_FORMAT = '%Y-%m-%d'
1818DATETIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
19+ FIRST_COLUMN_TYPES = [
20+ ColumnTypes .TEXT , ColumnTypes .NUMBER , ColumnTypes .DATE , ColumnTypes .SINGLE_SELECT , ColumnTypes .AUTO_NUMBER ]
1921AIRTABLE_API_URL = 'https://api.airtable.com/v0/'
2022ColumnTypes .BARCODE = 'barcode'
2123
@@ -221,21 +223,21 @@ def get_value_map(self, airtable_rows):
221223 value_map [column_name ].append (value )
222224 return value_map
223225
224- def get_select_data (self , select_list ):
225- return { 'options' : [{
226+ def get_select_options (self , select_list ):
227+ return [{
226228 'name' : str (value ),
227229 'id' : self .random_num_id (),
228230 'color' : self .random_color (),
229231 'textColor' : TEXT_COLOR ,
230- } for value in select_list ]}
232+ } for value in select_list ]
231233
232234 def get_column_data (self , link_map , table_name , column_name , column_type , values ):
233235 column_data = None
234236 try :
235237 if column_type == ColumnTypes .BARCODE :
236238 column_type = ColumnTypes .TEXT
237- # elif column_type == ColumnTypes.DATE and len(values[0]) == 24 :
238- # column_data = {'format': 'YYYY-MM-DD HH:mm '}
239+ elif column_type == ColumnTypes .DATE :
240+ column_data = {'format' : 'YYYY-MM-DD' }
239241 elif column_type == ColumnTypes .LINK :
240242 other_table_name = link_map [table_name ][column_name ]
241243 column_data = {'other_table' : other_table_name }
@@ -246,7 +248,7 @@ def get_column_data(self, link_map, table_name, column_name, column_type, values
246248 item = str (item )
247249 if item not in select_list :
248250 select_list .append (item )
249- column_data = self .get_select_data (select_list )
251+ column_data = { 'options' : self .get_select_options (select_list )}
250252 elif column_type == ColumnTypes .COLLABORATOR :
251253 if isinstance (values [0 ], dict ):
252254 column_type = ColumnTypes .SINGLE_SELECT
@@ -255,7 +257,7 @@ def get_column_data(self, link_map, table_name, column_name, column_type, values
255257 name = value ['name' ]
256258 if name not in select_list :
257259 select_list .append (name )
258- column_data = self .get_select_data (select_list )
260+ column_data = { 'options' : self .get_select_options (select_list )}
259261 elif isinstance (values [0 ], list ):
260262 column_type = ColumnTypes .MULTIPLE_SELECT
261263 select_list = []
@@ -264,9 +266,9 @@ def get_column_data(self, link_map, table_name, column_name, column_type, values
264266 name = item ['name' ]
265267 if name not in select_list :
266268 select_list .append (name )
267- column_data = self .get_select_data (select_list )
269+ column_data = { 'options' : self .get_select_options (select_list )}
268270 except Exception as e :
269- print ('[Warning] get column data error:' , e )
271+ print ('[Warning] get' , column_type . value , ' column data error:' , e )
270272 return column_type , column_data
271273
272274 def get_column_type (self , values ):
@@ -378,18 +380,10 @@ def get_select_value_map(self, columns, airtable_rows):
378380 select_value_map [column_name ].append (item )
379381 return select_value_map
380382
381- def get_select_options (self , select_list ):
382- return [{
383- 'name' : str (value ),
384- 'id' : self .random_num_id (),
385- 'color' : self .random_color (),
386- 'textColor' : TEXT_COLOR ,
387- } for value in select_list ]
388-
389383 def gen_select_columns (self , select_value_map ):
390384 select_columns = []
391- for column_name , values in select_value_map .items ():
392- options = self .get_select_options (values )
385+ for column_name , select_list in select_value_map .items ():
386+ options = self .get_select_options (select_list )
393387 column = {
394388 'name' : column_name ,
395389 'options' : options ,
@@ -441,7 +435,7 @@ def list_all_rows(self, table_name):
441435
442436class AirtableConvertor (object ):
443437
444- def __init__ (self , airtable_api_key , airtable_base_id , base , table_names , links = []):
438+ def __init__ (self , airtable_api_key , airtable_base_id , base , table_names , first_columns = [], links = []):
445439 """
446440 airtable_api_key: str
447441 airtable_base_id: str
@@ -452,10 +446,12 @@ def __init__(self, airtable_api_key, airtable_base_id, base, table_names, links=
452446 self .airtable_api = AirtableAPI (airtable_api_key , airtable_base_id )
453447 self .base = base
454448 self .table_names = table_names
449+ self .first_columns = first_columns
455450 self .links = links
456451 self .columns_parser = ColumnsParser ()
457452 self .rows_convertor = RowsConvertor ()
458453 self .links_convertor = LinksConvertor ()
454+ self .get_first_column_map ()
459455 self .get_link_map ()
460456
461457 def convert_metadata (self ):
@@ -480,6 +476,8 @@ def convert_tables(self):
480476 table = self .table_map .get (table_name )
481477 if not table :
482478 airtable_columns = self .airtable_column_map [table_name ]
479+ first_column_name = self .first_column_map .get (table_name ) or \
480+ airtable_columns [0 ]['name' ]
483481 columns = []
484482 for column in airtable_columns :
485483 if column ['type' ] == ColumnTypes .LINK :
@@ -489,9 +487,16 @@ def convert_tables(self):
489487 'column_type' : column ['type' ].value ,
490488 'column_data' : column ['data' ],
491489 }
492- columns .append (item )
490+ if column ['name' ] == first_column_name :
491+ if column ['type' ] not in FIRST_COLUMN_TYPES :
492+ item ['column_type' ] = ColumnTypes .TEXT .value
493+ item ['column_data' ] = None
494+ columns .insert (0 , item )
495+ else :
496+ columns .append (item )
493497 self .add_table (table_name , columns )
494- print ('[Info] Added table [ %s ] with %s columns' % (table_name , len (columns )))
498+ print (
499+ '[Info] Added table [ %s ] with %s columns' % (table_name , len (columns )))
495500 print ('[Info] Success\n ' )
496501 time .sleep (1 )
497502
@@ -570,6 +575,14 @@ def convert_select_columns(self):
570575 print ('[Info] Success\n ' )
571576 time .sleep (1 )
572577
578+ def get_first_column_map (self ):
579+ self .first_column_map = {}
580+ for column in self .first_columns :
581+ table_name = column [0 ]
582+ column_name = column [1 ]
583+ self .first_column_map [table_name ] = column_name
584+ return self .first_column_map
585+
573586 def get_link_map (self ):
574587 self .link_map = {}
575588 for link in self .links :
@@ -600,6 +613,7 @@ def get_airtable_column_map(self):
600613 columns = self .columns_parser .parse (
601614 self .link_map , table_name , airtable_rows )
602615 self .airtable_column_map [table_name ] = columns
616+ return self .airtable_column_map
603617
604618 def get_table_map (self ):
605619 self .table_map = {}
@@ -616,7 +630,7 @@ def get_table_map(self):
616630 time .sleep (0.1 )
617631 return self .table_map
618632
619- def add_table (self , table_name , columns = [] ):
633+ def add_table (self , table_name , columns ):
620634 table = self .base .add_table (table_name , columns = columns )
621635 time .sleep (0.1 )
622636 return table
0 commit comments