source: mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py @ 8433

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py@8433
Revision 8433, 5.1 KB checked in by mnagni, 8 years ago (diff)

Two main new features:
1) the migration generate a report. The report is sent by mail to the subscribers defined in the settings.py
2) require to display a missing observation adds a message on the page and hides the some fields in the html form
3) a new javascript library (commons.js)

Line 
1'''
2Created on 10 Jan 2012
3
4@author: mnagni
5'''
6from libs.migration.db.classes import DeploymentDataMigration, DataEntityMigration,\
7    DeploymentsMigration
8from sqlalchemy.sql.expression import asc
9from libs.epb import EPB
10from sqlalchemy.orm.collections import InstrumentedList
11
12
13class MigrationEPBFactory(EPB):
14   
15    def __init__(self, dbManager):
16        self._dbManager = dbManager
17
18    def _getSession(self):
19        if self._dbManager is not None:
20            return self._dbManager.createDbSession()               
21        return None
22       
23    def createEPB(self):
24        return MigrationEPB(self._getSession())
25       
26       
27class MigrationEPB(object):
28           
29    def __init__(self, session):
30        self._session = session           
31
32    def close(self):
33        return self._session.close()
34
35    def getAllDeploymentsMigrationByDataEntitySortedByDate(self, dataEntity, deploymentNames):
36        return EPB.getAllObjects(DeploymentsMigration, self._session).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all()
37        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation"))       
38        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_status', dataEntity.doc_status)]).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation"))
39
40    def _getMigrationObject(self, mo_type, doc_status, doc_owner, doc_name):
41        return self._session.query(mo_type).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()
42        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()       
43        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_status', doc_status)]).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()
44
45    def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name):
46        return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name)             
47
48    def getDeploymentsMigrationByName(self, migrationObject, doc_name):
49        if migrationObject is None:
50            raise Exception("migrationObject is None")
51        return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name)       
52
53    def getDeploymentDataMigrationByName(self, migrationObject, doc_name):
54        if migrationObject is None:
55            raise Exception("migrationObject is None")
56        return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name)
57
58    def getDataEntityMigrationbyPath(self, migrationObject):
59        """
60            Returns the DataEntityMigration associated with the given path
61            @param migrationObject: the migration object to look for. If None returns all the DataEntityMigration items
62        """
63        ret = []
64        if migrationObject:
65            ret.append(self._session.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first())               
66        else: #then process all the DataEntities
67            res = self._session.query(DataEntityMigration).all()
68            for item in res:
69                ret.append(item)             
70        return ret
71     
72    def updateMigrationObject(self, migration_object, cols_to_update):
73        """
74            Update and eventually commit a Migration Object in Migration db
75            @param ceda_object: the Migration object to persist
76            @param dict: a dictionary containing the columns to update for the given migration_object
77            @param session: the external session to use. If None a new session will be open to add and commit the object and then closed at the exit. The object is committed
78        """
79        coll = None
80        try:
81            coll = self._session.merge(migration_object)     
82        except Exception as e:
83            print e               
84        if coll != None:       
85            for k,v in cols_to_update.items():
86                if hasattr(coll, k):
87                    val = None
88                    try:
89                        val = self._session.merge(v)
90                    except Exception:
91                        val = v
92                    coll_k = getattr(coll, k)                       
93                    if type(coll_k) == list or type(coll_k) == InstrumentedList:
94                        if  type(val) == list or type(val) == InstrumentedList:
95                            coll_k.extend(val)
96                        else:
97                            coll_k.append(val)
98                    else:
99                        setattr(coll, k, val)
100        self._session.add(coll)
101        self._session.commit()                           
102
103    def search(self, clazz, inst_id):
104        if clazz is None or inst_id is None:
105            return None 
106        ret = EPB.search(clazz, inst_id, self._session)
107        return ret
Note: See TracBrowser for help on using the repository browser.