Skip to content

Instantly share code, notes, and snippets.

@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é"}
@gabigab117
gabigab117 / security.py
Created May 20, 2024 13:18
SuperUserAuth Ninja JWT
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
@gabigab117
gabigab117 / image_test.py
Created October 23, 2023 20:20
Create an image for Tests
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