def get_service_usage_summary(): appName = 'payments' modelName = 'Service' price_field = 'price' pk_field = 'pk' customer_field = 'customer' operator_field = 'operator' summary = None Service = utils.get_model(appName, modelName) queryset = Service.objects.all() if queryset is not None: logger.debug("get_service_usage: queryet not None") summary = queryset.aggregate(total_amount=Sum(price_field), usage_count=Count(pk_field), min_paid_amount=Min(price_field), max_paid_amount=Max(price_field), number_of_customer=Count(customer_field, distinct=True), number_of_operator=Count(operator_field, distinct=True), average_amount=Avg( price_field, output_field=IntegerField())) return summary
def get_number_of_validated_account_by_country(): ''' This method returns a queryset that contains the number of by country. The queryset is laid out this way : QuerySet [{'field_name': 'field_value', 'number_of_users': 'a number'}]. bellow is an example : QuerySet [{'country': 'Cameroon', 'number_of_users': 4}]. If the queryset return many country, then there will be more than one entry in the queryset. None is returned when no model could be found in the app defined by appName ''' appName = 'accounts' modelName = 'Account' field = 'country' model = utils.get_model(appName, modelName) queryset = None accounts_by_country = None if model is None: logger.warning('No model %s found in the app %s') else: logger.debug( "analytics query number of account grouped by Country from the app %s and model %s", appName, modelName) validated_account = model.objects.filter(email_validated=True) accounts_by_country = validated_account.values(field).annotate( number_of_users=Count(field)) return accounts_by_country
def get_number_of_validated_account_by_city(): ''' This method returns a queryset that contains the number of user by city grouped by country. The queryset is laid out this way : QuerySet [{'country_field_name': field_value,'city_field_name': fiedl_value, 'number_of_users': 'a number'}]. bellow is an example : QuerySet [{'country': 'Cameroon', 'city': 'Yaoundé', 'number_of_users': 4}]. An attention should be paid to the order in which the fields are entered in the values() function. The fields will appear in the same order in the result. If the queryset return many country, then there will be more than one entry in the queryset. None is returned when no model could be found in the app defined by appName ''' appName = 'accounts' modelName = 'Account' city_field = 'city' country_field = 'country' model = utils.get_model(appName, modelName) queryset = None accounts_by_city = None if model is None: logger.warning('No model %s found in the app %s') else: logger.debug( "analytics query number of account grouped by Country and City from the app %s and model %s", appName, modelName) validated_account = model.objects.filter(email_validated=True) accounts_by_city = validated_account.values( country_field, city_field).annotate(number_of_users=Count(city_field)) return accounts_by_city
def get_model_all_instance_filter_by(appName=None, modelName=None, **kwargs): model = utils.get_model(appName, modelName) queryset = None if model is None: logger.warning('No model %s found in the app %s', modelName, appName) else: logger.debug("analytics query from the app %s and model %s ", appName, modelName) queryset = model.objects.filter(**kwargs) return queryset
def get_model_instance_filter_by(appName=None, modelName=None, **kwargs): model = utils.get_model(appName, modelName) instance = None if model is None: logger.warning('No model %s found in the app %s', modelName, appName) return None try: instance = model.objects.get(**kwargs) except ObjectDoesNotExist as e: logger.error("No model %s entry found", modelName) except MultipleObjectsReturned as e: logger.error( "Multiple entry found with the following attribut. Only one entry is expected" ) return instance
def recharge_balance(cls, seller, customer, amount): result = { 'succeed': False, 'errors': '' } if not isinstance(seller, User) or not isinstance(customer, User): return result Recharge = utils.get_model("voucher", "Recharge") if amount > 0 : v = Voucher.objects.filter(activated=False,is_sold=False, is_used=False, amount=amount, created_by=seller).first() if v is None : v = Voucher.objects.create(name=DEFAULT_VENDOR_GENERATED_VOCUHER_NAME + "-" + seller.username.upper(), voucher_code = cls.get_voucher(seller), activated=True,is_sold=True, is_used=True, amount=amount, used_by=customer, sold_by=seller, activated_at=timezone.now(), created_by=seller, activated_by=seller, used_at=timezone.now(), sold_at=timezone.now()) else : Voucher.objects.filter(pk=v.pk).update(activated=True, is_sold=True, is_used=True, used_by=customer, activated_by=seller, sold_by=seller, activated_at=timezone.now(), used_at=timezone.now(), sold_at=timezone.now()) data = { 'sender' : seller, 'recipient': customer, 'amount': amount, 'recipient_amount' : amount, 'voucher': v, 'activity' : PAYMENTS_CONSTANTS.BALANCE_ACTIVITY_RECHARGE } update_balance(data) logger.info(f"Recharge : Balance {customer.balance.balance_uuid} has been recharged by the Use with the amount of {amount} from Voucher {v.voucher_uuid}") result['succeed'] = True else: logger.info(f"Recharge : Balance {customer.balance.balance_uuid} could not be recharged : Amount is negativ {amount}.") result['errors'] = f"Amount is negativ {amount}. The Balance could not be recharged" return result
def get_transfers_summary(): ''' This method returns a transfer summary as a queryset. The output of this queryset is as follow : QuerySet [{'created_at': value, 'number_of_transfers': value, 'max_transferred_amount': value, 'min_transferred_amount}, ..., ..., ] The query is sorted by the order_by_field variable. The value of this variable must be an attribut of the Transfer Model. TODO : Add a verification on the existence of the field in the model defined by modelName. ''' amount_field = 'amount' appName = 'payments' modelName = 'Transfer' pk_field = 'pk' order_by_field = '-created_at' recipient_field = 'recipient' sender_field = 'sender' recipient_name_field = 'recipient__user__username' created_at_field = 'created_at' summary = None Transfer = utils.get_model(appName, modelName) queryset = Transfer.objects.all() #queryset = get_model_all_instance_filter_by(appName, modelName, **{'created_at__month': datetime.now().month}) if queryset is not None: summary = queryset.aggregate( number_of_transfers=Count(pk_field), max_transferred_amount=Max(amount_field), min_transferred_amount=Min(amount_field), number_of_sender=Count(sender_field, distinct=True), number_of_recipients=Count(recipient_field, distinct=True), average_transferred_amount=Avg(amount_field, output_field=IntegerField())) return summary
def can_be_used(cls, voucher): flag = False Voucher = utils.get_model("voucher", "Voucher") if cls.is_valide(voucher): flag = Voucher.objects.filter(voucher_code=voucher, activated=True, is_used=False).exists() return flag
def get_number_of_policies_filter_by(**kwargs): return utils.get_model('payments', 'Policy').objects.filter(**kwargs).count()
def get_number_category_services_filter_by(**kwargs): return utils.get_model('payments', 'ServiceCategory').objects.filter(**kwargs).count()
def get_categories(): return utils.get_model('payments', 'ServiceCategory').objects.filter(is_active=True)
def get_services_filter_by(**kwargs): return utils.get_model('payments', 'Service').objects.filter(**kwargs)
def get_nomber_of_idcards_filter_by(**kwargs): return utils.get_model('payments', 'IDCard').objects.filter(**kwargs).count()
def get_number_of_account_filter_by(**kwargs): return utils.get_model('accounts', 'Account').objects.filter(**kwargs).count()
def get_account_filter_by(**kwargs): return utils.get_model('accounts', 'Account').objects.get(**kwargs)
def get_number_of_active_account(): return utils.get_model('accounts', 'Account').objects.filter(is_active=True).count()
from django.test import TestCase, RequestFactory from django.urls import reverse from django.http.response import Http404 from django.db.models import F, Q from django.contrib.auth.models import AnonymousUser, User from django.contrib.sessions.middleware import SessionMiddleware from payments.models import Transfer, Payment, Service, ServiceCategory, AvailableService, Policy from payments.forms import TransferForm from payments import views from payments.tests import user_test_data, policy_test_data from pay import utils import logging logger = logging.getLogger(__name__) Account = utils.get_model('accounts', 'Account') POLICY_DATA = { 'daily_limit': 150000, 'weekly_limit': 350000, 'monthly_limit': 550000, 'commission': 0.03 } ACCOUNT_BALANCE = 100000 # TRANSFER TRANSFER_AMOUNT = 25000 TRANSFER_AMOUNT_BIGGER_THAN_ACCOUNT_BALANCE = ACCOUNT_BALANCE + 1 PAYMENT_HOME_URL = reverse('payments:payment-home')
def get_number_available_services_filter_by(**kwargs): return utils.get_model( 'payments', 'AvailableService').objects.filter(**kwargs).count()