{"body":"# -*- encoding: utf-8 -*-\r\n# Copyright (c) 2016 b<>com\r\n#\r\n# Licensed under the Apache License, Version 2.0 (the \"License\");\r\n# you may not use this file except in compliance with the License.\r\n# You may obtain a copy of the License at\r\n#\r\n# http://www.apache.org/licenses/LICENSE-2.0\r\n#\r\n# Unless required by applicable law or agreed to in writing, software\r\n# distributed under the License is distributed on an \"AS IS\" BASIS,\r\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\r\n# implied.\r\n# See the License for the specific language governing permissions and\r\n# limitations under the License.\r\n\r\nimport logging\r\n\r\nfrom django.core import urlresolvers\r\nfrom django import shortcuts\r\nfrom django.template.defaultfilters import title  # noqa\r\nfrom django.utils.translation import pgettext_lazy\r\nfrom django.utils.translation import ugettext_lazy as _\r\nfrom django.utils.translation import ungettext_lazy\r\nimport horizon.exceptions\r\nimport horizon.messages\r\nimport horizon.tables\r\nfrom horizon.utils import filters\r\n\r\nfrom watcher_dashboard.api import watcher\r\n\r\nLOG = logging.getLogger(__name__)\r\n\r\nAUDIT_STATE_DISPLAY_CHOICES = (\r\n    (\"NO STATE\", pgettext_lazy(\"State of an audit\", u\"No State\")),\r\n    (\"ONGOING\", pgettext_lazy(\"State of an audit\", u\"On Going\")),\r\n    (\"SUCCEEDED\", pgettext_lazy(\"State of an audit\", u\"Succeeeded\")),\r\n    (\"SUBMITTED\", pgettext_lazy(\"State of an audit\", u\"Submitted\")),\r\n    (\"FAILED\", pgettext_lazy(\"State of an audit\", u\"Failed\")),\r\n    (\"DELETED\", pgettext_lazy(\"State of an audit\", u\"Deleted\")),\r\n    (\"PENDING\", pgettext_lazy(\"State of an audit\", u\"Pending\")),\r\n)\r\n\r\n\r\nclass AuditsFilterAction(horizon.tables.FilterAction):\r\n    # server = choices query = text\r\n    filter_type = \"server\"\r\n    filter_choices = (\r\n        ('audit_template', _(\"Audit Template =\"), True),\r\n    )\r\n\r\n\r\nclass CreateAudit(horizon.tables.LinkAction):\r\n    name = \"launch_audit\"\r\n    verbose_name = _(\"Launch Audit\")\r\n    url = \"horizon:admin:audits:create\"\r\n    classes = (\"ajax-modal\", \"btn-launch\")\r\n    # policy_rules = ((\"compute\", \"compute:create\"),)\r\n\r\n\r\nclass GoToActionPlan(horizon.tables.Action):\r\n    name = \"go_to_action_plan\"\r\n    verbose_name = _(\"Go to Action Plan\")\r\n    url = \"horizon:admin:action_plans:detail\"\r\n\r\n    def allowed(self, request, audit):\r\n        return audit or audit.state in (\"SUCCEEEDED\", )\r\n\r\n    def single(self, table, request, audit_id):\r\n        try:\r\n            action_plans = watcher.ActionPlan.list(\r\n                request,\r\n                audit_filter=audit_id)\r\n        except Exception:\r\n            horizon.exceptions.handle(\r\n                request,\r\n                _(\"Unable to retrieve action_plan information.\"))\r\n            return \"javascript:void(0);\"\r\n\r\n        return shortcuts.redirect(urlresolvers.reverse(\r\n            self.url,\r\n            args=[action_plans[0].uuid]))\r\n\r\n\r\nclass GoToAuditTemplate(horizon.tables.Action):\r\n    name = \"go_to_audit_template\"\r\n    verbose_name = _(\"Go to Audit Template\")\r\n    url = \"horizon:admin:audit_templates:detail\"\r\n    # classes = (\"ajax-modal\", \"btn-launch\")\r\n    # icon = \"send\"\r\n\r\n    def allowed(self, request, audit):\r\n        return audit or audit.state in (\"SUCCEEEDED\", )\r\n\r\n    def single(self, table, request, audit_id):\r\n        try:\r\n            audit = watcher.Audit.get(\r\n                request, audit_id=audit_id)\r\n        except Exception:\r\n            horizon.exceptions.handle(\r\n                request,\r\n                _(\"Unable to retrieve action_plan information.\"))\r\n            return \"javascript:void(0);\"\r\n\r\n        return shortcuts.redirect(urlresolvers.reverse(\r\n            self.url,\r\n            args=[audit.audit_template_uuid]))\r\n\r\n\r\nclass DeleteAudits(horizon.tables.DeleteAction):\r\n    verbose_name = _(\"Delete Audits\")\r\n\r\n    @staticmethod\r\n    def action_present(count):\r\n        return ungettext_lazy(\r\n            \"Delete Audit\",\r\n            \"Delete Audits\",\r\n            count\r\n        )\r\n\r\n    @staticmethod\r\n    def action_past(count):\r\n        return ungettext_lazy(\r\n            \"Deleted Audit\",\r\n            \"Deleted Audits\",\r\n            count\r\n        )\r\n\r\n    def delete(self, request, obj_id):\r\n        watcher.Audit.delete(request, obj_id)\r\n\r\n\r\nclass AuditsTable(horizon.tables.DataTable):\r\n    name = horizon.tables.Column(\r\n        'uuid',\r\n        verbose_name=_(\"UUID\"),\r\n        link=\"horizon:admin:audits:detail\")\r\n    audit_template = horizon.tables.Column(\r\n        'audit_template_name',\r\n        verbose_name=_('Audit Template'))\r\n    status = horizon.tables.Column(\r\n        'state',\r\n        verbose_name=_('State'),\r\n        status=True,\r\n        status_choices=AUDIT_STATE_DISPLAY_CHOICES)\r\n\r\n    def get_object_id(self, datum):\r\n        return datum.uuid\r\n\r\n    class Meta(object):\r\n        name = \"audits\"\r\n        verbose_name = _(\"Audits\")\r\n        launch_actions = (CreateAudit,)\r\n        table_actions = launch_actions + (\r\n            # CancelAudit,\r\n            AuditsFilterAction,\r\n            # ArchiveAudits,\r\n        )\r\n        row_actions = (\r\n            GoToActionPlan,\r\n            GoToAuditTemplate,\r\n            # CreateAudits,\r\n            # ArchiveAudits,\r\n            # CreateAudits,\r\n            DeleteAudits,\r\n        )\r\n\r\n\r\nclass RelatedAuditsTable(horizon.tables.DataTable):\r\n    name = horizon.tables.Column(\r\n        'uuid',\r\n        verbose_name=_(\"UUID\"),\r\n        link=\"horizon:admin:audits:detail\")\r\n    audit_template = horizon.tables.Column(\r\n        'audit_template_name',\r\n        verbose_name=_('Audit Template'),\r\n        filters=(title, filters.replace_underscores))\r\n    status = horizon.tables.Column(\r\n        'state',\r\n        verbose_name=_('State'),\r\n        status=True,\r\n        status_choices=AUDIT_STATE_DISPLAY_CHOICES)\r\n\r\n    def get_object_id(self, datum):\r\n        return datum.uuid\r\n\r\n    class Meta(object):\r\n        name = \"audits\"\r\n        verbose_name = _(\"Related audits\")\r\n        hidden_title = False\r\n","name":"","extension":"txt","url":"https://www.irccloud.com/pastebin/TFnNnHJQ","modified":1467989530,"id":"TFnNnHJQ","size":5971,"lines":190,"own_paste":false,"theme":"","date":1467989530}