def get_group(group_name): """Get the list of users belonging to a group""" gdal = GroupDAL(app.config['DB']) users = gdal.get(group_name) if users is None: return 'Not Found', status.HTTP_404_NOT_FOUND else: return json.dumps(list(users))
def save(self, user): """ Create or update a user user (dict): user details e.g.: user = { 'first_name': 'Joe', 'last_name': 'Smith', 'userid': 'jsmith', 'groups': ['admins', 'users'] } """ self.schema(user) self.conn.set(self.key(user["userid"]), json.dumps(user)) # Add user to groups if user.get("groups"): gdal = GroupDAL(conn=self.conn) for group in user["groups"]: # If empty group, delete it first # (Redis doesn't allow empty sets) if gdal.isempty(group): gdal.delete(group) gdal.add(group, user["userid"]) return user
def update_group(group_name): """Udate a group""" try: group = json.loads(request.data) except: return 'Bad Request', status.HTTP_400_BAD_REQUEST gdal = GroupDAL(app.config['DB']) try: gdal.schema(group) except MultipleInvalid: return 'Bad Request', status.HTTP_400_BAD_REQUEST if gdal.get(group_name) is not None: gdal.update(group_name, group) return json.dumps(group) else: return 'Not Found', status.HTTP_404_NOT_FOUND
def create_group(): """Create a new group""" try: group = json.loads(request.data) except: return 'Bad Request', status.HTTP_400_BAD_REQUEST gdal = GroupDAL(app.config['DB']) try: gdal.schema_group_name(group) except MultipleInvalid: return 'Bad Request', status.HTTP_400_BAD_REQUEST if gdal.get(group['name']) is not None: return 'Bad Request', status.HTTP_400_BAD_REQUEST gdal.create(group) return json.dumps(group)