<commit_before># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext_lazy as _
import logging
from django_services import admin
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import patterns, url
from django.contrib import messages
from django.utils.html import format_html, escape
from ..service.database import DatabaseService
from ..forms import DatabaseForm, CloneDatabaseForm
from ..models import Database
from account.models import Team
from drivers import DatabaseAlreadyExists
from logical.templatetags import capacity
from system.models import Configuration
from dbaas import constants
from django.db import router
from django.utils.encoding import force_text
from django.core.exceptions import PermissionDenied
from django.contrib.admin.util import get_deleted_objects, model_ngettext
from django.contrib.admin import helpers
from django.template.response import TemplateResponse
from notification.tasks import destroy_database
from notification.tasks import create_database
from util import get_credentials_for
from dbaas_credentials.models import CredentialType
LOG = logging.getLogger(__name__)


class DatabaseAdmin(admin.DjangoServicesAdmin):
	"""
	the form used by this view is returned by the method get_form
	"""

	database_add_perm_message = _(
		"You must be set to at least one team to add a database, and the service administrator has been notified about this.")
	perm_manage_quarantine_database = constants.PERM_MANAGE_QUARANTINE_DATABASE
	perm_add_database_infra = constants.PERM_ADD_DATABASE_INFRA

	service_class = DatabaseService
	search_fields = ("name", "databaseinfra__name")
	list_display_basic = ["name_html", "engine_type", "environment", "plan", "friendly_status", "clone_html" ,
	                      "get_capacity_html"]
	list_display_advanced = list_display_basic + ["metrics_html", "quarantine_dt_format"]
	list_filter_basic = ["project", "databaseinfra__environment", "databaseinfra__engine", "databaseinfra__plan"]
	list_filter_advanced = list_filter_basic + ["databaseinfra", "is_in_quarantine", "team"]
	add_form_template = "logical/database/database_add_form.html"
	change_form_template = "logical/database/database_change_form.html"
	delete_button_name = "Delete"
	fieldsets_add = (
		(None, {
			'fields': ('name', 'description', 'project', 'engine', 'environment', 'team', 'plan', 'is_in_quarantine')
		}
		),
	)

	fieldsets_change_basic = (
		(None, {
			'fields': ['name', 'description', 'project', 'team', ]
		}
		),
	)

	fieldsets_change_advanced = (
		(None, {
			'fields': fieldsets_change_basic[0][1]['fields'] + ["is_in_quarantine"]
		}
		),
	)
	# actions = ['delete_mode']

	def quarantine_dt_format(self, database):
		return database.quarantine_dt or ""

	quarantine_dt_format.short_description = "Quarantine since"
	quarantine_dt_format.admin_order_field = 'quarantine_dt'

	def environment(self, database):
		return database.environment

	environment.admin_order_field = 'name'

	def plan(self, database):
		return database.plan

	plan.admin_order_field = 'name'

	def friendly_status(self, database):

		html_ok = '<span class="label label-success">Alive</span>'
		html_nook = '<span class="label label-important">Dead</span>'

		if database.status == Database.ALIVE:
			return format_html(html_ok)
		else:
			return format_html(html_nook)

	friendly_status.short_description = "Status"

	def clone_html(self, database):
		html = []

		if database.is_in_quarantine or database.status != database.ALIVE:
			html.append("N/A")
		else:
			html.append("<a class='btn btn-info' href='%s'><i class='icon-file icon-white'></i></a>" % reverse(
				'admin:database_clone', args=(database.id,)))

		return format_html("".join(html))

	clone_html.short_description = "Clone"

	def metrics_html(self, database):
		html = []
		html.append("<a class='btn btn-info' href='%s'><i class='icon-list-alt icon-white'></i></a>" % reverse(
			'admin:database_metrics', args=(database.id,)))

		return format_html("".join(html))

	metrics_html.short_description = "Metrics"<commit_msg>Change metrics_html to not show metrics on PREPROVISIONED databases<commit_after># -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext_lazy as _
import logging
from django_services import admin
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.conf.urls.defaults import patterns, url
from django.contrib import messages
from django.utils.html import format_html, escape
from ..service.database import DatabaseService
from ..forms import DatabaseForm, CloneDatabaseForm
from ..models import Database
from account.models import Team
from drivers import DatabaseAlreadyExists
from logical.templatetags import capacity
from system.models import Configuration
from dbaas import constants
from django.db import router
from django.utils.encoding import force_text
from django.core.exceptions import PermissionDenied
from django.contrib.admin.util import get_deleted_objects, model_ngettext
from django.contrib.admin import helpers
from django.template.response import TemplateResponse
from notification.tasks import destroy_database
from notification.tasks import create_database
from util import get_credentials_for
from dbaas_credentials.models import CredentialType
LOG = logging.getLogger(__name__)


class DatabaseAdmin(admin.DjangoServicesAdmin):
	"""
	the form used by this view is returned by the method get_form
	"""

	database_add_perm_message = _(
		"You must be set to at least one team to add a database, and the service administrator has been notified about this.")
	perm_manage_quarantine_database = constants.PERM_MANAGE_QUARANTINE_DATABASE
	perm_add_database_infra = constants.PERM_ADD_DATABASE_INFRA

	service_class = DatabaseService
	search_fields = ("name", "databaseinfra__name")
	list_display_basic = ["name_html", "engine_type", "environment", "plan", "friendly_status", "clone_html" ,
	                      "get_capacity_html", "metrics_html"]
	list_display_advanced = list_display_basic + ["quarantine_dt_format"]
	list_filter_basic = ["project", "databaseinfra__environment", "databaseinfra__engine", "databaseinfra__plan"]
	list_filter_advanced = list_filter_basic + ["databaseinfra", "is_in_quarantine", "team"]
	add_form_template = "logical/database/database_add_form.html"
	change_form_template = "logical/database/database_change_form.html"
	delete_button_name = "Delete"
	fieldsets_add = (
		(None, {
			'fields': ('name', 'description', 'project', 'engine', 'environment', 'team', 'plan', 'is_in_quarantine')
		}
		),
	)

	fieldsets_change_basic = (
		(None, {
			'fields': ['name', 'description', 'project', 'team', ]
		}
		),
	)

	fieldsets_change_advanced = (
		(None, {
			'fields': fieldsets_change_basic[0][1]['fields'] + ["is_in_quarantine"]
		}
		),
	)
	# actions = ['delete_mode']

	def quarantine_dt_format(self, database):
		return database.quarantine_dt or ""

	quarantine_dt_format.short_description = "Quarantine since"
	quarantine_dt_format.admin_order_field = 'quarantine_dt'

	def environment(self, database):
		return database.environment

	environment.admin_order_field = 'name'

	def plan(self, database):
		return database.plan

	plan.admin_order_field = 'name'

	def friendly_status(self, database):

		html_ok = '<span class="label label-success">Alive</span>'
		html_nook = '<span class="label label-important">Dead</span>'

		if database.status == Database.ALIVE:
			return format_html(html_ok)
		else:
			return format_html(html_nook)

	friendly_status.short_description = "Status"

	def clone_html(self, database):
		html = []

		if database.is_in_quarantine or database.status != database.ALIVE:
			html.append("N/A")
		else:
			html.append("<a class='btn btn-info' href='%s'><i class='icon-file icon-white'></i></a>" % reverse(
				'admin:database_clone', args=(database.id,)))

		return format_html("".join(html))

	clone_html.short_description = "Clone"

	def metrics_html(self, database):
		html = []
		from physical.models import Plan
		if database.databaseinfra.plan.provider == Plan.PREPROVISIONED:
			html.append("N/A")
		else:
			html.append("<a class='btn btn-info' href='%s'><i class='icon-list-alt icon-white'></i></a>" % reverse(
				'admin:database_metrics', args=(database.id,)))

		return format_html("".join(html))

	metrics_html.short_description = "Metrics"
{'fraction_non_alphanumeric': 0.10749856897538637, 'fraction_numerical': 0.0006868918145392101, 'mean_word_length': 5.819672131147541, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '497652', 'n_tokens_mistral': 2785, 'n_tokens_neox': 2614, 'n_words': 640}