Exemplo n.º 1
0
 def create_charge_on_card(card, amount, unit, descr=None, stmt_descr=None):
     #pylint: disable=too-many-arguments,unused-argument
     LOGGER.debug('create_charge_on_card(amount=%s, unit=%s, descr=%s)',
                  amount, unit, descr)
     created_at = datetime_or_now()
     return (generate_random_slug(), created_at, '1234',
             created_at + datetime.timedelta(days=365))
Exemplo n.º 2
0
 def create_transfer(organization, amount, descr=None):
     """
     Transfer *amount* into the organization bank account.
     """
     LOGGER.debug("create_transfer(organization=%s, amount=%s, descr=%s)", organization, amount, descr)
     created_at = datetime_or_now()
     return (generate_random_slug(), created_at)
 def add_grant_key(self):
     self.stdout.write("BEGIN;")
     for subscription in Subscription.objects.filter(
             created_at='2018-08-13 07:54:31.875025-07'):
         self.stdout.write("UPDATE saas_subscription SET grant_key='%s'"\
             " WHERE id=%d;" % (generate_random_slug(), subscription.id))
     self.stdout.write("COMMIT;")
Exemplo n.º 4
0
 def create_transfer(organization, amount, descr=None):
     """
     Transfer *amount* into the organization bank account.
     """
     LOGGER.debug('create_transfer(organization=%s, amount=%s, descr=%s)',
                  organization, amount, descr)
     created_at = datetime_or_now()
     return (generate_random_slug(), created_at)
Exemplo n.º 5
0
 def create_charge_on_card(card, amount, unit,
     descr=None, stmt_descr=None):
     #pylint: disable=too-many-arguments,unused-argument
     LOGGER.debug('create_charge_on_card(amount=%s, unit=%s, descr=%s)',
         amount, unit, descr)
     created_at = datetime_or_now()
     return (generate_random_slug(), created_at,
         '1234', created_at + datetime.timedelta(days=365))
Exemplo n.º 6
0
 def generate_transactions(self,
                           provider,
                           processor,
                           from_date,
                           ends_at,
                           fake=None,
                           profile_pictures_dir=None):
     """
     Create Income transactions that represents a growing bussiness.
     """
     #pylint: disable=too-many-locals
     from saas.metrics.base import month_periods  # avoid import loop
     if not fake:
         fake = Faker()
     user_model = get_user_model()
     # Load list of profile pcitures
     profile_pictures_males = []
     profile_pictures_females = []
     if profile_pictures_dir:
         for picture_name in os.listdir(profile_pictures_dir):
             if picture_name.startswith("1"):
                 profile_pictures_males += [
                     "/media/livedemo/profiles/%s" % picture_name
                 ]
             else:
                 profile_pictures_females += [
                     "/media/livedemo/profiles/%s" % picture_name
                 ]
     for end_period in month_periods(from_date=from_date):
         nb_new_customers = random.randint(0, 9)
         for _ in range(nb_new_customers):
             queryset = Plan.objects.filter(organization=provider,
                                            period_amount__gt=0)
             plan = queryset[random.randint(0, queryset.count() - 1)]
             created = False
             trials = 0
             while not created:
                 try:
                     picture = None
                     if random.randint(0, 1):
                         full_name = fake.name_male()
                         if profile_pictures_males:
                             picture = profile_pictures_males[
                                 random.randint(
                                     0,
                                     len(profile_pictures_males) - 1)]
                     else:
                         full_name = fake.name_female()
                         if profile_pictures_females:
                             picture = profile_pictures_females[
                                 random.randint(
                                     0,
                                     len(profile_pictures_females) - 1)]
                     slug = slugify('demo%d' % random.randint(1, 1000))
                     email = "%s@%s" % (slug, fake.domain_name())
                     first_name, mid_name, last_name = \
                         full_name_natural_split(full_name)
                     customer, created = Organization.objects.get_or_create(
                         slug=slug,
                         full_name=full_name,
                         email=email,
                         phone=fake.phone_number(),
                         street_address=fake.street_address(),
                         locality=fake.city(),
                         postal_code=fake.postcode(),
                         region=fake.state_abbr(),
                         country=fake.country_code(),
                         picture=picture)
                     user, created = user_model.objects.get_or_create(
                         username=slug,
                         email=email,
                         first_name=first_name,
                         last_name=last_name)
                     customer.add_manager(user, at_time=end_period)
                 #pylint: disable=catching-non-exception
                 except IntegrityError:
                     trials = trials + 1
                     if trials > 10:
                         raise RuntimeError(
                             'impossible to create a new customer after 10 trials.'
                         )
             Organization.objects.filter(pk=customer.id).update(
                 created_at=end_period)
             subscription = Subscription.objects.create(
                 organization=customer,
                 plan=plan,
                 ends_at=ends_at + datetime.timedelta(days=31))
             Subscription.objects.filter(pk=subscription.id).update(
                 created_at=end_period)
         # Insert some churn in %
         churn_rate = 2
         all_subscriptions = Subscription.objects.filter(
             plan__organization=provider)
         nb_churn_customers = (all_subscriptions.count() * churn_rate //
                               100)
         subscriptions = random.sample(
             list(all_subscriptions),
             all_subscriptions.count() - nb_churn_customers)
         for subscription in subscriptions:
             nb_periods = random.randint(1, 6)
             subscription.ends_at = subscription.plan.end_of_period(
                 subscription.ends_at, nb_periods=nb_periods)
             transaction_item = Transaction.objects.new_subscription_order(
                 subscription,
                 amount=subscription.plan.period_amount * nb_periods,
                 descr=humanize.describe_buy_periods(
                     subscription.plan, subscription.ends_at, nb_periods),
                 created_at=end_period)
             if transaction_item.dest_amount < 50:
                 continue
             subscription.save()
             transaction_item.orig_amount = transaction_item.dest_amount
             transaction_item.orig_unit = transaction_item.dest_unit
             transaction_item.save()
             charge = Charge.objects.create(
                 created_at=transaction_item.created_at,
                 amount=transaction_item.dest_amount,
                 customer=subscription.organization,
                 description=humanize.DESCRIBE_CHARGED_CARD % {
                     'charge': generate_random_slug(prefix='ch_'),
                     'organization': subscription.organization
                 },
                 last4=1241,
                 exp_date=datetime_or_now(),
                 processor=processor,
                 processor_key="ch_%s" % str(transaction_item.pk),
                 state=Charge.CREATED)
             charge.created_at = transaction_item.created_at
             charge.save()
             ChargeItem.objects.create(invoiced=transaction_item,
                                       charge=charge)
             charge.payment_successful()
         churned = all_subscriptions.exclude(
             pk__in=[subscription.pk for subscription in subscriptions])
         for subscription in churned:
             subscription.ends_at = end_period
             subscription.save()
         self.stdout.write(
             "%d new and %d churned customers at %s\n" %
             (nb_new_customers, nb_churn_customers, end_period))
Exemplo n.º 7
0
 def create_charge(organization, amount, unit, descr=None, stmt_descr=None):
     # pylint: disable=too-many-arguments,unused-argument
     LOGGER.debug("create_charge(amount=%s, unit=%s, descr=%s)", amount, unit, descr)
     created_at = datetime_or_now()
     return (generate_random_slug(), created_at, "1234", created_at + datetime.timedelta(days=365))