Changeset 4214 for exist


Ignore:
Timestamp:
22/09/08 11:29:49 (11 years ago)
Author:
cbyrom
Message:

Add cmp method to DeploymentLink? to allow for object equality tests +
add new method to MolesEntity? to ensure that only unique deployment
links are added to the helper arrays + add new constructor input to
Atom to allow the content to be defined at creation.

Location:
exist/trunk/python/ndgUtils/models
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/models/Atom.py

    r4209 r4214  
    9999 
    100100    def fromString(self, linkString): 
    101         (self.term, self.scheme, self.label) = getTripleData(linkString) 
     101        (self.label, self.scheme, self.term) = getTripleData(linkString) 
    102102         
    103103    def fromETElement(self, linkTag): 
  • exist/trunk/python/ndgUtils/models/MolesEntity.py

    r4209 r4214  
    4444        logging.info("ElementTree version of Deployment element link created") 
    4545        return link 
    46              
    47  
     46 
     47    def __cmp__(self, link): 
     48        logging.debug("Comparing link objects") 
     49        if isinstance(link, DeploymentLink): 
     50            if link.url == self.url and \ 
     51                link.vocabUrl == self.vocabUrl and \ 
     52                link.title == self.title: 
     53                logging.debug("- links are equal") 
     54                return 0 
     55 
     56        logging.debug("- links are not equal") 
     57        return 1 
     58     
    4859class Deployments(object): 
    4960     
     
    166177 
    167178        logging.info(VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL) 
    168         if vocabURL.startswith(VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL) and \ 
    169             vocabURL not in self.activities: 
    170             self.activities.append(deploymentLink) 
    171         elif vocabURL.startswith(VTD.TERM_DATA[VTD.OBS_TERM].vocabURL) and \ 
    172             vocabURL not in self.obs: 
    173             self.obs.append(deploymentLink) 
    174         elif vocabURL.startswith(VTD.TERM_DATA[VTD.DPT_TERM].vocabURL) and \ 
    175             vocabURL not in self.dpts: 
    176             self.dpts.append(deploymentLink) 
     179        self._addDeploymentDetails(deploymentLink) 
    177180 
    178181        if deploymentID not in self.deployments: 
     
    185188 
    186189        logging.info("New deployment link data added to moles entity") 
    187          
     190 
     191         
     192    def _addDeploymentDetails(self, deploymentLink): 
     193        ''' 
     194        Add deployment details to local arrays for easy access by templates 
     195        ''' 
     196        deploymentType = None 
     197        # first, determine type of deployment 
     198        if deploymentLink.vocabUrl.startswith(VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL): 
     199            deploymentType = self.activities 
     200        elif deploymentLink.vocabUrl.startswith(VTD.TERM_DATA[VTD.OBS_TERM].vocabURL): 
     201            deploymentType = self.obs 
     202        elif deploymentLink.vocabUrl.startswith(VTD.TERM_DATA[VTD.DPT_TERM].vocabURL): 
     203            deploymentType = self.dpts 
     204        else: 
     205            return 
     206 
     207        # Now check if this has already been added; return if so 
     208        for link in deploymentType: 
     209            if link == deploymentLink: 
     210                return 
     211 
     212        deploymentType.append(deploymentLink) 
     213 
    188214         
    189215    def toXML(self): 
  • exist/trunk/python/ndgUtils/models/testmolesentity.py

    r4209 r4214  
    9494         
    9595 
     96    def testAddDeploymentDetails(self): 
     97        a = ME.DeploymentLink('http://blah', VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL, title='fred') 
     98        b = ME.DeploymentLink('http://blah', VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL, title='fred') 
     99        c = ME.DeploymentLink('http://blah', VTD.TERM_DATA[VTD.ACTIVITY_TERM].vocabURL, title='freddy') 
     100         
     101        self.ME._addDeploymentDetails(a) 
     102        self.assertEquals(self.ME.activities[0], a) 
     103         
     104        self.ME._addDeploymentDetails(b) 
     105        self.assertEquals(len(self.ME.activities), 1) 
     106         
     107        self.ME._addDeploymentDetails(c) 
     108        self.assertEquals(len(self.ME.activities), 2) 
     109         
     110        self.ME._addDeploymentDetails(c) 
     111        self.assertEquals(len(self.ME.activities), 2) 
     112         
    96113 
    97114 
Note: See TracChangeset for help on using the changeset viewer.