|
| 1 | +import boto3 |
| 2 | + |
| 3 | +class {{classNameEditor}}: |
| 4 | + def __init__(self) -> object: |
| 5 | + self.tableid = "{{tableID}}" |
| 6 | + self.workbookid = "{{workbookID}}" |
| 7 | + self.tablename = "{{tableName}}" |
| 8 | + self.honeycode_client = boto3.client('honeycode') |
| 9 | + |
| 10 | + #list all rows in the table |
| 11 | + def listAllRows(self): |
| 12 | + rowRet = [] |
| 13 | + nextToken2 = "" |
| 14 | + response = self.honeycode_client.list_table_rows( |
| 15 | + workbookId=self.workbookid, |
| 16 | + tableId=self.tableid, maxResults=100) |
| 17 | + if "nextToken" in response: |
| 18 | + nextToken2 = response["nextToken"] |
| 19 | + for row in response["rows"]: |
| 20 | + rowRet.append(self.__convertRowOfHoneycodeDataToObject(row)) |
| 21 | + while nextToken2: |
| 22 | + response = self.honeycode_client.list_table_rows( |
| 23 | + workbookId=self.workbookid, |
| 24 | + tableId=self.tableid, maxResults=100, nextToken=nextToken2) |
| 25 | + nextToken2 = "" |
| 26 | + if "nextToken" in response: |
| 27 | + nextToken2 = response["nextToken"] |
| 28 | + for row in response["rows"]: |
| 29 | + rowRet.append(self.__convertRowOfHoneycodeDataToObject(row)) |
| 30 | + return rowRet |
| 31 | + |
| 32 | + |
| 33 | + #find rows with criteria |
| 34 | + def find(self, columnName, operator, value): |
| 35 | + searchVal = "=Filter("+self.tablename+",\""+self.tablename+"["+columnName+"]"+operator+"%\", \""+value+"\")" |
| 36 | + response = self.honeycode_client.query_table_rows( |
| 37 | + workbookId=self.workbookid, |
| 38 | + tableId=self.tableid, |
| 39 | + filterFormula={"formula": searchVal}) |
| 40 | + retValue = [] |
| 41 | + for row in response["rows"]: |
| 42 | + retValue.append(self.__convertRowOfHoneycodeDataToObject(row)) |
| 43 | + return retValue |
| 44 | + |
| 45 | + #Update one row |
| 46 | + def updateRow(self, o_row): |
| 47 | + response = self.honeycode_client.batch_update_table_rows( |
| 48 | + workbookId=self.workbookid, |
| 49 | + tableId=self.tableid, |
| 50 | + rowsToUpdate= |
| 51 | + self.__convertRowsOfObjectsToHoneyCodeUpdateRows([o_row]) |
| 52 | + ) |
| 53 | + |
| 54 | + #Update multipule rows |
| 55 | + def updateRows(self, o_rows): |
| 56 | + rows = self.__convertRowsOfObjectsToHoneyCodeUpdateRows(o_rows) |
| 57 | + for i in range(0, len(rows), 100): |
| 58 | + response = self.honeycode_client.batch_update_table_rows( |
| 59 | + workbookId=self.workbookid, |
| 60 | + tableId=self.tableid, |
| 61 | + rowsToUpdate=rows[i:i + 100] |
| 62 | + |
| 63 | + ) |
| 64 | + #takes a row or an id |
| 65 | + def deleteRow(self, o_row): |
| 66 | + response = self.honeycode_client.batch_delete_table_rows( |
| 67 | + workbookId=self.workbookid, |
| 68 | + tableId=self.tableid, |
| 69 | + rowIds=[o_row.rowId]) |
| 70 | + |
| 71 | + #deletes all rows in this table |
| 72 | + def deleteAllRows(self): |
| 73 | + rows = self.listAllRows() |
| 74 | + for i in range(0, len(rows), 100): |
| 75 | + response = self.honeycode_client.batch_delete_table_rows( |
| 76 | + workbookId=self.workbookid, |
| 77 | + tableId=self.tableid, |
| 78 | + rowIds=rows[i:i + 100] |
| 79 | + ) |
| 80 | + |
| 81 | + def batchSaveRows(self, o_rows): |
| 82 | + maxBatchSize = 100 |
| 83 | + currentPage = 0 |
| 84 | + |
| 85 | + rows = self.__convertRowsOfObjectsToHoneyCodeRows(o_rows) |
| 86 | + while currentPage*maxBatchSize < len(rows): |
| 87 | + begin = currentPage * maxBatchSize |
| 88 | + end = (currentPage * maxBatchSize) + maxBatchSize |
| 89 | + if end > len(rows): |
| 90 | + end = len(rows) |
| 91 | + currentPage+=1 |
| 92 | + response = self.honeycode_client.batch_create_table_rows( |
| 93 | + workbookId=self.workbookid, |
| 94 | + tableId=self.tableid, |
| 95 | + rowsToCreate=rows[begin:end]) |
| 96 | + for batchid in response["createdRows"]: |
| 97 | + for row in o_rows: |
| 98 | + if row.batchid == batchid: |
| 99 | + row.rowId = response["createdRows"][batchid] |
| 100 | + |
| 101 | + #convert honeycode row to object |
| 102 | + def __convertRowOfHoneycodeDataToObject(self, row): |
| 103 | + ar = {{classnameRow}}({{findDataFromColumn}}, row["rowId"]) |
| 104 | + return ar |
| 105 | + |
| 106 | + #convience function to convert objects to rows |
| 107 | + def __convertRowsOfObjectsToHoneyCodeRows(self, rows): |
| 108 | + insertableRows = [] |
| 109 | + batchID = 0 |
| 110 | + for row in rows: |
| 111 | + batchID += 1 |
| 112 | + convertedRow = row.createInsertRowData("insert" + str(batchID)) |
| 113 | + row.batchid = "insert" + str(batchID) |
| 114 | + insertableRows.append(convertedRow) |
| 115 | + return insertableRows |
| 116 | + |
| 117 | + #convience function to convert objects to updates |
| 118 | + def __convertRowsOfObjectsToHoneyCodeUpdateRows(self, rows): |
| 119 | + insertableRows = [] |
| 120 | + batchID = 0 |
| 121 | + for row in rows: |
| 122 | + batchID += 1 |
| 123 | + convertedRow = row.createUpdateRowData() |
| 124 | + insertableRows.append(convertedRow) |
| 125 | + return insertableRows |
| 126 | + |
| 127 | +#Class For data |
| 128 | +class {{classnameRow}}: |
| 129 | + def __init__(self{{variableNamesParams}}, rowId=None) -> object: |
| 130 | + self.rowId = rowId |
| 131 | + self.batchid = "" |
| 132 | +{{variableNamesAlloc}} |
| 133 | + |
| 134 | + def __repr__(self): |
| 135 | + return "{{classnameRow}}<" + str(self.rowId) + ">" |
| 136 | + |
| 137 | + def __str__(self): |
| 138 | + return "{{classnameRow}}<" + {{printstatment}} + ">" |
| 139 | + |
| 140 | + def save(self): |
| 141 | + iface = {{classNameEditor}}() |
| 142 | + if self.rowId: |
| 143 | + iface.updateRow(self) |
| 144 | + else: |
| 145 | + iface.batchSaveRows([self]) |
| 146 | + |
| 147 | + def delete(self): |
| 148 | + iface = {{classNameEditor}}() |
| 149 | + iface.deleteRow(self) |
| 150 | + self.rowId = "" |
| 151 | + |
| 152 | + def createInsertRowData(self, batch_item_id): |
| 153 | + operation = "cellsToCreate" |
| 154 | + dictToAdd = { |
| 155 | + "batchItemId": batch_item_id, |
| 156 | + operation: { |
| 157 | + } |
| 158 | + } |
| 159 | +{{honeyCodeRowDefs}} |
| 160 | + return dictToAdd |
| 161 | + def createUpdateRowData(self): |
| 162 | + operation = "cellsToUpdate" |
| 163 | + dictToAdd = { |
| 164 | + "rowId": self.rowId, |
| 165 | + operation: { |
| 166 | + } |
| 167 | + } |
| 168 | +{{honeyCodeRowDefs}} |
| 169 | + return dictToAdd |
| 170 | + |
| 171 | + |
0 commit comments