Beispiel #1
0
    def initialize(self):
        self.initialized = True
        self.next_invoice_process_time = datetime.datetime.now()

        InvoicePrototype.reset_all()

        self.logger.info('BANK PROCESSOR INITIALIZED')
Beispiel #2
0
    def test_process__processed(self):
        self.assertEqual(BankInvoicePrototype._db_count(), 0)

        invoice = self.create_invoice(worker_call_count=1)

        self.assertEqual(invoice.bank_invoice_id, None)

        invoice.process()

        invoice.reload()

        self.assertEqual(BankInvoicePrototype._db_count(), 1)

        self.assertTrue(invoice.state.is_PROCESSED)

        bank_invoice = BankInvoicePrototype._db_get_object(0)

        self.assertEqual(bank_invoice.id, invoice.bank_invoice_id)
        self.assertTrue(bank_invoice.recipient_type.is_GAME_ACCOUNT)
        self.assertEqual(bank_invoice.recipient_id, self.fabric.account_id)
        self.assertTrue(bank_invoice.sender_type.is_XSOLLA)
        self.assertEqual(bank_invoice.sender_id, 0)
        self.assertTrue(bank_invoice.state.is_FORCED)
        self.assertTrue(bank_invoice.currency.is_PREMIUM)
        self.assertTrue(bank_invoice.amount, int(self.fabric.payment_sum))
        self.assertEqual(bank_invoice.operation_uid, 'bank-xsolla')
Beispiel #3
0
    def handle(self, *args, **options):

        actions = set(AccountPrototype._db_exclude(action_id=None).values_list('action_id', flat=True).order_by('action_id').distinct())

        for action_id in actions:
            print()
            print('----%s----' % action_id)
            registered_ids = set(AccountPrototype._db_filter(action_id=action_id, is_fast=False).values_list('id', flat=True))

            print('registrations: %d' % len(registered_ids))

            payers_ids = set(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                        sender_type=ENTITY_TYPE.XSOLLA,
                                                        currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', flat=True))

            payers_ids &= registered_ids

            print('payers: %d' % len(payers_ids))

            amounts = InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                                  sender_type=ENTITY_TYPE.XSOLLA,
                                                  recipient_id__in=payers_ids,
                                                  currency=CURRENCY_TYPE.PREMIUM).values_list('amount', flat=True)

            amount = sum(amounts)

            print('total gold: %d' % amount)

            if registered_ids:
                print('per account: %.2f' % (float(amount) / len(registered_ids)))

            if payers_ids:
                print('per payer: %.2f' % (float(amount) / len(payers_ids)))
Beispiel #4
0
    def test_check_frozen_expired_invoices__wrong_state(self):
        for state in INVOICE_STATE.records:
            if state.is_FROZEN:
                continue

            self.create_invoice(state=state)
            self.assertFalse(InvoicePrototype.check_frozen_expired_invoices())
            InvoicePrototype._db_all().delete()
Beispiel #5
0
    def test_get_unprocessed_invoice_success(self):
        for state in INVOICE_STATE.records:
            if not (state.is_REQUESTED or state.is_FORCED):
                continue

            invoice = self.create_invoice(state=state)
            self.assertEqual(InvoicePrototype.get_unprocessed_invoice().id, invoice.id)
            InvoicePrototype._db_all().delete()
Beispiel #6
0
    def test_process__test(self):
        self.assertEqual(BankInvoicePrototype._db_count(), 0)

        invoice = self.create_invoice(worker_call_count=1, test='1')
        invoice.process()

        invoice.reload()

        self.assertEqual(BankInvoicePrototype._db_count(), 0)

        self.assertTrue(invoice.state.is_SKIPPED_BECOUSE_TEST)
Beispiel #7
0
    def test_reset_all(self):
        for state in INVOICE_STATE.records:
            invoice = self.create_invoice()
            invoice.state = state
            invoice.save()

        InvoicePrototype.reset_all()

        self.assertEqual(InvoicePrototype._model_class.objects.filter(state=INVOICE_STATE.RESETED).count(), len(INVOICE_STATE.records) - 4)
        self.assertEqual(InvoicePrototype._model_class.objects.filter(state=INVOICE_STATE.CONFIRMED).count(), 1)
        self.assertEqual(InvoicePrototype._model_class.objects.filter(state=INVOICE_STATE.CANCELED).count(), 1)
        self.assertEqual(InvoicePrototype._model_class.objects.filter(state=INVOICE_STATE.REJECTED).count(), 1)
        self.assertEqual(InvoicePrototype._model_class.objects.filter(state=INVOICE_STATE.FORCED).count(), 1)
Beispiel #8
0
    def create_invoice(self,
                       recipient_type=ENTITY_TYPE.GAME_ACCOUNT,
                       recipient_id=3,
                       sender_type=ENTITY_TYPE.GAME_LOGIC,
                       sender_id=8,
                       currency=CURRENCY_TYPE.PREMIUM,
                       amount=317,
                       state=None,
                       description_for_sender='invoice-description-for-sender',
                       description_for_recipient='invoice-description-for-recipient',
                       operation_uid='test-uid',
                       force=False):
        invoice = InvoicePrototype.create(recipient_type=recipient_type,
                                          recipient_id=recipient_id,
                                          sender_type=sender_type,
                                          sender_id=sender_id,
                                          currency=currency,
                                          amount=amount,
                                          description_for_sender=description_for_sender,
                                          description_for_recipient=description_for_recipient,
                                          operation_uid=operation_uid,
                                          force=force)
        if state is not None:
            invoice.state = state
            invoice.save()

        return invoice
Beispiel #9
0
    def setUp(self):
        super(_BaseBuyPosponedTaskTests, self).setUp()

        create_test_map()

        self.initial_amount = 500
        self.amount = 130

        self.account = self.accounts_factory.create_account()

        self.bank_account = BankAccountPrototype.create(
            entity_type=ENTITY_TYPE.GAME_ACCOUNT, entity_id=self.account.id, currency=CURRENCY_TYPE.PREMIUM
        )
        self.bank_account.amount = self.initial_amount
        self.bank_account.save()

        self.invoice = InvoicePrototype.create(
            recipient_type=ENTITY_TYPE.GAME_ACCOUNT,
            recipient_id=self.account.id,
            sender_type=ENTITY_TYPE.GAME_LOGIC,
            sender_id=0,
            currency=CURRENCY_TYPE.PREMIUM,
            amount=-self.amount,
            description_for_sender="transaction-description-for-sender",
            description_for_recipient="transaction-description-for-recipient",
            operation_uid="transaction-operation-ui",
        )

        self.transaction = Transaction(self.invoice.id)

        self.task = None
        self.storage = None
        self.cmd_update_with_account_data__call_count = 1
        self.accounts_manages_worker = True
        self.supervisor_worker = False
Beispiel #10
0
    def test_get_unprocessed_invoice__no_invoice(self):
        for state in INVOICE_STATE.records:
            if state.is_REQUESTED or state.is_FORCED:
                continue
            self.create_invoice(state=state)

        self.assertEqual(InvoicePrototype.get_unprocessed_invoice(), None)
Beispiel #11
0
    def setUp(self):
        super(_BaseBuyPosponedTaskTests, self).setUp()

        create_test_map()

        self.initial_amount = 500
        self.amount = 130

        result, account_id, bundle_id = register_user('test_user', '*****@*****.**', '111111')
        self.account = AccountPrototype.get_by_id(account_id)

        self.bank_account = BankAccountPrototype.create(entity_type=ENTITY_TYPE.GAME_ACCOUNT,
                                                        entity_id=self.account.id,
                                                        currency=CURRENCY_TYPE.PREMIUM)
        self.bank_account.amount = self.initial_amount
        self.bank_account.save()

        self.invoice = InvoicePrototype.create(recipient_type=ENTITY_TYPE.GAME_ACCOUNT,
                                               recipient_id=self.account.id,
                                               sender_type=ENTITY_TYPE.GAME_LOGIC,
                                               sender_id=0,
                                               currency=CURRENCY_TYPE.PREMIUM,
                                               amount=-self.amount,
                                               description_for_sender='transaction-description-for-sender',
                                               description_for_recipient='transaction-description-for-recipient',
                                               operation_uid='transaction-operation-ui')

        self.transaction = Transaction(self.invoice.id)

        self.task = None
        self.storage = None
        self.cmd_update_with_account_data__call_count = 1
        self.accounts_manages_worker = True
        self.supervisor_worker = False
Beispiel #12
0
    def test_history(self):
        self.create_bank_account(self.account.id)
        history = self.create_entity_history(self.account.id)
        invoices = BankInvoicePrototype._db_all()

        histroy_ids = [invoice.id for invoice in history]

        texts = [('pgf-no-history-message', 0)]

        for invoice in invoices:
            if invoice.id in histroy_ids:
                continue

            if invoice.recipient_id == self.account.id:
                texts.append((invoice.description_for_recipient, 0))
            else:
                texts.append((invoice.description_for_sender, 0))

        for invoice in history:
            if invoice.recipient_id == self.account.id:
                texts.append((invoice.description_for_recipient, 1))
                texts.append((invoice.description_for_sender, 0))
            else:
                texts.append((invoice.description_for_recipient, 0))
                texts.append((invoice.description_for_sender, 1))

        self.check_html_ok(self.request_html(self.page_url), texts=texts)
Beispiel #13
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(is_fast=False, is_bot=False)

        # do not use accounts registered before payments turn on
        query = query.filter(self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                             self.db_date_gte('created_at', date=statistics_settings.PAYMENTS_START_DATE.date()))
        accounts = dict(query.values_list('id', 'created_at'))

        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM,
                                                    recipient_id__in=list(accounts.keys())).values_list('created_at', 'recipient_id'))

        account_ids = [id_ for created_at, id_ in invoices]

        if not account_ids:
            return 0

        delays = {account_id: min([(created_at - accounts[account_id])
                                   for created_at, id_ in invoices
                                   if id_==account_id])
                  for account_id in account_ids}

        total_time = reduce(lambda s, v: s+v, list(delays.values()), datetime.timedelta(seconds=0))

        days = float(total_time.total_seconds()) / len(account_ids) / (24*60*60)

        return days
Beispiel #14
0
    def test_buy(self):
        self.assertEqual(PostponedTaskPrototype._model_class.objects.all().count(), 0)
        self.assertEqual(InvoicePrototype._model_class.objects.all().count(), 0)

        with mock.patch('the_tale.common.postponed_tasks.prototypes.PostponedTaskPrototype.cmd_wait') as cmd_wait:
            self.purchase.buy(account=self.account)

        self.assertEqual(cmd_wait.call_count, 1)

        self.assertEqual(PostponedTaskPrototype._model_class.objects.all().count(), 1)
        self.assertEqual(InvoicePrototype._model_class.objects.all().count(), 1)

        postponed_logic = PostponedTaskPrototype._db_get_object(0).internal_logic

        self.assertTrue(isinstance(postponed_logic, BuyPremium))
        self.assertEqual(postponed_logic.account_id, self.account.id)
        self.assertEqual(postponed_logic.days, self.days)

        invoice = InvoicePrototype.get_by_id(postponed_logic.transaction.invoice_id)

        self.assertEqual(invoice.recipient_type, ENTITY_TYPE.GAME_ACCOUNT)
        self.assertEqual(invoice.recipient_id, self.account.id)
        self.assertEqual(invoice.sender_type, ENTITY_TYPE.GAME_LOGIC)
        self.assertEqual(invoice.sender_id, 0)
        self.assertEqual(invoice.currency, CURRENCY_TYPE.PREMIUM)
        self.assertEqual(invoice.amount, -self.cost)
        self.assertEqual(invoice.description_for_sender, 'premium-days-transaction-description')
        self.assertEqual(invoice.description_for_recipient, 'premium-days-transaction-description')
Beispiel #15
0
 def get_invoice_intervals_count(self, days, date):
     starts = list(InvoicePrototype._db_filter(models.Q(state=INVOICE_STATE.CONFIRMED)|models.Q(state=INVOICE_STATE.FORCED),
                                               operation_uid__contains='<%s%d' % (GOODS_GROUP.PREMIUM.uid_prefix, days),
                                               sender_type=ENTITY_TYPE.GAME_LOGIC,
                                               currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', flat=True))
     return len([True
                 for created_at in starts
                 if created_at <= datetime.datetime.combine(date, datetime.time()) < created_at + datetime.timedelta(days=days)] )
Beispiel #16
0
    def test_success(self):
        self.assertEqual(BankInvoicePrototype._db_count(), 0)
        response = self.post_ajax_json(url('shop:give-money', account=self.account.id), self.post_data(amount=5))
        self.assertEqual(BankInvoicePrototype._db_count(), 1)

        invoice = BankInvoicePrototype._db_get_object(0)

        self.assertTrue(invoice.recipient_type.is_GAME_ACCOUNT)
        self.assertEqual(invoice.recipient_id, self.account.id)
        self.assertTrue(invoice.sender_type.is_GAME_MASTER)
        self.assertEqual(invoice.sender_id, self.superuser.id)
        self.assertTrue(invoice.currency.is_PREMIUM)
        self.assertEqual(invoice.amount, 5)
        self.assertEqual(invoice.description_for_recipient, u'bla-bla')
        self.assertTrue(invoice.state.is_FORCED)

        self.check_ajax_ok(response)
Beispiel #17
0
    def get_value(self, date):
        income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                             self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                             sender_type=ENTITY_TYPE.XSOLLA,
                                             currency=CURRENCY_TYPE.PREMIUM).aggregate(income=models.Sum('amount'))['income']
        if income is None:
            return 0

        return income
Beispiel #18
0
    def initialize(self):
        super(Income, self).initialize()
        query = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                            self.db_date_gte('created_at', date=self.free_date - self.PREFETCH_DELTA),
                                            sender_type=ENTITY_TYPE.XSOLLA, currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'amount')
        invoices = [(created_at.date(), amount) for created_at, amount in query]

        self.invoices_values = {}
        for created_at, amount in invoices:
            self.invoices_values[created_at] = self.invoices_values.get(created_at, 0) + amount
Beispiel #19
0
    def check_frozen_expired_invoices(self):

        if time.time() - float(settings.get(bank_settings.SETTINGS_LAST_FROZEN_EXPIRED_CHECK_KEY, 0)) < bank_settings.FROZEN_INVOICE_EXPIRED_CHECK_TIMEOUT:
            return

        settings[bank_settings.SETTINGS_LAST_FROZEN_EXPIRED_CHECK_KEY] = str(time.time())

        if not InvoicePrototype.check_frozen_expired_invoices():
            return

        self.logger.error('We have some expired frozen invoices. Please, check them and remove or find error.')
Beispiel #20
0
 def _create_outcoming_invoice(self, amount, state=INVOICE_STATE.FROZEN):
     invoice = InvoicePrototype.create(sender_type=self.account.entity_type,
                                       sender_id=self.account.entity_id,
                                       recipient_type=ENTITY_TYPE.GAME_LOGIC,
                                       recipient_id=0,
                                       currency=CURRENCY_TYPE.PREMIUM,
                                       amount=amount,
                                       description_for_sender='outcoming invoice for sender',
                                       description_for_recipient='outcoming invoice for recipient',
                                       operation_uid='outcoming-operation-uid')
     invoice.state = state
     invoice.save()
Beispiel #21
0
    def get_value(self, date):
        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', 'amount'))

        if not invoices:
            return 0

        recipients = set(self.filter_recipients(next(zip(*invoices))))

        return sum([amount for recipient_id, amount in invoices if recipient_id in recipients], 0)
Beispiel #22
0
    def test_create(self):
        with mock.patch('the_tale.finances.bank.workers.bank_processor.Worker.cmd_init_invoice') as cmd_init_invoice:
            transaction = self.create_transaction()
        self.assertEqual(cmd_init_invoice.call_count, 1)

        self.assertEqual(InvoicePrototype._model_class.objects.all().count(), 1)

        invoice = InvoicePrototype._db_get_object(0)

        self.assertEqual(transaction.invoice_id, InvoicePrototype._db_get_object(0).id)

        self.assertTrue(invoice.state.is_REQUESTED)
        self.assertEqual(invoice.recipient_type, ENTITY_TYPE.GAME_ACCOUNT)
        self.assertEqual(invoice.recipient_id, 2)
        self.assertEqual(invoice.sender_type, ENTITY_TYPE.GAME_LOGIC)
        self.assertEqual(invoice.sender_id, 3)
        self.assertEqual(invoice.currency, CURRENCY_TYPE.PREMIUM)
        self.assertEqual(invoice.amount, 113)
        self.assertEqual(invoice.description_for_sender, 'transaction description for sender')
        self.assertEqual(invoice.description_for_recipient, 'transaction description for recipient')
        self.assertEqual(invoice.operation_uid, 'transaction-operation-uid')
Beispiel #23
0
    def test_process__wait_confirmation__transaction_confirmed__with_referal(self):
        self.invoice.state = INVOICE_STATE.CONFIRMED
        self.invoice.save()

        result, account_id, bundle_id = register_user('test_user_2', '*****@*****.**', '111111')
        self.account._model.referral_of_id = account_id
        self.account.save()

        self.task.state = self.task.RELATION.WAIT_TRANSACTION_CONFIRMATION

        self.assertEqual(self.task.process(main_task=mock.Mock()), POSTPONED_TASK_LOGIC_RESULT.SUCCESS)

        self.assertEqual(InvoicePrototype._db_count(), 2)

        referral_invoice = InvoicePrototype._db_get_object(1)

        self.assertTrue(referral_invoice.amount > 0)
        self.assertTrue(referral_invoice.amount < self.amount)
        self.assertEqual(referral_invoice.recipient_id, account_id)
        self.assertTrue(referral_invoice.state.is_FORCED)

        self.assertTrue(self.task.state.is_SUCCESSED)
Beispiel #24
0
    def get_value(self, date):
        incomes = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                              self.db_date_lte('created_at', date=date),
                                              sender_type=ENTITY_TYPE.XSOLLA,
                                              currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id', 'amount')

        accounts_incomes = {}
        for recipient_id, amount in incomes:
            accounts_incomes[recipient_id] = accounts_incomes.get(recipient_id, 0) + amount

        return sum([amount
                    for amount in accounts_incomes.values()
                    if self.BORDERS[0] < amount <= self.BORDERS[1]], 0)
Beispiel #25
0
    def initialize(self):
        super(IncomeTotal, self).initialize()
        query = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                            self.db_date_gte('created_at'),
                                            sender_type=ENTITY_TYPE.XSOLLA,
                                            currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'amount')
        invoices = [(created_at.date(), amount) for created_at, amount in query]

        invoices_values = {}
        for created_at, amount in invoices:
            invoices_values[created_at] = invoices_values.get(created_at, 0) + amount

        income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                             self.db_date_lt('created_at'),
                                             sender_type=ENTITY_TYPE.XSOLLA,
                                             currency=CURRENCY_TYPE.PREMIUM).aggregate(income=models.Sum('amount'))['income']
        if income is None:
            income = 0

        self.incomes = {}

        for date in days_range(*self._get_interval()):
            income += invoices_values.get(date, 0)
            self.incomes[date] = income
Beispiel #26
0
    def create(cls, recipient_type, recipient_id, sender_type, sender_id, currency, amount, description_for_sender, description_for_recipient, operation_uid, force=False):
        invoice = InvoicePrototype.create(recipient_type=recipient_type,
                                          recipient_id=recipient_id,
                                          sender_type=sender_type,
                                          sender_id=sender_id,
                                          currency=currency,
                                          amount=amount,
                                          description_for_sender=description_for_sender,
                                          description_for_recipient=description_for_recipient,
                                          operation_uid=operation_uid,
                                          force=force)

        environment.workers.bank_processor.cmd_init_invoice()

        return cls(invoice_id=invoice.id)
Beispiel #27
0
    def initialize(self):
        super(Payers, self).initialize()
        invoices = list(InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                    self.db_date_gte('created_at', date=self.free_date - self.PREFETCH_DELTA),
                                                    sender_type=ENTITY_TYPE.XSOLLA,
                                                    currency=CURRENCY_TYPE.PREMIUM).values_list('created_at', 'recipient_id'))

        self.invoices = {}

        for invoice_data, recipient_id in invoices:
            created_at = invoice_data.date()

            if created_at not in self.invoices:
                self.invoices[created_at] = set()

            self.invoices[created_at].add(recipient_id)
Beispiel #28
0
    def get_value(self, date):
        query = AccountPrototype._db_filter(self.db_date_interval('created_at', date=date, days=-self.PERIOD),
                                            is_fast=False,
                                            is_bot=False)
        accounts_ids = list(query.values_list('id', flat=True))

        if not accounts_ids:
            return 0

        total_income = InvoicePrototype._db_filter(ACCEPTED_INVOICE_FILTER,
                                                   sender_type=ENTITY_TYPE.XSOLLA,
                                                   currency=CURRENCY_TYPE.PREMIUM,
                                                   recipient_id__in=accounts_ids).aggregate(income=models.Sum('amount'))['income']

        if total_income is None:
            return 0

        return float(total_income) / len(accounts_ids)
Beispiel #29
0
    def process_init_invoice(self):

        invoice = InvoicePrototype.get_unprocessed_invoice()

        if invoice is None:
            return

        if (not bank_settings.ENABLE_BANK or
            settings.get(bank_settings.SETTINGS_ALLOWED_KEY) is None):
            self.logger.info('postpone invoice %d' % invoice.id)
            return

        self.logger.info('process invoice %s' % invoice.id)

        if invoice.state.is_REQUESTED:
            invoice.freeze()
        elif invoice.state.is_FORCED:
            invoice.force()
        else:
            raise BankException('unknown invoice %d state %s' % (invoice.id, invoice.state))

        self.logger.info('invoice %s status %s' % (invoice.id, invoice.state))
Beispiel #30
0
    def print_funnel(self, year, month, new_users):
        DATE_FROM = datetime.datetime(year, month, 1)

        if month == 12:
            year += 1
            month = 0

        DATE_TO = datetime.datetime(year, month + 1, 1)

        fast_registrations = sum(
            RecordPrototype.select_values(
                relations.RECORD_TYPE.REGISTRATIONS_TRIES,
                date_from=DATE_FROM,
                date_to=DATE_TO))

        registrations = sum(
            RecordPrototype.select_values(
                relations.RECORD_TYPE.REGISTRATIONS_COMPLETED,
                date_from=DATE_FROM,
                date_to=DATE_TO))

        new_accounts_ids = set(
            AccountPrototype._db_filter(created_at__gte=DATE_FROM,
                                        created_at__lte=DATE_TO).values_list(
                                            'id', flat=True))
        all_payers_ids = set(
            InvoicePrototype._db_filter(
                models.Q(state=INVOICE_STATE.CONFIRMED)
                | models.Q(state=INVOICE_STATE.FORCED),
                sender_type=ENTITY_TYPE.XSOLLA,
                currency=CURRENCY_TYPE.PREMIUM).values_list('recipient_id',
                                                            flat=True))

        payers = len(new_accounts_ids & all_payers_ids)

        alive_after_week_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=7)).values_list('id', flat=True))

        alive_after_week = len(alive_after_week_ids & new_accounts_ids)

        alive_after_1_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=30)).values_list('id', flat=True))
        alive_after_2_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=60)).values_list('id', flat=True))
        alive_after_3_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=90)).values_list('id', flat=True))
        alive_after_4_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=120)).values_list('id', flat=True))
        alive_after_5_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=150)).values_list('id', flat=True))
        alive_after_6_month_ids = set(
            AccountPrototype._db_filter(
                created_at__gte=DATE_FROM,
                created_at__lte=DATE_TO,
                active_end_at__gte=models.F('created_at') +
                datetime.timedelta(days=180)).values_list('id', flat=True))

        alive_after_1_month = len(alive_after_1_month_ids & new_accounts_ids)
        alive_after_2_month = len(alive_after_2_month_ids & new_accounts_ids)
        alive_after_3_month = len(alive_after_3_month_ids & new_accounts_ids)
        alive_after_4_month = len(alive_after_4_month_ids & new_accounts_ids)
        alive_after_5_month = len(alive_after_5_month_ids & new_accounts_ids)
        alive_after_6_month = len(alive_after_6_month_ids & new_accounts_ids)

        print('--------------------------------------')
        print('from %s to %s' % (DATE_FROM.date(), DATE_TO.date()))
        print('visitors: %d' % new_users)
        print('registration tries %d (%.3f from visitors)' %
              (fast_registrations, float(fast_registrations) / new_users))
        print('registration completed %d (%.3f from visitors)' %
              (registrations, float(registrations) / new_users))
        print('payers %d (%.3f from registrations)' %
              (payers, float(payers) / registrations))
        print('alive after week %d (%.3f from registrations)' %
              (alive_after_week, float(alive_after_week) / registrations))
        print(
            'alive after 1_month %d (%.3f from registrations)' %
            (alive_after_1_month, float(alive_after_1_month) / registrations))
        print(
            'alive after 2 month %d (%.3f from registrations)' %
            (alive_after_2_month, float(alive_after_2_month) / registrations))
        print(
            'alive after 3 month %d (%.3f from registrations)' %
            (alive_after_3_month, float(alive_after_3_month) / registrations))
        print(
            'alive after 4 month %d (%.4f from registrations)' %
            (alive_after_4_month, float(alive_after_4_month) / registrations))
        print(
            'alive after 5 month %d (%.5f from registrations)' %
            (alive_after_5_month, float(alive_after_5_month) / registrations))
        print(
            'alive after 6 month %d (%.6f from registrations)' %
            (alive_after_6_month, float(alive_after_6_month) / registrations))
Beispiel #31
0
from dext.views import handler

from the_tale.common.utils.decorators import staff_required
from the_tale.common.utils.resources import Resource

from the_tale.finances.bank.prototypes import AccountPrototype as BankAccountPrototype, InvoicePrototype
from the_tale.finances.bank.relations import ENTITY_TYPE as BANK_ENTITY_TYPE, INVOICE_STATE

from the_tale.accounts.prototypes import AccountPrototype

from the_tale.portal.developers_info.relations import PAYMENT_GROUPS
from the_tale.portal.conf import portal_settings

InvoiceQuery = InvoicePrototype._db_filter(
    models.Q(state=INVOICE_STATE.CONFIRMED)
    | models.Q(state=INVOICE_STATE.FORCED))


class RefererStatistics(
        collections.namedtuple(
            'RefererStatisticsBase',
            ('domain', 'count', 'active_accounts', 'premium_accounts',
             'active_and_premium', 'premium_currency'))):
    def __add__(self, s):
        return RefererStatistics(
            domain=self.domain,
            count=self.count + s.count,
            active_accounts=self.active_accounts + s.active_accounts,
            premium_accounts=self.premium_accounts + s.premium_accounts,
            active_and_premium=self.active_and_premium + s.active_and_premium,
Beispiel #32
0
    def process_confirm_invoice(self, invoice_id):
        invoice = InvoicePrototype.get_by_id(invoice_id)

        if invoice:
            invoice.confirm()
Beispiel #33
0
 def test_check_frozen_expired_invoices__exist(self):
     invoice = self.create_invoice(state=INVOICE_STATE.FROZEN)
     InvoicePrototype._db_filter(
         id=invoice.id).update(updated_at=datetime.datetime.now() -
                               bank_settings.FROZEN_INVOICE_EXPIRED_TIME)
     self.assertTrue(InvoicePrototype.check_frozen_expired_invoices())
Beispiel #34
0
    def process_cancel_invoice(self, invoice_id):
        invoice = InvoicePrototype.get_by_id(invoice_id)

        if invoice:
            invoice.cancel()
Beispiel #35
0
 def test_check_frozen_expired_invoices__exist(self):
     invoice = self.create_invoice(state=INVOICE_STATE.FROZEN)
     InvoicePrototype._db_filter(id=invoice.id).update(
         updated_at=datetime.datetime.now() - bank_settings.FROZEN_INVOICE_EXPIRED_TIME
     )
     self.assertTrue(InvoicePrototype.check_frozen_expired_invoices())
Beispiel #36
0
 def test_check_frozen_expired_invoices__wrong_time(self):
     self.create_invoice(state=INVOICE_STATE.FROZEN)
     self.assertFalse(InvoicePrototype.check_frozen_expired_invoices())