Esempio n. 1
0
    def extend_account(self, plan, pricing):
        """
        Manages extending account after plan or pricing order
        :param plan:
        :param pricing: if pricing is None then account will be only upgraded
        :return:
        """

        status = False  # flag; if extending account was successful?
        if pricing is None:
            # Process a plan change request (downgrade or upgrade)
            # No account activation or extending at this point
            self.plan = plan
            self.save()
            account_change_plan.send(sender=self, user=self.user)
            mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan})
            send_template_email([self.user.email], 'mail/change_plan_title.txt', 'mail/change_plan_body.txt',
                                mail_context, get_user_language(self.user))
            accounts_logger.info(
                "Account '%s' [id=%d] plan changed to '%s' [id=%d]" % (self.user, self.user.pk, plan, plan.pk))
            status = True
        else:
            # Processing standard account extending procedure
            if self.plan == plan:
                status = True
                if self.expire is None:
                    pass
                elif self.expire > date.today():
                    self.expire += timedelta(days=pricing.period)
                else:
                    self.expire = date.today() + timedelta(days=pricing.period)

            else:
                # This should not ever happen (as this case should be managed by plan change request)
                # but just in case we consider a case when user has a different plan
                if self.expire is None:
                    status = True
                elif self.expire > date.today():
                    status = False
                    accounts_logger.warning("Account '%s' [id=%d] plan NOT changed to '%s' [id=%d]" % (
                        self.user, self.user.pk, plan, plan.pk))
                else:
                    status = True
                    account_change_plan.send(sender=self, user=self.user)
                    self.plan = plan
                    self.expire = date.today() + timedelta(days=pricing.period)

            if status:
                self.save()
                accounts_logger.info("Account '%s' [id=%d] has been extended by %d days using plan '%s' [id=%d]" % (
                    self.user, self.user.pk, pricing.period, plan, plan.pk))
                mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan, 'pricing': pricing})
                send_template_email([self.user.email], 'mail/extend_account_title.txt', 'mail/extend_account_body.txt',
                                    mail_context, get_user_language(self.user))

        if status:
            self.clean_activation()

        return status
Esempio n. 2
0
    def extend_account(self, plan, pricing):
        """
        Manages extending account after plan or pricing order
        :param plan:
        :param pricing: if pricing is None then account will be only upgraded
        :return:
        """

        status = False  # flag; if extending account was successful?
        if pricing is None:
            # Process a plan change request (downgrade or upgrade)
            # No account activation or extending at this point
            self.plan = plan
            self.save()
            account_change_plan.send(sender=self, user=self.user)
            mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan})
            send_template_email([self.user.email], 'mail/change_plan_title.txt', 'mail/change_plan_body.txt',
                                mail_context, get_user_language(self.user))
            accounts_logger.info(
                "Account '%s' [id=%d] plan changed to '%s' [id=%d]" % (self.user, self.user.pk, plan, plan.pk))
            status = True
        else:
            # Processing standard account extending procedure
            if self.plan == plan:
                status = True
                if self.expire is None:
                    pass
                elif self.expire > date.today():
                    self.expire += timedelta(days=pricing.period)
                else:
                    self.expire = date.today() + timedelta(days=pricing.period)

            else:
                # This should not ever happen (as this case should be managed by plan change request)
                # but just in case we consider a case when user has a different plan
                if self.expire is None:
                    status = True
                elif self.expire > date.today():
                    status = False
                    accounts_logger.warning("Account '%s' [id=%d] plan NOT changed to '%s' [id=%d]" % (
                        self.user, self.user.pk, plan, plan.pk))
                else:
                    status = True
                    account_change_plan.send(sender=self, user=self.user)
                    self.plan = plan
                    self.expire = date.today() + timedelta(days=pricing.period)

            if status:
                self.save()
                accounts_logger.info("Account '%s' [id=%d] has been extended by %d days using plan '%s' [id=%d]" % (
                    self.user, self.user.pk, pricing.period, plan, plan.pk))
                mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan, 'pricing': pricing})
                send_template_email([self.user.email], 'mail/extend_account_title.txt', 'mail/extend_account_body.txt',
                                    mail_context, get_user_language(self.user))

        if status:
            self.clean_activation()

        return status
Esempio n. 3
0
    def create(cls, order, invoice_type):
        language_code = get_user_language(order.user)

        if language_code is not None:
            translation.activate(language_code)

        try:
            billing_info = BillingInfo.objects.get(user=order.user)
        except BillingInfo.DoesNotExist:
            return

        day = date.today()
        pday = order.completed
        if invoice_type == Invoice.INVOICE_TYPES['PROFORMA']:
            pday = day + timedelta(days=14)

        invoice = cls(
            issued=day, selling_date=order.completed, payment_date=pday
        )  # FIXME: 14 - this should set accordingly to ORDER_TIMEOUT in days
        invoice.type = invoice_type
        invoice.copy_from_order(order)
        invoice.set_issuer_invoice_data()
        invoice.set_buyer_invoice_data(billing_info)
        invoice.clean()
        invoice.save()
        if language_code is not None:
            translation.deactivate()
Esempio n. 4
0
    def create(cls, order, invoice_type):
        language_code = get_user_language(order.user)

        if language_code is not None:
            translation.activate(language_code)

        try:
            billing_info = BillingInfo.objects.get(user=order.user)
        except BillingInfo.DoesNotExist:
            return

        day = date.today()
        pday = order.completed
        if invoice_type == Invoice.INVOICE_TYPES['PROFORMA']:
            pday = day + timedelta(days=14)

        invoice = cls(issued=day, selling_date=order.completed,
                      payment_date=pday)  # FIXME: 14 - this should set accordingly to ORDER_TIMEOUT in days
        invoice.type = invoice_type
        invoice.copy_from_order(order)
        invoice.set_issuer_invoice_data()
        invoice.set_buyer_invoice_data(billing_info)
        invoice.clean()
        invoice.save()
        if language_code is not None:
            translation.deactivate()
Esempio n. 5
0
    def extend_account(self, plan, pricing):
        """
        Manages extending account after plan or pricing order
        :param plan:
        :param pricing: if pricing is None then account will be only upgraded
        :return:
        """

        # Processing standard account extending procedure
        if self.plan != plan:
            self.plan = plan
            
            account_change_plan.send(sender=self, user=self.user)
            mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan})
            send_template_email([self.user.email], 'mail/change_plan_title.txt', 'mail/change_plan_body.txt',
                                mail_context, get_user_language(self.user))
                                
                
        if self.expire is None:
            pass
        elif self.expire > date.today():
            self.expire += timedelta(days=pricing.period)
        else:
            self.expire = date.today() + timedelta(days=pricing.period)

        self.save()
        accounts_logger.info("Account '%s' [id=%d] has been extended by %d days using plan '%s' [id=%d]" % (
            self.user, self.user.pk, pricing.period, plan, plan.pk))
        #mail_context = Context({'user': self.user, 'userplan': self, 'plan': plan, 'pricing': pricing})
        #send_template_email([self.user.email], 'mail/extend_account_title.txt', 'mail/extend_account_body.txt',
        #                    mail_context, get_user_language(self.user))

        self.clean_activation()

        return True
Esempio n. 6
0
 def remind_expire_soon(self):
     """reminds about soon account expiration"""
     mail_context = {'user': self.user,
                     'userplan': self,
                     'days': self.days_left()}
     send_template_email([self.user.email],
                         'mail/remind_expire_title.txt',
                         'mail/remind_expire_body.txt',
                         mail_context, get_user_language(self.user))
Esempio n. 7
0
    def remind_expire_soon(self):
        """reminds about soon account expiration"""

        mail_context = {
            'user': self.user,
            'userplan': self,
            'days': self.days_left()
        }
        send_template_email([self.user.email], 'mail/remind_expire_title.txt', 'mail/remind_expire_body.txt',
                            mail_context, get_user_language(self.user))
Esempio n. 8
0
    def remind_expire_soon(self):
        """reminds about soon account expiration"""

        mail_context = Context({"user": self.user, "userplan": self, "days": self.days_left()})
        send_template_email(
            [self.user.email],
            "mail/remind_expire_title.txt",
            "mail/remind_expire_body.txt",
            mail_context,
            get_user_language(self.user),
        )
Esempio n. 9
0
    def expire_account(self):
        """manages account expiration"""

        self.deactivate()

        accounts_logger.info("Account '%s' [id=%d] has expired" % (self.user, self.user.pk))

        mail_context = Context({'user': self.user, 'userplan': self})
        send_template_email([self.user.email], 'mail/expired_account_title.txt', 'mail/expired_account_body.txt',
                            mail_context, get_user_language(self.user))

        account_expired.send(sender=self, user=self.user)
Esempio n. 10
0
    def expire_account(self):
        """manages account expiration"""
        self.deactivate()
        accounts_logger.info("Account '{0}' [id={1}] has expired".format(
            self.user, self.user.pk))
        mail_context = {'user': self.user, 'userplan': self}
        send_template_email([self.user.email],
                            'mail/expired_account_title.txt',
                            'mail/expired_account_body.txt', mail_context,
                            get_user_language(self.user))

        account_expired.send(sender=self, user=self.user)
Esempio n. 11
0
    def send_invoice_by_email(self):
        language_code = get_user_language(self.user)

        if language_code is not None:
            translation.activate(language_code)
        mail_context = {'user': self.user,
                        'invoice_type': self.get_type_display(),
                        'invoice_number': self.get_full_number(),
                        'order': self.order.id,
                        'url': self.get_absolute_url(), }
        if language_code is not None:
            translation.deactivate()
        send_template_email([self.user.email], 'mail/invoice_created_title.txt', 'mail/invoice_created_body.txt',
                            mail_context, language_code)
Esempio n. 12
0
    def send_invoice_by_email(self):
        language_code = get_user_language(self.user)

        if language_code is not None:
            translation.activate(language_code)
        mail_context = Context({'user': self.user,
                                'invoice_type' : unicode(self.get_type_display()),
                                'invoice_number': self.get_full_number(),
                                'order' : self.order.id,
                                'url' : self.get_absolute_url(),
                                })
        if language_code is not None:
            translation.deactivate()
        send_template_email([self.user.email], 'mail/invoice_created_title.txt', 'mail/invoice_created_body.txt', mail_context, language_code)
Esempio n. 13
0
    def send_invoice_by_email(self):
        if self.type in getattr(settings, 'PLANS_SEND_EMAILS_DISABLED_INVOICE_TYPES', []):
            return

        language_code = get_user_language(self.user)

        if language_code is not None:
            translation.activate(language_code)
        mail_context = {'user': self.user,
                        'invoice_type': self.get_type_display(),
                        'invoice_number': self.get_full_number(),
                        'order': self.order.id,
                        'order_object': self.order,
                        'url': self.get_absolute_url(), }
        if language_code is not None:
            translation.deactivate()
        send_template_email([self.user.email], 'mail/invoice_created_title.txt', 'mail/invoice_created_body.txt',
                            mail_context, language_code)
Esempio n. 14
0
def manage_expired_subscriptions():
    expired_user_plans = UserPlan.objects.filter(expire__lt=date.today(),
                                                 active=True)
    for user_plan in expired_user_plans:
        user_plan.active = False
        user_plan.save()
        user = user_plan.user
        plan_slug = user_plan.plan
        radius_user_groups = user.radiususergroup_set \
                                 .filter(groupname__contains=slugify(plan_slug)) \
                                 .select_related('group')
        for rug in radius_user_groups:
            org = rug.group.organization
            temporary = get_or_create_temporary_radius_group(org)
            rug.group = temporary
            rug.full_clean()
            rug.save()
            last_order = user.order_set.last()
            try:
                plan_pricing = PlanPricing.objects.get(plan=last_order.plan,
                                                       pricing=last_order.pricing)
            except (AttributeError, PlanPricing.DoesNotExist):
                plan_pricing = user_plan.plan.planpricing_set.first()
            payment = create_order(user, plan_pricing, None, org, renew=True)
            site = Site.objects.first()
            url = reverse('subscriptions:process_payment', args=[payment.pk])
            protocol = 'https' if not settings.DEBUG else 'https'
            url = '{0}://{1}{2}'.format(protocol, site.domain, url)
            mail_context = {
                'user': user,
                'plan': plan_pricing.plan,
                'pricing': plan_pricing.pricing,
                'url': url,
                'site_name': site.name
            }
            language_code = get_user_language(user)
            send_template_email([user.email],
                                'mail/subscription_expired_subject.txt',
                                'mail/subscription_expired_body.txt',
                                mail_context,
                                language_code)
Esempio n. 15
0
    def send_invoice_by_email(self):
        language_code = get_user_language(self.user)

        if language_code is not None:
            translation.activate(language_code)
        mail_context = Context(
            {
                "user": self.user,
                "invoice_type": self.get_type_display(),
                "invoice_number": self.get_full_number(),
                "order": self.order.id,
                "url": self.get_absolute_url(),
            }
        )
        if language_code is not None:
            translation.deactivate()
        send_template_email(
            [self.user.email],
            "mail/invoice_created_title.txt",
            "mail/invoice_created_body.txt",
            mail_context,
            language_code,
        )
Esempio n. 16
0
def send_invoice_by_email(self):
    if self.type != self.INVOICE_TYPES.INVOICE:
        return
    language_code = get_user_language(self.user)
    if language_code is not None:
        translation.activate(language_code)
    mail_context = {
        'user': self.user,
        'invoice_type': self.get_type_display(),
        'invoice_number': self.get_full_number(),
        'order': self.order.id
    }
    if language_code is not None:
        translation.deactivate()
    attachments = [[
        self.get_invoice_pdf_filename(),
        self.generate_invoice_pdf().getvalue(), 'application/pdf'
    ]]
    send_template_email([self.user.email],
                        'mail/invoice_created_title.txt',
                        'mail/invoice_created_body.txt',
                        mail_context,
                        language_code,
                        attachments=attachments)
Esempio n. 17
0
    def extend_account(self, plan, pricing):
        """
        Manages extending account after plan or pricing order
        :param plan:
        :param pricing: if pricing is None then account will be only upgraded
        :return:
        """

        status = False  # flag; if extending account was successful?
        expire = self.get_plan_extended_until(plan, pricing)
        if pricing is None:
            # Process a plan change request (downgrade or upgrade)
            # No account activation or extending at this point
            self.plan = plan

            if self.expire is not None and not plan.planpricing_set.count():
                # Assume no expiry date for plans without pricing.
                self.expire = None

            self.save()
            account_change_plan.send(sender=self, user=self.user)
            if getattr(settings, 'PLANS_SEND_EMAILS_PLAN_CHANGED', True):
                mail_context = {
                    'user': self.user,
                    'userplan': self,
                    'plan': plan
                }
                send_template_email([self.user.email],
                                    'mail/change_plan_title.txt',
                                    'mail/change_plan_body.txt', mail_context,
                                    get_user_language(self.user))
            accounts_logger.info(
                "Account '%s' [id=%d] plan changed to '%s' [id=%d]" %
                (self.user, self.user.pk, plan, plan.pk))
            status = True
        else:
            # Processing standard account extending procedure
            if self.plan == plan:
                status = True
            else:
                # This should not ever happen (as this case should be managed by plan change request)
                # but just in case we consider a case when user has a different plan
                if not self.plan.is_free() and self.expire is None:
                    status = True
                elif not self.plan.is_free() and self.expire > date.today():
                    status = False
                    accounts_logger.warning(
                        "Account '%s' [id=%d] plan NOT changed to '%s' [id=%d]"
                        % (self.user, self.user.pk, plan, plan.pk))
                else:
                    status = True
                    account_change_plan.send(sender=self, user=self.user)
                    self.plan = plan

            if status:
                self.expire = expire
                self.save()
                accounts_logger.info(
                    "Account '%s' [id=%d] has been extended by %d days using plan '%s' [id=%d]"
                    % (self.user, self.user.pk, pricing.period, plan, plan.pk))
                if getattr(settings, 'PLANS_SEND_EMAILS_PLAN_EXTENDED', True):
                    mail_context = {
                        'user': self.user,
                        'userplan': self,
                        'plan': plan,
                        'pricing': pricing
                    }
                    send_template_email([self.user.email],
                                        'mail/extend_account_title.txt',
                                        'mail/extend_account_body.txt',
                                        mail_context,
                                        get_user_language(self.user))

        if status:
            self.clean_activation()

        return status