<s> from __future__ import absolute_import , unicode_literals <EOL> import logging <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . forms import models <EOL> from django import forms <EOL> from logical . widgets . database_offering_field import DatabaseOfferingWidget <EOL> from dbaas_cloudstack . models import CloudStackPack <EOL> from drivers . factory import DriverFactory <EOL> from backup . models import Snapshot <EOL> from physical . models import Plan , Environment , Engine <EOL> from logical . forms . fields import AdvancedModelChoiceField <EOL> from logical . models import Database <EOL> from logical . validators import database_name_evironment_constraint <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class CloneDatabaseForm ( forms . Form ) : <EOL> database_clone = forms . CharField ( <EOL> label = u'<STR_LIT>' , max_length = <NUM_LIT:64> , required = True ) <EOL> environment = forms . ModelChoiceField ( queryset = Environment . objects , widget = forms . Select ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , required = '<STR_LIT:True>' , ) <EOL> engine = forms . CharField ( widget = forms . HiddenInput ( ) , ) <EOL> origin_database_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> old_plan = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CloneDatabaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> instance = Database . objects . get ( <EOL> id = kwargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> elif '<STR_LIT>' in self . data : <EOL> instance = Database . objects . get ( id = self . data [ '<STR_LIT>' ] ) <EOL> if instance : <EOL> LOG . debug ( "<STR_LIT>" % instance ) <EOL> self . define_engine_field ( database = instance ) <EOL> self . define_available_plans ( database = instance ) <EOL> self . initial [ '<STR_LIT>' ] = instance . plan . id <EOL> def define_engine_field ( self , database ) : <EOL> self . initial [ '<STR_LIT>' ] = database . infra . engine . id <EOL> def define_available_plans ( self , database ) : <EOL> self . fields [ '<STR_LIT>' ] = forms . ModelChoiceField ( queryset = Plan . objects . filter ( engine = database . infra . engine , <EOL> is_active = True ) , <EOL> widget = forms . Select ( <EOL> attrs = { '<STR_LIT:class>' : '<STR_LIT>' } ) , <EOL> required = True , ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( CloneDatabaseForm , self ) . clean ( ) <EOL> if '<STR_LIT>' in cleaned_data : <EOL> origindatabase = Database . objects . get ( <EOL> pk = cleaned_data [ '<STR_LIT>' ] ) <EOL> if len ( cleaned_data [ '<STR_LIT>' ] ) > <NUM_LIT> : <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> dbs = origindatabase . team . databases_in_use_for ( <EOL> origindatabase . environment ) <EOL> database_alocation_limit = origindatabase . team . database_alocation_limit <EOL> LOG . debug ( "<STR_LIT>" % ( dbs , type ( dbs ) ) ) <EOL> if ( database_alocation_limit != <NUM_LIT:0> and len ( dbs ) >= database_alocation_limit ) : <EOL> LOG . warning ( "<STR_LIT>" % ( <EOL> database_alocation_limit , origindatabase . team , list ( dbs ) ) ) <EOL> raise forms . ValidationError ( [ _ ( "<STR_LIT>" ) % ( <EOL> database_alocation_limit , origindatabase . team , list ( dbs ) ) ] ) <EOL> driver = DriverFactory . get_driver_class ( <EOL> origindatabase . plan . engines [ <NUM_LIT:0> ] . name ) <EOL> if cleaned_data [ '<STR_LIT>' ] in driver . RESERVED_DATABASES_NAME : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" % cleaned_data [ '<STR_LIT>' ] ) ) <EOL> environment = cleaned_data . get ( '<STR_LIT>' , None ) <EOL> database_name = cleaned_data . get ( '<STR_LIT>' , None ) <EOL> if Database . objects . filter ( name = database_name , <EOL> environment = environment ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" . format ( database_name , <EOL> environment ) ) ) <EOL> if database_name_evironment_constraint ( database_name , environment . name ) : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % database_name <EOL> ) <EOL> if self . _errors : <EOL> return cleaned_data <EOL> return cleaned_data <EOL> class DatabaseForm ( models . ModelForm ) : <EOL> plan = AdvancedModelChoiceField ( queryset = Plan . objects . filter ( is_active = '<STR_LIT:True>' ) , <EOL> required = False , widget = forms . RadioSelect , <EOL> empty_label = None ) <EOL> engine = forms . ModelChoiceField ( queryset = Engine . objects ) <EOL> environment = forms . ModelChoiceField ( queryset = Environment . objects ) <EOL> class Meta : <EOL> model = Database <EOL> fields = ( '<STR_LIT:name>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> def remove_fields_not_in_models ( self ) : <EOL> """<STR_LIT>""" <EOL> fields_to_remove = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> for field_name in fields_to_remove : <EOL> if field_name in self . fields : <EOL> del self . fields [ field_name ] <EOL> @ classmethod <EOL> def setup_offering_field ( cls , form , db_instance ) : <EOL> form . declared_fields [ '<STR_LIT>' ] = forms . CharField ( <EOL> widget = DatabaseOfferingWidget ( attrs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : db_instance } ) , <EOL> required = False , initial = db_instance . offering ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DatabaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> instance = kwargs . get ( '<STR_LIT>' ) <EOL> if instance : <EOL> LOG . debug ( "<STR_LIT>" % instance ) <EOL> self . remove_fields_not_in_models ( ) <EOL> else : <EOL> self . fields [ '<STR_LIT>' ] . widget = forms . HiddenInput ( ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( DatabaseForm , self ) . clean ( ) <EOL> if self . instance and self . instance . id : <EOL> return cleaned_data <EOL> if '<STR_LIT>' in cleaned_data : <EOL> team = cleaned_data [ '<STR_LIT>' ] <EOL> LOG . debug ( "<STR_LIT>" % team ) <EOL> if not team : <EOL> LOG . warning ( "<STR_LIT>" ) <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> if not self . is_valid ( ) : <EOL> raise forms . ValidationError ( self . errors ) <EOL> if len ( cleaned_data [ '<STR_LIT:name>' ] ) > <NUM_LIT> : <EOL> self . _errors [ "<STR_LIT:name>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> if '<STR_LIT>' in cleaned_data : <EOL> plan = cleaned_data . get ( '<STR_LIT>' , None ) <EOL> if not plan : <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> if '<STR_LIT>' in cleaned_data : <EOL> project = cleaned_data . get ( '<STR_LIT>' , None ) <EOL> if not project : <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> if '<STR_LIT:description>' in cleaned_data : <EOL> description = cleaned_data . get ( '<STR_LIT:description>' , None ) <EOL> if not description : <EOL> self . _errors [ "<STR_LIT:description>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> if '<STR_LIT>' in cleaned_data : <EOL> environment = cleaned_data . get ( '<STR_LIT>' , None ) <EOL> database_name = cleaned_data . get ( '<STR_LIT:name>' , None ) <EOL> if not environment or environment not in plan . environments . all ( ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> if Database . objects . filter ( name = database_name , <EOL> environment = environment ) : <EOL> self . _errors [ "<STR_LIT:name>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) ] ) <EOL> del cleaned_data [ "<STR_LIT:name>" ] <EOL> dbs = team . databases_in_use_for ( environment ) <EOL> database_alocation_limit = team . database_alocation_limit <EOL> LOG . debug ( "<STR_LIT>" % ( dbs , type ( dbs ) ) ) <EOL> if ( database_alocation_limit != <NUM_LIT:0> and len ( dbs ) >= database_alocation_limit ) : <EOL> LOG . warning ( "<STR_LIT>" % ( <EOL> database_alocation_limit , team , list ( dbs ) ) ) <EOL> self . _errors [ "<STR_LIT>" ] = self . error_class ( <EOL> [ _ ( "<STR_LIT>" ) % ( database_alocation_limit , list ( dbs ) ) ] ) <EOL> driver = DriverFactory . get_driver_class ( plan . engines [ <NUM_LIT:0> ] . name ) <EOL> if '<STR_LIT:name>' in cleaned_data and cleaned_data [ '<STR_LIT:name>' ] in driver . RESERVED_DATABASES_NAME : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" % cleaned_data [ '<STR_LIT:name>' ] ) ) <EOL> if database_name_evironment_constraint ( database_name , environment . name ) : <EOL> raise forms . ValidationError ( <EOL> _ ( '<STR_LIT>' ) % database_name <EOL> ) <EOL> if self . _errors : <EOL> return cleaned_data <EOL> return cleaned_data <EOL> def save_m2m ( self , * args , ** kwargs ) : <EOL> pass <EOL> class ResizeDatabaseForm ( forms . Form ) : <EOL> database_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> original_offering_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ResizeDatabaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> instance = Database . objects . get ( <EOL> id = kwargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if instance : <EOL> LOG . debug ( "<STR_LIT>" % instance ) <EOL> if instance . plan . provider == instance . plan . CLOUDSTACK : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> self . define_target_offering_field ( <EOL> database_instance = instance , origin_offer = kwargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> def define_target_offering_field ( self , database_instance , origin_offer ) : <EOL> self . fields [ '<STR_LIT>' ] = forms . ModelChoiceField ( <EOL> queryset = CloudStackPack . objects . filter ( <EOL> offering__region__environment = database_instance . environment , <EOL> engine_type__name = database_instance . engine_type <EOL> ) . exclude ( offering__serviceofferingid = origin_offer ) , <EOL> label = u'<STR_LIT>' , <EOL> required = True ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( ResizeDatabaseForm , self ) . clean ( ) <EOL> if '<STR_LIT>' in cleaned_data : <EOL> if cleaned_data [ '<STR_LIT>' ] . offering . serviceofferingid == cleaned_data [ '<STR_LIT>' ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> if self . _errors : <EOL> return cleaned_data <EOL> return cleaned_data <EOL> class RestoreDatabaseForm ( forms . Form ) : <EOL> database_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( RestoreDatabaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> instance = Database . objects . get ( <EOL> id = kwargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if instance : <EOL> LOG . debug ( "<STR_LIT>" % instance ) <EOL> if instance . plan . provider == instance . plan . CLOUDSTACK : <EOL> LOG . debug ( "<STR_LIT>" ) <EOL> self . define_target_snapshot_field ( <EOL> database_instance = instance ) <EOL> def define_target_snapshot_field ( self , database_instance ) : <EOL> instances = database_instance . databaseinfra . instances . all ( ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ModelChoiceField ( <EOL> queryset = Snapshot . objects . filter ( instance__in = instances , <EOL> purge_at = None ) , <EOL> label = u'<STR_LIT>' , <EOL> required = True ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( RestoreDatabaseForm , self ) . clean ( ) <EOL> if '<STR_LIT>' not in cleaned_data : <EOL> LOG . info ( '<STR_LIT>' ) <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return cleaned_data <EOL> class LogDatabaseForm ( forms . Form ) : <EOL> database_id = forms . CharField ( widget = forms . HiddenInput ( ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( LogDatabaseForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if '<STR_LIT>' in kwargs : <EOL> instance = Database . objects . get ( <EOL> id = kwargs [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if instance : <EOL> LOG . debug ( "<STR_LIT>" % instance ) </s>
