def _init(self, stmtrs): # BANKTRANLIST tranlist = stmtrs.find('BANKTRANLIST') if tranlist is not None: self.transactions = TransactionList(self.account, tranlist) DBSession.add_all(self.transactions) # LEDGERBAL - mandatory ledgerbal = stmtrs.find('LEDGERBAL') self.ledgerbal = ledgerbal.instantiate(acctfrom=self.account) DBSession.add(self.ledgerbal) # AVAILBAL availbal = stmtrs.find('AVAILBAL') if availbal is not None: self.availbal = availbal.instantiate(acctfrom=self.account) DBSession.add(self.availbal) ballist = stmtrs.find('BALLIST') if ballist: self.other_balances = [bal.instantiate() for bal in ballist] DBSession.add_all(self.other_balances) # Unsupported subaggregates for tag in ('MKTGINFO', ): child = stmtrs.find(tag) if child: stmtrs.remove
def instantiate(self): """ """ if not hasattr(self, '_root'): raise ValueError('Must first call parse() to have data to instantiate') # SECLIST - list of description of securities referenced by # INVSTMT (investment account statement) seclist = self.find('SECLISTMSGSRSV1/SECLIST') if seclist is not None: self.securities = [ sec.instantiate() for sec in seclist ] DBSession.add_all(self.securities) # TRNRS - transaction response, which is the main section # containing account statements # # N.B. This iteration method doesn't preserve the original # ordering of the statements within the OFX response for stmtClass in (BankStatement, CreditCardStatement, InvestmentStatement): tagname = stmtClass._tagName for trnrs in self.findall('*/%sTRNRS' % tagname): # *STMTTRNRS may have no *STMTRS (in case of error). # Don't blow up; skip silently. stmtrs = trnrs.find('%sRS' % tagname) if stmtrs is not None: stmt = stmtClass(stmtrs) self.statements.append(stmt)
def _init(self, invstmtrs): dtasof = invstmtrs.find('DTASOF').text self.datetime = ofxtools.types.DateTime.convert(dtasof) # INVTRANLIST tranlist = invstmtrs.find('INVTRANLIST') if tranlist is not None: self.transactions = TransactionList(self.account, tranlist) DBSession.add_all(self.transactions) # INVPOSLIST poslist = invstmtrs.find('INVPOSLIST') if poslist is not None: # FIs can list multiple INVPOS lots per security, so we # can't just naively instantiate what they give us or we'll # violate uniqueness constraints on the PKs. # Instead we need to manually tally up the units and instantiate # only a single INVPOS lot per security positions = {} for pos in poslist: secid = pos.find('.//SECID') uniqueid = secid.find('UNIQUEID') uniqueidtype = secid.find('UNIQUEIDTYPE') units = pos.find('./INVPOS/UNITS') seckey = (uniqueid.text, uniqueidtype.text) position = positions.get(seckey, None) if position is None: positions[seckey] = (pos, Decimal(units.text)) else: positions[seckey] = (position[0], position[1] + Decimal(units.text) ) self.positions = [pos.instantiate( units=units, acctfrom=self.account, dtasof=self.datetime) \ for pos, units in positions.values()] DBSession.add_all(self.positions) # INVBAL invbal = invstmtrs.find('INVBAL') if invbal is not None: # First strip off BALLIST & process it ballist = invbal.find('BALLIST') if ballist is not None: invbal.remove(ballist) self.other_balances = [ bal.instantiate(acctfrom=self.account, dtasof=self.datetime, ) for bal in ballist ] DBSession.add_all(self.other_balances) # Now we can flatten the rest of INVBAL self.balances = invbal.instantiate( acctfrom=self.account, dtasof=self.datetime, ) DBSession.add(self.balances) # Unsupported subaggregates for tag in ('INVOOLIST', 'INV401K', 'INV401KBAL', 'MKTGINFO'): child = invstmtrs.find(tag) if child is not None: invstmtrs.remove
def process(self): # Process Twitter -> Weibo self.__log('START_PROCESSING') c_ = DBSession.query(Customer).all() # obtain twitter handler api = TwiboEngine.getTwitterHandler() if not api: self.__log('TWITTER_API_ERROR' ) return for c in c_: self.__log('START_PROCESSING_CUSTOMER', c.id ) # load tweets t_ = TwiboEngine.getNewTweetsByUserId(api, c.tw_name) t_set = list() #w_set = list() for t in t_: new_t = Tweet(t, c.id) self.__log('NEW_TWEET', c.id, str(t.id), t.text.encode('utf-8')) t_set.append(new_t) with transaction.manager: DBSession.add_all(t_set) #DBSession.add_all(w_set) self.__log('NEW_TWEETS_LOADED', c.id) # Login Weibo if c.wi_account_name.strip() is not '' and c.wi_account_pwd.strip() is not '': weibo_client = TwiboEngine.getWeiboHandler(c.wi_account_name, c.wi_account_pwd) else: weibo_client = None if not weibo_client: self.__log('WRONG_WEIBO_ACCOUNT', c.id, None,'NO WEIBO ACCOUNT for Customer '+str(c.tw_name)+', Auto Sync would be turned off') if c.auto_sync: c.auto_sync = False DBSession.commit() continue w_set = list() for t in c.tweets: if not t.Weibo: try: w = weibo_client.statuses.update.post(status = TwitterParser.process(t.text).encode('utf-8')) except weibo.APIError: w = None if w: w_set.append(Weibo(w, t.id, c.id)) self.__log('NEW_WEIBO', c.id, str(w.id), w.text.encode('utf-8')) else: self.__log('WRONG_POST_WEIBO', c.id, str(t.id) ) with transaction.manager: DBSession.add_all(w_set) self.__log('WEIBO_SYNDICATED', c.id) self.__log('END_PROCESSING_CUSTOMER', c.id ) self.__log('END_PROCESSING')
from models import Session, User, Photo, Friendship, Friendrequest, Base, engine, DBSession Base.metadata.drop_all(engine) Base.metadata.create_all(engine) user1 = User(name = "Ann", lastname ="Smith",login='******', password ='******',city='Ekb', age=20,avatar = 'http://localhost:6543/static/An.jpg') user2 = User(name = "Sarah", lastname ="Bet",login='******', password ='******',city='New York', age=17,avatar = 'http://localhost:6543/static/SB.jpg') user3 = User(name = "John", lastname ="Worf",login='******', password ='******',city='London', age=21,avatar = 'http://localhost:6543/static/JJ.jpg') photo1 = Photo(user_id = 1, path = 'http://localhost:6543/static/9.jpg') photo2 = Photo(user_id = 1, path = 'http://localhost:6543/static/10.jpg') photo3 = Photo(user_id = 2, path = 'http://localhost:6543/static/11.jpg') fr = Friendrequest(user_from_id = 1, user_to_id = 3) fs1 = Friendship(user_from_id = 1, user_to_id = 2, dialogLocPath = 'socialnetwork/static/2-1.txt',dialogPath ='http://localhost:6543/static/2-1.txt') fs2 = Friendship(user_from_id = 2, user_to_id = 3, dialogLocPath = 'socialnetwork/static/2-3.txt',dialogPath ='http://localhost:6543/static/2-3.txt') DBSession.add_all([user1,user2,user3,photo1,photo2,photo3,fr,fs1,fs2]) #print(DBSession.query(User).all())