
# Django Dynamic Content

A Django library for creating and managing dynamic, customizable content structures with ease. It enhances multilingual support in Django applications, facilitating the creation of HTML or text content that can be easily translated into multiple languages. This library offers a flexible solution for integrating varied content parts, making it ideal for managing dynamic content structures.

## Features

- Create and manage dynamic content formats.
- Easily integrate with multilingual setups using Django Modeltranslation.
- Add and update content parts dynamically.
- Generate text or HTML content based on customizable formats.
- Extensive admin interface for managing dynamic contents and parts.
- REST API support for dynamic content management.

## 1. Installation

Install directly from PyPI:

```bash
$ pip install -U django-dynamic-contents
```

## 2. Quickstart

### Step 1: Update `settings.py`

Add `modeltranslation` and `dynamic_contents` to your `INSTALLED_APPS`:

```python
INSTALLED_APPS = [
    ...,
    'modeltranslation',
    'dynamic_contents'
]
```

### Step 2: Update `urls.py`

Include the package's URLs:

```python
from django.urls import path, include

urlpatterns = [
    ...,
    path('dynamic_contents/', include('dynamic_contents.urls')),
]
```

### Step 3: Database Migration

Run migrations to create necessary models:

```bash
$ python manage.py migrate
```

## 3. Configuration

Customize settings as required:

```python
# Supported languages
LANGUAGES = [
    ("en", "English"),
    ("ja", "Japanese"),
    ("ko", "Korean"),
]

# DYNAMIC_CONTENT
DYNAMIC_CONTENT_CHOICES = [
    ('ALARM', '알람'),
    ('HISTORY', '히스토리')
]
```

## 4. Usage

### Creating Dynamic Content

- Define a `Format` for your content, specifying placeholders within curly braces.
- Create `Part` instances to fill in the placeholders defined in the `Format`.
- Associate `Part` instances with a `DynamicContent` instance.

Create example:

```python
# Create a format
format = Format.objects.create(type='notification', content='{user} liked your post {post}.')

# Create parts data as a list of dictionaries
parts_data = [
    {'field': 'user', 'content': 'John Doe'},
    {'field': 'post', 'content': 'Summer Vacation'}
]

# Create dynamic content using the manager
dynamic_content = DynamicContent.objects.create_dynamic_content(format, parts_data)
```

Update example:

```python
# Assuming you have an existing dynamic_content object
dynamic_content = DynamicContent.objects.get(pk=some_id)

# Update parts data as a list of dictionaries
updated_parts_data = [
    {'field': 'user', 'content': 'Jane Smith'},
    {'field': 'post', 'content': 'Winter Retreat'}
]

# Update dynamic content using the manager
dynamic_content.update_dynamic_content(updated_parts_data)

```

### Models and Managers

- `Format`: Define the format of dynamic content.
- `Part`: Manage parts of the dynamic content.
- `DynamicContent`: Create and manage dynamic content instances.

### Admin Interface

Use the Django admin interface to manage formats, parts, and dynamic contents.

### API Usage

Leverage provided API views and serializers for handling dynamic contents in RESTful services.

## 5. Updating the Package

### Update Version in `setup.cfg`

Change the version number in `setup.cfg`:

```
[metadata]
name = django-dynamic-contents
version = x.x.x
...
```

### Build the Package

```bash
$ python setup.py sdist bdist_wheel
```

### Deploy the Package

Upload to PyPI:

```bash
$ twine upload --verbose dist/django-dynamic-contents-x.x.x.tar.gz
```

## License

Django Dynamic Content is under the MIT License:

```
Copyright (c) 2023 Runners Co., Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
