def add_member(request, board_id): if request.method != "PUT": return JsonResponseMethodNotAllowed( {"message": "HTTP method not allowed."}) member = request.user.member put_body = json.loads(request.body) if not put_body.get("member"): return HttpResponseBadRequest() member_id = put_body.get("member") try: board = get_board_or_404(request, board_id) except Http404: return JsonResponseNotFound({"message": "Board not found"}) serializer = Serializer(board=board) if board.members.filter(id=member_id).exists(): return JsonResponse( serializer.serialize_member(board.members.get(id=member_id))) member_type = put_body.get("member_type") if not member_type or not member_type in ("admin", "normal", "guest"): return JsonResponseNotFound({"message": "Member type not found"}) try: new_member = Member.objects.get(id=member_id) board.add_member(member=member, member_to_add=new_member) member_role, member_role_created = MemberRole.objects.get_or_create( board=board, type=member_type) member_role.members.add(new_member) except Member.DoesNotExist: return JsonResponseNotFound({"message": "Not found."}) return JsonResponse(serializer.serialize_member(new_member))
def get_members(request): if request.method != "GET": return JsonResponseMethodNotAllowed( {"message": "HTTP method not allowed."}) current_member = request.user.member members = current_member.viewable_members serializer = Serializer() response_json = [] for member in members: response_json.append(serializer.serialize_member(member)) return JsonResponse(response_json, safe=False)
def remove_member(request, board_id, member_id): if request.method != "DELETE": return JsonResponseMethodNotAllowed( {"message": "HTTP method not allowed."}) member = request.user.member try: board = get_board_or_404(request, board_id) except Http404: return JsonResponseNotFound({"message": "Board not found"}) try: member_to_remove = board.members.get(id=member_id) board.remove_member(member=member, member_to_remove=member_to_remove) except Member.DoesNotExist: return JsonResponseNotFound({"message": "Member nor found."}) serializer = Serializer(board=board) return JsonResponse(serializer.serialize_member(member_to_remove))