def post(self, request): request.data['user'] = request.user.id serializer = BoardSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_list(request): try: required_args = ['key', 'board_id'] if len(request.data) and all(arg in required_args for arg in request.data): board = Board.objects.get(id=request.data['board_id']) if str(board.owner.api_key) == str(request.data['key']): board_data = board.content board_data['lists'].append({'title': '', 'stickers': []}) board_serializer = BoardSerializer(board, data=board_data) if board_serializer.is_valid(): board_serializer.save() return JsonResponse( {'message': 'List created successfully created.'}, status=status.HTTP_200_OK) else: return JsonResponse(board_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( {'message': 'API key for this board invalid.'}, status=status.HTTP_403_FORBIDDEN) else: return JsonResponse({'message': 'Not enough arguments.'}, status=status.HTTP_400_BAD_REQUEST) except Board.DoesNotExist: return JsonResponse({'message': 'Board not found'}, status=status.HTTP_404_NOT_FOUND) except: print(traceback.format_exc()) return JsonResponse({'message': 'Server API error.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def put(self, request, board_pk): board = self.get_object(board_pk) serializer = BoardSerializer(board, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete_sticker(request): try: required_args = ['key', 'board_id', 'list_id', 'sticker_id'] if len(request.data) and all(arg in required_args for arg in request.data): board = Board.objects.get(id=request.data['board_id']) if str(board.owner.api_key) == str(request.data['key']): board_data = board.content if (is_int(request.data['list_id'])) and ( 0 <= request.data['list_id'] < len( board_data['lists'])): if 'stickers' not in board_data['lists'][ request.data['list_id']]: board_data['lists'][ request.data['list_id']]['stickers'] = [] if (is_int(request.data['sticker_id'])) and ( 0 <= request.data['sticker_id'] < len( board_data['lists'][ request.data['list_id']]['stickers'])): board_data['lists'][ request.data['list_id']]['stickers'].remove( board_data['lists'][request.data['list_id']] ['stickers'][request.data['sticker_id']]) board_serializer = BoardSerializer(board, data=board_data) if board_serializer.is_valid(): board_serializer.save() return JsonResponse( {'message': 'Sticker successfully deleted'}, status=status.HTTP_200_OK) else: return JsonResponse( board_serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( { 'message': 'Content not updated. Sticker id invalid.' }, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( {'message': 'Content not updated. List id invalid.'}, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( {'message': 'API key for this board invalid.'}, status=status.HTTP_403_FORBIDDEN) else: return JsonResponse({'message': 'Not enough arguments.'}, status=status.HTTP_400_BAD_REQUEST) except Board.DoesNotExist: return JsonResponse({'message': 'Board not found'}, status=status.HTTP_404_NOT_FOUND) except: print(traceback.format_exc()) return JsonResponse({'message': 'Server API error.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def put(self, request, pk, format=None): board = self.get_object(pk) request.data['user'] = request.user.id serializer = BoardSerializer(Board, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args): user = request.user body = request.data input = {"name": body['name']} serializer = BoardSerializer(data=input, user=user) if serializer.is_valid(raise_exception=True): serializer.create(serializer.validated_data) return Response(status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class TaskSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) board = BoardSerializer(read_only=True) class Meta: model = Task fields = ('id', 'user', 'board', 'name', 'description', 'due_date', 'position', 'created_at')
def test_retrieve_board_details(self): """Test retrieving a board's details""" board = sample_board() topic = sample_topic(self.user.profile, board) url = detail_board_url(board) res = self.client.get(url) serializer = BoardSerializer(board, context={'request': RequestFactory().get(url)}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(res.data['topics'][0]['title'], topic.title) self.assertAllIn(('id', 'title', 'description', 'topics', 'created_at'), res.data.keys())
class PinSerializer(serializers.ModelSerializer): author = AccountSerializer(read_only=True, required=False) board = BoardSerializer(read_only=True, required=False) class Meta: model = Pin fields = ('id', 'author', 'board', 'title', 'content', 'image', 'latitude', 'longitude', 'link', 'location', 'mapsURL', 'usPOC', 'hnPOC', 'mgrs', 'created_at', 'updated_at') read_only_fields = ('id', 'created_at', 'updated_at') def get_validation_exclusions(self, *args, **kwargs): exclusions = super(PinSerializer, self).get_validation_exclusions() return exclusions + ['author']
def test_retrieve_board_list(self): """Test retrieving a list of board""" board = sample_board(title='Test board', description='Test board description') sample_topic(self.user.profile, board) sample_board(title='Another test board', description='Another test board description') res = self.client.get(BOARDS_URL) boards = Board.objects.all().order_by('id') serializer = BoardSerializer(boards, many=True, context={'request': RequestFactory().get(BOARDS_URL)}) self.assertEqual(res.status_code, status.HTTP_200_OK) self.assertEqual(res.data, serializer.data) self.assertEqual(len(res.data), 2) self.assertAllIn(('id', 'title', 'description', 'topics', 'created_at'), res.data[0].keys()) self.assertAllIn(('href', 'title'), res.data[0]['topics'][0].keys())
def get(self, request): if (request.GET.get('my')): boards = Board.objects.filter( Q(user_id=request.user.id)).order_by('-created') else: if (request.GET.get('search')): boards = Board.objects.filter( Q(user_id=request.user.id, title__icontains=request.GET.get('search')) | Q(is_public=True, title__icontains=request.GET.get('search'))).order_by( '-created') else: boards = Board.objects.filter( Q(user_id=request.user.id) | Q(is_public=True)).order_by('-created') serrializer = BoardSerializer(boards, many=True) return Response(serrializer.data)
def get(self, request, *args, **kwargs): # GET request handler for the model user = request.user queryset = self.get_queryset(user) serializer = BoardSerializer(queryset, user=user, many=True) return Response(serializer.data)
def get(self, request, board_pk): board = self.get_object(board_pk) serializer = BoardSerializer(board) return Response(serializer.data)
def get(self, request, pk, format=None): board = self.get_object(pk) serializer = BoardSerializer(board) return Response(serializer.data)
def retrieve(self, request, pk=None): queryset = Board.objects.filter() board = get_object_or_404(queryset, pk=pk) serializer = BoardSerializer(board) return Response(serializer.data)
def list(self, request): queryset = Board.objects.filter() serializer = BoardSerializer(queryset, many=True) return Response(serializer.data)
def boards(self, request, pk=None): team = self.get_object() boards = team.board_set.all() return Response([BoardSerializer(board).data for board in boards])
def update_sticker_postition(request): try: required_args = [ 'key', 'board_id', 'old_list_id', 'old_sticker_id', 'new_list_id', 'new_sticker_id' ] if len(request.data) and all(arg in required_args for arg in request.data): board = Board.objects.get(id=request.data['board_id']) if str(board.owner.api_key) == str(request.data['key']): board_data = board.content if (is_int(request.data['old_list_id'])) and ( 0 <= request.data['old_list_id'] < len( board_data['lists'])): if (is_int(request.data['new_list_id'])) and ( 0 <= request.data['new_list_id'] < len( board_data['lists'])): if (is_int(request.data['old_sticker_id'])) and ( 0 <= request.data['old_sticker_id'] < len(board_data['lists'][ request.data['old_list_id']]['stickers'])): if (is_int(request.data['new_sticker_id'])) and ( 0 <= request.data['new_sticker_id'] < len(board_data['lists'][request.data[ 'new_list_id']]['stickers']) + 1): sticker = board_data['lists'][ request.data['old_list_id']]['stickers'][ request.data['old_sticker_id']] board_data['lists'][request.data[ 'old_list_id']]['stickers'].remove( board_data['lists'][request.data[ 'old_list_id']]['stickers'] [request.data['old_sticker_id']]) board_data['lists'][request.data[ 'new_list_id']]['stickers'].insert( request.data['new_sticker_id'], sticker) board_serializer = BoardSerializer( board, data=board_data) if board_serializer.is_valid(): board_serializer.save() return JsonResponse( { 'message': 'Sticker posititon successfully updated' }, status=status.HTTP_200_OK) else: return JsonResponse( { 'message': 'Sticker posititon not updated' }, status=status. HTTP_503_SERVICE_UNAVAILABLE) else: return JsonResponse( { 'message': 'Sticker posititon not updated. New sticker id invalid.' }, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( { 'message': 'Sticker posititon not updated. Old sticker id invalid.' }, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( { 'message': 'Sticker posititon not updated. New list id invalid.' }, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( { 'message': 'Sticker posititon not updated. Old list id invalid.' }, status=status.HTTP_400_BAD_REQUEST) else: return JsonResponse( {'message': 'API key for this board invalid.'}, status=status.HTTP_403_FORBIDDEN) else: return JsonResponse({'message': 'Not enough arguments.'}, status=status.HTTP_400_BAD_REQUEST) except Board.DoesNotExist: return JsonResponse({'message': 'Board not found.'}, status=status.HTTP_404_NOT_FOUND) except: print(traceback.format_exc()) return JsonResponse({'message': 'Server API error.'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)