|
| 1 | +import django_filters |
1 | 2 | import graphene
|
2 |
| -import swapper |
3 |
| -from baseapp_core.graphql import CountedConnection |
4 |
| -from baseapp_pages.models import Language, Metadata, URLPath |
5 | 3 | from graphene import relay
|
6 |
| -from graphene_django import DjangoObjectType |
| 4 | +from graphene.types.generic import GenericScalar |
| 5 | +from baseapp_core.graphql import DjangoObjectType |
| 6 | +from graphene_django.filter import DjangoFilterConnectionField |
| 7 | +from django.contrib.contenttypes.models import ContentType |
7 | 8 |
|
8 |
| -Page = swapper.load_model("baseapp_pages", "Page") |
| 9 | +from ..models import File |
9 | 10 |
|
10 |
| -LanguageEnum = graphene.Enum.from_enum(Language) |
| 11 | +# FileTypeEnum = graphene.Enum.from_enum(FileType) |
11 | 12 |
|
12 | 13 |
|
13 |
| -class PageInterface(relay.Node): |
14 |
| - url_paths = graphene.List(lambda: URLPathNode) |
15 |
| - meta_title = graphene.String() |
16 |
| - meta_description = graphene.String(required=False) |
17 |
| - meta_canonical = graphene.String(required=False) |
18 |
| - meta_robots = graphene.String(required=False) |
19 |
| - meta_og_type = graphene.String(required=False) |
20 |
| - meta_og_image = graphene.String(required=False) |
| 14 | +class FileFilter(django_filters.FilterSet): |
| 15 | + no_parent = django_filters.BooleanFilter(field_name='parent_object_id', lookup_expr='isnull') |
21 | 16 |
|
22 |
| - # TO DO: URL alternates for SEO: |
23 |
| - # https://developers.google.com/search/docs/specialty/international/localized-versions |
24 |
| - |
25 |
| - @classmethod |
26 |
| - def resolve_meta_title(cls, instance, info, **kwargs): |
27 |
| - raise NotImplementedError |
28 |
| - |
29 |
| - @classmethod |
30 |
| - def resolve_meta_description(cls, instance, info, **kwargs): |
31 |
| - raise NotImplementedError |
32 |
| - |
33 |
| - @classmethod |
34 |
| - def resolve_meta_canonical(cls, instance, info, **kwargs): |
35 |
| - # TO DO: implement here in the interface |
36 |
| - |
37 |
| - # return active url path in this language the user is visiting from |
38 |
| - # we can use this to make 301 redirects? |
39 |
| - raise NotImplementedError |
40 |
| - |
41 |
| - @classmethod |
42 |
| - def resolve_meta_robots(cls, instance, info, **kwargs): |
43 |
| - raise NotImplementedError |
44 |
| - |
45 |
| - @classmethod |
46 |
| - def resolve_meta_og_type(cls, instance, info, **kwargs): |
47 |
| - raise NotImplementedError |
48 |
| - |
49 |
| - @classmethod |
50 |
| - def resolve_meta_og_image(cls, instance, info, **kwargs): |
51 |
| - raise NotImplementedError |
| 17 | + class Meta: |
| 18 | + model = File |
| 19 | + fields = ['no_parent'] |
| 20 | + # fields = { |
| 21 | + # "id": ["exact"], |
| 22 | + # "file_type": ["exact"], |
| 23 | + # "file": ["exact"], |
| 24 | + # "file__icontains": ["exact"], |
| 25 | + # } |
52 | 26 |
|
53 | 27 |
|
54 |
| -class URLPathNode(DjangoObjectType): |
55 |
| - target = graphene.Field(PageInterface) |
56 |
| - language = graphene.Field(LanguageEnum) |
| 28 | +class FileNode(DjangoObjectType): |
| 29 | + parent = graphene.Field(relay.Node) |
| 30 | + file = graphene.String() |
| 31 | + # file_type = graphene.Field(FileTypeEnum) |
57 | 32 |
|
58 | 33 | class Meta:
|
59 | 34 | interfaces = (relay.Node,)
|
60 |
| - model = URLPath |
61 |
| - fields = ( |
62 |
| - "id", |
63 |
| - "path", |
64 |
| - "language", |
65 |
| - "is_active", |
66 |
| - "created", |
67 |
| - "modified", |
68 |
| - "target", |
69 |
| - ) |
70 |
| - filter_fields = { |
71 |
| - "id": ["exact"], |
72 |
| - } |
73 |
| - name = "URLPath" |
74 |
| - connection_class = CountedConnection |
75 |
| - |
76 |
| - |
77 |
| -class MetadataNode(DjangoObjectType): |
78 |
| - class Meta: |
79 |
| - interfaces = (relay.Node, PageInterface) |
80 |
| - model = Metadata |
81 |
| - # fields = ("id", "meta_title", "meta_description", "meta_robots", "meta_og_type", "meta_og_image", "meta_canonical") |
82 |
| - fields = ("id",) |
83 |
| - name = "Metadata" |
84 |
| - connection_class = CountedConnection |
| 35 | + model = File |
| 36 | + filterset_class = FileFilter |
| 37 | + |
| 38 | + def resolve_file(self, info, **kwargs): |
| 39 | + return self.file.url |
| 40 | + |
| 41 | + # @classmethod |
| 42 | + # def get_node(cls, info, id): |
| 43 | + # if not info.context.user.is_authenticated: |
| 44 | + # return None |
| 45 | + |
| 46 | + # try: |
| 47 | + # queryset = cls.get_queryset(cls._meta.model.objects, info) |
| 48 | + # return queryset.get(id=id, recipient=info.context.user) |
| 49 | + # except cls._meta.model.DoesNotExist: |
| 50 | + # return None |
| 51 | + |
| 52 | + |
| 53 | +class FilesNode(relay.Node): |
| 54 | + files_count = GenericScalar() |
| 55 | + files = DjangoFilterConnectionField(lambda: FileNode) |
| 56 | + |
| 57 | + def resolve_files(self, info, **kwargs): |
| 58 | + parent_content_type = ContentType.objects.get_for_model(self) |
| 59 | + return File.objects.filter( |
| 60 | + parent_content_type=parent_content_type, |
| 61 | + parent_object_id=self.pk, |
| 62 | + ).order_by("-created") |
0 commit comments