Example #1
0
 def post(self):
     if not userIsLoggedIn(self):
         return
     
     user = users.get_current_user()
     lang = getLanguage(self, user)
     type = self.request.get('type')
     fromMember = Membership.get(self.request.get('fromMember'))
     toMember = Membership.get(self.request.get('toMember'))
     amount = self.request.get('amount')
     reason = self.request.get('reason')
     
     # See which one is the creator
     if fromMember.user == user:
         creatorMember = fromMember 
     elif toMember.user == user:
         creatorMember = toMember
     else:
         # Can't happen, only with hackery
         return
     
     # Check that the amount is a valid number
     try:
       amount = float(amount)
     except BaseException, e:
         error = _('Invalid amount: %s.', lang) % amount
         alertMessage(self, error)
         return
Example #2
0
    def post(self):
        if not userIsLoggedIn(self):
            return
        
        user = users.get_current_user()
        lang = getLanguage(self, user)
        
        groupName = self.request.get('name').strip()
        escapedGroupName = escape(groupName)

        # Verificar que el nombre no sea vacio
        if groupName == "": 
            error = _('You must enter a group name.', lang)
            alertMessage(self,error)
            return
        
        group = self.createGroupAndInsertMember(groupName)

        # Si el usuario es miembro de un grupo con alias igual al nombre 
        # del grupo que quiere crear, no dejarlo
        if group is None: 
            error = _('You already belong to a group with the name %s.', lang) % escapedGroupName
            error += '\\n';
            error += _('Please select another name.', lang)
            alertMessage(self,error)
            return

        location = '/group?group=%s&msg=%s' % (group.key(), _('Group successfully created', lang))
        redirectPage(self,location)
Example #3
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("cow")
		members = group.memberships
		
		parser = CowParser()
		parser.lang = lang
		transaction = parser.parse(members, command)
		
		if transaction.error:
			alertMessage(self, transaction.error)
			return
		
		result = transaction.getResult()
		
		# Update balance and send mails
		for member, balance in result.balanceChange.iteritems():
			balanceBefore = member.balance
			balanceNow = member.balance + balance
			
			# Balance
			member.balance += balance
			member.put()
			
			# Send mail, but not to the creator of this mail
			if member.user != creatorMember.user:
				message = createCowMail(creatorMember, transaction, result, member, balanceBefore, balanceNow, lang)
				sendEmail(message)
			
		# Create transactions
		for debt in result.debts:
			for singleDebt in debt.singleDebts:
				tr = Transaction(
					group = group,
					creatorMember = creatorMember, 
					fromMember = debt.fromMember,
					toMember = singleDebt.toMember,
					type = 'debt',
					amount = singleDebt.money, 
					reason = transaction.reason,
					isRejected = False
				)
				tr.put()
				
		location = '/group?group=%s&msg=%s' % (group.key(), _('Debts saved!', lang))
		redirectPage(self,location)
Example #4
0
	def post(self):
		if not userIsLoggedIn(self):
			return
			
		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
		
		elems = {}
		total = 0.0
		
		for debt in transaction.debts:
			total += debt.money
			razon = debt.reason
			comidas = razon.split(',')
			for comida in comidas:
				comida = comida.strip()
				[cantidad, comida2] = quantity(comida)
				elems[comida2] = elems.get(comida2, 0) + cantidad
				
		result = ''
		for comida, cantidad in elems.items():
			result += ' - %s %s\\n' % (cantidad, comida)
			
		result += '\\n';
		result += _('Total', lang)
		result += ': $%s'% total;
		
		alertMessage(self, result)
Example #5
0
    def post(self):
        if not userIsLoggedIn(self):
            return
        
        user = users.get_current_user()
        lang = getLanguage(self, user)
        
        groupKey = self.request.get('group').strip()
        
        if groupKey == "":
            error = _('Group is required', lang)
            alertMessage(self,error)
            return
        
        group = Group.get(groupKey)
        memberships = Membership.gql("WHERE group = :1 AND user = :2", group, user)

        if memberships.count() <> 1:
            error = _('You don\'t belong to this group.', lang)
            alertMessage(self,error)
            return
        
        membership = memberships.get()
        if not abs(membership.balance) <= 1e-07:
            error = _('You cannot leave this group, your balance must be zero.', lang)
            alertMessage(self,error)
            return
        
        membership.delete()
        if Membership.gql("WHERE group = :1", group).count() == 0:
            group.delete()
        
        msg = _('You have been succesfully unsubscribed from the group %s!', lang) % escape(group.name)
        location = '/?msg=' + msg
        redirectPage(self,location)
Example #6
0
 def post(self):
     if not userIsLoggedIn(self):
         return
     
     user = users.get_current_user()
     lang = getLanguage(self, user)
     groupKey = self.request.get('group')
     group = Group.get(groupKey)
     invitationText = self.request.get('invitationText')
     emails = self.request.get('emails')
     emails = emails.split(',')
     urlBuilder = UrlBuilder(self.request)
     
     # Check that all emails are valid
     for email in emails:
     	if not mail.is_email_valid(email.strip()):
     		alertMessage(self, _('%s is not a valid email address', lang) % email)
     		return
     
     for email in emails:
         self.sendInvitation(user, email.strip(), group, invitationText, urlBuilder, lang)
         
     redirectPage(self, "/group?group=%s&msg=%s" % (groupKey, escape(_('Your invite has been sent!', lang))))
    def post(self):
		if not userIsLoggedIn(self):
			return
            
		user = users.get_current_user()
		lang = getLanguage(self, user)
		userMembership = Membership.get(self.request.get('userMembership'))
        
		# Verificar que la membership que se paso sea efectivamente del usuario
		if not self.isMember(userMembership):
		    return
		
		newGroupNick = self.request.get('groupNick').strip()
		newUserNick = self.request.get('userNick').strip()
		
		# Verificar que el nombre de grupo no sea vacio
		if newGroupNick == "":
		    error = _('The name by which you want to see this group is required.', lang)
		    alertMessage(self,error)
		    return
		
		# Verificar que no exista un alias del usuario con el mismo nombre
		if self.isGroupNickTaken(newGroupNick, userMembership.group):
		    error = _('You already have a Group with the selected name, please select another name.', lang)
		    alertMessage(self,error)
		    return
		
		# Verificar que el nickname no sea vacio
		if newUserNick == "":
		    error = _('The name by which you want others to see you in this group is required.', lang)
		    alertMessage(self,error)
		    return
		
		# Verificar que el nickname no este tomado
		for other in Membership.gql("WHERE group = :1 AND user != :2", userMembership.group, userMembership.user):
		    if other.userNick == newUserNick:
		        error = _('The name by which you want others to see you in this group is already used by another member.', lang)
		        alertMessage(self,error)
		        return
		
		userMembership.alias = newGroupNick
		userMembership.nickname = newUserNick
		userMembership.put()
		
		location = '/group?group=%s&msg=%s' % (userMembership.group.key(), _('Properties changed!', lang))
		redirectPage(self,location)
Example #8
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 #9
0
	def post(self):
		if not userIsLoggedIn(self):
			return
			
		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("cow")
		members = group.memberships
		
		parser = CowParser()
		parser.lang = lang
		transaction = parser.parse(members, command)
		
		if transaction.error:
			alertMessage(self, transaction.error)
			return
		
		result = transaction.getResult()
		
		msg = ''
		msg += _('Total', lang)
		msg += ': $%s\\n'% result.total;
		msg += _('Each', lang)
		msg += ': $%s\\n\\n'% round(result.each, 2);
		
		for debt in result.debts:
			i = 0
			msg += ' - '
			for singleDebt in debt.singleDebts:
				tuple = { 'from': debt.fromMember.userNick, 'to': singleDebt.toMember.userNick, 'amount': round(singleDebt.money, 2) }
				if i == 0:
					if debt.fromMember.user == creatorMember.user:
						msg += _('You owe %(to)s $%(amount)s', lang) % tuple
					elif singleDebt.toMember.user == creatorMember.user:
						msg += _('%(from)s owes you $%(amount)s', lang) % tuple
					else:
						msg += _('%(from)s owes %(to)s $%(amount)s', lang) % tuple
				elif i < len(debt.singleDebts) - 1:
					msg += ', '
					if singleDebt.toMember.user == creatorMember.user:
						msg += _('you $%(amount)s', lang) % tuple
					else:
						msg += _('%(to)s $%(amount)s', lang) % tuple
				else:
					msg += ' '
					msg += _('and', lang)
					msg += ' '
					if singleDebt.toMember.user == creatorMember.user:
						msg += _('you $%(amount)s', lang) % tuple
					else:
						msg += _('%(to)s $%(amount)s', lang) % tuple
				i = i + 1
			msg += '\\n'
		
		alertMessage(self, msg)