<s> import json <EOL> import os <EOL> from django . conf import settings <EOL> from django import http <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from horizon import exceptions <EOL> from horizon import views <EOL> ADD_TO_FIELD_HEADER = "<STR_LIT>" <EOL> class ModalBackdropMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> modal_backdrop = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> super ( ModalBackdropMixin , self ) . __init__ ( ) <EOL> config = getattr ( settings , '<STR_LIT>' , { } ) <EOL> if '<STR_LIT>' in config : <EOL> self . modal_backdrop = config [ '<STR_LIT>' ] <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ModalBackdropMixin , self ) . get_context_data ( ** kwargs ) <EOL> context [ '<STR_LIT>' ] = self . modal_backdrop <EOL> return context <EOL> class ModalFormMixin ( object ) : <EOL> def get_template_names ( self ) : <EOL> if self . request . is_ajax ( ) : <EOL> if not hasattr ( self , "<STR_LIT>" ) : <EOL> bits = list ( os . path . split ( self . template_name ) ) <EOL> bits [ <NUM_LIT:1> ] = "<STR_LIT>" . join ( ( "<STR_LIT:_>" , bits [ <NUM_LIT:1> ] ) ) <EOL> self . ajax_template_name = os . path . join ( * bits ) <EOL> template = self . ajax_template_name <EOL> else : <EOL> template = self . template_name <EOL> return template <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ModalFormMixin , self ) . get_context_data ( ** kwargs ) <EOL> if self . request . is_ajax ( ) : <EOL> context [ '<STR_LIT>' ] = True <EOL> if ADD_TO_FIELD_HEADER in self . request . META : <EOL> context [ '<STR_LIT>' ] = self . request . META [ ADD_TO_FIELD_HEADER ] <EOL> return context <EOL> class ModalFormView ( ModalBackdropMixin , ModalFormMixin , views . HorizonFormView ) : <EOL> """<STR_LIT>""" <EOL> modal_id = None <EOL> modal_header = "<STR_LIT>" <EOL> form_id = None <EOL> submit_url = None <EOL> submit_label = _ ( "<STR_LIT>" ) <EOL> cancel_label = _ ( "<STR_LIT>" ) <EOL> cancel_url = None <EOL> def _populate_context ( self , context ) : <EOL> context [ '<STR_LIT>' ] = self . modal_id <EOL> context [ '<STR_LIT>' ] = self . modal_header <EOL> context [ '<STR_LIT>' ] = self . form_id <EOL> context [ '<STR_LIT>' ] = self . submit_url <EOL> context [ '<STR_LIT>' ] = self . submit_label <EOL> context [ '<STR_LIT>' ] = self . cancel_label <EOL> context [ '<STR_LIT>' ] = self . get_cancel_url ( ) <EOL> return context <EOL> def get_context_data ( self , ** kwargs ) : <EOL> context = super ( ModalFormView , self ) . get_context_data ( ** kwargs ) <EOL> context = self . _populate_context ( context ) <EOL> return context <EOL> def get_cancel_url ( self ) : <EOL> return self . cancel_url or self . success_url <EOL> def get_object_id ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return obj . id <EOL> def get_object_display ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return obj . name <EOL> def get_form ( self , form_class ) : <EOL> """<STR_LIT>""" <EOL> return form_class ( self . request , ** self . get_form_kwargs ( ) ) <EOL> def form_invalid ( self , form ) : <EOL> context = super ( ModalFormView , self ) . get_context_data ( ) <EOL> context = self . _populate_context ( context ) <EOL> context [ '<STR_LIT>' ] = form <EOL> return self . render_to_response ( context ) <EOL> def form_valid ( self , form ) : <EOL> try : <EOL> handled = form . handle ( self . request , form . cleaned_data ) <EOL> except Exception : <EOL> handled = None <EOL> exceptions . handle ( self . request ) <EOL> if handled : <EOL> if ADD_TO_FIELD_HEADER in self . request . META : <EOL> field_id = self . request . META [ ADD_TO_FIELD_HEADER ] <EOL> data = [ self . get_object_id ( handled ) , <EOL> self . get_object_display ( handled ) ] <EOL> response = http . HttpResponse ( json . dumps ( data ) ) <EOL> response [ "<STR_LIT>" ] = field_id <EOL> elif isinstance ( handled , http . HttpResponse ) : <EOL> return handled <EOL> else : <EOL> success_url = self . get_success_url ( ) <EOL> response = http . HttpResponseRedirect ( success_url ) <EOL> response [ '<STR_LIT>' ] = success_url <EOL> return response <EOL> else : <EOL> return self . form_invalid ( form ) </s>
