def test_transfer_item(self):
        """A user should be able to transfer an item on their order to an
        email address."""
        view = self.set_up_view(
            orderer=PersonFactory(),
            is_confirmed=True,
        )
        view.post(view.request)

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn('offered an item transfer', mail.outbox[0].body)

        # TODO: Use subTest after Python 3.4 upgrade
        # with self.subTest('summary view includes a pending transfer'):
        summary_view = SummaryView()
        summary_view.request = self.factory.get('/')
        summary_view.request.user = view.request.user
        summary_view.event = view.event
        summary_view.order = view.order
        summary_view.workflow = RegistrationWorkflow(order=view.order, event=view.event)
        summary_view.current_step = summary_view.workflow.steps.get(summary_view.current_step_slug)
        response = summary_view.get(summary_view.request)
        self.assertEqual(len(response.context_data['pending_transfers']), 1)
        self.assertEqual(response.context_data['pending_transfers'][0]['bought_item'], view.order.bought_items.first())
        self.assertIn('invite', response.context_data['pending_transfers'][0])
        self.assertEqual(len(response.context_data['transferred_items']), 0)

        # with self.subTest('summary view includes a completed transfer'):
        invite = response.context_data['pending_transfers'][0]['invite']
        transfer_invite = TransferInvite(summary_view.request, invite)
        transfer_invite.accept()
        response = summary_view.get(summary_view.request)
        self.assertEqual(len(response.context_data['pending_transfers']), 0)
        self.assertEqual(len(response.context_data['transferred_items']), 1)
Exemplo n.º 2
0
 def clean_bought_item(self):
     bought_item = self.cleaned_data['bought_item']
     if not bought_item.can_transfer():
         raise forms.ValidationError("This item is not eligible for transfer.")
     if TransferInvite.get_invites(bought_item).exists():
         raise ValidationError("A transfer has already been initiated for this item.")
     return bought_item
Exemplo n.º 3
0
 def form_valid(self, form):
     invite, created = TransferInvite.get_or_create(
         request=self.request,
         email=form.cleaned_data['email'],
         content=form.cleaned_data['bought_item'],
     )
     if created:
         invite.send()
     return super(TransferView, self).form_valid(form)
Exemplo n.º 4
0
 def form_valid(self, form):
     invite, created = TransferInvite.get_or_create(
         request=self.request,
         email=form.cleaned_data['email'],
         content=form.cleaned_data['bought_item'],
     )
     if created:
         invite.send()
     return super(TransferView, self).form_valid(form)
Exemplo n.º 5
0
 def clean_bought_item(self):
     bought_item = self.cleaned_data['bought_item']
     if not bought_item.can_transfer():
         raise forms.ValidationError(
             "This item is not eligible for transfer.")
     if TransferInvite.get_invites(bought_item).exists():
         raise ValidationError(
             "A transfer has already been initiated for this item.")
     return bought_item
    def test_transfer_item(self):
        """A user should be able to transfer an item on their order to an
        email address."""
        view = self.set_up_view(
            orderer=PersonFactory(),
            is_confirmed=True,
        )
        view.post(view.request)

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn('offered an item transfer', mail.outbox[0].body)

        # TODO: Use subTest after Python 3.4 upgrade
        # with self.subTest('summary view includes a pending transfer'):
        summary_view = SummaryView()
        summary_view.request = self.factory.get('/')
        summary_view.request.user = view.request.user
        summary_view.event = view.event
        summary_view.order = view.order
        summary_view.workflow = RegistrationWorkflow(order=view.order,
                                                     event=view.event)
        summary_view.current_step = summary_view.workflow.steps.get(
            summary_view.current_step_slug)
        response = summary_view.get(summary_view.request)
        self.assertEqual(len(response.context_data['pending_transfers']), 1)
        self.assertEqual(
            response.context_data['pending_transfers'][0]['bought_item'],
            view.order.bought_items.first())
        self.assertIn('invite', response.context_data['pending_transfers'][0])
        self.assertEqual(len(response.context_data['transferred_items']), 0)

        # with self.subTest('summary view includes a completed transfer'):
        invite = response.context_data['pending_transfers'][0]['invite']
        transfer_invite = TransferInvite(summary_view.request, invite)
        transfer_invite.accept()
        response = summary_view.get(summary_view.request)
        self.assertEqual(len(response.context_data['pending_transfers']), 0)
        self.assertEqual(len(response.context_data['transferred_items']), 1)
Exemplo n.º 7
0
 def test_email_subject__transfer(self):
     request = self.factory.get('/')
     request.user = self.person
     invite, created = TransferInvite.get_or_create(
         request=request,
         email='*****@*****.**',
         content=self.bought_item,
     )
     invite.send()
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(
         mail.outbox[0].subject,
         "{} wants to transfer an item to you".format(
             self.person.get_full_name()),
     )
Exemplo n.º 8
0
 def test_email_subject__transfer(self):
     request = self.factory.get('/')
     request.user = self.person
     invite, created = TransferInvite.get_or_create(
         request=request,
         email='*****@*****.**',
         content=self.bought_item,
     )
     invite.send()
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(
         mail.outbox[0].subject,
         "{} wants to transfer an item to you".format(
             self.person.get_full_name()
         ),
     )
Exemplo n.º 9
0
    def get_context_data(self, **kwargs):
        context = super(SummaryView, self).get_context_data(**kwargs)

        context.update({
            'attendees': self.order.attendees.all(),
            'new_card_form': getattr(self, 'new_card_form', None),
            'choose_card_form': getattr(self, 'choose_card_form', None),
            'check_form': getattr(self, 'check_form', None),
            'net_balance': self.net_balance,
            'STRIPE_PUBLISHABLE_KEY': getattr(settings,
                                              'STRIPE_PUBLISHABLE_KEY',
                                              ''),
            'STRIPE_TEST_PUBLISHABLE_KEY': getattr(settings,
                                                   'STRIPE_TEST_PUBLISHABLE_KEY',
                                                   ''),
        })
        context.update(self.summary_data)

        # TODO: Improve this overall assembly. It's possible the data model
        # will need to change to allow this. Invite doesn't have a reference
        # back to its content though, just an IntegerField content_id.
        bought_items = self.order.bought_items.filter(status=BoughtItem.BOUGHT)
        invites = TransferInvite.get_invites(bought_items)
        context['pending_transfers'] = [
            dict(
                invite=invite,
                bought_item=bought_items.get(pk=invite.content_id),
            )
            for invite in invites
        ]

        # This is pretty awful and hopefully only used as a stopgap measure
        # before Invites get refactored. Then again, this comment is one of
        # those kinds that stick around for years.
        context['transferred_items'] = BoughtItem.objects.filter(
            transactions__related_transaction__transaction_type=Transaction.TRANSFER,
            transactions__related_transaction__order=self.order,
        ).prefetch_related(Prefetch(
            'transactions',
            queryset=Transaction.objects.filter(transaction_type=Transaction.TRANSFER),
            to_attr='transfer_transactions',
        ))
        return context
Exemplo n.º 10
0
 def test_subject__transfer(self):
     person = PersonFactory(first_name="Conan", last_name="O'Brien")
     event = EventFactory()
     order = OrderFactory(event=event, person=person)
     TransactionFactory(event=event, order=order, amount=130)
     item = ItemFactory(event=event, name='Multipass')
     item_option1 = ItemOptionFactory(price=100, item=item, name='Gold')
     order.add_to_cart(item_option1)
     boughtitem = order.bought_items.all()[0]
     request = self.factory.get('/')
     request.user = person
     invite, created = TransferInvite.get_or_create(
         request=request,
         email='*****@*****.**',
         content=boughtitem,
     )
     invite.send()
     self.assertEqual(len(mail.outbox), 1)
     self.assertEqual(
         mail.outbox[0].subject,
         "{} wants to transfer an item to you".format(
             person.get_full_name()))
Exemplo n.º 11
0
 def clean_boughtitem(self):
     bought_item = self.cleaned_data['bought_item']
     if TransferInvite.get_invites(bought_item).exists():
         raise ValidationError(
             "A transfer has already been initiated for this item.")
     return bought_item
    def test_summary_data__transfer(self):
        """Items sent or received in a transfer shouldn't count towards net cost and savings"""
        event = EventFactory()
        order = OrderFactory(
            event=event,
            person=PersonFactory(),
        )
        person2 = PersonFactory()
        transaction = TransactionFactory(
            event=event,
            order=order,
            is_confirmed=True,
            transaction_type=Transaction.PURCHASE,
            amount=80,
        )
        item = ItemFactory(event=event)
        item_option = ItemOptionFactory(price=100, item=item)
        discount = DiscountFactory(amount=20, event=event, item_options=[item_option])

        order.add_to_cart(item_option)
        order.add_discount(discount)
        order.mark_cart_paid(transaction)

        bought_item = transaction.bought_items.get()
        invite = TransferInvite.get_or_create(
            request=Mock(user=person2, session={}),
            email=person2.email,
            content=bought_item,
        )[0]
        invite.accept()

        summary_data = order.get_summary_data()
        self.assertEqual(summary_data['gross_cost'], 100)
        self.assertEqual(summary_data['total_savings'], -20)
        self.assertEqual(summary_data['total_refunds'], 0)
        self.assertEqual(summary_data['total_payments'], 80)
        self.assertEqual(summary_data['net_cost'], 80)
        self.assertEqual(summary_data['net_balance'], 0)
        self.assertEqual(summary_data['unconfirmed_check_payments'], False)
        transactions = summary_data['transactions']
        self.assertEqual(len(transactions), 2)
        transfer_txn, transfer_txn_dict = transactions.items()[0]
        self.assertEqual(transfer_txn.transaction_type, Transaction.TRANSFER)
        self.assertEqual(transfer_txn_dict['items'], [bought_item])
        self.assertEqual(transfer_txn_dict['gross_cost'], 0)
        self.assertEqual(transfer_txn_dict['discounts'], [])
        self.assertEqual(transfer_txn_dict['total_savings'], 0)
        self.assertEqual(transfer_txn_dict['net_cost'], 0)
        purchase_txn, purchase_txn_dict = transactions.items()[1]
        self.assertEqual(purchase_txn.transaction_type, Transaction.PURCHASE)
        self.assertEqual(purchase_txn_dict['items'], [bought_item])
        self.assertEqual(purchase_txn_dict['gross_cost'], 100)
        self.assertEqual(purchase_txn_dict['discounts'], [bought_item.discounts.first()])
        self.assertEqual(purchase_txn_dict['total_savings'], -20)
        self.assertEqual(purchase_txn_dict['net_cost'], 80)

        order2 = Order.objects.get(event=event, person=person2)
        summary_data2 = order2.get_summary_data()
        self.assertEqual(summary_data2['gross_cost'], 0)
        self.assertEqual(summary_data2['total_savings'], 0)
        self.assertEqual(summary_data2['total_refunds'], 0)
        self.assertEqual(summary_data2['total_payments'], 0)
        self.assertEqual(summary_data2['net_cost'], 0)
        self.assertEqual(summary_data2['net_balance'], 0)
        self.assertEqual(summary_data2['unconfirmed_check_payments'], False)
        transactions2 = summary_data2['transactions']
        self.assertEqual(len(transactions2), 1)
        transfer_txn2, transfer_txn2_dict = transactions.items()[0]
        self.assertEqual(transfer_txn2.transaction_type, Transaction.TRANSFER)
        self.assertEqual(transfer_txn2_dict['items'], [bought_item])
        self.assertEqual(transfer_txn2_dict['gross_cost'], 0)
        self.assertEqual(transfer_txn2_dict['discounts'], [])
        self.assertEqual(transfer_txn2_dict['total_savings'], 0)
        self.assertEqual(transfer_txn2_dict['net_cost'], 0)
Exemplo n.º 13
0
    def test_summary_data__transfer(self):
        """Items sent or received in a transfer shouldn't count towards net cost and savings"""
        event = EventFactory()
        order = OrderFactory(
            event=event,
            person=PersonFactory(),
        )
        person2 = PersonFactory()
        transaction = TransactionFactory(
            event=event,
            order=order,
            is_confirmed=True,
            transaction_type=Transaction.PURCHASE,
            amount=80,
        )
        item = ItemFactory(event=event)
        item_option = ItemOptionFactory(price=100, item=item)
        discount = DiscountFactory(amount=20,
                                   event=event,
                                   item_options=[item_option])

        order.add_to_cart(item_option)
        order.add_discount(discount)
        order.mark_cart_paid(transaction)

        bought_item = transaction.bought_items.get()
        invite = TransferInvite.get_or_create(
            request=Mock(user=person2, session={}),
            email=person2.email,
            content=bought_item,
        )[0]
        invite.accept()

        summary_data = order.get_summary_data()
        self.assertEqual(summary_data['gross_cost'], 100)
        self.assertEqual(summary_data['total_savings'], -20)
        self.assertEqual(summary_data['total_refunds'], 0)
        self.assertEqual(summary_data['total_payments'], 80)
        self.assertEqual(summary_data['net_cost'], 80)
        self.assertEqual(summary_data['net_balance'], 0)
        self.assertEqual(summary_data['unconfirmed_check_payments'], False)
        transactions = summary_data['transactions']
        self.assertEqual(len(transactions), 2)
        transfer_txn, transfer_txn_dict = transactions.items()[0]
        self.assertEqual(transfer_txn.transaction_type, Transaction.TRANSFER)
        self.assertEqual(transfer_txn_dict['items'], [bought_item])
        self.assertEqual(transfer_txn_dict['gross_cost'], 0)
        self.assertEqual(transfer_txn_dict['discounts'], [])
        self.assertEqual(transfer_txn_dict['total_savings'], 0)
        self.assertEqual(transfer_txn_dict['net_cost'], 0)
        purchase_txn, purchase_txn_dict = transactions.items()[1]
        self.assertEqual(purchase_txn.transaction_type, Transaction.PURCHASE)
        self.assertEqual(purchase_txn_dict['items'], [bought_item])
        self.assertEqual(purchase_txn_dict['gross_cost'], 100)
        self.assertEqual(purchase_txn_dict['discounts'],
                         [bought_item.discounts.first()])
        self.assertEqual(purchase_txn_dict['total_savings'], -20)
        self.assertEqual(purchase_txn_dict['net_cost'], 80)

        order2 = Order.objects.get(event=event, person=person2)
        summary_data2 = order2.get_summary_data()
        self.assertEqual(summary_data2['gross_cost'], 0)
        self.assertEqual(summary_data2['total_savings'], 0)
        self.assertEqual(summary_data2['total_refunds'], 0)
        self.assertEqual(summary_data2['total_payments'], 0)
        self.assertEqual(summary_data2['net_cost'], 0)
        self.assertEqual(summary_data2['net_balance'], 0)
        self.assertEqual(summary_data2['unconfirmed_check_payments'], False)
        transactions2 = summary_data2['transactions']
        self.assertEqual(len(transactions2), 1)
        transfer_txn2, transfer_txn2_dict = transactions.items()[0]
        self.assertEqual(transfer_txn2.transaction_type, Transaction.TRANSFER)
        self.assertEqual(transfer_txn2_dict['items'], [bought_item])
        self.assertEqual(transfer_txn2_dict['gross_cost'], 0)
        self.assertEqual(transfer_txn2_dict['discounts'], [])
        self.assertEqual(transfer_txn2_dict['total_savings'], 0)
        self.assertEqual(transfer_txn2_dict['net_cost'], 0)