def list_worksheets(self): ''' Return a list of row dicts, one per worksheet. These dicts do NOT contain worksheet items; this method is meant to make it easy for a user to see the currently existing worksheets. ''' with self.engine.begin() as connection: rows = connection.execute(cl_worksheet.select()).fetchall() return [dict(row) for row in sorted(rows, key=lambda row: row.id)]
def batch_get_worksheets(self, fetch_items, **kwargs): ''' Get a list of worksheets, all of which satisfy the clause given by kwargs. ''' base_worksheet_uuid = kwargs.pop('base_worksheet_uuid', None) clause = self.make_kwargs_clause(cl_worksheet, kwargs) # Handle base_worksheet_uuid specially if base_worksheet_uuid: clause = and_(clause, cl_worksheet_item.c.subworksheet_uuid == cl_worksheet.c.uuid, cl_worksheet_item.c.worksheet_uuid == base_worksheet_uuid) with self.engine.begin() as connection: worksheet_rows = connection.execute( cl_worksheet.select().distinct().where(clause) ).fetchall() if not worksheet_rows: if base_worksheet_uuid != None: # We didn't find any results restricting to base_worksheet_uuid, # so do a global search return self.batch_get_worksheets(fetch_items, **kwargs) return [] # Fetch the items of all the worksheets if fetch_items: uuids = set(row.uuid for row in worksheet_rows) item_rows = connection.execute(cl_worksheet_item.select().where( cl_worksheet_item.c.worksheet_uuid.in_(uuids) )).fetchall() # Make a dictionary for each worksheet with both its main row and its items. worksheet_values = {row.uuid: dict(row) for row in worksheet_rows} if fetch_items: for value in worksheet_values.itervalues(): value['items'] = [] for item_row in sorted(item_rows, key=item_sort_key): if item_row.worksheet_uuid not in worksheet_values: raise IntegrityError('Got item %s without worksheet' % (item_row,)) worksheet_values[item_row.worksheet_uuid]['items'].append(item_row) return [Worksheet(value) for value in worksheet_values.itervalues()]
def batch_get_worksheets(self, **kwargs): ''' Get a list of worksheets, all of which satisfy the clause given by kwargs. ''' clause = self.make_kwargs_clause(cl_worksheet, kwargs) with self.engine.begin() as connection: worksheet_rows = connection.execute( cl_worksheet.select().where(clause) ).fetchall() if not worksheet_rows: return [] uuids = set(row.uuid for row in worksheet_rows) item_rows = connection.execute(cl_worksheet_item.select().where( cl_worksheet_item.c.worksheet_uuid.in_(uuids) )).fetchall() # Make a dictionary for each worksheet with both its main row and its items. worksheet_values = {row.uuid: dict(row) for row in worksheet_rows} for value in worksheet_values.itervalues(): value['items'] = [] for item_row in sorted(item_rows, key=item_sort_key): if item_row.worksheet_uuid not in worksheet_values: raise IntegrityError('Got item %s without worksheet' % (item_row,)) worksheet_values[item_row.worksheet_uuid]['items'].append(item_row) return [Worksheet(value) for value in worksheet_values.itervalues()]