Ejemplo n.º 1
0
    def wrapper(func, self, *args, **kwargs):
        """Decorator Wrapper function"""
        try:
            return func(self, *args, **kwargs)
        except error_types as e:
            try:
                message = unicode_text(e)
            except:
                message = 'Unknown Error'

            if request.response_type == 'application/json':
                response.status_code = 400
                return dict(message=message)

            if isinstance(e, sqla_errors):
                #if the error is a sqlalchemy error suppose we need to rollback the transaction
                #so that the error handler can perform queries.
                if transaction is not None and config.get('tgext.crud.abort_transactions', False):
                    #This is in case we need to support multiple databases or two phase commit.
                    transaction.abort()
                else:
                    self.session.rollback()

            flash(message, status="status_alert")

            # Get the instance that matches the error handler.
            # This is not a great solution, but it's what we've got for now.
            func = getattr(self, error_handler)
            raise HTTPOk(body=self._call(func, params=kwargs, remainder=list(args)))
Ejemplo n.º 2
0
    def get_all(self, *args, **kw):
        """Return all records.
           Pagination is done by offset/limit in the filler method.
           Returns an HTML page with the records if not json.
        """
        if self.pagination:
            paginator = request.paginators['value_list']
            paginator.paginate_items_per_page = self.pagination['items_per_page']
        else:
            paginator = request.paginators['value_list']
            paginator.paginate_items_per_page = -1
            paginator.paginate_page = 0

        if tg.request.response_type == 'application/json':
            adapt_params_for_pagination(kw, self.pagination_enabled)
            try:
                count, values = self.table_filler._do_get_provider_count_and_objs(**kw)
            except Exception as e:
                log.exception('Failed to retrieve table data')
                abort(400, detail=unicode_text(e))
            values = self._dictify(values, length=count)
            if self.pagination_enabled:
                values = SmartPaginationCollection(values, count)
            return dict(value_list=values)

        if not getattr(self.table.__class__, '__retrieves_own_value__', False):
            kw.pop('substring_filters', None)
            if self.substring_filters is True:
                substring_filters = list(set(kw.keys()) - set(['limit', 'offset', 'order_by', 'desc']))
            else:
                substring_filters = self.substring_filters

            adapt_params_for_pagination(kw, self.pagination_enabled)
            try:
                values = self.table_filler.get_value(substring_filters=substring_filters, **kw)
            except Exception as e:
                log.exception('Failed to retrieve table data')
                flash('Unable to retrieve data (Filter "%s": %s)' % (request.query_string, e), 'warn')
                # Reset all variables to sane defaults
                kw = {}
                values = []
                self.table_filler.__count__ = 0
            if self.pagination_enabled:
                values = SmartPaginationCollection(values, self.table_filler.__count__)
        else:
            values = []

        tmpl_context.widget = self.table
        search_fields = self._get_search_fields(kw)
        current_search = self._get_current_search(search_fields)
        return dict(model=self.model.__name__, value_list=values,
                    mount_point=self._mount_point(),
                    headers=search_fields,  # Just for backwards compatibility
                    search_fields=search_fields, current_search=current_search)
Ejemplo n.º 3
0
    def wrapper(func, self, *args, **kwargs):
        """Decorator Wrapper function"""
        try:
            value = func(self, *args, **kwargs)
        except error_types as e:
            try:
                message = unicode_text(e)
            except:
                message = None

            if message:
                if request.response_type == 'application/json':
                    response.status_code = 400
                    return dict(message=message)

                flash(message, status="status_alert")
                # have to get the instance that matches the error handler.  This is not a great solution, but it's 
                # what we've got for now.
                if isinstance(error_handler, string_type):
                    name = error_handler
                else:
                    name = error_handler.__name__
                func = getattr(self, name)
                remainder = []
                remainder.extend(args)

                if isinstance(e, sqla_errors):
                    #if the error is a sqlalchemy error suppose we need to rollback the transaction
                    #so that the error handler can perform queries.
                    if transaction is not None and config.get('tgext.crud.abort_transactions', False):
                        #This is in case we need to support multiple databases or two phase commit.
                        transaction.abort()
                    else:
                        self.session.rollback()

                return self._call(func, params=kwargs, remainder=remainder)

        return value