Ejemplo n.º 1
0
    def update(self, record=None, recprops=None, constraint=None):
        ''' Update a record in the repository based on identifier '''

        if record is not None:
            identifier = getattr(record,
            self.context.md_core_model['mappings']['pycsw:Identifier'])
            xml = getattr(self.dataset,
            self.context.md_core_model['mappings']['pycsw:XML'])
            anytext = getattr(self.dataset,
            self.context.md_core_model['mappings']['pycsw:AnyText'])

        if recprops is None and constraint is None:  # full update
            LOGGER.debug('full update')
            update_dict = dict([(getattr(self.dataset, key),
            getattr(record, key)) \
            for key in record.__dict__.keys() if key != '_sa_instance_state'])

            try:
                self.session.begin()
                self._get_repo_filter(self.session.query(self.dataset)).filter_by(
                identifier=identifier).update(update_dict, synchronize_session='fetch')
                self.session.commit()
            except Exception as err:
                self.session.rollback()
                raise RuntimeError('ERROR: %s' % str(err.orig))
        else:  # update based on record properties
            LOGGER.debug('property based update')
            try:
                rows = rows2 = 0
                self.session.begin()
                for rpu in recprops:
                    # update queryable column and XML document via XPath
                    if 'xpath' not in rpu['rp']:
                        self.session.rollback()
                        raise RuntimeError('XPath not found for property %s' % rpu['rp']['name'])
                    if 'dbcol' not in rpu['rp']:
                        self.session.rollback()
                        raise RuntimeError('property not found for XPath %s' % rpu['rp']['name'])
                    rows += self._get_repo_filter(self.session.query(self.dataset)).filter(
                        text(constraint['where'])).params(self._create_values(constraint['values'])).update({
                            getattr(self.dataset,
                            rpu['rp']['dbcol']): rpu['value'],
                            'xml': func.update_xpath(str(self.context.namespaces),
                                   getattr(self.dataset,
                                   self.context.md_core_model['mappings']['pycsw:XML']),
                                   str(rpu)),
                        }, synchronize_session='fetch')
                    # then update anytext tokens
                    rows2 += self._get_repo_filter(self.session.query(self.dataset)).filter(
                        text(constraint['where'])).params(self._create_values(constraint['values'])).update({
                            'anytext': func.get_anytext(getattr(
                            self.dataset, self.context.md_core_model['mappings']['pycsw:XML']))
                        }, synchronize_session='fetch')
                self.session.commit()
                return rows
            except Exception as err:
                self.session.rollback()
                raise RuntimeError('ERROR: %s' % str(err.orig))
Ejemplo n.º 2
0
                    if 'dbcol' not in rpu['rp']:
                        self.session.rollback()
                        raise RuntimeError('property not found for XPath %s' % rpu['rp']['name'])
                    rows += self.session.query(self.dataset).filter(
                        text(constraint['where'])).params(self._create_values(constraint['values'])).update({
                            getattr(self.dataset,
                            rpu['rp']['dbcol']): rpu['value'],
                            'xml': func.update_xpath(str(self.context.namespaces),
                                   getattr(self.dataset,
                                   self.context.md_core_model['mappings']['pycsw:XML']),
                                   str(rpu)),
                        }, synchronize_session='fetch')
                    # then update anytext tokens
                    rows2 += self.session.query(self.dataset).filter(
                        text(constraint['where'])).params(self._create_values(constraint['values'])).update({
                            'anytext': func.get_anytext(getattr(
                            self.dataset, self.context.md_core_model['mappings']['pycsw:XML']))
                        }, synchronize_session='fetch')
                self.session.commit()
                return rows
            except Exception, err:
                self.session.rollback()
                raise RuntimeError('ERROR: %s' % str(err.orig))

    def delete(self, constraint):
        ''' Delete a record from the repository '''

        try:
            self.session.begin()
            rows = self.session.query(self.dataset).filter(
            text(constraint['where'])).params(self._create_values(constraint['values']))
Ejemplo n.º 3
0
                                    getattr(
                                        self.dataset,
                                        self.context.md_core_model['mappings']
                                        ['pycsw:XML']), str(rpu)),
                            },
                            synchronize_session='fetch')
                    # then update anytext tokens
                    rows2 += self._get_repo_filter(
                        self.session.query(self.dataset)
                    ).filter(text(constraint['where'])).params(
                        self._create_values(constraint['values'])).update(
                            {
                                'anytext':
                                func.get_anytext(
                                    getattr(
                                        self.dataset,
                                        self.context.md_core_model['mappings']
                                        ['pycsw:XML']))
                            },
                            synchronize_session='fetch')
                self.session.commit()
                return rows
            except Exception, err:
                self.session.rollback()
                raise RuntimeError('ERROR: %s' % str(err.orig))

    def delete(self, constraint):
        ''' Delete a record from the repository '''

        try:
            self.session.begin()
Ejemplo n.º 4
0
                                    getattr(self.dataset, self.context.md_core_model["mappings"]["pycsw:XML"]),
                                    str(rpu),
                                ),
                            },
                            synchronize_session="fetch",
                        )
                    )
                    # then update anytext tokens
                    rows2 += (
                        self.session.query(self.dataset)
                        .filter(text(constraint["where"]))
                        .params(self._create_values(constraint["values"]))
                        .update(
                            {
                                "anytext": func.get_anytext(
                                    getattr(self.dataset, self.context.md_core_model["mappings"]["pycsw:XML"])
                                )
                            },
                            synchronize_session="fetch",
                        )
                    )
                self.session.commit()
                return rows
            except Exception, err:
                self.session.rollback()
                raise RuntimeError("ERROR: %s" % str(err))

    def delete(self, constraint):
        """ Delete a record from the repository """

        try: