def response_mobile_worker_creation(self): """ Get the allowed number of mobile workers based on plan version. """ from corehq.apps.accounting.models import FeatureType, FeatureRate num_users = CommCareUser.total_by_domain(self.domain.name, is_active=True) try: user_rate = self.new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') num_allowed = user_rate.monthly_limit num_extra = num_users - num_allowed if num_extra > 0: return self._fmt_alert( ungettext( "You have %(num_users)d Mobile Worker over the monthly limit of %(monthly_limit)d for " "this new plan. There will be an additional monthly charge of USD %(excess_fee)s per " "Mobile Worker, totalling USD %(monthly_total)s per month, if you select this plan.", "You have %(num_users)d Mobile Workers over the monthly limit of %(monthly_limit)d for " "this new plan. There will be an additional monthly charge of USD %(excess_fee)s per " "Mobile Worker, totalling USD %(monthly_total)s per month, if you select this plan.", num_extra ) % { 'num_users': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, } ) except FeatureRate.DoesNotExist: global_logger.error( "It seems that the plan %s did not have rate for Mobile Workers. This is problematic." % self.new_plan_version.plan.name )
def calculate_users_in_all_domains(): for domain in Domain.get_all_names(): num_users = CommCareUser.total_by_domain(domain) record_date = datetime.date.today() - relativedelta(days=1) user_history = DomainUserHistory.create(domain=domain, num_users=num_users, record_date=record_date) user_history.save()
def calculate_users_in_all_domains(): for domain in Domain.get_all_names(): num_users = CommCareUser.total_by_domain(domain) record_date = datetime.date.today() - relativedelta(days=1) user_history = DomainUserHistory.create( domain=domain, num_users=num_users, record_date=record_date ) user_history.save()
def calculate_users_in_all_domains(today=None): today = today or datetime.date.today() for domain in Domain.get_all_names(): num_users = CommCareUser.total_by_domain(domain) record_date = today - relativedelta(days=1) DomainUserHistory.objects.create( domain=domain, num_users=num_users, record_date=record_date )
def can_add_extra_mobile_workers(request): from corehq.apps.users.models import CommCareUser from corehq.apps.accounting.models import BillingAccount num_web_users = CommCareUser.total_by_domain(request.domain) user_limit = request.plan.user_limit if user_limit == -1 or num_web_users < user_limit: return True if not has_privilege(request, privileges.ALLOW_EXCESS_USERS): account = BillingAccount.get_account_by_domain(request.domain) if account is None or account.date_confirmed_extra_charges is None: return False return True
def can_add_extra_mobile_workers(request): from corehq.apps.users.models import CommCareUser from corehq.apps.accounting.models import Subscription num_web_users = CommCareUser.total_by_domain(request.domain) user_limit = request.plan.user_limit if user_limit == -1 or num_web_users < user_limit: return True if not has_privilege(request, privileges.ALLOW_EXCESS_USERS): current_subscription = Subscription.get_active_subscription_by_domain(request.domain) if current_subscription is None or current_subscription.account.date_confirmed_extra_charges is None: return False return True
def calculate_users_in_all_domains(today=None): today = today or datetime.date.today() for domain in Domain.get_all_names(): num_users = CommCareUser.total_by_domain(domain) record_date = today - relativedelta(days=1) try: DomainUserHistory.objects.create(domain=domain, num_users=num_users, record_date=record_date) except Exception as e: log_accounting_error( "Something went wrong while creating DomainUserHistory for domain %s: %s" % (domain, e), show_stack_trace=True, )
def calculate_users_in_all_domains(today=None): today = today or datetime.date.today() for domain in Domain.get_all_names(): num_users = CommCareUser.total_by_domain(domain) record_date = today - relativedelta(days=1) try: DomainUserHistory.objects.create(domain=domain, num_users=num_users, record_date=record_date) except Exception as e: log_accounting_error( "Something went wrong while creating DomainUserHistory for domain %s: %s" % (domain, e), show_stack_trace=True, ) # kick off the auto-deactivation of mobile workers after we calculate the # DomainUserHistory for projects. This ensures this feature is never abused # to get around our billing system. from corehq.apps.enterprise.tasks import auto_deactivate_mobile_workers auto_deactivate_mobile_workers.delay()
def response_mobile_worker_creation(self): """ Get the allowed number of mobile workers based on plan version. """ from corehq.apps.accounting.models import FeatureType, FeatureRate, UNLIMITED_FEATURE_USAGE num_users = CommCareUser.total_by_domain(self.domain.name, is_active=True) try: user_rate = self.new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') if user_rate.monthly_limit == UNLIMITED_FEATURE_USAGE: return num_allowed = user_rate.monthly_limit num_extra = num_users - num_allowed if num_extra > 0: return _fmt_alert( ungettext( "You have %(num_users)d Mobile Worker over the monthly " "limit of %(monthly_limit)d for this new plan. There " "will be an additional monthly charge of USD " "%(excess_fee)s per Mobile Worker, totalling USD " "%(monthly_total)s per month, if you select this plan.", "You have %(num_users)d Mobile Workers over the " "monthly limit of %(monthly_limit)d for this new plan. " "There will be an additional monthly charge " "of USD %(excess_fee)s per Mobile Worker, totalling " "USD %(monthly_total)s per month, if you " "select this plan.", num_extra ) % { 'num_users': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, } ) except FeatureRate.DoesNotExist: log_accounting_error( "It seems that the plan %s did not have rate for Mobile " "Workers. This is problematic." % self.new_plan_version.plan.name )
def base_view(request, domain, template="users/mobile/users_list.html"): page = request.GET.get('page', 1) limit = request.GET.get('limit', DEFAULT_USER_LIST_LIMIT) more_columns = json.loads(request.GET.get('more_columns', 'false')) cannot_share = json.loads(request.GET.get('cannot_share', 'false')) show_inactive = json.loads(request.GET.get('show_inactive', 'false')) total = CommCareUser.total_by_domain(domain, is_active=not show_inactive) context = _users_context(request, domain) context.update(users_list=dict( page=page, limit=limit, total=total, ), cannot_share=cannot_share, show_inactive=show_inactive, more_columns=more_columns, show_case_sharing=request.project.case_sharing_included(), pagination_limit_options=range(DEFAULT_USER_LIST_LIMIT, 51, DEFAULT_USER_LIST_LIMIT)) return render(request, template, context)
def response_mobile_worker_creation(domain, new_plan_version): """ Deactivates users if there are too many for a community plan """ from corehq.apps.accounting.models import (DefaultProductPlan, FeatureType, UNLIMITED_FEATURE_USAGE) # checks for community plan if (new_plan_version != DefaultProductPlan.get_default_plan_version()): return True # checks if unlimited is on for this user user_rate = new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') if user_rate.monthly_limit == UNLIMITED_FEATURE_USAGE: return True # checks for extra users num_users = CommCareUser.total_by_domain(domain.name, is_active=True) num_allowed = user_rate.monthly_limit if num_users > num_allowed: # offloads deactivation onto a separate thread bulk_deactivate_users.delay(domain) return True
def response_mobile_worker_creation(domain, new_plan_version): """ Deactivates users if there are too many for a community plan """ from corehq.apps.accounting.models import ( DefaultProductPlan, FeatureType, UNLIMITED_FEATURE_USAGE) # checks for community plan if (new_plan_version != DefaultProductPlan.get_default_plan_version()): return True # checks if unlimited is on for this user user_rate = new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') if user_rate.monthly_limit == UNLIMITED_FEATURE_USAGE: return True # checks for extra users num_users = CommCareUser.total_by_domain( domain.name, is_active=True) num_allowed = user_rate.monthly_limit if num_users > num_allowed: # offloads deactivation onto a separate thread # there should be a task that deactivates users here pass return True
def base_view(request, domain, template="users/mobile/users_list.html"): page = request.GET.get('page', 1) limit = request.GET.get('limit', DEFAULT_USER_LIST_LIMIT) more_columns = json.loads(request.GET.get('more_columns', 'false')) cannot_share = json.loads(request.GET.get('cannot_share', 'false')) show_inactive = json.loads(request.GET.get('show_inactive', 'false')) total = CommCareUser.total_by_domain(domain, is_active=not show_inactive) context = _users_context(request, domain) context.update( users_list=dict( page=page, limit=limit, total=total, ), cannot_share=cannot_share, show_inactive=show_inactive, more_columns=more_columns, show_case_sharing=request.project.case_sharing_included(), pagination_limit_options=range(DEFAULT_USER_LIST_LIMIT, 51, DEFAULT_USER_LIST_LIMIT) ) return render(request, template, context)
def users_list_total(self): if self.query: return self.total_users_from_es return CommCareUser.total_by_domain(self.domain, is_active=not self.show_inactive)
def response_mobile_worker_creation(self): """ Get the allowed number of mobile workers based on plan version. """ from corehq.apps.accounting.models import FeatureType, FeatureRate, UNLIMITED_FEATURE_USAGE num_users = CommCareUser.total_by_domain(self.domain.name, is_active=True) try: user_rate = self.new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') if user_rate.monthly_limit == UNLIMITED_FEATURE_USAGE: return num_allowed = user_rate.monthly_limit num_extra = num_users - num_allowed if num_extra > 0: from corehq.apps.accounting.models import DefaultProductPlan if self.new_plan_version != DefaultProductPlan.get_default_plan_version(): return _fmt_alert( ungettext( "You have %(num_extra)d Mobile Worker over the monthly " "limit of %(monthly_limit)d for this new plan. There " "will be an additional monthly charge of USD " "%(excess_fee)s per Mobile Worker, totalling USD " "%(monthly_total)s per month, if you select this plan.", "You have %(num_extra)d Mobile Workers over the " "monthly limit of %(monthly_limit)d for this new plan. " "There will be an additional monthly charge " "of USD %(excess_fee)s per Mobile Worker, totalling " "USD %(monthly_total)s per month, if you " "select this plan.", num_extra ) % { 'num_extra': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, } ) else: return _fmt_alert( ungettext( "Community plans include %(monthly_limit)s Mobile Workers by default. " "Because you have %(num_extra)d extra Mobile Worker, " "all your project's Mobile Workers will be deactivated. " "You can re-activate these manually after downgrade. " "Each active Mobile Worker over %(monthly_limit)s will result " "in an additional charge of USD %(excess_fee)s, totalling " "USD %(monthly_total)s per month.", "Community plans include %(monthly_limit)s Mobile Workers by default. " "Because you have %(num_extra)d extra Mobile Workers, " "all your project's Mobile Workers will be deactivated. " "You can re-activate these manually after downgrade. " "Each active Mobile Worker over %(monthly_limit)s will result " "in an additional charge of USD %(excess_fee)s, totalling " "USD %(monthly_total)s per month.", num_extra ) % { 'num_extra': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, } ) except FeatureRate.DoesNotExist: log_accounting_error( "It seems that the plan %s did not have rate for Mobile " "Workers. This is problematic." % self.new_plan_version.plan.name )
def _get_user_usage(self): return CommCareUser.total_by_domain(self.domain, is_active=True)
def users_list_total(self): return CommCareUser.total_by_domain(self.domain, is_active=not self.show_inactive)
def num_users(self): total_users = 0 for domain in self.subscribed_domains: total_users += CommCareUser.total_by_domain(domain, is_active=True) return total_users
def get_n_users_in_domain(domain): return CommCareUser.total_by_domain(domain, is_active=True)
def response_mobile_worker_creation(self): """ Get the allowed number of mobile workers based on plan version. """ from corehq.apps.accounting.models import FeatureType, FeatureRate, UNLIMITED_FEATURE_USAGE num_users = CommCareUser.total_by_domain(self.domain.name, is_active=True) try: user_rate = self.new_plan_version.feature_rates.filter( feature__feature_type=FeatureType.USER).latest('date_created') if user_rate.monthly_limit == UNLIMITED_FEATURE_USAGE: return num_allowed = user_rate.monthly_limit num_extra = num_users - num_allowed if num_extra > 0: from corehq.apps.accounting.models import DefaultProductPlan if self.new_plan_version != DefaultProductPlan.get_default_plan_version( ): return _fmt_alert( ungettext( "You have %(num_extra)d Mobile Worker over the monthly " "limit of %(monthly_limit)d for this new plan. There " "will be an additional monthly charge of USD " "%(excess_fee)s per Mobile Worker, totalling USD " "%(monthly_total)s per month, if you select this plan.", "You have %(num_extra)d Mobile Workers over the " "monthly limit of %(monthly_limit)d for this new plan. " "There will be an additional monthly charge " "of USD %(excess_fee)s per Mobile Worker, totalling " "USD %(monthly_total)s per month, if you " "select this plan.", num_extra) % { 'num_extra': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, }) else: return _fmt_alert( ungettext( "Community plans include %(monthly_limit)s Mobile Workers by default. " "Because you have %(num_extra)d extra Mobile Worker, " "all your project's Mobile Workers will be deactivated. " "You can re-activate these manually after downgrade. " "Each active Mobile Worker over %(monthly_limit)s will result " "in an additional charge of USD %(excess_fee)s, totalling " "USD %(monthly_total)s per month.", "Community plans include %(monthly_limit)s Mobile Workers by default. " "Because you have %(num_extra)d extra Mobile Workers, " "all your project's Mobile Workers will be deactivated. " "You can re-activate these manually after downgrade. " "Each active Mobile Worker over %(monthly_limit)s will result " "in an additional charge of USD %(excess_fee)s, totalling " "USD %(monthly_total)s per month.", num_extra) % { 'num_extra': num_extra, 'monthly_limit': user_rate.monthly_limit, 'excess_fee': user_rate.per_excess_fee, 'monthly_total': user_rate.per_excess_fee * num_extra, }) except FeatureRate.DoesNotExist: log_accounting_error( "It seems that the plan %s did not have rate for Mobile " "Workers. This is problematic." % self.new_plan_version.plan.name)
def _get_user_count(domain): return CommCareUser.total_by_domain(domain, is_active=True)