def new_card(request): if request.content_type != "application/json": return utils.create_415_json_response() if len(request.body) == 0: message = "Missing request body" return utils.create_400_json_response(message) if not request.user.is_authenticated: return utils.create_401_json_response() response = None card_data = json.loads(request.body) if isinstance(card_data, dict): result = utils.import_card(card_data, request.user) if result[0] == 201: card_obj = utils.create_card_object(result[1]) response = JsonResponse(card_obj, status=201) elif result[0] == 409: response = utils.create_409_json_response(result[1]) else: response = utils.create_400_json_response(result[1]) else: message = "Invalid json format. Root must be an object" response = utils.create_400_json_response(message) return response
def process_request(request): if not request.user.is_authenticated: return utils.create_401_json_response() if request.method == 'GET': return get_tags(request) else: return utils.create_405_json_response(allow="GET")
def process_request(request): if request.method == 'POST': if not request.user.is_authenticated: return utils.create_401_json_response() if (len(request.FILES) > 0) and ('archive_file' in request.FILES): num_cards_imported = utils.import_card_archive(request.FILES['archive_file'], request.user) return JsonResponse({'num_cards_imported': num_cards_imported}, status=200) else: return utils.create_400_json_response('No archive file found') else: return utils.create_405_json_response(allow="POST")
def process_request(request, card_uuid): if not request.user.is_authenticated: return utils.create_401_json_response() card = Card.from_uuid(card_uuid, request.user) if not card: return utils.create_404_json_response("Card") if request.method == 'GET': return get_card_retrieval_attempts(request, card) elif request.method == 'POST': return new_card_retrieval_attempt(request, card) else: return utils.create_405_json_response(allow="GET, POST")
def process_request(request, card_uuid): if not request.user.is_authenticated: return utils.create_401_json_response() card = Card.from_uuid(card_uuid, request.user) if not card: return utils.create_404_json_response("Card") if request.method == 'GET': return get_card(request, card) elif request.method == 'PATCH': return update_card(request, card) elif request.method == 'DELETE': return delete_card(request, card) else: return utils.create_405_json_response(allow="GET, PATCH, DELETE")
def process_request(request, card_uuid, retrieval_attempt_id): if not request.user.is_authenticated: return utils.create_401_json_response() card = Card.from_uuid(card_uuid, request.user) if not card: return utils.create_404_json_response("Card") retrieval_attempt = RetrievalAttempt.from_id(retrieval_attempt_id) if not retrieval_attempt: return utils.create_404_json_response("RetrievalAttempt") if retrieval_attempt.card != card: message = "RetrievalAttempt exists but does not belong to specified card" return utils.create_400_json_response(message) if request.method == 'GET': return get_card_retrieval_attempt(request, retrieval_attempt) else: return utils.create_405_json_response(allow="GET")
def advance_review_date(request): if request.content_type != "application/json": return utils.create_415_json_response() if not request.user.is_authenticated: return utils.create_401_json_response() ard_data = json.loads(request.body) if not 'num_days' in ard_data: message = "num_days not specified" return utils.create_400_json_response(message) num_days = int(ard_data['num_days']) if num_days < 0: message = "num_days must be a positive integer" return utils.create_400_json_response(message) filter_params = {} if 'filter' in ard_data and isinstance(ard_data['filter'], dict): filter_params = utils.parse_card_filter(ard_data['filter']) # Get all the cards corresponding to the filters # Not just the cards for the current page filter_params['page'] = 1 filter_params['cards_per_page'] = 0 cards = utils.get_filtered_cards(filter_params, request.user) with transaction.atomic(): for card in cards: card.next_retrieval_date = card.next_retrieval_date + timedelta( days=int(num_days)) card.save() return JsonResponse({}, status=200)
def process_request(request, card_uuid, file_id): if not request.user.is_authenticated: return utils.create_401_json_response() card = Card.from_uuid(card_uuid, request.user) if not card: return utils.create_404_json_response("Card") file_attachment = FileAttachment.from_id(file_id) if not file_attachment: return utils.create_404_json_response("File") if file_attachment.card != card: message = "File exists but does not belong to the specified card" return JsonResponse({'message': message}, status=400) if request.method == 'GET': return get_card_file_attachment(request, file_attachment) elif request.method == 'DELETE': return delete_card_file_attachment(request, file_attachment) else: return utils.create_405_json_response(allow="GET, DELETE")
def process_request(request, card_uuid, tag_id): if not request.user.is_authenticated: return utils.create_401_json_response() card = Card.from_uuid(card_uuid, request.user) if not card: return utils.create_404_json_response("Card") tag = None tag_id = int(tag_id) for card_tag in card.tags.all(): if card_tag.id == tag_id: tag = card_tag break if not tag: return utils.create_404_json_response("Tag") if request.method == 'DELETE': return delete_card_tag(request, card, tag) else: return utils.create_405_json_response(allow="DELETE")