Esempio n. 1
0
 def Model (self):
     if not hasattr(self, '_Model'):
         self._Model = get_model(self._module, self._model_name)
     return self._Model
Esempio n. 2
0
    def handle (self, *args, **options):
        session.bind.echo = options['debug']
        filepath = options['filepath']

        model_info = {}

        with open(filepath, 'r') as data_file:
            content = ''.join(line.split('//')[0] for line in data_file)
            full_data = json.loads(content, object_pairs_hook = OrderedDict)

        for data_block in full_data:
            Model = get_model(data_block['application'], data_block['Model'])
            unique_indexes = {}

            # Prepare indeces of unique-constraints.
            for table in Model.__mapper__.tables:
                for constraint in table.constraints:
                    if isinstance(constraint, UniqueConstraint):
                        unique_indexes[constraint] = set()

            for datum in data_block['data']:
                datum = prepare(session, Model, datum)
                update_target = None

                for constraint, index in unique_indexes.items():
                    # Look for item in index.
                    key = key_from_constraint(constraint, datum)
                    if key in index:
                        self.stderr.write("ERROR: Second import of '{}' " +
                            "record with unique constraint:".format(Model))
                        for column, value in zip(constraint.columns, key):
                            self.stderr.write("       {} = {}".format(
                                column, value))
                        return

                    # Look for item in database.
                    target = session.query(Model).filter(
                        *filter_from_constraint(constraint, datum)).scalar()
                    if update_target and update_target != target:
                        self.stderr.write('ERROR: Second unique-index match does not yield the same model.')
                        self.stderr.write("       Model: {}".format(Model))
                        self.stderr.write("       constraint: {}".format(constraint))
                        self.stderr.write("       data: {}".format(datum))
                        return
                    update_target = target

                if update_target:
                    updated = False
                    for field, value in datum.items():
                        if getattr(update_target, field) != value:
                            updated = True
                            setattr(update_target, field, value)
                    if updated:
                        self.stdout.write("updating {}".format(update_target))
                else:
                    model = Model(**datum)
                    session.add(model)
                    self.stdout.write("adding {}".format(model))

            session.flush()

        session.commit()