<commit_before># -*- coding: utf-8 -*-
import logging
from django_services import admin
from django.utils.html import format_html
from django.core.urlresolvers import reverse
from django.conf.urls import url, patterns
from django.http import HttpResponseRedirect
from dateutil import tz
from notification.models import TaskHistory
from ..models import DatabaseRegionMigration
from ..models import DatabaseRegionMigrationDetail
from ..service.databaseregionmigration import DatabaseRegionMigrationService
from ..tasks import execute_database_region_migration
from ..tasks import execute_database_region_migration_undo
from django.shortcuts import render_to_response
from django.template import RequestContext
from ..forms import DatabaseRegionMigrationDetailForm

LOG = logging.getLogger(__name__)


class DatabaseRegionMigrationAdmin(admin.DjangoServicesAdmin):
    model = DatabaseRegionMigration
    list_display = ('database', 'steps_information', 'database_engine',
                    'status', 'description', 'schedule_next_step_html',
                    'user_friendly_warning', 'schedule_rollback_html')

    actions = None
    service_class = DatabaseRegionMigrationService
    list_display_links = ()
    template = 'region_migration/databaseregionmigration/schedule_next_step.html'

    def __init__(self, *args, **kwargs):
        super(DatabaseRegionMigrationAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )

    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request, obj=None):
        return False

    def get_step_id(self, databaseregionmigration,):
        id = databaseregionmigration.id
        current_step = databaseregionmigration.current_step
        last_step = len(databaseregionmigration.get_steps()) - 1

        is_migration_finished = current_step == last_step

        waiting_status = DatabaseRegionMigrationDetail.WAITING
        running_status = DatabaseRegionMigrationDetail.RUNNING

        migration_running_or_waiting = DatabaseRegionMigrationDetail.objects.\
            filter(database_region_migration=databaseregionmigration,
                   status__in=[waiting_status, running_status])

        if is_migration_finished or migration_running_or_waiting:
            return ''

        return id

    def schedule_next_step_html(self, databaseregionmigration):
        id = self.get_step_id(databaseregionmigration)
        html = ''

        if id:
            html = "<a class='btn btn-info' href='{}/schedulenextstep/'><i\
                    class='icon-chevron-right icon-white'></i></a>".format(id)

        return format_html(html)

    schedule_next_step_html.short_description = "Schedule Next Macro Step"

    def schedule_rollback_html(self, databaseregionmigration):
        id = self.get_step_id(databaseregionmigration)
        html = ''

        if id and databaseregionmigration.current_step > 0:
            html = "<a class='btn btn-info' href='{}/schedulenextstep/?rollback=true'><i\
                    class='icon-chevron-left icon-white'></i></a>".format(id)

        return format_html(html)

    schedule_rollback_html.short_description = "Schedule Rollback"

    def database_engine(self, databaseregionmigration):
        return databaseregionmigration.database.engine_type

    database_engine.short_description = "Engine"

    def user_friendly_warning(self, databaseregionmigration):
        warning_message = databaseregionmigration.warning

        if warning_message:
            html = '<span class="label label-warning"><font size=3.5>\
                    {}</font></span>'.format(warning_message)
            return format_html(html)

        return warning_message

    user_friendly_warning.short_description = "Warning"

    def steps_information(self, databaseregionmigration):
        current_step = databaseregionmigration.current_step + 1
        steps_len = str(len(databaseregionmigration.get_steps()) - 1)
        html = "<a href='../databaseregionmigrationdetail/?database_region_migration={}'>{}</a>"
        information = '{} of {}'.format(current_step, steps_len)

        if current_step > int(steps_len):
            html = '<center>-</center>'
        else:
            html = html.format(databaseregionmigration.id, information)

        return format_html(html)<commit_msg>Improve database region migration serach and filters<commit_after># -*- coding: utf-8 -*-
import logging
from django_services import admin
from django.utils.html import format_html
from django.core.urlresolvers import reverse
from django.conf.urls import url, patterns
from django.http import HttpResponseRedirect
from dateutil import tz
from notification.models import TaskHistory
from ..models import DatabaseRegionMigration
from ..models import DatabaseRegionMigrationDetail
from ..service.databaseregionmigration import DatabaseRegionMigrationService
from ..tasks import execute_database_region_migration
from ..tasks import execute_database_region_migration_undo
from django.shortcuts import render_to_response
from django.template import RequestContext
from ..forms import DatabaseRegionMigrationDetailForm

LOG = logging.getLogger(__name__)


class DatabaseRegionMigrationAdmin(admin.DjangoServicesAdmin):
    search_fields = ("database__name", "database__team__name",
                     "database__databaseinfra__name")

    list_filter = ["database__project", "database__environment",
                   "database__databaseinfra__engine", "database__team",
                   "database__databaseinfra__plan",
                   "database__databaseinfra__engine__engine_type"]

    list_display = ('database', 'steps_information', 'database_engine',
                    'get_database_team', 'status', 'description',
                    'schedule_next_step_html', 'user_friendly_warning',
                    'schedule_rollback_html',)

    model = DatabaseRegionMigration
    actions = None
    service_class = DatabaseRegionMigrationService
    list_display_links = ()
    template = 'region_migration/databaseregionmigration/schedule_next_step.html'

    def __init__(self, *args, **kwargs):
        super(DatabaseRegionMigrationAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None, )

    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request, obj=None):
        return False

    def get_step_id(self, databaseregionmigration,):
        id = databaseregionmigration.id
        current_step = databaseregionmigration.current_step
        last_step = len(databaseregionmigration.get_steps()) - 1

        is_migration_finished = current_step == last_step

        waiting_status = DatabaseRegionMigrationDetail.WAITING
        running_status = DatabaseRegionMigrationDetail.RUNNING

        migration_running_or_waiting = DatabaseRegionMigrationDetail.objects.\
            filter(database_region_migration=databaseregionmigration,
                   status__in=[waiting_status, running_status])

        if is_migration_finished or migration_running_or_waiting:
            return ''

        return id

    def schedule_next_step_html(self, databaseregionmigration):
        id = self.get_step_id(databaseregionmigration)
        html = ''

        if id:
            html = "<a class='btn btn-info' href='{}/schedulenextstep/'><i\
                    class='icon-chevron-right icon-white'></i></a>".format(id)

        return format_html(html)

    schedule_next_step_html.short_description = "Schedule Next Macro Step"

    def schedule_rollback_html(self, databaseregionmigration):
        id = self.get_step_id(databaseregionmigration)
        html = ''

        if id and databaseregionmigration.current_step > 0:
            html = "<a class='btn btn-info' href='{}/schedulenextstep/?rollback=true'><i\
                    class='icon-chevron-left icon-white'></i></a>".format(id)

        return format_html(html)

    schedule_rollback_html.short_description = "Schedule Rollback"

    def database_engine(self, databaseregionmigration):
        return databaseregionmigration.database.engine_type

    database_engine.short_description = "Engine"

    def user_friendly_warning(self, databaseregionmigration):
        warning_message = databaseregionmigration.warning

        if warning_message:
            html = '<span class="label label-warning"><font size=3.5>\
                    {}</font></span>'.format(warning_message)
            return format_html(html)

        return warning_message

    user_friendly_warning.short_description = "Warning"

    def get_database_team(self, databaseregionmigration):
        return databaseregionmigration.database.team

    get_database_team.short_description = "Team"

    def steps_information(self, databaseregionmigration):
        current_step = databaseregionmigration.current_step + 1
        steps_len = str(len(databaseregionmigration.get_steps()) - 1)
        html = "<a href='../databaseregionmigrationdetail/?database_region_migration={}'>{}</a>"
        information = '{} of {}'.format(current_step, steps_len)

        if current_step > int(steps_len):
            html = '<center>-</center>'
        else:
            html = html.format(databaseregionmigration.id, information)

        return format_html(html)
{'fraction_non_alphanumeric': 0.08629170230966639, 'fraction_numerical': 0.0014970059880239522, 'mean_word_length': 3.535887487875849, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, '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': '412634', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2429, 'n_words': 496}