<s> import logging <EOL> from django_services import admin <EOL> from django . utils . html import format_html <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf . urls import url , patterns <EOL> from django . http import HttpResponseRedirect <EOL> from dateutil import tz <EOL> from notification . models import TaskHistory <EOL> from . . models import DatabaseRegionMigration <EOL> from . . models import DatabaseRegionMigrationDetail <EOL> from . . service . databaseregionmigration import DatabaseRegionMigrationService <EOL> from . . tasks import execute_database_region_migration <EOL> from . . tasks import execute_database_region_migration_undo <EOL> from django . shortcuts import render_to_response <EOL> from django . template import RequestContext <EOL> from . . forms import DatabaseRegionMigrationDetailForm <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class DatabaseRegionMigrationAdmin ( admin . DjangoServicesAdmin ) : <EOL> search_fields = ( "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> list_filter = [ "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> list_display = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:status>' , '<STR_LIT:description>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> model = DatabaseRegionMigration <EOL> actions = None <EOL> service_class = DatabaseRegionMigrationService <EOL> list_display_links = ( ) <EOL> template = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DatabaseRegionMigrationAdmin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . list_display_links = ( None , ) <EOL> def has_delete_permission ( self , request , obj = None ) : <EOL> return False <EOL> def has_add_permission ( self , request , obj = None ) : <EOL> return False <EOL> def get_step_id ( self , databaseregionmigration , ) : <EOL> id = databaseregionmigration . id <EOL> current_step = databaseregionmigration . current_step <EOL> last_step = len ( databaseregionmigration . get_steps ( ) ) - <NUM_LIT:1> <EOL> is_migration_finished = current_step == last_step <EOL> waiting_status = DatabaseRegionMigrationDetail . WAITING <EOL> running_status = DatabaseRegionMigrationDetail . RUNNING <EOL> migration_running_or_waiting = DatabaseRegionMigrationDetail . objects . filter ( database_region_migration = databaseregionmigration , <EOL> status__in = [ waiting_status , running_status ] ) <EOL> if is_migration_finished or migration_running_or_waiting : <EOL> return '<STR_LIT>' <EOL> return id <EOL> def schedule_next_step_html ( self , databaseregionmigration ) : <EOL> id = self . get_step_id ( databaseregionmigration ) <EOL> html = '<STR_LIT>' <EOL> if id : <EOL> html = "<STR_LIT>" . format ( id ) <EOL> return format_html ( html ) <EOL> schedule_next_step_html . short_description = "<STR_LIT>" <EOL> def schedule_rollback_html ( self , databaseregionmigration ) : <EOL> id = self . get_step_id ( databaseregionmigration ) <EOL> html = '<STR_LIT>' <EOL> if id and databaseregionmigration . current_step > <NUM_LIT:0> : <EOL> html = "<STR_LIT>" . format ( id ) <EOL> return format_html ( html ) <EOL> schedule_rollback_html . short_description = "<STR_LIT>" <EOL> def database_engine ( self , databaseregionmigration ) : <EOL> return databaseregionmigration . database . engine_type <EOL> database_engine . short_description = "<STR_LIT>" <EOL> def user_friendly_warning ( self , databaseregionmigration ) : <EOL> warning_message = databaseregionmigration . warning <EOL> if warning_message : <EOL> html = '<STR_LIT>' . format ( warning_message ) <EOL> return format_html ( html ) <EOL> return warning_message <EOL> user_friendly_warning . short_description = "<STR_LIT>" <EOL> def get_database_team ( self , databaseregionmigration ) : <EOL> return databaseregionmigration . database . team <EOL> get_database_team . short_description = "<STR_LIT>" <EOL> def steps_information ( self , databaseregionmigration ) : <EOL> current_step = databaseregionmigration . current_step + <NUM_LIT:1> <EOL> steps_len = str ( len ( databaseregionmigration . get_steps ( ) ) - <NUM_LIT:1> ) <EOL> html = "<STR_LIT>" <EOL> information = '<STR_LIT>' . format ( current_step , steps_len ) <EOL> if current_step > int ( steps_len ) : <EOL> html = '<STR_LIT>' <EOL> else : <EOL> html = html . format ( databaseregionmigration . id , information ) <EOL> return format_html ( html ) <EOL> steps_information . short_description = "<STR_LIT>" <EOL> def get_urls ( self ) : <EOL> urls = super ( DatabaseRegionMigrationAdmin , self ) . get_urls ( ) <EOL> my_urls = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , <EOL> self . admin_site . admin_view ( self . databaseregionmigration_view ) ) , <EOL> ) <EOL> return my_urls + urls <EOL> def databaseregionmigration_view ( self , request , databaseregionmigration_id ) : <EOL> form = DatabaseRegionMigrationDetailForm <EOL> database_region_migration = DatabaseRegionMigration . objects . get ( <EOL> id = databaseregionmigration_id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = DatabaseRegionMigrationDetailForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> scheduled_for = form . cleaned_data [ '<STR_LIT>' ] <EOL> database_region_migration_detail = DatabaseRegionMigrationDetail ( <EOL> database_region_migration = database_region_migration , <EOL> step = database_region_migration . current_step , <EOL> scheduled_for = scheduled_for , <EOL> created_by = request . user . username ) <EOL> database_region_migration_detail . save ( ) <EOL> task_history = TaskHistory ( ) <EOL> task_history . task_name = "<STR_LIT>" <EOL> task_history . task_status = task_history . STATUS_WAITING <EOL> description = database_region_migration . description ( ) <EOL> task_history . arguments = "<STR_LIT>" . format ( database_region_migration . database . name , <EOL> description ) <EOL> task_history . user = request . user <EOL> task_history . save ( ) <EOL> is_rollback = request . GET . get ( '<STR_LIT>' ) <EOL> scheduled_for . replace ( <EOL> tzinfo = tz . tzlocal ( ) ) . astimezone ( tz . tzutc ( ) ) <EOL> if is_rollback : <EOL> LOG . info ( "<STR_LIT>" ) <EOL> database_region_migration_detail . step -= <NUM_LIT:1> <EOL> database_region_migration_detail . save ( ) <EOL> task = execute_database_region_migration_undo . apply_async ( args = [ database_region_migration_detail . id , <EOL> task_history , <EOL> request . user ] , <EOL> eta = scheduled_for ) <EOL> else : <EOL> task = execute_database_region_migration . apply_async ( args = [ database_region_migration_detail . id , <EOL> task_history , request . user ] , <EOL> eta = scheduled_for ) <EOL> database_region_migration_detail . celery_task_id = task . task_id <EOL> database_region_migration_detail . save ( ) <EOL> url = reverse ( '<STR_LIT>' ) <EOL> return HttpResponseRedirect ( url + "<STR_LIT>" % request . user . username ) <EOL> return render_to_response ( "<STR_LIT>" , <EOL> locals ( ) , <EOL> context_instance = RequestContext ( request ) ) </s>
