def post(request: Request): try: inv_token: str = request.data.get('inv_code') errors = [] if not inv_token: errors.append("invitation token not valid") username: str = request.data.get('username') if not username: errors.append("username not valid") password: str = request.data.get('password') if not password: errors.append("password not valid") conf_password: str = request.data.get('password_confirmation') if not conf_password: errors.append("confirmation password not valid") else: if conf_password != password: errors.append("two passwords don't match") email: str = request.data.get('email') if not email: errors.append("email not valid") if errors: return BadRequestResponse(errors) t = InvitationToken.objects.get(token=inv_token) if t: user = User.objects.create(username=username, password=password, email=email) if user: tok, _ = Token.objects.get_or_create(user=user) InvitationToken.delete(t) return OKResponse({ "message": f"user {username} created successfully", "token": tok.key }) else: return BadRequestResponse(['user not created']) else: return BadRequestResponse(['token not found']) except Exception as eee: return BadRequestResponse( ["invalid or missing token", eee.__str__()])
def post(request: Request): if not request.user.is_authenticated: return NotAuthenticatedResponse() serial = CollectionGeneralInfo(data=request.data) if serial.is_valid(): serial.save() return OKResponse(serial.data) else: return BadRequestResponse(serial.errors)
class NoteUpdateAPIView(APIView): parser_classes = [JSONParser] @staticmethod def put(request: Request, collection_id, note_id): bad_response, collection = _check_request(request, collection_id) if bad_response: return bad_response if not (note := find_note(note_id)): return BadRequestResponse(['note not found']) if note.collection.collection_id != collection.collection_id: return BadRequestResponse(['note not in the collection']) serial = NoteSerializer(note, data=request.data, partial=True) if serial.is_valid(): serial.save() return OKResponse(serial.data) else: return BadRequestResponse(serial.errors)
def post(request: Request, collection_id): bad_response, collection = _check_request(request, collection_id) if bad_response: return bad_response request.data['collection'] = collection_id serial = NoteSerializer(data=request.data) if serial.is_valid(): serial.save() return OKResponse(serial.data) else: return BadRequestResponse(serial.errors)
class NoteDeleteAPIView(APIView): @staticmethod def delete(request: Request, collection_id, note_id): bad_response, collection = _check_request(request, collection_id) if bad_response: return bad_response if not (note := find_note(note_id)): return BadRequestResponse(['note not found']) if note.collection.collection_id != collection.collection_id: return BadRequestResponse(['note not in the collection']) Note.delete(note) return DeleteResponse('note', note_id)
class CollectionUpdateAPIView(APIView): parser_classes = [JSONParser] @staticmethod def put(request, collection_id: int): if not request.user.is_authenticated: return NotAuthenticatedResponse() if not (collection := find_collection(collection_id)): return NotFoundResponse("collection", collection_id.__str__()) serial = CollectionGeneralInfo(collection, data=request.data, partial=True) if serial.is_valid(): serial.save() return OKResponse(serial.data) else: return BadRequestResponse(serial.errors)
def delete(request: Request, collection_id, note_id): bad_response, collection = _check_request(request, collection_id) if bad_response: return bad_response if not (note := find_note(note_id)): return BadRequestResponse(['note not found'])