Ejemplo n.º 1
0
 def __init__(self, file_or_filename):
     try:
         self.workbook = WorkbookJSONReader(file_or_filename)
     except AttributeError:
         raise FixtureUploadError(
             _("Error processing your Excel (.xlsx) file"))
     except Exception:
         raise FixtureUploadError(
             _("Invalid file-format. Please upload a valid xlsx file."))
Ejemplo n.º 2
0
 def __init__(self, file_or_filename):
     try:
         self.workbook = WorkbookJSONReader(file_or_filename)
     except AttributeError:
         # todo: I don't know what would cause this error and it's a bad message
         raise FixtureUploadError([_("Error processing your Excel (.xlsx) file")])
     except InvalidExcelFileException:
         raise FixtureUploadError([FAILURE_MESSAGES['not_excel_file']])
     except HeaderValueError as e:
         raise FixtureUploadError([six.text_type(e)])
     except JSONReaderError as e:
         raise FixtureUploadError([six.text_type(e)])
Ejemplo n.º 3
0
 def get_types_sheet(self):
     try:
         return self.workbook.get_worksheet(title='types')
     except WorksheetNotFound as e:
         raise FixtureUploadError(
             _("Workbook does not contain a sheet called '%(title)s'") %
             {'title': e.title})
Ejemplo n.º 4
0
def validate_fixture_file_format(file_or_filename):
    """
    Does basic validation on the uploaded file. Raises a FixtureUploadError if
    something goes wrong.
    """
    workbook = get_workbook(file_or_filename)
    workbook.get_types_sheet()
    error_messages = _validate_fixture_upload(workbook)
    if error_messages:
        raise FixtureUploadError(error_messages)
Ejemplo n.º 5
0
def do_fixture_upload(domain, file_ref, replace, task=None):
    workbook = get_workbook(file_ref.get_filename())
    try:
        return run_upload(domain, workbook, replace=replace, task=task)
    except WorksheetNotFound as e:
        raise FixtureUploadError(
            _("Workbook does not contain a sheet called '%(title)s'") %
            {'title': e.title})
    except ExcelMalformatException as e:
        raise FixtureUploadError(
            _("Uploaded excel file has following formatting-problems: '%(e)s'")
            % {'e': e})
    except FixtureAPIException as e:
        raise FixtureUploadError(unicode(e))
    except Exception:
        raise FixtureUploadError(
            _("Fixture upload failed for some reason and we have noted this failure. "
              "Please make sure the excel file is correctly formatted and try again."
              ))
Ejemplo n.º 6
0
 def _get_field_properties(prop_key):
     properties = []
     if prop_key in row_dict and 'property' in row_dict[prop_key]:
         properties = row_dict[prop_key]["property"]
         if not isinstance(properties, list):
             error_message = _(
                 FAILURE_MESSAGES["wrong_property_syntax"]).format(
                     prop_key=prop_key, )
             raise FixtureUploadError([error_message])
     return properties
Ejemplo n.º 7
0
 def _get_field_is_indexed(prop_key):
     is_indexed = False
     if prop_key in row_dict and 'is_indexed' in row_dict[prop_key]:
         is_indexed = row_dict[prop_key]["is_indexed"]
         if not isinstance(is_indexed, bool):
             error_message = _(
                 FAILURE_MESSAGES["wrong_index_syntax"]).format(
                     prop_key=prop_key, )
             raise FixtureUploadError([error_message])
     return is_indexed
Ejemplo n.º 8
0
def do_fixture_upload(domain, file_ref, replace, task=None):
    workbook = get_workbook(file_ref.get_filename())
    try:
        return run_upload(domain, workbook, replace=replace, task=task)
    except WorksheetNotFound as e:
        raise FixtureUploadError(
            _("Workbook does not contain a sheet called '%(title)s'")
            % {'title': e.title})
    except ExcelMalformatException as e:
        raise FixtureUploadError(
            _("Uploaded excel file has following formatting-problems: '%(e)s'")
            % {'e': '\n'.join(e.errors)})
    except FixtureAPIException as e:
        raise FixtureUploadError(unicode(e))
    except Exception:
        soft_assert('@'.join(['droberts', 'dimagi.com'])).call(
            False, 'Unknown fixture upload exception',
            {'filename': file_ref.get_filename()}
        )
        raise FixtureUploadError(_("Fixture upload failed for some reason and we have noted this failure. "
                                   "Please make sure the excel file is correctly formatted and try again."))
Ejemplo n.º 9
0
    def get_all_type_sheets(self):
        type_sheets = []
        seen_tags = set()
        for number_of_fixtures, dt in enumerate(self.get_types_sheet()):
            table_definition = _FixtureTableDefinition.from_row(dt)
            if table_definition.table_id in seen_tags:
                raise FixtureUploadError([
                    _(FAILURE_MESSAGES['duplicate_tag'])
                    .format(tag=table_definition.table_id)])

            seen_tags.add(table_definition.table_id)
            type_sheets.append(table_definition)
        return type_sheets
Ejemplo n.º 10
0
 def _get_field_properties(prop_key):
     if prop_key in row_dict:
         try:
             properties = row_dict[prop_key]["property"]
             assert isinstance(properties, list)
         except (KeyError, AssertionError):
             error_message = _(
                 FAILURE_MESSAGES["wrong_property_syntax"]).format(
                     prop_key=prop_key, )
             raise FixtureUploadError([error_message])
         else:
             return properties
     else:
         return []
Ejemplo n.º 11
0
    def from_row(cls, row_dict):
        tag = row_dict.get('table_id') or row_dict.get('tag')
        if tag is None:
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['has_no_column']).format(
                    column_name='table_id')
            ])
        if is_identifier_invalid(tag):
            raise FixtureUploadError(
                [_(FAILURE_MESSAGES['invalid_table_id']).format(tag=tag)])

        field_names = row_dict.get('field')
        item_attributes = row_dict.get('property')

        if field_names is None and item_attributes is None:
            raise FixtureUploadError([
                _(FAILURE_MESSAGES['neither_fields_nor_attributes']).format(
                    tag=tag)
            ])

        field_names = [] if field_names is None else field_names
        item_attributes = [] if item_attributes is None else item_attributes

        def _get_field_properties(prop_key):
            properties = []
            if prop_key in row_dict and 'property' in row_dict[prop_key]:
                properties = row_dict[prop_key]["property"]
                if not isinstance(properties, list):
                    error_message = _(
                        FAILURE_MESSAGES["wrong_property_syntax"]).format(
                            prop_key=prop_key, )
                    raise FixtureUploadError([error_message])
            return properties

        def _get_field_is_indexed(prop_key):
            is_indexed = False
            if prop_key in row_dict and 'is_indexed' in row_dict[prop_key]:
                is_indexed = row_dict[prop_key]["is_indexed"]
                if not isinstance(is_indexed, bool):
                    error_message = _(
                        FAILURE_MESSAGES["wrong_index_syntax"]).format(
                            prop_key=prop_key, )
                    raise FixtureUploadError([error_message])
            return is_indexed

        def is_number(text):
            text = six.text_type(text)
            try:
                float(text)
                return True
            except ValueError:
                return False

        for i, field_name in enumerate(field_names):
            if is_number(field_name):
                message = _(
                    FAILURE_MESSAGES['invalid_field_name_numerical']).format(
                        i=i + 1,
                        val=field_name,
                    )
                raise FixtureUploadError([message])

        fields = [
            FixtureTypeField(
                field_name=field,
                properties=_get_field_properties(
                    'field {count}'.format(count=i + 1)),
                is_indexed=_get_field_is_indexed(
                    'field {count}'.format(count=i + 1)),
            ) for i, field in enumerate(field_names)
        ]

        return cls(
            table_id=tag,
            fields=fields,
            item_attributes=item_attributes,
            is_global=row_dict.get('is_global', False),
            uid=row_dict.get('UID'),
            delete=(row_dict.get(DELETE_HEADER) or '').lower() == 'y',
        )
Ejemplo n.º 12
0
 def get_types_sheet(self):
     try:
         return self.workbook.get_worksheet(title='types')
     except WorksheetNotFound:
         raise FixtureUploadError([FAILURE_MESSAGES['no_types_sheet']])
Ejemplo n.º 13
0
 def __init__(self, file_or_filename):
     try:
         self.workbook = excel_get_workbook(file_or_filename)
     except WorkbookJSONError as e:
         raise FixtureUploadError([str(e)])