|
| 1 | +# BaseApp Pages |
| 2 | + |
| 3 | +Reusable app to handle pages, URL's paths and metadata. It provides useful models and GraphQL Interfaces. |
| 4 | + |
| 5 | +## Whats missing |
| 6 | +- [ ] Allow for custom settings.LANGUAGES per project |
| 7 | +- [ ] Make create migration work with TranslatedField |
| 8 | + |
| 9 | +Currenly if you have a different set of languages in your projects it will create a new migration changing the fields. So if you have a migration check test it will fail because the `settings.LANGUAGES are different. |
| 10 | + |
| 11 | +## How to install: |
| 12 | + |
| 13 | +Add dependencies to your `requirements/base.txt` file: |
| 14 | + |
| 15 | +``` |
| 16 | +baseapp-pages |
| 17 | +``` |
| 18 | + |
| 19 | +And run provision or manually `pip install -r requirements/base.ext` |
| 20 | + |
| 21 | +If you want to develop, [install using this other guide](#how-to-develop). |
| 22 | + |
| 23 | +## How to use |
| 24 | + |
| 25 | +Add `baseapp_pages` and `django_quill` to your project's `INSTALLED_APPS` and run `./manage.py migrate` as any other django model: |
| 26 | + |
| 27 | +```python |
| 28 | +INSTALLED_APPS = [ |
| 29 | + # ... |
| 30 | + 'baseapp_pages', |
| 31 | + 'django_quill', |
| 32 | + # ... |
| 33 | +] |
| 34 | +``` |
| 35 | + |
| 36 | +Add `django.middleware.locale.LocaleMiddleware` to the `MIDDLEWARE` list in your django settings file. [Check django's documentation for more information](https://docs.djangoproject.com/en/5.0/topics/i18n/translation/#how-django-discovers-language-preference). |
| 37 | + |
| 38 | +Add `baseapp_pages.permissions.PagesPermissionsBackend` to the `AUTHENTICATION_BACKENDS` list in your django settings file. |
| 39 | + |
| 40 | +Expose `PagesMutations` and `PagesQuery` in your GraphQL/graphene endpoint, like: |
| 41 | + |
| 42 | +```python |
| 43 | +from baseapp_pages.graphql.mutations import PagesMutations |
| 44 | +from baseapp_pages.graphql.queries import PagesQuery |
| 45 | + |
| 46 | +class Query(graphene.ObjectType, PagesQuery): |
| 47 | + pass |
| 48 | + |
| 49 | +class Mutation(graphene.ObjectType, PagesMutations): |
| 50 | + pass |
| 51 | + |
| 52 | +schema = graphene.Schema(query=Query, mutation=Mutation) |
| 53 | +``` |
| 54 | + |
| 55 | +This will expose `urlPath` and `page` query. |
| 56 | + |
| 57 | +### `urlPath` query: |
| 58 | + |
| 59 | +Example: |
| 60 | + |
| 61 | +```graphql |
| 62 | +{ |
| 63 | + urlPath(path: '/about') { |
| 64 | + path |
| 65 | + language |
| 66 | + target { |
| 67 | + metadata { |
| 68 | + metaTitle |
| 69 | + } |
| 70 | + |
| 71 | + ... on Page { |
| 72 | + title |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### PageInterface |
| 80 | + |
| 81 | +`PageInterface` is a GraphQL interface that can be used to query for pages. It has the following fields: |
| 82 | + |
| 83 | +- `urlPath` return the active `URLPath` |
| 84 | +- `urlPaths` return all `URLPath` for the object, including inactive ones and in other languages |
| 85 | +- `metadata` return the `Metadata` for the object |
| 86 | + |
| 87 | +ObjectTypes that implements `PageInterface` is required to implement a resolve for `metadata` like this: |
| 88 | + |
| 89 | +```python |
| 90 | +from django.utils.translation import get_language |
| 91 | +from baseapp_core.graphql import DjangoObjectType |
| 92 | +from baseapp_pages.graphql import PageInterface, MetadataObjectType |
| 93 | + |
| 94 | + |
| 95 | +class MyModelObjectType(DjangoObjectType): |
| 96 | + class Meta: |
| 97 | + model = MyModel |
| 98 | + interfaces = (relay.Node, PageInterface) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def resolve_metadata(cls, instance, info, **kwargs): |
| 102 | + return MetadataObjectType( |
| 103 | + meta_title=instance.title, |
| 104 | + meta_description=instance.body[:160], |
| 105 | + meta_og_image=instance.image.url, |
| 106 | + meta_robots='noindex,nofollow' |
| 107 | + ) |
| 108 | +``` |
| 109 | + |
| 110 | +If you want to support `Metadata` being manually set or overriden in the admin you can use the following code: |
| 111 | + |
| 112 | +```python |
| 113 | +class MyModelObjectType(DjangoObjectType): |
| 114 | + # ... |
| 115 | + |
| 116 | + @classmethod |
| 117 | + def resolve_metadata(cls, instance, info, **kwargs): |
| 118 | + target_content_type = ContentType.objects.get_for_model(instance) |
| 119 | + metadata = MetadataObjectType._model.objects.filter( |
| 120 | + target_content_type=target_content_type, |
| 121 | + target_object_id=self.id, |
| 122 | + language=get_language(), |
| 123 | + ).first() |
| 124 | + if not metadata: |
| 125 | + return MetadataObjectType( |
| 126 | + meta_title=instance.title, |
| 127 | + # ... |
| 128 | + ) |
| 129 | + return metadata |
| 130 | +``` |
| 131 | + |
| 132 | +<!-- ## How to to customize the Page model |
| 133 | +
|
| 134 | +In some cases you may need to extend Page model, and we can do it following the next steps: |
| 135 | +
|
| 136 | +Start by creating a barebones django app: |
| 137 | +
|
| 138 | +``` |
| 139 | +mkdir my_project/pages |
| 140 | +touch my_project/pages/__init__.py |
| 141 | +touch my_project/pages/models.py |
| 142 | +``` |
| 143 | +
|
| 144 | +Your `models.py` will look something like this: |
| 145 | +
|
| 146 | +```python |
| 147 | +from django.db import models |
| 148 | +
|
| 149 | +from baseapp_pages.models import AbstractPage |
| 150 | +
|
| 151 | +
|
| 152 | +class Page(AbstractPage): |
| 153 | + custom_field = models.CharField(null=True) |
| 154 | +``` |
| 155 | +
|
| 156 | +Now make your to add your new app to your `INSTALLED_APPS` and run `makemigrations` and `migrate` like any normal django app. |
| 157 | +
|
| 158 | +Now in your `settings/base.py` make sure to tell baseapp-pages what is your custom model for Page: |
| 159 | +
|
| 160 | +```python |
| 161 | +BASEAPP_PAGES_PAGE_MODEL = 'pages.Page' |
| 162 | +``` --> |
| 163 | + |
| 164 | +## How to develop |
| 165 | + |
| 166 | +General development instructions can be found in [main README](..#how-to-develop). |
0 commit comments