Skip to content

Commit

Permalink
Add QuillHtmlField and QuillPlainField to integrate with DRF
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeHanYeong committed Jan 4, 2022
1 parent 5c13798 commit 7ba2e65
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 4 deletions.
Empty file added django_quill/drf/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions django_quill/drf/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from rest_framework import serializers

from django_quill.fields import FieldQuill

__all__ = (
"QuillFieldMixin",
"QuillHtmlField",
"QuillPlainField",
)


class QuillFieldMixin:
pass


class QuillHtmlField(QuillFieldMixin, serializers.Field):
def to_representation(self, value: FieldQuill):
return value.quill.html


class QuillPlainField(QuillFieldMixin, serializers.Field):
def to_representation(self, value: FieldQuill):
return value.quill.plain
6 changes: 6 additions & 0 deletions django_quill/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils.html import strip_tags

from .forms import QuillFormField
from .quill import Quill
Expand Down Expand Up @@ -55,6 +56,11 @@ def delta(self):
self._require_quill()
return self.quill.delta

@property
def plain(self):
self._require_quill()
return self.quill.plain

def save(self, json_string, save=True):
setattr(self.instance, self.field.name, json_string)
self._committed = True
Expand Down
9 changes: 7 additions & 2 deletions django_quill/management/commands/convert_to_quill.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.management import BaseCommand

from django_quill.fields import FieldQuill
from django_quill.quill import QuillParseError


class Command(BaseCommand):
Expand All @@ -26,8 +27,12 @@ def handle(self, *args, **options):
)
field_data = getattr(instance, field_name)
if isinstance(field_data, FieldQuill):
print(f" This is already a QuillField.")
continue
try:
quill = field_data.quill
print(f" This is already a QuillField.")
continue
except QuillParseError:
field_data = field_data.json_string
try:
json_data = json.loads(field_data)
if "delta" in json_data:
Expand Down
3 changes: 3 additions & 0 deletions django_quill/quill.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"Quill",
)

from django.utils.html import strip_tags


class QuillParseError(Exception):
def __init__(self, value):
Expand All @@ -21,5 +23,6 @@ def __init__(self, json_string):
json_data = json.loads(json_string)
self.delta = json_data["delta"]
self.html = json_data.get("html", "")
self.plain = strip_tags(self.html).strip()
except (json.JSONDecodeError, KeyError, TypeError):
raise QuillParseError(json_string)
39 changes: 39 additions & 0 deletions docs/pages/rest-framework.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Integration with DRF

## Serializer Fields

- `django_quill.drf.fields.QuillHtmlField`
HTML content of QuillField
- `django_quill.drf.fields.QuillPlainField`
Plain text of QuillField

example:

**models.py**

```python
from django_quill.fields import QuillField

class QuillPost(models.Model):
content = QuillField()
```

**serializers.py**

```python
from rest_framework import serializers
from django_quill.drf.fields import QuillHtmlField, QuillPlainField
from posts.models import QuillPost

class QuillPostSerializer(serializers.ModelSerializer):
content = QuillHtmlField()
content_plain = QuillPlainField(source='content')

class Meta:
model = QuillPost
fields = "__all__"

```



1 change: 1 addition & 0 deletions playground/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"django.contrib.staticfiles",
"django_extensions",
"django_quill",
"rest_framework",
]

ROOT_URLCONF = "config.urls"
Expand Down
14 changes: 14 additions & 0 deletions playground/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
import importlib.util

from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import SimpleRouter

from . import views

Expand All @@ -26,3 +29,14 @@
path("reset/", views.ResetView.as_view(), name="reset"),
path("posts/", include("posts.urls")),
]
urlpatterns_apis = []

if (spec := importlib.util.find_spec("rest_framework")) is not None:
from posts import apis

router = SimpleRouter()
router.register(r"", apis.QuillPostViewSet)
urlpatterns_apis.append(path("posts/", include(router.urls)))

if urlpatterns_apis:
urlpatterns.append(path("api/", include(urlpatterns_apis)))
11 changes: 11 additions & 0 deletions playground/posts/apis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework import viewsets

from posts.models import QuillPost
from posts.serializers import QuillPostSerializer

__all__ = ("QuillPostViewSet",)


class QuillPostViewSet(viewsets.ModelViewSet):
queryset = QuillPost.objects.all()
serializer_class = QuillPostSerializer
15 changes: 15 additions & 0 deletions playground/posts/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from rest_framework import serializers

from django_quill.drf.fields import QuillHtmlField, QuillPlainField
from posts.models import QuillPost

__all__ = ("QuillPostSerializer",)


class QuillPostSerializer(serializers.ModelSerializer):
content = QuillHtmlField()
content_plain = QuillPlainField(source="content")

class Meta:
model = QuillPost
fields = "__all__"
18 changes: 17 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages = [
[tool.poetry.dependencies]
python = "^3.9"
Django = "^3.2.9"
djangorestframework = "^3.13.1"
gunicorn = "^20.1.0"
model-bakery = "^1.3.3"

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
asgiref==3.4.1; python_version >= "3.6"
django==3.2.9; python_version >= "3.6"
djangorestframework==3.13.1; python_version >= "3.6"
gunicorn==20.1.0; python_version >= "3.5"
model-bakery==1.3.3
pytz==2021.3; python_version >= "3.6"
Expand Down
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ defusedxml==0.7.1; python_version >= "3.7" and python_full_version < "3.0.0" or
distlib==0.3.3; python_version >= "2.7" and python_full_version < "3.0.0" or python_full_version >= "3.5.0"
django-extensions==3.1.5; python_version >= "3.6"
django==3.2.9; python_version >= "3.6"
djangorestframework==3.13.1; python_version >= "3.6"
docutils==0.17.1; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"
entrypoints==0.3; python_full_version >= "3.6.1" and python_version >= "3.7"
filelock==3.4.0; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.33
0.1.34

0 comments on commit 7ba2e65

Please sign in to comment.