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)
def get(request: Request): if (r_user := request.user).is_authenticated: try: inv_token = InvitationToken.objects.get(user=r_user) except: t = uuid.uuid4().__str__().lower() inv_token = InvitationToken.objects.create(token=t, user=r_user) return OKResponse(data={"token": inv_token.token})
class CollectionGetAPIView(APIView): @staticmethod def get(request, collection_id): if not request.user.is_authenticated: return NotAuthenticatedResponse() if not (collection := find_collection(collection_id)): return NotFoundResponse("collection", collection_id.__str__()) serial = CollectionGeneralInfo(collection) return OKResponse(serial.data)
class NotesListAPIView(APIView): @staticmethod def get(request, collection_id): if not request.user.is_authenticated: return NotAuthenticatedResponse() if not (collection := find_collection(collection_id)): return NotFoundResponse('collection', collection_id) data = Note.objects.filter(collection=collection) serial = NoteSerializer(data, many=True) return OKResponse(serial.data)
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 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 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__()])
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 get(request): if request.user.is_authenticated: serial = CollectionGeneralInfo(Collection.objects.all(), many=True) return OKResponse(serial.data) else: return NotAuthenticatedResponse()