Ejemplo n.º 1
0
    def post(self, data):
        conn = pecan.request.db_conn
        if data.expired_at == wsme.Unset:
            data.expired_at = datetime.datetime.utcnow() + datetime.timedelta(days=365)

        try:
            conn.create_precharge(pecan.request.context, **data.as_dict())
        except exception.NotAuthorized as e:
            LOG.exception('Fail to create precharges')
            raise exception.NotAuthorized()
        except Exception as e:
            LOG.exception('Fail to create precharges: %s, for reason: %s' %
                          (data.as_dict(), e))
            raise exception.PreChargeException()
Ejemplo n.º 2
0
    def delete(self):
        """Delete the precharge. Actually set the deleted status as True"""
        conn = pecan.request.db_conn
        context = pecan.request.context
        check_policy(context, "account:precharge")

        try:
            conn.delete_precharge(context, self.code)
        except exception.PreChargeNotFound:
            raise
        except Exception as e:
            msg = 'Failed to delete precharge:%s, for reason:%s' % \
                (self.code, e)
            LOG.error(msg)
            raise exception.PreChargeException()
Ejemplo n.º 3
0
    def dispatched(self, data):
        conn = pecan.request.db_conn
        if data.remarks == wsme.Unset:
            data.remarks = None

        try:
            precharge = conn.dispatch_precharge(pecan.request.context,
                                                self.code,
                                                remarks=data.remarks)
        except exception.PreChargeNotFound:
            LOG.error('The precharge %s not found' % self.code)
            raise exception.PreChargeNotFound(precharge_code=self.code)
        except Exception as e:
            LOG.error('Fail to dispatch precharge(%s), for reason: %s' % (self.code, e))
            raise exception.PreChargeException()

        return models.PreCharge.from_db_model(precharge)
Ejemplo n.º 4
0
    def used(self):
        conn = pecan.request.db_conn
        context = pecan.request.context
        user_id = context.user_id
        project_id = context.project_id
        user_name = context.user_name

        key = str("gring-precharge-limit-%s" % user_id)
        cache = _get_cache()
        count = cache.get(key)

        max_count, lock_time = \
            self._parse_limit_rule(cfg.CONF.precharge_limit_rule)

        if count is None:
            cache.set(key, str(max_count), lock_time)
            count = max_count

        price = utils._quantize_decimal('0')
        ret_code = 0

        if int(count) > 0:
            try:
                precharge = conn.use_precharge(context,
                                               self.code,
                                               user_id=user_id,
                                               project_id=project_id)
                price = precharge.price
            except exception.PreChargeNotFound:
                LOG.error('The precharge %s not found' % self.code)
                ret_code = 1
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except exception.PreChargeHasUsed:
                LOG.error('The precharge %s has been used' % self.code)
                ret_code = 2
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except exception.PreChargeHasExpired:
                LOG.error('The precharge %s has been expired' % self.code)
                ret_code = 3
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except Exception as e:
                LOG.error('Fail to use precharge(%s), for reason: %s' %
                          (self.code, e))
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
                raise exception.PreChargeException()
            else:
                cache.set(key, str(max_count), lock_time)
                # Notifier account
                if cfg.CONF.notify_account_charged:
                    account = conn.get_account(context, user_id).as_dict()
                    contact = kunkka.get_uos_user(user_id)
                    self.notifier = notifier.NotifierService(
                        cfg.CONF.checker.notifier_level)
                    self.notifier.notify_account_charged(
                        context,
                        account,
                        contact,
                        'coupon',
                        price,
                        bonus=0,
                        operator=user_id,
                        operator_name=user_name,
                        remarks='coupon')
        left_count = int(cache.get(key))

        if left_count == 0:
            ret_code = 4

        return models.PreChargeSimple.transform(price=price,
                                                ret_code=ret_code,
                                                left_count=left_count,
                                                lock_time=lock_time / 60)
Ejemplo n.º 5
0
    def used(self):
        conn = pecan.request.db_conn
        user_id = pecan.request.context.user_id
        project_id = pecan.request.context.project_id

        key = str("gring-precharge-limit-%s" % project_id)
        cache = _get_cache()
        count = cache.get(key)

        max_count, lock_time = self._parse_limit_rule(cfg.CONF.precharge_limit_rule)

        if count is None:
            cache.set(key, str(max_count), lock_time)
            count = max_count

        price = utils._quantize_decimal('0')
        ret_code = 0

        if int(count) > 0:
            try:
                precharge = conn.use_precharge(pecan.request.context,
                                               self.code,
                                               user_id=user_id,
                                               project_id=project_id)
                price = precharge.price
            except exception.PreChargeNotFound:
                LOG.error('The precharge %s not found' % self.code)
                ret_code = 1
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except exception.PreChargeHasUsed:
                LOG.error('The precharge %s has been used' % self.code)
                ret_code = 2
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except exception.PreChargeHasExpired:
                LOG.error('The precharge %s has been expired' % self.code)
                ret_code = 3
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
            except Exception as e:
                LOG.error('Fail to use precharge(%s), for reason: %s' % (self.code, e))
                try:
                    cache.decr(key)
                except AttributeError:
                    cache.incr(key, delta=-1)
                raise exception.PreChargeException()
            else:
                cache.set(key, str(max_count), lock_time)

        left_count = int(cache.get(key))

        if left_count == 0:
            ret_code = 4

        return models.PreChargeSimple.transform(price=price,
                                                ret_code=ret_code,
                                                left_count=left_count,
                                                lock_time=lock_time/60)