Example #1
0
	def post(self):
		if not userIsLoggedIn(self):
			return
			
		rejectPath = UrlBuilder(self.request).buildUrl('/reject')
		
		user = users.get_current_user()
		lang = getLanguage(self, user)
		group = Group.get(self.request.get("group"))
		
		creatorMember = Membership.gql("WHERE group = :1 AND user = :2", group, user)[0]
		if not creatorMember:
			return
		
		command = self.request.get("command")
		members = group.memberships
		
		parser = OrderParser()
		parser.lang = lang
		transaction = parser.parse(members, command)
		
		if transaction.error:
			alertMessage(self, transaction.error)
			return
			
		payersBalanceBefore = transaction.payer.balance
		
		for debt in transaction.debts:
		    debtor = debt.member
		    payer = transaction.payer
		    debtorLang = getLanguage(self, debtor.user)
			
		    if debtor.user.email().lower() == payer.user.email().lower():
		        continue
			
		    debtorsBalanceBefore = debtor.balance
		    
		    # Adjust balance
		    debtor.balance -= debt.money
		    debtor.put()
		    
		    payer.balance += debt.money
		    
		    debtorsBalanceNow = debtor.balance
		    
		    # Create transaction
		    tr = Transaction(
		        group = group,
		        creatorMember = creatorMember, 
		        fromMember = debtor,
		        toMember = payer,
		        type = 'debt',
		        amount = debt.money, 
		        reason = debt.reason,
		        isRejected = False
		        )
		    tr.put()
			
		    # If the one that created this transaction is the one that owes,
		    # don't sent a mail to him/her
		    if creatorMember.user == debtor.user:
		    	continue
		    
		    # Build the reject url
		    rejectUrl = UrlBuilder(self.request).buildUrl('/reject')
		    rejectUrl += "?key=%s&h=%s" % (str(tr.key()), tr.hash)
		    
		    # Try send email to the debtor
		    if creatorMember.user == transaction.payer.user:
		    	message = createActionMail(payer, debtor, debt.money, debt.reason, debtorsBalanceBefore, debtorsBalanceNow, rejectUrl, youOwedSomeone(debtorLang), debtorLang) 
		    else:
		    	message = createThirdPartyActionMail(creatorMember, payer, debtor, debt.money, debt.reason, debtorsBalanceBefore, debtorsBalanceNow, rejectUrl, creatorSaysYouOwedSomeone(debtorLang), debtorLang)
		    
		    sendEmail(message)
				
		transaction.payer.put()
		
		payersBalanceNow = transaction.payer.balance
		
		# Now try send email to the payer with a summary
		if not creatorMember.user == transaction.payer.user:
			payerLang = getLanguage(self, transaction.payer.user)
			message = createBulkMail(transaction, creatorMember, payersBalanceBefore, payersBalanceNow, payerLang)
			sendEmail(message)
				
		location = '/group?group=%s&msg=%s' % (group.key(), _('Debts saved!', lang))
		redirectPage(self,location)
Example #2
0
    def post(self):
        if not userIsLoggedIn(self):
            return
        
        key = self.request.get('key')
        hash = self.request.get('h')
        reason = self.request.get('reason').strip()
        user = users.get_current_user()
        lang = getLanguage(self, user)
        
        # Check that all is ok
        tr = isValidTransaction(key, hash, user)
        if not tr:
            self.redirect('/')
            return
        
        # Check that the transaction is not rejected
        if tr.isRejected:
            self.redirect('/')
            return
        
        # Reject it and everything else...
        tr.isRejected = True
        
        compensateTr = tr.getCompensateFor(user)
        
        # See who is me, and who is someone
        if compensateTr.creatorMember == compensateTr.fromMember:
            me = compensateTr.fromMember
            someone = compensateTr.toMember
        else:
            me = compensateTr.toMember
            someone = compensateTr.fromMember
            
        someoneLang = getLanguage(self, someone.user)
            
        balanceBefore = someone.balance

        # ========================================================= #
        # See what's the type of the transaction and adjust balance
        # ========================================================= #
        
        if tr.type == 'debt':
            # If it's a debt, fromMember always wins
            tr.fromMember.balance += tr.amount
            tr.toMember.balance -= tr.amount
            
            if compensateTr.creatorMember.user == tr.fromMember.user:
                # I owe someone
                mailBody = someoneOwedYou(someoneLang, reject = True)
            else:
                # Someone owes me
                mailBody = youOwedSomeone(someoneLang, reject = True)
        elif tr.type == 'payment':
            # If it's a payment, fromMember always looses
            tr.fromMember.balance -= tr.amount
            tr.toMember.balance += tr.amount
            
            if compensateTr.creatorMember.user == tr.fromMember.user:
                # I paid someone
                mailBody = someonePayedYou(someoneLang, reject = True)
            else:
                # Someone paid me
                mailBody = youPayedSomeone(someoneLang, reject = True)
        else:
            # Can't happen, only with hackery
            return
        
        tr.fromMember.put()
        tr.toMember.put()
        tr.put()
        compensateTr.put()
        
        balanceNow = someone.balance

        # ========================== #
        # Try send notification mail #
        # ========================== #        
        
        # Try send mail
        message = createRejectionMail(me, someone, tr, reason, balanceBefore, balanceNow, mailBody, someoneLang)
        sendEmail(message)
        
        location = '/group?group=%s&msg=%s' % (tr.group.key(), _('You rejected the transaction', lang))
        redirectPage(self,location)