This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@account_api.post("/official-document", url_name="add-document") | |
def add_official_document(request, file: UploadedFile = File(...)): | |
# create an instance of the OfficialDocument model with an user and a file | |
official_document = OfficialDocument.objects.create(user=User.objects.get(username="gab_test")) | |
# save the file in the media folder | |
official_document.document.save(file.name, ContentFile(file.read())) | |
return {"message": "Document enregistré"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ninja_jwt.authentication import JWTAuth | |
from ninja_jwt.exceptions import AuthenticationFailed | |
class SuperUserAuth(JWTAuth): | |
def get_user(self, validated_token): | |
user = super().get_user(validated_token) | |
if not user.is_superuser: | |
raise AuthenticationFailed("L'utilisateur n'est pas un super utilisateur") | |
return user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_test_image(): | |
image = Image.new('RGB', (100, 100), color='white') | |
image_file = BytesIO() | |
image.save(image_file, format="JPEG") | |
image_file.seek(0) | |
file_name = 'test_image.jpg' | |
uploaded_image = SimpleUploadedFile(name=file_name, content=image_file.getvalue(), content_type='image/jpeg') | |
return uploaded_image |