Ejemplo n.º 1
0
    def send_command(self, request):
        response = CommandResponse()
        response.result = CommandResponse.Result.RESULT_OK

        # Checks for active Google account session

        user = endpoints.get_current_user()
        logging.info("Current user: %s with id: %s" % (user, user.user_id()))

        if user:
            account = AccountSettingsHelper()
            user_id = user.user_id()

            # check if user exists in db
            if account.user_exists(user_id):
                    usr = account.get_user_info(user_id)
                    url = '%s/api/%s?command=%s' % (usr.agent_url, usr.device_mac, request.command)
                    logging.info("Submiting request to:  %s" % url)

                    try:
                        res = urlfetch.fetch(url)
                    except Exception:
                        res = None
                        pass

                    if res:
                        if res.status_code == 200:
                            logging.info("Got response: %s" % res.content)

                            res_json = json.loads(res.content)
                            response.command = CommandRequest.Commands(res_json['command'])

                            if res_json['result'] == 'ok':
                                response.result = CommandResponse.Result.RESULT_OK
                            else:
                                response.result = CommandResponse.Result.RESULT_ERROR
                                response.error_message = res_json['error']

                            if 'data' in res_json:
                                response.data = json.dumps(res_json['data'])
                        else:
                            response.result = CommandResponse.Result.RESULT_ERROR
                            response.error_message = '%s, %s' % (res.status_code, res.content)
                    else:
                        response.result = CommandResponse.Result.RESULT_ERROR
                        response.error_message = 'Cannot connect to server'
            # else if user not in db send error message as response
            else:
                response.result = CommandResponse.Result.RESULT_ERROR
                response.error_message = 'Cannot find agent url information in DB. Check your settings'

        # else if request came from non authorized user
        else:
            response.result = CommandResponse.Result.RESULT_ERROR
            response.error_message = 'Unauthorized'

        logging.info('Sending response: %s' % response)
        return response
Ejemplo n.º 2
0
    def get(self):

        # Checks for active Google account session
        user = users.get_current_user()

        if user:
            # check if we have message in cookies

            cookie_message = self.request.cookies.get("message")
            if not cookie_message:
                info = ""
                error = ""
            else:
                self.response.delete_cookie("message")
                message = json.loads(cookie_message)
                info = message["info"] if "info" in message else ""
                error = message["error"] if "error" in message else ""

            # prepeare default template values
            template_values = {
                'user': user.nickname(),
                'agent_url': "",
                'device_mac': "",
                'info': info,
                'error': error
            }

            account = AccountSettingsHelper()
            user_id = user.user_id()
            logging.info("Current user: %s with id: %s" %
                         (user, user.user_id()))
            # check if user is already in DB, otehrwise add it
            if not account.user_exists(user_id):
                logging.info("User account doesn't exist for id %s" % user_id)
                try:
                    account.add_new_user(user_id)
                except Exception, e:
                    logging.exception("Exception %s happened" % e)
            else:
                # if user exists get values form DB
                usr = account.get_user_info(user_id)
                template_values['agent_url'] = usr.agent_url
                template_values['device_mac'] = usr.device_mac

            logging.info("Generating template using values: %s" %
                         template_values)

            template = JINJA_ENVIRONMENT.get_template(
                os.path.join('templates', 'settings.html'))
            self.response.write(template.render(template_values))
Ejemplo n.º 3
0
    def get(self):

        # Checks for active Google account session
        user = users.get_current_user()

        if user:
            # check if we have message in cookies

            cookie_message = self.request.cookies.get("message")
            if not cookie_message:
                info = ""
                error = ""
            else:
                self.response.delete_cookie("message")
                message = json.loads(cookie_message)
                info = message["info"] if "info" in message else ""
                error = message["error"] if "error" in message else ""

            # prepeare default template values
            template_values = {"user": user.nickname(), "agent_url": "", "device_mac": "", "info": info, "error": error}

            account = AccountSettingsHelper()
            user_id = user.user_id()
            logging.info("Current user: %s with id: %s" % (user, user.user_id()))
            # check if user is already in DB, otehrwise add it
            if not account.user_exists(user_id):
                logging.info("User account doesn't exist for id %s" % user_id)
                try:
                    account.add_new_user(user_id)
                except Exception, e:
                    logging.exception("Exception %s happened" % e)
            else:
                # if user exists get values form DB
                usr = account.get_user_info(user_id)
                template_values["agent_url"] = usr.agent_url
                template_values["device_mac"] = usr.device_mac

            logging.info("Generating template using values: %s" % template_values)

            template = JINJA_ENVIRONMENT.get_template(os.path.join("templates", "settings.html"))
            self.response.write(template.render(template_values))