from markupsafe import Markup
from onegov.core.utils import increment_name
from onegov.core.utils import normalize_for_url
from onegov.form import Form
from onegov.quill import QuillField
from onegov.swissvotes import _
from onegov.swissvotes.models import TranslatablePage
from wtforms.fields import BooleanField
from wtforms.fields import StringField
from wtforms.validators import InputRequired
[docs]
class PageForm(Form):
[docs]
title = StringField(
label=_('Title'),
validators=[
InputRequired()
]
)
[docs]
show_timeline = BooleanField(
label=_('Show Mastodon timeline')
)
[docs]
content = QuillField(
label=_('Content'),
tags=('strong', 'em', 'a', 'h3', 'ol', 'ul', 'blockquote'),
validators=[
InputRequired()
]
)
@property
[docs]
def id(self) -> str:
""" An ID based on the title. """
id = normalize_for_url(self.title.data or 'page')
query = self.request.session.query(TranslatablePage)
while query.filter_by(id=id).first():
id = increment_name(id)
return id
[docs]
def update_model(self, model: TranslatablePage) -> None:
model.title = self.title.data
model.content = self.content.data
model.show_timeline = self.show_timeline.data
model.id = model.id or self.id
[docs]
def apply_model(self, model: TranslatablePage) -> None:
self.title.data = model.title
self.content.data = model.content or Markup('')
self.show_timeline.data = model.show_timeline