Beispiel #1
0
def parse_credits(string):
    """Given a numerical string, this will return a Money instance in the
    local currency.
    """
    currency_code = configdb['currency_code']
    money = currency.parse_credits(string, currency.CURRENCIES[currency_code])
    return money
    def parse_subscriber_cost(self, string, currency=DEFAULT_CURRENCY):
        """Parses input cost strings for the network prices page and performs
        value checks. Outputs a Money instance. The cost must be non-negative
        and the integer reperesentation must fit in a 32 bit signed integer.

        Arguments:
            string: numerical string with support for thousands commas and
            decimal places
            currency: the desired output currency. Default currency
            controlled by common/currency extension
        Returns:
            A Money instance
        Raises:
            ValueError: if the string doesn't parse or is out of bounds.
        """
        money = parse_credits(string, currency)
        if money.amount_raw < 0:
            raise ValueError("Cost must be non-negative")
        # This is the max value that is allowed by the DB
        if money.amount_raw > 2147483647:
            raise ValueError("Value is too large")
        return money
Beispiel #3
0
    def post(self, request, imsi=None):
        """Operators can use this API to add credit to a subscriber.

        These additions become PendingCreditUpdates and celery will try to
        "apply" then by sending the info to the corresponding BTS.  The
        operator may also delete PendingCreditUpdates, effectively canceling
        them.
        """
        user_profile = UserProfile.objects.get(user=request.user)
        network = user_profile.network
        # In all cases we'll redirect back to the adjust-credit page.
        sub = Subscriber.objects.get(imsi=imsi, network=network)
        params = {
            'imsi': sub.imsi
        }
        adjust_credit_redirect = redirect(
            urlresolvers.reverse('subscriber-adjust-credit', kwargs=params))
        # Validate the input.
        if 'amount' not in request.POST:
            return HttpResponseBadRequest()
        error_text = 'Error: credit value must be between -10M and 10M.'
        try:
            currency = network.subscriber_currency
            amount = parse_credits(request.POST['amount'],
                    CURRENCIES[currency]).amount_raw
            if abs(amount) > 2147483647:
                raise ValueError(error_text)
        except ValueError:
            messages.error(request, error_text)
            return adjust_credit_redirect
        # Validation suceeded, create a PCU and start the update credit task.
        msgid = str(uuid.uuid4())
        credit_update = PendingCreditUpdate(subscriber=sub, uuid=msgid,
                                            amount=amount)
        credit_update.save()
        tasks.update_credit.delay(sub.imsi, msgid)
        return adjust_credit_redirect
Beispiel #4
0
    def post(self, request):
        """Operators can use this API to add denomination to a network.

        These denomination bracket will be used to recharge subscriber,
        set balance validity and status
        """
        user_profile = models.UserProfile.objects.get(user=request.user)
        network = user_profile.network
        try:
            currency = network.subscriber_currency
            start_amount_raw = request.POST.get('start_amount')
            start_amount = parse_credits(start_amount_raw,
                                         CURRENCIES[currency]).amount_raw
            end_amount_raw = request.POST.get('end_amount')
            end_amount = parse_credits(end_amount_raw,
                                       CURRENCIES[currency]).amount_raw
            validity_days = int(request.POST.get('validity_days')) or 0

            dnm_id = int(request.POST.get('dnm_id')) or 0
            if validity_days > settings.ENDAGA['MAX_VALIDITY_DAYS']:
                message = ('Validity days value exceeds maximum permissible '
                           'limit (%s Days).' %
                           (settings.ENDAGA['MAX_VALIDITY_DAYS']))
                messages.error(request,
                               message,
                               extra_tags='alert alert-danger')
                return redirect(urlresolvers.reverse('network-denominations'))
            elif start_amount <= 0 or end_amount <= 0:
                messages.error(request,
                               'Enter value >0 for start/end amount.',
                               extra_tags='alert alert-danger')
                return redirect(urlresolvers.reverse('network-denominations'))
            elif validity_days <= 0:
                messages.error(request,
                               'Validity can not be 0 day.',
                               extra_tags='alert alert-danger')
                return redirect(urlresolvers.reverse('network-denominations'))
            elif end_amount <= start_amount:
                messages.error(
                    request,
                    'End amount should be greater than start amount.',
                    extra_tags='alert alert-danger')
                return redirect(urlresolvers.reverse('network-denominations'))

            user_profile = models.UserProfile.objects.get(user=request.user)
            with transaction.atomic():
                if dnm_id > 0:
                    try:
                        denom = models.NetworkDenomination.objects.get(
                            id=dnm_id)
                        # Check for existing denomination range exist.
                        denom_exists = \
                          models.NetworkDenomination.objects.filter(
                              end_amount__gte=start_amount,
                              start_amount__lte=end_amount,
                              network=user_profile.network).exclude(
                                  id=dnm_id).count()
                        if denom_exists:
                            messages.error(
                                request,
                                'Denomination range already exists.',
                                extra_tags='alert alert-danger')
                            return redirect(
                                urlresolvers.reverse('network-denominations'))
                        denom.network = user_profile.network
                        denom.start_amount = start_amount
                        denom.end_amount = end_amount
                        denom.validity_days = validity_days
                        denom.save()
                        messages.success(
                            request,
                            'Denomination is updated successfully.',
                            extra_tags='alert alert-success')
                    except models.NetworkDenomination.DoesNotExist:
                        messages.error(request,
                                       'Invalid denomination ID.',
                                       extra_tags='alert alert-danger')
                        return redirect(
                            urlresolvers.reverse('network-denominations'))
                else:
                    # Check for existing denomination range exist.
                    denom_exists = models.NetworkDenomination.objects.filter(
                        end_amount__gte=start_amount,
                        start_amount__lte=end_amount,
                        network=user_profile.network).count()
                    if denom_exists:
                        messages.error(request,
                                       'Denomination range already exists.',
                                       extra_tags='alert alert-danger')
                        return redirect(
                            urlresolvers.reverse('network-denominations'))
                    # Create new denomination for selected network
                    denom = models.NetworkDenomination(
                        network=user_profile.network)
                    denom.network = user_profile.network
                    denom.start_amount = start_amount
                    denom.end_amount = end_amount
                    denom.validity_days = validity_days
                    denom.save()
                    messages.success(request,
                                     'Denomination is created successfully.',
                                     extra_tags='alert alert-success')
        except Exception:
            messages.error(request,
                           'Invalid validity value. Enter greater than '
                           '0 digit value',
                           extra_tags='alert alert-danger')
        return redirect(urlresolvers.reverse('network-denominations'))