Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ __pycache__/
assets/
# C extensions
*.so

# pytest
.pytest_cache
# Distribution / packaging
.Python
build/
Expand Down
35 changes: 35 additions & 0 deletions content/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import factory
from factory.django import DjangoModelFactory

from content.models import Content, Tag
from user.factories import UserFactory


class TagFactory(DjangoModelFactory):
class Meta: # type: ignore[override]
model = Tag

name = factory.Sequence(lambda n: f"Tag {n}") # type: ignore[override]
description = factory.Faker("sentence", nb_words=5) # type: ignore[override]


class ContentFactory(DjangoModelFactory):
class Meta: # type: ignore[override]
model = Content

document_file = factory.django.FileField(filename="test.txt")
extracted_file = factory.django.FileField(filename="extracted.txt")
created_by = factory.SubFactory(UserFactory) # type: ignore[override]
modified_by = factory.SubFactory(UserFactory) # type: ignore[override]

@factory.post_generation # type: ignore[override]
def tag(self, create, extracted, **kwargs):
if not create:
return
# If tags are passed, add them to the M2M field
if extracted:
if isinstance(extracted, list):
for tag in extracted:
self.tag.add(tag) # type: ignore[override]
else:
self.tag.add(extracted) # type: ignore[override]
12 changes: 10 additions & 2 deletions content/filters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import strawberry
import strawberry_django

from .models import Tag
from content.enums import DocumentStatusTypeEnum

from .models import Content, Tag

@strawberry_django.filters.filter(Tag, lookups=True)

@strawberry_django.filter_type(Content, lookups=True)
class ContentFilter:
id: strawberry.auto
document_status: DocumentStatusTypeEnum


@strawberry_django.filter_type(Tag, lookups=True)
class TagFilter:
name: strawberry.auto
16 changes: 9 additions & 7 deletions content/mutations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import typing

import strawberry
import strawberry_django
from asgiref.sync import sync_to_async
from strawberry.file_uploads import Upload
from strawberry_django.permissions import IsAuthenticated

from content.serializers import (
ArchiveContentSerializer,
Expand Down Expand Up @@ -35,20 +37,20 @@ class FolderInput:


@strawberry.type
class PrivateMutation:
@strawberry.mutation
class Mutation:
@strawberry_django.mutation(extensions=[IsAuthenticated()])
async def create_content(
self,
data: CreateContentMutation.InputType, # type: ignore[reportInvalidTypeForm]
info: Info,
) -> MutationResponseType[ContentType]:
return await CreateContentMutation.handle_create_mutation(data, info, None)

@strawberry.mutation
@strawberry_django.mutation(extensions=[IsAuthenticated()])
def read_file(self, file: Upload) -> str:
return file.read().decode("utf-8")

@strawberry.mutation
@strawberry_django.mutation(extensions=[IsAuthenticated()])
@sync_to_async
def update_content_title(
self,
Expand All @@ -70,7 +72,7 @@ def update_content_title(
result=content, # type: ignore[reportReturnType]
)

@strawberry.mutation
@strawberry_django.mutation(extensions=[IsAuthenticated()])
@sync_to_async
def retrigger_content(
self,
Expand All @@ -92,7 +94,7 @@ def retrigger_content(
result=content, # type: ignore[reportReturnType]
)

@strawberry.mutation
@strawberry_django.mutation(extensions=[IsAuthenticated()])
@sync_to_async
def archive_content(
self, data: ArchiveContentInput, info: Info # type: ignore[reportInvalidTypeForm]
Expand All @@ -112,7 +114,7 @@ def archive_content(
result=content, # type: ignore[reportReturnType]
)

@strawberry.mutation
@strawberry_django.mutation(extensions=[IsAuthenticated()])
async def create_tag(
self,
data: CreateTagMutation.InputType, # type: ignore[reportInvalidTypeForm]
Expand Down
10 changes: 10 additions & 0 deletions content/orders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import strawberry
import strawberry_django

from content.models import Content


@strawberry_django.order_type(Content)
class ContentOrder:
id: strawberry.auto
created_at: strawberry.auto
32 changes: 17 additions & 15 deletions content/queries.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import strawberry
import strawberry_django
from strawberry_django.pagination import OffsetPaginated
from strawberry_django.permissions import IsAuthenticated

from content.filters import TagFilter
from content.filters import ContentFilter, TagFilter
from content.orders import ContentOrder
from content.types import ContentType, TagType
from main.graphql.context import Info
from utils.strawberry.paginations import CountList, pagination_field


@strawberry.type
class PrivateQuery:
contents: CountList[ContentType] = pagination_field(
pagination=True,
class Query:
contents: OffsetPaginated[ContentType] = strawberry_django.offset_paginated(
filters=ContentFilter,
order=ContentOrder,
extensions=[IsAuthenticated()],
)

@strawberry_django.field(description="Return all content")
async def content(self, info: Info, pk: strawberry.ID) -> ContentType | None:
return await ContentType.get_queryset(None, None, info).filter(pk=pk).afirst()

tags: CountList[TagType] = pagination_field(
pagination=True,
content: ContentType = strawberry_django.field(
extensions=[IsAuthenticated()],
)
tags: OffsetPaginated[TagType] = strawberry_django.offset_paginated(
filters=TagFilter,
extensions=[IsAuthenticated()],
)

@strawberry_django.field()
async def tag(self, info: Info, pk: strawberry.ID) -> TagType | None:
return await TagType.get_queryset(None, None, info).filter(pk=pk).afirst()
tag: TagType = strawberry_django.field(
extensions=[IsAuthenticated()],
)
1 change: 0 additions & 1 deletion content/tests.py

This file was deleted.

Empty file added content/tests/__init__.py
Empty file.
Loading
Loading