<s> import json <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import IntegrityError <EOL> from django . http import Http404 , HttpResponse <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from core . models import Department , Application , Environment , Server <EOL> from forms import ( <EOL> ApplicationForm , core_create_form , DepartmentForm , EnvironmentForm , <EOL> ServerForm , ServerRoleForm ) <EOL> from . views import ServerRole <EOL> class ModalPermissionException ( Exception ) : <EOL> pass <EOL> def modal_form ( request , form_name , id = None , parent_name = None , parent_id = None , app = None ) : <EOL> try : <EOL> return _get_app_modal ( app , form_name ) ( id , parent_id ) . render ( request ) <EOL> except ModalPermissionException : <EOL> return HttpResponse ( { '<STR_LIT:status>' : False , '<STR_LIT:message>' : '<STR_LIT>' } , content_type = "<STR_LIT:application/json>" , status = <NUM_LIT> ) <EOL> def modal_delete ( request , form_name , id , app = '<STR_LIT>' ) : <EOL> try : <EOL> return _get_app_modal ( app , form_name ) ( id ) . delete ( request ) <EOL> except ModalPermissionException : <EOL> return HttpResponse ( { '<STR_LIT:status>' : False , '<STR_LIT:message>' : '<STR_LIT>' } , content_type = "<STR_LIT:application/json>" , status = <NUM_LIT> ) <EOL> def _get_app_modal ( app , form_name ) : <EOL> if not app : <EOL> app = '<STR_LIT>' <EOL> if app in settings . INSTALLED_APPS : <EOL> obj = __import__ ( app , fromlist = [ '<STR_LIT>' ] ) . modal <EOL> try : <EOL> obj = getattr ( obj , '<STR_LIT>' % form_name . title ( ) ) <EOL> except AttributeError : <EOL> raise Http404 ( ) <EOL> else : <EOL> raise Http404 ( ) <EOL> return obj <EOL> class BaseModal ( object ) : <EOL> """<STR_LIT>""" <EOL> form = None <EOL> form_name = '<STR_LIT>' <EOL> parent = None <EOL> def __init__ ( self , id = None , parent_id = None ) : <EOL> self . id = id <EOL> self . parent_id = parent_id <EOL> self . data = { '<STR_LIT:status>' : True , '<STR_LIT:action>' : '<STR_LIT>' } <EOL> self . form = None <EOL> self . instance = None <EOL> self . request = None <EOL> self . is_new = False <EOL> def create_form ( self ) : <EOL> """<STR_LIT>""" <EOL> self . form = self . get_form_creator ( ) ( self . form_name , self . request , self . id , self . get_form_args ( ) ) <EOL> self . on_form_create ( ) <EOL> def get_form_args ( self ) : <EOL> args = { } <EOL> if self . parent : <EOL> args [ self . parent ] = self . parent_id <EOL> return args <EOL> def _permission_check ( self ) : <EOL> if self . is_new : <EOL> self . permission_check ( '<STR_LIT>' ) <EOL> else : <EOL> self . permission_check ( '<STR_LIT>' ) <EOL> def render ( self , request ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . create_form ( ) <EOL> form_template = '<STR_LIT>' + self . form_name + '<STR_LIT>' <EOL> self . is_new = not bool ( self . id ) <EOL> self . _permission_check ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> template = form_template <EOL> result = self . save_form ( ) <EOL> if result : <EOL> return result <EOL> else : <EOL> template = '<STR_LIT>' <EOL> self . data = { <EOL> '<STR_LIT>' : self . form , <EOL> '<STR_LIT>' : form_template , <EOL> '<STR_LIT>' : self . is_new , <EOL> '<STR_LIT>' : self . form . instance , <EOL> '<STR_LIT>' : self . form . Meta . model . __name__ , <EOL> '<STR_LIT>' : request . path <EOL> } <EOL> self . on_view ( ) <EOL> return render ( request , template , self . data ) <EOL> def save_form ( self ) : <EOL> if self . form . is_valid ( ) : <EOL> try : <EOL> self . on_before_save ( ) <EOL> self . instance = self . form . save ( ) <EOL> if self . is_new : <EOL> self . on_create ( ) <EOL> self . message ( '<STR_LIT>' ) <EOL> else : <EOL> self . on_update ( ) <EOL> self . message ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( self . data ) , content_type = "<STR_LIT:application/json>" ) <EOL> except IntegrityError as e : <EOL> from django . forms . util import ErrorList <EOL> errors = self . form . _errors . setdefault ( "<STR_LIT>" , ErrorList ( ) ) <EOL> errors . append ( '<STR_LIT>' ) <EOL> errors . append ( e ) <EOL> def message ( self , message ) : <EOL> """<STR_LIT>""" <EOL> messages . success ( self . request , message ) <EOL> def delete ( self , request ) : <EOL> """<STR_LIT>""" <EOL> self . request = request <EOL> self . create_form ( ) <EOL> self . instance = get_object_or_404 ( self . form . Meta . model , pk = self . id ) <EOL> self . permission_check ( '<STR_LIT>' ) <EOL> self . instance . delete ( ) <EOL> self . on_delete ( ) <EOL> self . message ( '<STR_LIT>' ) <EOL> return HttpResponse ( json . dumps ( self . data ) , content_type = "<STR_LIT:application/json>" ) <EOL> def permission_check ( self , action ) : <EOL> return True <EOL> def on_before_save ( self ) : <EOL> pass <EOL> def on_create ( self ) : <EOL> pass <EOL> def on_update ( self ) : <EOL> pass <EOL> def on_delete ( self ) : <EOL> pass <EOL> def on_form_create ( self ) : <EOL> pass <EOL> def on_view ( self ) : <EOL> pass <EOL> class BaseCoreModal ( BaseModal ) : <EOL> def get_form_creator ( self ) : <EOL> return core_create_form <EOL> class DepartmentModal ( BaseCoreModal ) : <EOL> form = DepartmentForm <EOL> form_name = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if not self . request . user . is_superuser : <EOL> raise ModalPermissionException <EOL> class ServerroleModal ( BaseCoreModal ) : <EOL> form = ServerRoleForm <EOL> form_name = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , Department ( id = self . request . current_department_id ) ) : <EOL> raise ModalPermissionException <EOL> def on_before_save ( self ) : <EOL> instance = self . form . instance <EOL> if not instance . id : <EOL> instance . department_id = self . request . current_department_id <EOL> class ApplicationModal ( BaseCoreModal ) : <EOL> form = ApplicationForm <EOL> form_name = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if action == '<STR_LIT>' : <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , Department ( id = self . request . current_department_id ) ) : <EOL> raise ModalPermissionException <EOL> elif not self . request . user . has_perm ( '<STR_LIT>' , Application ( id = self . id ) ) : <EOL> raise ModalPermissionException <EOL> def on_before_save ( self ) : <EOL> instance = self . form . instance <EOL> if not instance . id : <EOL> instance . department_id = self . request . current_department_id <EOL> def on_create ( self ) : <EOL> self . data [ '<STR_LIT:action>' ] = '<STR_LIT>' <EOL> self . data [ '<STR_LIT:target>' ] = self . instance . get_absolute_url ( ) <EOL> def on_delete ( self ) : <EOL> self . data [ '<STR_LIT:action>' ] = '<STR_LIT>' <EOL> self . data [ '<STR_LIT:target>' ] = reverse ( '<STR_LIT:index>' ) <EOL> class EnvironmentModal ( BaseCoreModal ) : <EOL> form = EnvironmentForm <EOL> form_name = '<STR_LIT>' <EOL> parent = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if action == '<STR_LIT>' : <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , Application ( id = self . parent_id ) ) : <EOL> raise ModalPermissionException <EOL> elif not self . request . user . has_perm ( '<STR_LIT>' , Environment ( id = self . id ) ) : <EOL> raise ModalPermissionException <EOL> def on_delete ( self ) : <EOL> self . data [ '<STR_LIT:action>' ] = '<STR_LIT>' <EOL> self . data [ '<STR_LIT:target>' ] = self . instance . application . get_absolute_url ( ) <EOL> class ServerModal ( BaseCoreModal ) : <EOL> form = ServerForm <EOL> form_name = '<STR_LIT>' <EOL> parent = '<STR_LIT>' <EOL> def permission_check ( self , action ) : <EOL> if self . parent_id : <EOL> environment = Environment ( id = self . parent_id ) <EOL> else : <EOL> server = get_object_or_404 ( self . form . Meta . model , pk = self . id ) <EOL> environment = server . environment <EOL> if not self . request . user . has_perm ( '<STR_LIT>' , environment ) : <EOL> raise ModalPermissionException <EOL> def on_view ( self ) : <EOL> from backend . tasks import read_public_key <EOL> try : <EOL> self . data [ '<STR_LIT>' ] = read_public_key . delay ( self . form . instance . environment_id ) . get ( ) <EOL> except Exception as e : <EOL> self . data [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if not self . is_new : <EOL> self . form . fields [ '<STR_LIT:password>' ] . help_text = "<STR_LIT>" <EOL> def on_create ( self ) : <EOL> self . on_update ( ) <EOL> def on_update ( self ) : <EOL> from backend . tasks import SaveServerAuthenticationData <EOL> SaveServerAuthenticationData ( ) . delay ( self . instance . id , password = self . form . cleaned_data [ '<STR_LIT:password>' ] ) . get ( ) <EOL> def on_form_create ( self ) : <EOL> self . form . fields [ '<STR_LIT>' ] . queryset = ServerRole . objects . filter ( department_id = self . request . current_department_id ) </s>
