Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django field for Quill input #655

Merged
merged 18 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions frontend/animal/EditEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class EditEndpoint {
// reorganize forms
$("#endpointGroups").insertBefore($("div.form-actions"));

// html5 enabled form
$("#id_results_notes").quillify();
$("#id_endpoint_notes").quillify();

//update values in doses table
$("#doses_title").html(`Dose<br>(${firstDoses.name})`);
$(".doses").each(function(i, v) {
Expand Down
5 changes: 1 addition & 4 deletions frontend/epiv2/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {EXPOSURE_BIOMONITORING} from "./constants";

const designFormStartup = function(form) {
$(form)
.find(".html5text")
.quillify();
$(form)
.find("#id_study_design")
.find("#id_summary")
.focus();
},
exposureFormStartup = function(form) {
Expand Down
8 changes: 6 additions & 2 deletions frontend/shared/utils/Quillify.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ const toolbarOptions = {
};

export default function() {
let modal = $("#smartTagModal"),
let focusedItem = $(":focus"),
modal = $("#smartTagModal"),
showHawcTools = modal.length === 1;

return this.each(function() {
this.each(function() {
let editor = document.createElement("div"),
textarea = $(this),
q;
Expand Down Expand Up @@ -98,4 +99,7 @@ export default function() {
q.stc.enableModals();
}
});

// restore original focus
$(focusedItem).focus();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi; I added this, now I think quill doesn't mess with which form field is focused on anymore

}
17 changes: 10 additions & 7 deletions hawc/apps/animal/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
from ..assessment.lookups import DssToxIdLookup, EffectTagLookup, SpeciesLookup, StrainLookup
from ..assessment.models import DoseUnits
from ..common import selectable
from ..common.forms import BaseFormHelper, CopyAsNewSelectorForm, form_actions_apply_filters
from ..common.forms import (
BaseFormHelper,
CopyAsNewSelectorForm,
QuillField,
form_actions_apply_filters,
)
from ..study.lookups import AnimalStudyLookup
from ..vocab.constants import VocabularyNamespace
from . import constants, lookups, models
Expand All @@ -22,6 +27,7 @@ class ExperimentForm(ModelForm):
class Meta:
model = models.Experiment
exclude = ("study",)
field_classes = {"description": QuillField}

def __init__(self, *args, **kwargs):
parent = kwargs.pop("parent", None)
Expand Down Expand Up @@ -63,8 +69,6 @@ def helper(self):
if type(widget) != forms.CheckboxInput:
widget.attrs["class"] = "form-control"

self.fields["description"].widget.attrs["rows"] = 4

if self.instance.id:
inputs = {
"legend_text": f"Update {self.instance}",
Expand Down Expand Up @@ -135,6 +139,7 @@ class Meta:
model = models.AnimalGroup
exclude = ("experiment", "dosing_regime", "generation", "parents")
labels = {"lifestage_assessed": "Lifestage at assessment"}
field_classes = {"comments": QuillField}

def __init__(self, *args, **kwargs):
parent = kwargs.pop("parent", None)
Expand Down Expand Up @@ -166,8 +171,6 @@ def __init__(self, *args, **kwargs):
experiment=self.instance.experiment
)

self.fields["comments"].widget.attrs["rows"] = 4

@property
def helper(self):
for fld in list(self.fields.keys()):
Expand Down Expand Up @@ -252,14 +255,13 @@ class DosingRegimeForm(ModelForm):
class Meta:
model = models.DosingRegime
exclude = ("dosed_animals",)
field_classes = {"description": QuillField}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@property
def helper(self):

self.fields["description"].widget.attrs["rows"] = 4
for fld in list(self.fields.keys()):
self.fields[fld].widget.attrs["class"] = "form-control"

Expand Down Expand Up @@ -414,6 +416,7 @@ class Meta:
"effect_term": forms.HiddenInput,
"effect_subtype_term": forms.HiddenInput,
}
field_classes = {"results_notes": QuillField, "endpoint_notes": QuillField}

def __init__(self, *args, **kwargs):
animal_group = kwargs.pop("parent", None)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
{% endblock content %}

{% block extrajs %}
{{ form.media }}
<script type="text/javascript">
$(document).ready(function(){

Expand Down
3 changes: 0 additions & 3 deletions hawc/apps/animal/templates/animal/animalgroup_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
{% endblock %}

{% block extrajs %}
{{form.media}}
<script type="text/javascript">
$(document).ready(function() {
var printf = window.app.HAWCUtils.printf,
Expand Down Expand Up @@ -68,8 +67,6 @@
);
};

$('#id_comments').quillify();
$('#id_description').quillify();
$('#id_species')
.change(onSpeciesChange)
.trigger('change');
Expand Down
2 changes: 0 additions & 2 deletions hawc/apps/animal/templates/animal/dosingregime_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

{% block extrajs %}
<script type="text/javascript">
$('#id_description').quillify();

// reorganize forms
var actions = $('.form-actions');
$('#doseGroupsDiv').insertBefore($('.form-actions'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
{% endblock content %}

{% block extrajs %}
{{ form.media }}
<script type="text/javascript">
let rerunSearch = function() {
// rerun whenever settings change and an item isn't selected
Expand Down
1 change: 0 additions & 1 deletion hawc/apps/animal/templates/animal/endpoint_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
{% endblock %}

{% block extrajs %}
{{ form.media }}
<script type="text/json" id="config">
{
"vocabulary": {{vocabulary|safe}},
Expand Down
1 change: 0 additions & 1 deletion hawc/apps/animal/templates/animal/endpoint_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ <h2>Assessment endpoints ({{page_obj.paginator.count}} found)</h2>
{% endblock content %}

{% block extrajs %}
{{ form.media }}
{{ config|json_script:"config" }}
<script type="text/javascript">
window.app.startup("animalStartup", function(animal){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
{% endblock content %}

{% block extrajs %}
{{ form.media }}
<script type="text/javascript">
$(document).ready(function(){

Expand Down
2 changes: 0 additions & 2 deletions hawc/apps/animal/templates/animal/experiment_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
{% endblock %}

{% block extrajs %}
{{ form.media }}
<script type="text/javascript">
$(document).ready(function(){
$('#id_description').quillify();
$('#id_name').focus();
var pa = $('#id_purity_available'),
addBlankOption = function(setAsBlank){
Expand Down
33 changes: 15 additions & 18 deletions hawc/apps/assessment/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@

from hawc.services.epa.dsstox import DssSubstance

from ..common.forms import BaseFormHelper, form_actions_apply_filters, form_actions_create_or_close
from ..common.forms import (
BaseFormHelper,
QuillField,
form_actions_apply_filters,
form_actions_create_or_close,
)
from ..common.helper import new_window_a, tryParseInt
from ..common.selectable import AutoCompleteSelectMultipleWidget, AutoCompleteWidget
from ..common.widgets import DateCheckboxInput
Expand All @@ -25,10 +30,9 @@

class AssessmentForm(forms.ModelForm):

internal_communications = forms.CharField(
internal_communications = QuillField(
required=False,
help_text="Internal communications regarding this assessment; this field is only displayed to assessment team members.",
widget=forms.Textarea,
)

class Meta:
Expand All @@ -46,6 +50,12 @@ class Meta:
widgets = {
"public_on": DateCheckboxInput,
}
field_classes = {
"assessment_objective": QuillField,
"authors": QuillField,
"conflicts_of_interest": QuillField,
"funding_source": QuillField,
}

def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
Expand Down Expand Up @@ -85,13 +95,6 @@ def save(self, commit=True):

@property
def helper(self):
# by default take-up the whole row
for fld in list(self.fields.keys()):
widget = self.fields[fld].widget
if type(widget) == forms.Textarea:
widget.attrs["rows"] = 3
widget.attrs["class"] = widget.attrs.get("class", "") + " html5text"

if self.instance.id:
inputs = {
"legend_text": f"Update {self.instance}",
Expand Down Expand Up @@ -223,6 +226,7 @@ class AttachmentForm(forms.ModelForm):
class Meta:
model = models.Attachment
exclude = ("content_type", "object_id", "content_object")
field_classes = {"description": QuillField}

def __init__(self, *args, **kwargs):
obj = kwargs.pop("parent", None)
Expand All @@ -234,7 +238,6 @@ def __init__(self, *args, **kwargs):

@property
def helper(self):
self.fields["description"].widget.attrs["class"] = "html5text"
helper = BaseFormHelper(self)
helper.form_tag = False
helper.add_row("title", 2, "col-md-6")
Expand Down Expand Up @@ -441,13 +444,6 @@ def __init__(self, *args, **kwargs):

@property
def helper(self):
# by default take-up the whole row
for fld in self.fields.keys():
widget = self.fields[fld].widget
if type(widget) == forms.Textarea:
widget.attrs["rows"] = 3
widget.attrs["class"] = widget.attrs.get("class", "") + " html5text"

if self.instance.id:
inputs = {
"legend_text": f"Update {self.instance}",
Expand Down Expand Up @@ -537,6 +533,7 @@ def save(self, commit=True):
class Meta:
model = models.Dataset
fields = ("name", "description", "published")
field_classes = {"description": QuillField}


class LogFilterForm(forms.Form):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
$('#id_vocabulary').on('change', toggleCuration)
toggleCuration();

$('.html5text').quillify();
$('#id_name').focus();
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
{% endblock %}

{% block extrajs %}
{{ form.media }}
{% include 'assessment/_assessment_form_js.html' %}
{% endblock extrajs %}
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ <h3>Assessment details for team members*</h3>
// attachment-form events
const form = $(target).find('.attachment-form');
if (form) {
form.find('.html5text').quillify();
form.find("#id_title").focus();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
{% endblock %}

{% block extrajs %}
{{ form.media }}
{% include 'assessment/_assessment_form_js.html' %}
{% endblock extrajs %}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,3 @@ <h2>{{assessment}} Logs</h2>
{% include "includes/paginator.html" %}
{% include 'assessment/_logs_note.html' %}
{% endblock content %}

{% block extrajs %}
{{ form.media }}
{% endblock extrajs %}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
{% block extrajs %}
<script type="text/javascript">
$(document).ready(function () {
$('.html5text').quillify();
$('#id_name').focus();

// show/hide excel worksheet name depending on what was file was selected
Expand Down
4 changes: 0 additions & 4 deletions hawc/apps/assessment/templates/assessment/doseunits_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@
{% block content %}
{% crispy form %}
{% endblock content %}

{% block extrajs %}
{{ form.media }}
{% endblock extrajs %}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
{% endblock content %}

{% block extrajs %}
{{ form.media }}
<script type="text/javascript">
$(document).ready(function(){
$('#id_name').on('keyup', function(){
Expand Down
26 changes: 25 additions & 1 deletion hawc/apps/common/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django import forms
from django.template.loader import render_to_string

from . import selectable, validators
from . import selectable, validators, widgets

ASSESSMENT_UNIQUE_MESSAGE = "Must be unique for assessment (current value already exists)."

Expand All @@ -32,6 +32,7 @@ class BaseFormHelper(cf.FormHelper):

error_text_inline = False
use_custom_control = True
include_media = False

def __init__(self, form=None, **kwargs):
self.attrs = {}
Expand Down Expand Up @@ -222,3 +223,26 @@ def format_value(self, value) -> List[str]:
if value is None:
return []
return value.split(",")


class QuillField(forms.CharField):
"""
Quill text editor input.
Cleans HTML and validates urls.
"""

widget = widgets.QuillWidget

def __init__(self, *args, **kwargs):
# Force use of Quill widget
kwargs["widget"] = self.widget
super().__init__(*args, **kwargs)

def to_python(self, value):
value = super().to_python(value)
return validators.clean_html(value) if value else value

def validate(self, value):
super().validate(value)
if value:
validators.validate_hyperlinks(value)
Loading