Example #1
0
def validateOfferEmails(emails, userNode):
    infos = []
    for email in emails[:]:  # use slice operator to make a copy of emails because it will be modified during loop
        remove = False

        # check if email address belongs to someone
        if EmailAddr.objects.filter(email=email).count() > 0:
            emailAddr = EmailAddr.objects.get(email=email)

            # check if user already has account with owner of this email address
            if Account.objects.filter(owner=userNode,
                                      partner__pk=emailAddr.node_id,
                                      shared_data__active=True).count() != 0:
                remove = True
                acct = Account.objects.get(owner=userNode,
                                           partner__pk=emailAddr.node_id,
                                           shared_data__active=True)
                infos.append(
                    'You already have an account with %s. <a href="/accounts/%d/">You can modify it here.</a>'
                    % (escape(email), acct.id))

            # check if email address has made an offer to this user
            if Offer.objects.filter(
                    recipient_email__in=getEmails(userNode),
                    initiator__pk=emailAddr.node_id).count() != 0:
                remove = True
                offer = Offer.objects.filter(
                    recipient_email__in=getEmails(userNode),
                    initiator__pk=emailAddr.node_id)[0]
                infos.append(
                    '%s has already offered <b>you</b> %s %.2f credit. <a href="/offers/%d/accept/">Click here to accept.</a>'
                    % (escape(email), offer.currency.short_name,
                       escape(offer.amount), offer.id))

        # check if user has already made an offer to this email address
        if Offer.objects.filter(initiator=userNode,
                                recipient_email=email).count() != 0:
            remove = True
            offer = Offer.objects.get(initiator=userNode,
                                      recipient_email=email)
            infos.append(
                'You have already made an offer to %s. <a href="/offers/%d/withdraw/">Click here to withdraw it.</a>'
                % (escape(email), offer.id))

        # remove userNode's email if present
        if email in getEmails(userNode, confirmed=False):
            remove = True
            infos.append("You can't connect to yourself! Silly monkey.")

        if remove:
            emails.remove(email)

    return emails, infos
Example #2
0
def rejectionNote(request, offerId):
    userNode = checkLogin(request)
    if not userNode: return HttpResponseRedirect('/login/?redirect=%s' % request.path)
    
    if Offer.objects.filter(pk=offerId).count() == 0:
        request.session['infos'] = ["Offer has been withdrawn by the sender."]
        return HttpResponseRedirect('/summary/')
    
    offer = Offer.objects.get(pk=offerId)
    if offer.recipient_email not in getEmails(userNode):
        return HttpResponseRedirect('/summary/')
    
    d = {'offer': offer}
    return render_to_response('rejectionNote.html', d, context_instance=RequestContext(request))
Example #3
0
def accountList(request):
    userNode = checkLogin(request)
    if not userNode: return HttpResponseRedirect('/login/?redirect=%s' % request.path)
    
    d = {}
    d['infos'] = getSessionInfos(request)
    
    d['accountCurrencies'] = getAccountsSummary(userNode)
    
      # outstanding offers of credit received/made
    d['receivedOffers'] = Offer.objects.filter(recipient_email__in=getEmails(userNode)).order_by('-id')
    d['sentOffers'] = userNode.sent_offer_set.order_by('-id')
    d['rateProposals'] = getRateProposalAccts(userNode)
    return render_to_response('accountList.html', d, context_instance=RequestContext(request))
Example #4
0
def validateOfferEmails(emails, userNode):
    infos = []
    for email in emails[:]: # use slice operator to make a copy of emails because it will be modified during loop
        remove = False
        
        # check if email address belongs to someone
        if EmailAddr.objects.filter(email=email).count() > 0:
            emailAddr = EmailAddr.objects.get(email=email)
            
            # check if user already has account with owner of this email address
            if Account.objects.filter(owner=userNode, partner__pk=emailAddr.node_id, shared_data__active=True).count() != 0:
                remove = True
                acct = Account.objects.get(owner=userNode, partner__pk=emailAddr.node_id, shared_data__active=True)
                infos.append('You already have an account with %s. <a href="/accounts/%d/">You can modify it here.</a>' % (escape(email), acct.id))
            
            # check if email address has made an offer to this user
            if Offer.objects.filter(recipient_email__in=getEmails(userNode), initiator__pk=emailAddr.node_id).count() != 0:
                remove = True
                offer = Offer.objects.filter(recipient_email__in=getEmails(userNode), initiator__pk=emailAddr.node_id)[0]
                infos.append('%s has already offered <b>you</b> %s %.2f credit. <a href="/offers/%d/accept/">Click here to accept.</a>' % (escape(email), offer.currency.short_name, escape(offer.amount), offer.id))
                
        # check if user has already made an offer to this email address
        if Offer.objects.filter(initiator=userNode, recipient_email=email).count() != 0:
            remove = True
            offer = Offer.objects.get(initiator=userNode, recipient_email=email)
            infos.append('You have already made an offer to %s. <a href="/offers/%d/withdraw/">Click here to withdraw it.</a>' % (escape(email), offer.id))
        
        # remove userNode's email if present
        if email in getEmails(userNode, confirmed=False):
            remove = True
            infos.append("You can't connect to yourself! Silly monkey.")
        
        if remove:
            emails.remove(email)

    return emails, infos
Example #5
0
def rejectionNote(request, offerId):
    userNode = checkLogin(request)
    if not userNode:
        return HttpResponseRedirect('/login/?redirect=%s' % request.path)

    if Offer.objects.filter(pk=offerId).count() == 0:
        request.session['infos'] = ["Offer has been withdrawn by the sender."]
        return HttpResponseRedirect('/summary/')

    offer = Offer.objects.get(pk=offerId)
    if offer.recipient_email not in getEmails(userNode):
        return HttpResponseRedirect('/summary/')

    d = {'offer': offer}
    return render_to_response('rejectionNote.html',
                              d,
                              context_instance=RequestContext(request))
Example #6
0
def accountList(request):
    userNode = checkLogin(request)
    if not userNode:
        return HttpResponseRedirect('/login/?redirect=%s' % request.path)

    d = {}
    d['infos'] = getSessionInfos(request)

    d['accountCurrencies'] = getAccountsSummary(userNode)

    # outstanding offers of credit received/made
    d['receivedOffers'] = Offer.objects.filter(
        recipient_email__in=getEmails(userNode)).order_by('-id')
    d['sentOffers'] = userNode.sent_offer_set.order_by('-id')
    d['rateProposals'] = getRateProposalAccts(userNode)
    return render_to_response('accountList.html',
                              d,
                              context_instance=RequestContext(request))
Example #7
0
def offerAction(request, offerId, action):
    """Accept or reject somebodys offer of credit"""
    userNode = checkLogin(request)
    if not userNode:
        return HttpResponseRedirect('/login/?redirect=%s' % request.path)

    message = ''
    if Offer.objects.filter(pk=offerId).count() == 1:
        offer = Offer.objects.get(pk=offerId)
    else:  # offer has been deleted since action link was displayed to user
        if action == 'accept' or action == 'reject':
            message = "Offer has been withdrawn and cannot be accepted or rejected."
        else:
            message = "Offer has already been acted on and cannot be withdrawn."
        request.session['infos'] = [message]
        return HttpResponseRedirect('/summary/')

    # check the offer is for the logged-in user if action is accept or reject
    if (action == 'accept' or action == 'reject') and \
            offer.recipient_email not in getEmails(userNode):
        return HttpResponseRedirect('/summary/')

    if action == 'accept':
        initiator = offer.initiator
        # check for existing account between nodes
        # *** temporary until multiple accounts permitted
        if Account.objects.filter(owner=userNode,
                                  partner=initiator,
                                  shared_data__active=True).count() > 0:
            request.session['infos'] = [
                "Account already exists with %s.  Multiple accounts with the same neighbour are not permitted (yet)."
                % escape(initiator.name)
            ]
            offer.delete()
            return HttpResponseRedirect('/summary/')
        currencyId = offer.currency_id
        shared = SharedAccountData(currency_id=currencyId,
                                   balance=0.0,
                                   active=True,
                                   interest_rate=offer.interest_rate / 100.0)
        shared.save()
        acct1 = Account(owner=initiator,
                        partner=userNode,
                        shared_data=shared,
                        balance_multiplier=1,
                        iou_limit=0.0,
                        partner_limit=offer.amount,
                        name=userNode.name)
        acct1.save()
        acct2 = Account(owner=userNode,
                        partner=initiator,
                        shared_data=shared,
                        balance_multiplier=-1,
                        iou_limit=offer.amount,
                        partner_limit=0.0,
                        name=initiator.name)
        acct2.save()
        offer.delete()

        # shuffle node locations *** slow - consider doing as a regular script instead
        circle = routing.Circle()
        circle.shuffle(10 * Node.objects.count())

        # send email informing initiator that offer was accepted
        t = template_loader.get_template('emailInvitationAccepted.txt')
        c = RequestContext(request, {'offer': offer, 'acct': acct1})
        initiator_email = initiator.getPrimaryEmail()
        sendEmail("Invitation accepted", t.render(c), initiator_email)

        request.session['infos'] = [
            "This is your new connection account with %s.  Please take the time to give your account a meaningful name and assign your new neighbour a credit limit."
            % escape(initiator_email)
        ]
        return HttpResponseRedirect('/accounts/%d/' % acct2.id)

    elif action == 'reject':
        offer.delete()

        note = ''
        if request.method == 'POST':
            note = request.POST['note']

        # send email informing initiator that offer was rejected
        t = template_loader.get_template('emailInvitationRejected.txt')
        c = RequestContext(request, {'offer': offer, 'note': note})
        sendEmail("Your invitation was not accepted", t.render(c),
                  offer.initiator.getPrimaryEmail())

        message = "Offer from %s rejected." % escape(
            offer.initiator.getPrimaryEmail())
    elif action == 'withdraw':
        if userNode != offer.initiator:
            return HttpResponseRedirect('/accounts/')
        offer.delete()
        message = "Offer to %s withdrawn." % offer.recipient_email

    if message:
        request.session['infos'] = [message]
    return HttpResponseRedirect('/accounts/')
Example #8
0
def offerAction(request, offerId, action):
    """Accept or reject somebodys offer of credit"""
    userNode = checkLogin(request)
    if not userNode: return HttpResponseRedirect('/login/?redirect=%s' % request.path)
    
    message = ''
    if Offer.objects.filter(pk=offerId).count() == 1:
        offer = Offer.objects.get(pk=offerId)
    else: # offer has been deleted since action link was displayed to user
        if action == 'accept' or action == 'reject':
            message = "Offer has been withdrawn and cannot be accepted or rejected."
        else:
            message = "Offer has already been acted on and cannot be withdrawn."
        request.session['infos'] = [message]
        return HttpResponseRedirect('/summary/')

    # check the offer is for the logged-in user if action is accept or reject
    if (action == 'accept' or action == 'reject') and \
            offer.recipient_email not in getEmails(userNode):
        return HttpResponseRedirect('/summary/')

    if action == 'accept':
        initiator = offer.initiator
        # check for existing account between nodes
        # *** temporary until multiple accounts permitted
        if Account.objects.filter(owner=userNode, partner=initiator, shared_data__active=True).count() > 0:
            request.session['infos'] = ["Account already exists with %s.  Multiple accounts with the same neighbour are not permitted (yet)." % escape(initiator.name)]
            offer.delete()
            return HttpResponseRedirect('/summary/')
        currencyId = offer.currency_id
        shared = SharedAccountData(currency_id=currencyId, balance=0.0, active=True,
                                                              interest_rate=offer.interest_rate/100.0)
        shared.save()
        acct1 = Account(owner=initiator, partner=userNode, shared_data=shared, balance_multiplier=1, 
                                        iou_limit=0.0, partner_limit=offer.amount, name=userNode.name)
        acct1.save()
        acct2 = Account(owner=userNode, partner=initiator, shared_data=shared, balance_multiplier=-1, 
                                        iou_limit=offer.amount, partner_limit=0.0, name=initiator.name)
        acct2.save()
        offer.delete()
        
        # shuffle node locations *** slow - consider doing as a regular script instead
        circle = routing.Circle()
        circle.shuffle(10 * Node.objects.count())
        
        # send email informing initiator that offer was accepted
        t = template_loader.get_template('emailInvitationAccepted.txt')
        c = RequestContext(request, {'offer':offer, 'acct':acct1})
        sendEmail("Invitation accepted", t.render(c), initiator.getPrimaryEmail())
        
        request.session['infos'] = ["This is your new connection account with %s.  Please take the time to give your account a meaningful name and assign your new neighbour a credit limit." % escape(initiator.getPrimaryEmail())]
        return HttpResponseRedirect('/accounts/%d/' % acct2.id)
    elif action == 'reject':
        offer.delete()
        
        note = ''
        if request.method == 'POST':
            note = request.POST['note']
        
        # send email informing initiator that offer was rejected
        t = template_loader.get_template('emailInvitationRejected.txt')
        c = RequestContext(request, {'offer':offer, 'note':note})
        sendEmail("Your invitation was not accepted", t.render(c), offer.initiator.getPrimaryEmail())
        
        message = "Offer from %s rejected." % escape(offer.initiator.getPrimaryEmail())
    elif action == 'withdraw':
        if userNode != offer.initiator:
            return HttpResponseRedirect('/accounts/')
        offer.delete()
        message = "Offer to %s withdrawn." % offer.recipient_email

    if message:
        request.session['infos'] = [message]
    return HttpResponseRedirect('/accounts/')