From 6a5c1852e4d2f311773c7fb5e53d342c855e41af Mon Sep 17 00:00:00 2001 From: ewandor Date: Thu, 9 Feb 2023 15:42:49 +0100 Subject: [PATCH] Adding Richtext type in schema --- back/app/core/models.py | 15 ++++++++++++++ back/app/template/models.py | 20 ++++++++++++------- back/app/template/schemas.py | 6 +++--- .../crud/crud-formly-jsonschema.service.ts | 3 +++ 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/back/app/core/models.py b/back/app/core/models.py index b292bcea..4882df0f 100644 --- a/back/app/core/models.py +++ b/back/app/core/models.py @@ -31,4 +31,19 @@ def text_area(*args, **kwargs): return Field(*args, **kwargs) +def RichtextMultiline(*args, **kwargs): + kwargs['props'] = { + "richtext": True, + "multiline": True + } + return Field(*args, **kwargs) + + +def RichtextSingleline(*args, **kwargs): + kwargs['props'] = { + "richtext": True, + "multiline": False + } + + return Field(*args, **kwargs) diff --git a/back/app/template/models.py b/back/app/template/models.py index 951b1e25..accf7756 100644 --- a/back/app/template/models.py +++ b/back/app/template/models.py @@ -1,8 +1,9 @@ from typing import List, Dict +from html import unescape from pydantic import BaseModel, Field, validator -from ..core.models import CrudDocument, text_area +from ..core.models import CrudDocument, RichtextMultiline, RichtextSingleline class PartyTemplate(BaseModel): @@ -18,15 +19,22 @@ class PartyTemplate(BaseModel): part: str +def remove_html_tags(text): + """Remove html tags from a string""" + import re + clean = re.compile('<.*?>') + return re.sub(clean, '', text) + + class ProvisionTemplate(CrudDocument): name: str - title: str - label: str = None - body: str = text_area(size=8) + title: str = RichtextSingleline() + label: str = "" + body: str = RichtextMultiline(size=8) @validator("label", always=True) def generate_label(cls, v, values, **kwargs): - return "{} - \"{}\"".format(values['name'], values['title']) + return "{} - \"{}\"".format(values['name'], unescape(remove_html_tags(values['title']))) class Settings(CrudDocument.Settings): fulltext_search = ['name', 'title', 'body'] @@ -61,5 +69,3 @@ class ContractTemplate(CrudDocument): default=[], format="dictionary", ) - - diff --git a/back/app/template/schemas.py b/back/app/template/schemas.py index c6d8d9df..e1c58c7b 100644 --- a/back/app/template/schemas.py +++ b/back/app/template/schemas.py @@ -3,7 +3,7 @@ from typing import List from .models import ContractTemplate, ProvisionTemplate, PartyTemplate, ProvisionTemplateReference, DictionaryEntry from ..core.schemas import Writer -from ..core.models import text_area +from ..core.models import RichtextMultiline, RichtextSingleline class ContractTemplateRead(ContractTemplate): @@ -31,8 +31,8 @@ class ProvisionTemplateRead(ProvisionTemplate): class ProvisionTemplateCreate(Writer): name: str - title: str - body: str = text_area(size=8) + title: str = RichtextSingleline() + body: str = RichtextMultiline(size=8) class ProvisionTemplateUpdate(BaseModel): diff --git a/front/app/src/common/crud/crud-formly-jsonschema.service.ts b/front/app/src/common/crud/crud-formly-jsonschema.service.ts index 72c392cf..5ef8bec9 100644 --- a/front/app/src/common/crud/crud-formly-jsonschema.service.ts +++ b/front/app/src/common/crud/crud-formly-jsonschema.service.ts @@ -47,6 +47,9 @@ export class CrudFormlyJsonschemaOptions implements FormlyJsonschemaOptions { field.type = "hidden"; } else if (schema.type == "array" && schema.format == "dictionary") { field.type = "dictionary"; + } else if (schema.type == "string" && schema.hasOwnProperty('props') + && schema.props.hasOwnProperty("richtext") && schema.props.richtext) { + field.type = "richtext"; } if (schema.hasOwnProperty('props')) {