def table(self): tables = [] for resource in self.return_type.resources: resource_name = resource.get('name') schema = Schema(resource.get('schema')) headers = [] for f in schema.fields: # print(type(f.name)) header = {"title": f.name, "required": f.required} if f.is_species: header["species"] = f.species_type headers.append(header) table = { 'name': resource_name, 'title': resource.get('title', resource.get('name')), 'headers': headers, 'data': None } try: return_table = self.returntable_set.get(name=resource_name) rows = [ return_row.data for return_row in return_table.returnrow_set.all() ] validated_rows = schema.rows_validator(rows) table['data'] = validated_rows except ReturnTable.DoesNotExist: result = {} results = [] for field_name in schema.fields: result[field_name.name] = {'value': None} results.append(result) table['data'] = results tables.append(table) return tables
def _is_post_data_valid(ret, tables_info, post_data): print(type(tables_info)) print(tables_info) print("from utils===================") # print(ast.literal_eval(table)) table_rows = _get_table_rows_from_post(tables_info, post_data) print("=======printing table rows=====") print(table_rows) if len(table_rows) == 0: return False schema = Schema(ret.return_type.get_schema_by_name(tables_info)) print("===========Schema Info========") print(schema.is_all_valid(table_rows)) if not schema.is_all_valid(table_rows): return False return True
def table(self): tables = [] for resource in self.return_type.resources: resource_name = resource.get('name') schema = Schema(resource.get('schema')) headers = [] for f in schema.fields: # print(type(f.name)) header = { "title": f.name, "required": f.required } if f.is_species: header["species"] = f.species_type headers.append(header) table = { 'name': resource_name, 'title': resource.get('title', resource.get('name')), 'headers': headers, 'data': None } try: return_table = self.returntable_set.get(name=resource_name) rows = [return_row.data for return_row in return_table.returnrow_set.all()] validated_rows = schema.rows_validator(rows) table['data'] = validated_rows except ReturnTable.DoesNotExist: result = {} results=[] for field_name in schema.fields: result[field_name.name] = { 'value': None } results.append(result) table['data']=results tables.append(table) return tables
class ReturnDataSheet(SpreadSheet): ''' Specialised utility object for Return Data Spreadsheet. ''' RETURN_DATA = 'return-data' def __init__(self, _ret, _filename): super(ReturnDataSheet, self).__init__(_ret, _filename) self.schema = Schema( self.ret.return_type.get_schema_by_name( self.ret.return_type.resources[0]['name'])) def is_valid(self): ''' Validates against schema. :return: Boolean ''' table_rows = self.get_table_rows() if len(table_rows) == 0: return False for row in table_rows: self.errors.append(self.schema.get_error_fields(row)) return self.errors[1].__len__() == 0 def create_return_data(self): ''' Method to persist Return record. :return: ''' if self.rows_list: return_table = ReturnTable.objects.get_or_create( name=self.RETURN_DATA, ret=self.ret)[0] # delete any existing rows as they will all be recreated return_table.returnrow_set.all().delete() return_rows = [ ReturnRow(return_table=return_table, data=row) for row in self.rows_list ] ReturnRow.objects.bulk_create(return_rows) return True
class Regulation15Sheet(SpreadSheet): """ Specialised utility object for Regulation 15 Spreadsheet. """ REGULATION_15 = 'regulation-15' def __init__(self, _ret, _filename): super(Regulation15Sheet, self).__init__(_ret, _filename) self.schema = Schema( self.ret.return_type.get_schema_by_name(self.REGULATION_15)) def is_valid(self): """ Validates against schema. :return: Boolean """ table_rows = self.get_table_rows() if len(table_rows) == 0: return False for row in table_rows: self.errors.append(self.schema.get_error_fields(row)) return self.errors[1].__len__() == 0 def create_return_data(self): """ Method to persist Return record. :return: """ if self.rows_list: return_table = ReturnTable.objects.get_or_create( name=self.REGULATION_15, ret=self.ret)[0] # delete any existing rows as they will all be recreated return_table.returnrow_set.all().delete() return_rows = [ ReturnRow(return_table=return_table, data=row) for row in self.rows_list ] ReturnRow.objects.bulk_create(return_rows) return True
def __init__(self, _ret, _filename): super(ReturnDataSheet, self).__init__(_ret, _filename) self.schema = Schema( self.ret.return_type.get_schema_by_name( self.ret.return_type.resources[0]['name']))
def __init__(self, _ret, _filename): super(Regulation15Sheet, self).__init__(_ret, _filename) self.schema = Schema( self.ret.return_type.get_schema_by_name(self.REGULATION_15))