<s> from django import forms <EOL> from django . conf import settings <EOL> from django . core . exceptions import ValidationError , ObjectDoesNotExist <EOL> from django . utils import timezone , dateparse <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> import aristotle_mdr . models as MDR <EOL> from aristotle_mdr . exceptions import NoUserGivenForUserForm <EOL> from aristotle_mdr . perms import user_can_move_between_workgroups , user_can_move_any_workgroup , user_can_remove_from_workgroup , user_can_move_to_workgroup <EOL> import autocomplete_light <EOL> class UserAwareForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if '<STR_LIT:user>' in kwargs . keys ( ) : <EOL> self . user = kwargs . pop ( '<STR_LIT:user>' ) <EOL> elif '<STR_LIT>' in kwargs . keys ( ) : <EOL> self . user = kwargs [ '<STR_LIT>' ] . user <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> self . user = self . request . user <EOL> else : <EOL> raise NoUserGivenForUserForm ( "<STR_LIT>" ) <EOL> super ( UserAwareForm , self ) . __init__ ( * args , ** kwargs ) <EOL> class UserAwareModelForm ( UserAwareForm , autocomplete_light . ModelForm ) : <EOL> class Meta : <EOL> model = MDR . _concept <EOL> exclude = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def _media ( self ) : <EOL> js = ( '<STR_LIT>' , ) <EOL> media = forms . Media ( js = js ) <EOL> for field in self . fields . values ( ) : <EOL> media = media + field . widget . media <EOL> return media <EOL> media = property ( _media ) <EOL> class WorkgroupVerificationMixin ( forms . ModelForm ) : <EOL> cant_move_any_permission_error = _ ( "<STR_LIT>" ) <EOL> cant_move_from_permission_error = _ ( "<STR_LIT>" ) <EOL> cant_move_to_permission_error = _ ( "<STR_LIT>" ) <EOL> def clean_workgroup ( self ) : <EOL> if self . instance . pk is not None : <EOL> if '<STR_LIT>' in self . data . keys ( ) and str ( self . data [ '<STR_LIT>' ] ) is not None : <EOL> if str ( self . data [ '<STR_LIT>' ] ) != str ( self . instance . workgroup . pk ) : <EOL> if not user_can_move_any_workgroup ( self . user ) : <EOL> raise forms . ValidationError ( WorkgroupVerificationMixin . cant_move_any_permission_error ) <EOL> if not user_can_remove_from_workgroup ( self . user , self . instance . workgroup ) : <EOL> raise forms . ValidationError ( WorkgroupVerificationMixin . cant_move_from_permission_error ) <EOL> new_workgroup = self . cleaned_data [ '<STR_LIT>' ] <EOL> if self . instance . pk is not None : <EOL> if '<STR_LIT>' in self . cleaned_data . keys ( ) and self . instance . workgroup != new_workgroup : <EOL> if not user_can_move_between_workgroups ( self . user , self . instance . workgroup , new_workgroup ) : <EOL> self . data = self . data . copy ( ) <EOL> self . data [ '<STR_LIT>' ] = self . instance . workgroup . pk <EOL> raise forms . ValidationError ( WorkgroupVerificationMixin . cant_move_to_permission_error ) <EOL> return new_workgroup <EOL> class CheckIfModifiedMixin ( forms . ModelForm ) : <EOL> modified_since_form_fetched_error = _ ( <EOL> "<STR_LIT>" <EOL> ) <EOL> modified_since_field_missing = _ ( <EOL> "<STR_LIT>" <EOL> ) <EOL> last_fetched = forms . CharField ( <EOL> widget = forms . widgets . HiddenInput ( ) , <EOL> initial = timezone . now ( ) , required = True , <EOL> error_messages = { '<STR_LIT>' : modified_since_field_missing } <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( CheckIfModifiedMixin , self ) . __init__ ( * args , ** kwargs ) <EOL> self . initial [ '<STR_LIT>' ] = timezone . now ( ) <EOL> self . fields [ '<STR_LIT>' ] . initial = timezone . now ( ) <EOL> def clean_last_fetched ( self ) : <EOL> modified_time = timezone . localtime ( self . instance . modified , timezone . utc ) <EOL> last_fetched = self . cleaned_data [ '<STR_LIT>' ] <EOL> last_fetched = dateparse . parse_datetime ( last_fetched ) <EOL> self . cleaned_data [ '<STR_LIT>' ] = last_fetched <EOL> if self . cleaned_data [ '<STR_LIT>' ] is None or self . cleaned_data [ '<STR_LIT>' ] == "<STR_LIT>" : <EOL> self . initial [ '<STR_LIT>' ] = timezone . now ( ) <EOL> raise forms . ValidationError ( CheckIfModifiedMixin . modified_since_field_missing ) <EOL> if modified_time > self . cleaned_data [ '<STR_LIT>' ] : <EOL> self . initial [ '<STR_LIT>' ] = timezone . now ( ) <EOL> raise forms . ValidationError ( CheckIfModifiedMixin . modified_since_form_fetched_error ) <EOL> class ConceptForm ( WorkgroupVerificationMixin , UserAwareModelForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> first_load = kwargs . pop ( '<STR_LIT>' , None ) <EOL> super ( ConceptForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if not self . user . is_superuser : <EOL> self . fields [ '<STR_LIT>' ] . queryset = self . user . profile . editable_workgroups <EOL> self . fields [ '<STR_LIT:name>' ] . widget = forms . widgets . TextInput ( ) <EOL> self . show_slots_tab = True <EOL> def concept_fields ( self ) : <EOL> field_names = [ field . name for field in MDR . baseAristotleObject . _meta . fields ] + [ '<STR_LIT:version>' , '<STR_LIT>' ] <EOL> concept_field_names = [ <EOL> field . name for field in MDR . concept . _meta . fields <EOL> if field . name not in field_names <EOL> ] <EOL> for name in self . fields : <EOL> if name in concept_field_names and name != '<STR_LIT>' : <EOL> yield self [ name ] <EOL> def object_specific_fields ( self ) : <EOL> obj_field_names = [ <EOL> field . name for field in self . _meta . model . _meta . fields <EOL> if field not in MDR . concept . _meta . fields <EOL> ] <EOL> fields = [ ] <EOL> for name in self . fields : <EOL> if name in obj_field_names : <EOL> fields . append ( self [ name ] ) <EOL> return fields <EOL> class Concept_1_Search ( UserAwareForm ) : <EOL> template = "<STR_LIT>" <EOL> name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> version = forms . CharField ( max_length = <NUM_LIT> , required = False ) <EOL> definition = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> def subclassed_modelform ( set_model ) : <EOL> class MyForm ( ConceptForm ) : <EOL> class Meta ( ConceptForm . Meta ) : <EOL> model = set_model <EOL> fields = '<STR_LIT>' <EOL> return MyForm <EOL> def subclassed_edit_modelform ( set_model ) : <EOL> class MyForm ( ConceptForm , CheckIfModifiedMixin ) : <EOL> change_comments = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> class Meta ( ConceptForm . Meta ) : <EOL> model = set_model <EOL> if set_model . edit_page_excludes : <EOL> exclude = set_model . edit_page_excludes <EOL> else : <EOL> fields = '<STR_LIT>' <EOL> return MyForm <EOL> def subclassed_wizard_2_Results ( set_model ) : <EOL> class MyForm ( Concept_2_Results ) : <EOL> class Meta ( Concept_2_Results . Meta ) : <EOL> model = set_model <EOL> fields = '<STR_LIT>' <EOL> return MyForm <EOL> class Concept_2_Results ( ConceptForm ) : <EOL> make_new_item = forms . BooleanField ( <EOL> initial = False , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . check_similar = kwargs . pop ( '<STR_LIT>' , True ) <EOL> super ( Concept_2_Results , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . queryset = self . user . profile . editable_workgroups <EOL> self . fields [ '<STR_LIT>' ] . initial = self . user . profile . activeWorkgroup <EOL> self . fields [ '<STR_LIT:name>' ] . widget = forms . widgets . TextInput ( ) <EOL> if not self . check_similar : <EOL> self . fields . pop ( '<STR_LIT>' ) <EOL> class DEC_OCP_Search ( UserAwareForm ) : <EOL> template = "<STR_LIT>" <EOL> oc_name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> oc_desc = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> pr_name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> pr_desc = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DEC_OCP_Results ( UserAwareForm ) : <EOL> def __init__ ( self , oc_similar = None , pr_similar = None , oc_duplicate = None , pr_duplicate = None , * args , ** kwargs ) : <EOL> super ( DEC_OCP_Results , self ) . __init__ ( * args , ** kwargs ) <EOL> if oc_similar : <EOL> oc_options = [ ( oc . object . id , oc ) for oc in oc_similar ] <EOL> oc_options . append ( ( "<STR_LIT:X>" , "<STR_LIT>" ) ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> label = "<STR_LIT>" , <EOL> choices = oc_options , <EOL> widget = forms . RadioSelect ( ) <EOL> ) <EOL> if pr_similar : <EOL> pr_options = [ ( pr . object . id , pr ) for pr in pr_similar ] <EOL> pr_options . append ( ( "<STR_LIT:X>" , "<STR_LIT>" ) ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> label = "<STR_LIT>" , <EOL> choices = tuple ( pr_options ) , <EOL> widget = forms . RadioSelect ( ) <EOL> ) <EOL> def clean_oc_options ( self ) : <EOL> if self . cleaned_data [ '<STR_LIT>' ] == "<STR_LIT:X>" : <EOL> return None <EOL> try : <EOL> oc = MDR . ObjectClass . objects . get ( pk = self . cleaned_data [ '<STR_LIT>' ] ) <EOL> return oc <EOL> except ObjectDoesNotExist : <EOL> return None <EOL> def clean_pr_options ( self ) : <EOL> if self . cleaned_data [ '<STR_LIT>' ] == "<STR_LIT:X>" : <EOL> return None <EOL> try : <EOL> return MDR . Property . objects . get ( pk = self . cleaned_data [ '<STR_LIT>' ] ) <EOL> except ObjectDoesNotExist : <EOL> return None <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DEC_Find_DEC_Results ( Concept_2_Results ) : <EOL> class Meta ( Concept_2_Results . Meta ) : <EOL> model = MDR . DataElementConcept <EOL> class DEC_Complete ( UserAwareForm ) : <EOL> make_items = forms . BooleanField ( <EOL> initial = False , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DE_OCPVD_Search ( UserAwareForm ) : <EOL> template = "<STR_LIT>" <EOL> oc_name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> oc_desc = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> pr_name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> pr_desc = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> vd_name = forms . CharField ( max_length = <NUM_LIT> ) <EOL> vd_desc = forms . CharField ( widget = forms . Textarea , required = False ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DE_OCPVD_Results ( DEC_OCP_Results ) : <EOL> def __init__ ( self , vd_similar = None , vd_duplicate = None , * args , ** kwargs ) : <EOL> super ( DE_OCPVD_Results , self ) . __init__ ( * args , ** kwargs ) <EOL> if vd_similar : <EOL> vd_options = [ ( vd . object . id , vd ) for vd in vd_similar ] <EOL> vd_options . append ( ( "<STR_LIT:X>" , "<STR_LIT>" ) ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> label = "<STR_LIT>" , <EOL> choices = tuple ( vd_options ) , <EOL> widget = forms . RadioSelect ( ) <EOL> ) <EOL> def clean_vd_options ( self ) : <EOL> if self . cleaned_data [ '<STR_LIT>' ] == "<STR_LIT:X>" : <EOL> return None <EOL> try : <EOL> return MDR . ValueDomain . objects . get ( pk = self . cleaned_data [ '<STR_LIT>' ] ) <EOL> except ObjectDoesNotExist : <EOL> return None <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DE_Find_DEC_Results ( UserAwareForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> dec_similar = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( DE_Find_DEC_Results , self ) . __init__ ( * args , ** kwargs ) <EOL> if dec_similar : <EOL> dec_options = [ ( dec . id , dec ) for dec in dec_similar ] <EOL> dec_options . append ( ( "<STR_LIT:X>" , "<STR_LIT>" ) ) <EOL> self . fields [ '<STR_LIT>' ] = forms . ChoiceField ( <EOL> label = "<STR_LIT>" , <EOL> choices = tuple ( dec_options ) , <EOL> widget = forms . RadioSelect ( ) <EOL> ) <EOL> def clean_dec_options ( self ) : <EOL> if self . cleaned_data [ '<STR_LIT>' ] == "<STR_LIT:X>" : <EOL> return None <EOL> try : <EOL> dec = MDR . DataElementConcept . objects . get ( pk = self . cleaned_data [ '<STR_LIT>' ] ) <EOL> return dec <EOL> except ObjectDoesNotExist : <EOL> return None <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DE_Find_DE_Results_from_components ( UserAwareForm ) : <EOL> make_new_item = forms . BooleanField ( <EOL> initial = False , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass <EOL> class DE_Find_DE_Results ( Concept_2_Results ) : <EOL> class Meta ( Concept_2_Results . Meta ) : <EOL> model = MDR . DataElement <EOL> class DE_Complete ( UserAwareForm ) : <EOL> make_items = forms . BooleanField ( <EOL> initial = False , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> pass </s>
