@@ -0,0 +1,2497 @@
+from flask import flash, session%0Afrom flask.views import View%0Afrom kqueen_ui.api import get_kqueen_client, get_service_client%0A%0Aimport logging%0A%0Alogger = logging.getLogger(__name__)%0A%0A%0Aclass KQueenView(View):%0A    %22%22%22%0A    KQueen UI base view with methods to handle backend API calls.%0A    %22%22%22%0A    def _get_kqueen_client(self):%0A        token = session.get('user', %7B%7D).get('token', None)%0A        if token:%0A            return get_kqueen_client(token=token)%0A        return None%0A%0A    def _get_kqueen_service_client(self):%0A        return get_service_client()%0A%0A    def _handle_response(self, response):%0A        if response:%0A            if response.status == -1:%0A                user_msg = 'Backend is unavailable at this time, please try again later.'%0A                flash(user_msg, 'danger')%0A                return None%0A            elif response.status == 401:%0A                fmt_action = str(action).lower()%0A                fmt_resource = str(resource).lower()%0A                user_msg = 'You are not authorized to %7B%7D %7B%7D.'.format(fmt_action, fmt_resource)%0A                flash(user_msg, 'warning')%0A                return None%0A            elif response.status %3E= 400:%0A                user_msg = 'Exception occured while contacting backend, please try again later.'%0A                flash(user_msg, 'danger')%0A            return response.data%0A%0A    def handle(self):%0A        %22%22%22%0A        Override this method with view function%0A        %22%22%22%0A        raise NotImplementedError%0A%0A    def kqueen_request(self, resource, action, fnargs=(), fnkwargs=%7B%7D, service=False):%0A        client = self._get_kqueen_service_client() if service else self._get_kqueen_client()%0A        if not client:%0A            return None%0A        try:%0A            manager = getattr(client, resource)%0A            response = getattr(manager, action)(*fnargs, **fnkwargs)%0A        except AttributeError:%0A            msg = 'Unknown API method reference %22%7B%7D.%7B%7D%22'.format(resource, action)%0A            self.logger('error', msg)%0A            return None%0A        except TypeError:%0A            msg = 'Invalid API method arguments; args: %7B%7D, kwargs: %7B%7D'.format(str(fnargs), str(fnkwargs))%0A            self.logger('error', msg)%0A            return None%0A        return self._handle_response(response)%0A%0A    def logger(self, severity, message):%0A        view = self.__class__.__name__%0A        msg = '%7B%7D view: %7B%7D'.format(view, message)%0A        logger_fn = getattr(logger, severity)%0A        logger_fn(msg)%0A%0A    def dispatch_request(self):%0A        return self.handle()%0A

Add base class for KQueen UI views