Skip to content

Commit

Permalink
Project description TinyMCE HTML editor (#618)
Browse files Browse the repository at this point in the history
* Replace project description text field with TinyMCE HTML editor (unsanitized)

* Add HTML input sanitization for project descriptions

* Fix indentation inconsistency

---------

Co-authored-by: Job Doesburg <[email protected]>
  • Loading branch information
1058274 and JobDoesburg authored May 8, 2023
1 parent f6e655d commit 653bbc1
Show file tree
Hide file tree
Showing 7 changed files with 878 additions and 748 deletions.
1,550 changes: 806 additions & 744 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ uWSGI = {version = "^2.0.19", optional = true}
admin-totals = "^1.0.1"
django-bootstrap5 = "^22.1"
django-easy-admin-object-actions = "^1.1.0"
django-tinymce = "^3.4.0"
django-bleach = "^3.0.1"

[tool.poetry.extras]
production = ["uwsgi", "psycopg2-binary"]
Expand Down
43 changes: 43 additions & 0 deletions website/giphousewebsite/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
'admin_auto_filters',
'admin_totals',
'django_easy_admin_object_actions',
'tinymce',
'django_bleach',

'questionnaires.apps.QuestionnairesConfig',
'github_oauth.apps.GithubConfig',
Expand Down Expand Up @@ -146,3 +148,44 @@
"https://www.googleapis.com/auth/admin.directory.group",
"https://www.googleapis.com/auth/apps.groups.settings",
]

TINYMCE_DEFAULT_CONFIG = {
"max_height": 500,
"menubar": False,
"plugins": "autolink autoresize link image code media paste lists",
"toolbar": "h2 h3 | bold italic underline strikethrough | image | link unlink "
"| bullist numlist | undo redo | code",
"contextmenu": "bold italic underline strikethrough | link",
"paste_as_text": True,
"relative_urls": False,
"remove_script_host": False,
"autoresize_bottom_margin": 50,
}

# HTML input sanitization settings for the bleach template filter
BLEACH_ALLOWED_TAGS = [
"h2",
"h3",
"p",
"a",
"div",
"strong",
"em",
"i",
"b",
"ul",
"li",
"br",
"ol",
"img",
"span",
]

BLEACH_ALLOWED_ATTRIBUTES = {
"*": ["class", "style"],
"a": ["href", "rel", "target", "title"],
"img": ["alt", "title", "src"],
}

BLEACH_STRIP_TAGS = True
BLEACH_STRIP_COMMENTS = False
1 change: 1 addition & 0 deletions website/giphousewebsite/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ def get_redirect_url(self, *args, **kwargs):
path("projects/", include("projects.urls")),
path("reservations/", include("room_reservation.urls")),
path("lectures/", include("lecture_registrations.urls")),
path("tinymce/", include("tinymce.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
19 changes: 19 additions & 0 deletions website/projects/migrations/0007_alter_project_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.3 on 2023-02-15 20:50

from django.db import migrations
import tinymce.models


class Migration(migrations.Migration):

dependencies = [
("projects", "0006_alter_project_unique_together_project_slug_and_more"),
]

operations = [
migrations.AlterField(
model_name="project",
name="description",
field=tinymce.models.HTMLField(),
),
]
4 changes: 3 additions & 1 deletion website/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from django.db.models.signals import pre_delete
from django.dispatch import receiver

from tinymce.models import HTMLField

from courses.models import Semester

from registrations.models import Employee
Expand Down Expand Up @@ -38,7 +40,7 @@ class Meta:
slug = models.SlugField("slug", max_length=50, blank=False, null=False)

semester = models.ForeignKey(Semester, on_delete=models.CASCADE)
description = models.TextField()
description = HTMLField()
client = models.ForeignKey(Client, on_delete=models.SET_NULL, blank=True, null=True)

comments = models.TextField(
Expand Down
7 changes: 4 additions & 3 deletions website/projects/templates/projects/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends 'base.html' %}
{% load bleach_tags %}

{% block title %}Projects - {{ block.super }}{% endblock %}

Expand All @@ -14,11 +15,11 @@ <h5>No projects found.</h5>
{% if project.client.logo %}<img class="project-logo mt-2 mx-2" src="{{ project.client.logo.url }}" alt="logo {{ project.client.name }}">{% endif %}
<div class="card-body">
<h4 class="card-title">{{ project.name }}</h4>
{% if project.client %}<h6 class="card-subtitle text-muted mb-2">By {{ project.client.name}}</h6>{% endif %}
<p class="card-text">{{ project.description|linebreaks }}</p>
{% if project.client %}<h6 class="card-subtitle text-muted mb-2">By {{ project.client.name }}</h6>{% endif %}
<p class="card-text">{{ project.description | bleach }}</p>
</div>
</div>
</div>
{% endfor %}
{% endif %}
{% endblock %}
{% endblock %}

0 comments on commit 653bbc1

Please sign in to comment.