def is_limit_exceeded(cls, app_config): """ Returns True if any of artifact creation rate limits are exceeded, False otherwise """ pkg = cls.__module__.split('.', 1)[0] opt = u'{}.rate_limits'.format(pkg) count = cls.query.find(dict(app_config_id=app_config._id)).count() provider = plugin.ProjectRegistrationProvider.get() start = provider.registration_date(app_config.project) # have to have the replace because, the generation_time is offset-aware # UTC and h.rate_limit uses offset-naive UTC dates start = start.replace(tzinfo=None) try: h.rate_limit(opt, count, start) except forge_exc.RatelimitError: return True return False
def is_limit_exceeded(cls, app_config, user=None, count_by_user=None): """ Returns True if any of artifact creation rate limits are exceeded, False otherwise """ pkg = cls.__module__.split('.', 1)[0] opt = u'{}.rate_limits'.format(pkg) count_in_app = lambda: cls.query.find(dict(app_config_id=app_config._id)).count() provider = plugin.ProjectRegistrationProvider.get() start = provider.registration_date(app_config.project) try: h.rate_limit(opt, count_in_app, start) if user and not user.is_anonymous() and count_by_user is not None: h.rate_limit(opt + '_per_user', count_by_user, user.registration_date()) except forge_exc.RatelimitError: return True return False
def is_limit_exceeded(cls, app_config, user=None, count_by_user=None): """ Returns True if any of artifact creation rate limits are exceeded, False otherwise """ pkg = cls.__module__.split('.', 1)[0] opt = u'{}.rate_limits'.format(pkg) def count_in_app(): return cls.query.find(dict(app_config_id=app_config._id)).count() provider = plugin.ProjectRegistrationProvider.get() start = provider.registration_date(app_config.project) try: h.rate_limit(opt, count_in_app, start) if user and not user.is_anonymous() and count_by_user is not None: h.rate_limit(opt + '_per_user', count_by_user, user.registration_date()) except forge_exc.RatelimitError: return True return False
def test(self): # Keys are number of seconds, values are max number allowed until that time period is reached with h.push_config(h.tg.config, **{self.key_comment: self.rate_limits}): now = datetime.utcnow() start_date = now - timedelta(seconds=30) h.rate_limit(self.key_comment, 0, start_date) with assert_raises(exc.RatelimitError): h.rate_limit(self.key_comment, 1, start_date) start_date = now - timedelta(seconds=61) h.rate_limit(self.key_comment, 1, start_date) h.rate_limit(self.key_comment, 2, start_date) with assert_raises(exc.RatelimitError): h.rate_limit(self.key_comment, 3, start_date) start_date = now - timedelta(seconds=86301) h.rate_limit(self.key_comment, 19, start_date) with assert_raises(exc.RatelimitError): h.rate_limit(self.key_comment, 20, start_date) start_date = now - timedelta(seconds=86401) h.rate_limit(self.key_comment, 21, start_date) h.rate_limit(self.key_comment, 49, start_date) with assert_raises(exc.RatelimitError): h.rate_limit(self.key_comment, 50, start_date)