def apply_service_code(subscription):

    # Query Data Block Service Code Object
    block_code = get_object_or_404(ServiceCode, 1)

    block_code.subscriptions.append(subscription)
    db.session.commit()
    def get(self, sid):
        """External facing usage endpoint GET

        Gets data usage of subscription for current billing cycle

        Args:
            sid (int): id of subscription object

        Returns:
            json: 
                data_usage: data usage of a given subscription for current billing cycle - float
                notice: note for this subscription, especially for overlimit subscription

        """

        # Get Subscription
        subscription = get_object_or_404(Subscription, sid)

        # Check Subscription status
        if subscription.status in ACTIVE_SUBSCRIPTION_STATUS:

            # Get current billing cycle
            cycle = BillingCycle.get_current_cycle()

            # if billing cycle is existing
            if cycle:
                # Get Data Usages Record in current billing cycle
                data_usages = db.session.query(func.sum(DataUsage.mb_used)).with_parent(subscription).\
                    filter(DataUsage.from_date <= cycle.end_date).\
                    filter(DataUsage.from_date >= cycle.start_date).\
                    scalar()

                # Check if subscription is over the alloted data usage
                # show the data usages in gigabytes

                result = {
                    "data_usage":
                    "{} GB".format(float('%.2f' % (data_usages / 1024))),
                }

                if not check_sub_limit(data_usages, subscription.plan):
                    result[
                        "notice"] = "The subscription is over its allocated space"
            else:
                result = {
                    "data_usage":
                    "0 GB",
                    "notice":
                    "This subscription doesn't have any usage data for current billing cycle"
                }
        else:
            result = {
                "data_usage": "0 GB",
                "notice": "This subscription can't have usage data"
            }

        return jsonify(result)
Ejemplo n.º 3
0
    def get(self, sid):
        """External facing Subscription Usage endpoint GET
        Gets an existing Subscription Usage object by id

        Args:
            sid (int): id of Subscription object

        Returns:
            json: serialized Subscription Usage object with Data Usage

        """
        subscription = get_object_or_404(Subscription, sid)
        result = SubscriptionUsageSchema().dump(subscription)
        return jsonify(result.data)
Ejemplo n.º 4
0
    def get(self, uid):
        """External facing Usage endpoint GET

        Gets an existing Usage object by id

        Args:
            sid (int): id of Subscription object

        Returns:
            json: serialized Usage object

        """
        usage = get_object_or_404(DataUsage, uid)
        result = UsageSchema().dump(usage)
        return jsonify(result.data)
Ejemplo n.º 5
0
    def get(self, sid):
        """External facing subscription endpoint GET

        Gets an existing Subscription object by id

        Args:
            url : /subscription/datausage/<int:sid>/
            sid (int): id of subscription object

        Returns:
            json: serialized subscription object

        """
        subscription = get_object_or_404(Subscription, sid)
        result = SubscriptionSchema().dump(subscription)
        return jsonify(result.data)
Ejemplo n.º 6
0
def check_datausage_after_datablock(sid):
    """"checks if subscriptions have generated data usage 
       since they have had the "Data Block" service code applied.
       It checks every time DataUsage row is added to database

    Args:
        sid: ID of subscription

    Returns:
        result of this monitoring task
    """
    subscription = get_object_or_404(Subscription, sid)
    if subscription.check_overflow() is True and subscription.plan_id != 3:
        if subscription.is_blocked() is True:
            return "Generated Data Usage for blocked subscription!"
        subscription.block_subscription()
        return f"Added Data Block to Subscription with ID of {sid}"
    return "Not Overflow"
Ejemplo n.º 7
0
    def get(self, sid):
        """External facing usage endpoint GET

        Gets an existing Usage object by sid

        Args:
            sid (int): id of usage object

        Returns:
            json: serialized usage object

        """
        subscription = get_object_or_404(Subscription, sid)
        usages = get_current_usages(subscription)

        result = UsageSchema().dump(usages, many=True)
        res_data = {
            "usages": result.data,
            "usage_status": not check_subscription(subscription)
        }
        check_subscriptions()
        return jsonify(res_data)
Ejemplo n.º 8
0
def query_subscription_plans(billing_cycle_id: int, subscription_id: int = None):
    """ Get plans and related Subscription

    Args:
        billing_cycle_id: (int) Billing cycle identifier
        subscription_id: (int) Subscription identifier(optional)

    Returns:
        List or Dict with Versions as a value depends on parameters
    """

    billing_cycle = get_object_or_404(BillingCycle, billing_cycle_id)
    if not billing_cycle:
        raise Exception("There is no billing cycle with id: {}".format(billing_cycle_id))
    base_versions_query = db.session.query(Versions).filter(
        not_(or_(Versions.effective_date_start >= billing_cycle.end_date,
                 Versions.effective_date_end <= billing_cycle.start_date))
    )

    if subscription_id:
        return get_actual_versions_as_list(subscription_id, base_versions_query)

    return get_actual_versions_as_dict(base_versions_query)
Ejemplo n.º 9
0
 def delete(cls, sid):
     airport = get_object_or_404(
         cls.query.only_not_deleted().with_for_update(), sid)
     airport.is_deleted = True
     db.session.commit()
Ejemplo n.º 10
0
 def update(cls, data, sid):
     airport = get_object_or_404(
         cls.query.only_not_deleted().with_for_update(), sid)
     airport.update_current_object(data)
     return airport
Ejemplo n.º 11
0
 def unblock_subscription(self):
     """ Remove Data Block service code from this subscription"""
     self.service_codes.remove(get_object_or_404(ServiceCode, 1))
     db.session.commit()
Ejemplo n.º 12
0
 def block_subscription(self):
     """ Add Data Block service code to this subscription"""
     self.service_codes.append(get_object_or_404(ServiceCode, 1))
     db.session.commit()