# Path: ethicsapplication/signals.py



from django.db import models
from django.contrib.auth.models import User
from questionnaire.models import AnswerSet, Questionnaire
from django.db.models.manager import Manager
from workflows.utils import set_workflow
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from workflows.models import Workflow, State
from permissions.models import Role
from permissions.utils import remove_local_role, add_local_role, get_object_for_principle_as_role
from workflows.utils import get_state
from ethicsapplication.signals import application_created
        It is thisobject that will be manipulated by the workflow engine.   
    '''
    
    title = models.CharField(max_length=255, default=None)  #default=None stops null strings which effectively makes it mandatory
    principle_investigator = models.ForeignKey(User ,related_name='pi')
    application_form = models.ForeignKey(Questionnaire, related_name='application_form', blank=True, null=True)
    checklist = models.ForeignKey(Questionnaire, related_name='checklist_questionnaire', blank=True, null=True)
    #TODO test the new checklist attribute
    objects = EthicsApplicationManager()
    
    __original_principle_investigator = None
    __original_id = None
    
    def __init__(self, *args, **kwargs):
        super(EthicsApplication, self).__init__(*args, **kwargs)
        
        #save the orginal values of the pi and the id so we can detect if they have changed without having to ask the db
        self.__original_principle_investigator = kwargs.get('principle_investigator', None)
        self.__original_id = self.id
        
    def save(self, force_insert=False, force_update=False, using=None):
        super(EthicsApplication, self).save(force_insert, force_update, using) 
        
        if(self.__original_id != self.id): 
            #this is a new application that has been changed (or somehow the id has changed?!)
            self._add_to_workflow()
            self._add_to_principle_investigator_role()
            self.__original_id = self.id
            
            

# Path: ethicsapplication/signals.py



# Path: ethicsapplication/models.py
from django.db import models
from django.contrib.auth.models import User
from questionnaire.models import AnswerSet, Questionnaire
from django.db.models.manager import Manager
from workflows.utils import set_workflow
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist
from workflows.models import Workflow, State
from permissions.models import Role
from permissions.utils import remove_local_role, add_local_role, get_object_for_principle_as_role
from workflows.utils import get_state
from ethicsapplication.signals import application_created

        It is thisobject that will be manipulated by the workflow engine.   
    '''
    
    title = models.CharField(max_length=255, default=None)  #default=None stops null strings which effectively makes it mandatory
    principle_investigator = models.ForeignKey(User ,related_name='pi')
    application_form = models.ForeignKey(Questionnaire, related_name='application_form', blank=True, null=True)
    checklist = models.ForeignKey(Questionnaire, related_name='checklist_questionnaire', blank=True, null=True)
    #TODO test the new checklist attribute
    objects = EthicsApplicationManager()
    
    __original_principle_investigator = None
    __original_id = None
    
    def __init__(self, *args, **kwargs):
        super(EthicsApplication, self).__init__(*args, **kwargs)
        
        #save the orginal values of the pi and the id so we can detect if they have changed without having to ask the db
        self.__original_principle_investigator = kwargs.get('principle_investigator', None)
        self.__original_id = self.id
        
    def save(self, force_insert=False, force_update=False, using=None):
        super(EthicsApplication, self).save(force_insert, force_update, using) 
        
        if(self.__original_id != self.id): 
            #this is a new application that has been changed (or somehow the id has changed?!)
            self._add_to_workflow()
            self._add_to_principle_investigator_role()
            self.__original_id = self.id
            
            

            application_created.send(sender=self, application=self)