Exemple #1
0
    def ipn(self):
        """The main IPN handler, called by the IPN service."""
        # skip over to-be-ignored products
        if self.params.product_id in self.request.registry.settings.get(
                'bimt.products_to_ignore', '').split(','):
            logger.info('The product is listed on the ignore list: {}'.format(
                self.params.product_id))
            return 'Done.'

        # try to find an existing user with given email
        user = User.by_email(self.params.email)
        if not user:
            user = User.by_billing_email(self.params.email)

        # create a new user if no existing user found
        if not user:
            password = generate()
            user = User(
                email=self.params.email,
                billing_email=self.params.email,
                password=encrypt(password),
                fullname=u'{}'.format(self.params.fullname),
                affiliate=u'{}'.format(self.params.get('affiliate', '')),
            )
            Session.add(user)

            comment = COMMENT.format(
                u'Created',
                self.provider,
                self.params.trans_id,
                self.params.trans_type,
                '',
            )
            logger.info(comment)
            self.request.registry.notify(
                UserCreated(self.request, user, password, comment))

        # find a group that is used for given product
        group = Group.by_product_id(self.params.product_id)
        if not group:
            raise ValueError('Cannot find group with product_id "{}"'.format(
                self.params.product_id))

        # perform IPN transaction actions
        self.ipn_transaction(user, group)

        # send request with same parameters to the URL specified on group
        if group.forward_ipn_to_url:
            requests.post(
                group.forward_ipn_to_url,
                params=self.request.POST,
            )
            logger.info('IPN re-posted to {}.'.format(
                group.forward_ipn_to_url))

        logger.info('IPN done.')
        return 'Done.'
Exemple #2
0
def expire_subscriptions():
    """Find all outstanding subscriptions and expire them."""
    with transaction.manager:
        for user in User.get_all():
            if user.enabled:
                if user.valid_to < date.today():
                    user.disable()
                    msg = u'Disabled user {} ({}) because its valid_to ({}) ' \
                        'has expired.'.format(
                            user.email, user.id, user.valid_to)
                    Session.add(
                        AuditLogEntry(
                            user_id=user.id,
                            event_type_id=AuditLogEventType.by_name(
                                'UserDisabled').id,
                            comment=msg,
                        ))
                    logger.info(msg)
                    continue

                # handle addons
                for prop in user.properties:
                    if not prop.key.startswith('addon_'):
                        continue
                    if not prop.key.endswith('_valid_to'):
                        continue
                    valid_to = datetime.strptime(prop.value, '%Y-%m-%d').date()
                    if valid_to >= date.today():
                        continue
                    group = Group.by_product_id(
                        prop.key.split('addon_')[1].split('_valid_to')[0])
                    user.groups.remove(group)
                    msg = u'Addon "{}" disabled for user {} ({}) because ' \
                        'its valid_to ({}) has expired.'.format(
                            group.name, user.email, user.id, prop.value)
                    Session.add(
                        AuditLogEntry(
                            user_id=user.id,
                            event_type_id=AuditLogEventType.by_name(
                                'UserDisabled').id,
                            comment=msg,
                        ))
Exemple #3
0
 def test_valid_product_id(self):
     _make_group(name='foo', product_id=1)
     group = Group.by_product_id(1)
     self.assertEqual(group.name, 'foo')
Exemple #4
0
 def validator(node, cstruct):
     if Group.by_product_id(cstruct):
         raise colander.Invalid(
             node,
             u'Group with product id "{}" already exists.'.format(cstruct))
Exemple #5
0
 def test_invalid_product_id(self):
     self.assertEqual(Group.by_product_id(1), None)
     self.assertEqual(Group.by_product_id('foo'), None)
     self.assertEqual(Group.by_product_id(None), None)