+ def check_quota(self, session):
+ """
+ Check whether topic quota is exceeded by the given project
+ Used by relevant topics' 'new' function to decide whether or not creation of the new item should be allowed
+ :param session[project_id]: projects (tuple) for which quota should be checked
+ :param session[force]: boolean. If true, skip quota checking
+ :return: None
+ :raise:
+ DbException if project not found
+ ValidationError if quota exceeded in one of the projects
+ """
+ if session["force"]:
+ return
+ projects = session["project_id"]
+ for project in projects:
+ proj = self.auth.get_project(project)
+ pid = proj["_id"]
+ quota_name = self.quota_name or self.topic
+ quota = proj.get("quotas", {}).get(quota_name, self.default_quota)
+ count = self.db.count(self.topic, {"_admin.projects_read": pid})
+ if count >= quota:
+ name = proj["name"]
+ raise ValidationError(
+ "quota ({}={}) exceeded for project {} ({})".format(
+ quota_name, quota, name, pid
+ ),
+ http_code=HTTPStatus.UNPROCESSABLE_ENTITY,
+ )
+