def add_group(): group_schema = Group.from_json(json.loads(request.data.decode('utf-8'))) if group_schema.errors: return {'errors': group_schema.errors}, 400 group = Group(**group_schema.data) db.session.add(group) db.session.commit() return group, 201
def edit_group(group_id): group = Group.query.get_or_404(group_id) group_schema = Group.from_json(json.loads(request.data.decode('utf-8')), partial=True) if group_schema.errors: return {'errors': group_schema.errors}, 400 for field, value in group_schema.data.items(): if hasattr(group, field): setattr(group, field, value) db.session.commit() return group, 200
def add_user(): user_schema = User.from_json(json.loads(request.data.decode('utf-8'))) if user_schema.errors: return {'errors': user_schema.errors}, 400 email_duplicate_user = User.query.filter(User.email == user_schema.data['email']).first() if email_duplicate_user: return {'errors': {'email': 'User with this email is already exists'}}, 400 group = Group(name=str(uuid.uuid4())) db.session.add(group) db.session.commit() user = User(**user_schema.data) user.group = group db.session.add(user) db.session.commit() return user, 201