def get_groups(self): self._verify_owner() if self.db.user.is_admin: groups = self.db.get_groups() else: groups = [ g for g in self.owner.groups ] groups.extend([m.group for m in self.owner.memberships]) return [ Group.from_db(g) for g in groups ]
def get_groups(self): self._verify_owner() if self.db.user.is_admin: groups = self.db.get_groups() else: groups = [g for g in self.owner.groups] groups.extend([m.group for m in self.owner.memberships]) return [Group.from_db(g) for g in groups]
def get(self, resource_id=None): """Fetch group(s)""" logging.info('groups::get', extra=dict(resource_id=resource_id)) if self.user.is_admin: owner_groups = self.db.get_groups() else: owner_groups = [g for g in self.owner.groups] owner_groups.extend([m.group for m in self.owner.memberships]) if resource_id is None: self.dump([JsGroup.from_db(g) for g in owner_groups]) else: group = self.db.get_group(int(resource_id)) if group not in owner_groups: raise PermissionDeniedError() self.dump(JsGroup.from_db(group))
def update_group(self, id, name, desc): self._verify_owner() g = self.db.get_group(id) if not self.db.is_group_name_unique(name, g.key()): raise DuplicateNameError(Group, name) g.name = name g.description = desc return Group.from_db(g.put())
def post(self): """Create a new group""" logging.info('groups::post', extra=dict(body=self.request.body)) data = self.parse_json(self.request.body) if not self.db.is_group_name_unique(data.name): raise DuplicateNameError(JsGroup, data.name) group = self.db.add_group(data.name, data.description, self.owner) self.db.add_group_member(group, self.owner) self.dump(JsGroup.from_db(group))
def add_group(self, name, desc): self._verify_owner() if not self.db.user.is_admin: raise PermissionDeniedError() if not self.db.is_group_name_unique(name): raise DuplicateNameError(Group, name) g = self.db.add_group(name, desc, self.owner) self.db.add_group_member(g, self.owner) return Group.from_db(g)
def put(self, resource_id): """Update an existing resource""" extra = dict(resource_id=resource_id, body=self.request.body) logging.info('groups::put', extra=extra) data = self.parse_json(self.request.body) group = self.db.get_group(int(resource_id)) # You can only update your own groups (unless you're an admin) is_own_group = group.owner.key().id() == self.owner.key().id() if not self.user.is_admin and not is_own_group: raise PermissionDeniedError() if not self.db.is_group_name_unique(data.name, group.key()): raise DuplicateNameError(JsGroup, data.name) group.name = data.name group.description = data.description self.dump(JsGroup.from_db(group.put()))