Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Cleanup and fixes

Roland Geider3a033ae8e89b
diff --git a/wger/exercises/api/serializers.py b/wger/exercises/api/serializers.py
index 916bf1946..a7f9e9a20 100644
--- a/wger/exercises/api/serializers.py
+++ b/wger/exercises/api/serializers.py
@@ -507,7 +507,7 @@ class ExerciseTranslationSerializer(serializers.ModelSerializer):
'license_author',
)
- read_only_fields = 'description' # Prevents API from accepting raw HTML
+ read_only_fields = ('description',) # Prevents API from accepting raw HTML
def validate(self, value):
"""
diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.py
index e07febdb7..76bbc626b 100644
--- a/wger/exercises/api/views.py
+++ b/wger/exercises/api/views.py
@@ -84,9 +84,6 @@ from wger.exercises.models import (
from wger.exercises.views.helper import StreamVerbs
from wger.utils.constants import (
ENGLISH_SHORT_NAME,
- HTML_ATTRIBUTES_WHITELIST,
- HTML_STYLES_WHITELIST,
- HTML_TAG_WHITELIST,
SEARCH_ALL_LANGUAGES,
)
from wger.utils.db import is_postgres_db
diff --git a/wger/exercises/migrations/0036_add_markdown_description_field.py b/wger/exercises/migrations/0036_add_markdown_description_field.py
index ebdb0a242..77f5b97f6 100644
--- a/wger/exercises/migrations/0036_add_markdown_description_field.py
+++ b/wger/exercises/migrations/0036_add_markdown_description_field.py
@@ -6,16 +6,19 @@ from markdownify import markdownify
from django.db import migrations, models
+from wger.utils.markdown import render_markdown
+
+
def migrate_description_to_markdown(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
Translation = apps.get_model('exercises', 'Translation')
- translations_to_migrate = Translation.objects.exclude(description='')
+ translations_to_migrate = Translation.objects.exclude(description='').exclude(description=None)
for trans in translations_to_migrate:
# ATX to ensure # headings instead of underlined headings.
trans.description_source = markdownify(trans.description, heading_style='ATX')
- # save() triggers "description = render_markdown(description_source)". This
- # ensures a clean HTML cache
+ # Call render_markdown() explicitly
+ trans.description = render_markdown(trans.description_source)
trans.save()
diff --git a/wger/exercises/models/translation.py b/wger/exercises/models/translation.py
index 0538018fb..ea0f5167d 100644
--- a/wger/exercises/models/translation.py
+++ b/wger/exercises/models/translation.py
@@ -212,13 +212,6 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model):
"""
return self.images.all().filter(is_main=True).first()
- @property
- def description_clean(self):
- """
- Return the exercise description with all markup removed
- """
- return sanitize_html(self.description)
-
def get_owner_object(self):
"""
Exercise has no owner information
diff --git a/wger/exercises/sync.py b/wger/exercises/sync.py
index a8a6bba98..95cb0ab33 100644
--- a/wger/exercises/sync.py
+++ b/wger/exercises/sync.py
@@ -103,6 +103,7 @@ def sync_exercises(
trans_uuid = translation_data['uuid']
name = translation_data['name']
description = translation_data['description']
+ description_source = translation_data['description_source']
language_id = translation_data['language']
translation, translation_created = Translation.objects.update_or_create(
@@ -111,6 +112,7 @@ def sync_exercises(
'exercise': exercise,
'name': name,
'description': description,
+ 'description_source': description_source,
'license_id': license_id,
'license_author': license_author,
'language_id': language_id,
diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.py
index 5a4a4471a..f305d30c4 100644
--- a/wger/exercises/tests/test_sync.py
+++ b/wger/exercises/tests/test_sync.py
@@ -392,7 +392,8 @@ class MockExerciseResponse:
"uuid": "c788d643-150a-4ac7-97ef-84643c6419bf",
"name": "Zweihandiges Kettlebell",
"exercise": 123,
- "description": "Hier könnte Ihre Werbung stehen!",
+ "description": "<p>Hier könnte Ihre Werbung stehen!</p>",
+ "description_source": "Hier könnte Ihre Werbung stehen!",
"created": "2015-08-03",
"language": 1,
"aliases": [
@@ -423,7 +424,8 @@ class MockExerciseResponse:
"uuid": "ab4185dd-2e68-4579-af1f-0c03957c0a9e",
"name": "2 Handed Kettlebell Swing",
"exercise": 123,
- "description": "TBD",
+ "description": "<p>TBD</p>",
+ "description_source": "TBD",
"created": "2023-08-03",
"language": 2,
"aliases": [],
@@ -499,7 +501,8 @@ class MockExerciseResponse:
"uuid": "7524ca8d-032e-482d-ab18-40e8a97851f6",
"name": "A new, better, updated name",
"exercise": 2,
- "description": "Two Handed Russian Style Kettlebell swing",
+ "description": "<p>Two Handed Russian Style Kettlebell swing</p>",
+ "description_source": "Two Handed Russian Style Kettlebell swing",
"created": "2015-08-03",
"language": 1,
"aliases": [
@@ -539,7 +542,8 @@ class MockExerciseResponse:
"uuid": "581338a1-8e52-405b-99eb-f0724c528bc8",
"name": "Balançoire Kettlebell à 2 mains",
"exercise": 2,
- "description": "Balançoire Kettlebell à deux mains de style russe",
+ "description": "<p>Balançoire Kettlebell à deux mains de style russe</p>",
+ "description_source": "Balançoire Kettlebell à deux mains de style russe",
"created": "2015-08-03",
"language": 3,
"aliases": [],
@@ -762,7 +766,14 @@ class TestSyncMethods(WgerTestCase):
translation_de = new_exercise.get_translation('de')
self.assertEqual(translation_de.language_id, 1)
self.assertEqual(translation_de.name, 'Zweihandiges Kettlebell')
- self.assertEqual(translation_de.description, 'Hier könnte Ihre Werbung stehen!')
+ self.assertEqual(
+ translation_de.description_source,
+ 'Hier könnte Ihre Werbung stehen!',
+ )
+ self.assertEqual(
+ translation_de.description,
+ '<p>Hier könnte Ihre Werbung stehen!</p>\n',
+ )
self.assertEqual(translation_de.alias_set.first().alias, 'Kettlebell mit zwei Händen')
self.assertEqual(
translation_de.exercisecomment_set.first().comment,
@@ -772,7 +783,8 @@ class TestSyncMethods(WgerTestCase):
translation_en = new_exercise.get_translation('en')
self.assertEqual(translation_en.language_id, 2)
self.assertEqual(translation_en.name, '2 Handed Kettlebell Swing')
- self.assertEqual(translation_en.description, 'TBD')
+ self.assertEqual(translation_en.description_source, 'TBD')
+ self.assertEqual(translation_en.description, '<p>TBD</p>\n')
# Existing exercise was updated
exercise = Exercise.objects.get(uuid='ae3328ba-9a35-4731-bc23-5da50720c5aa')
diff --git a/wger/utils/generic_views.py b/wger/utils/generic_views.py
index f9a030fda..b9c177475 100644
--- a/wger/utils/generic_views.py
+++ b/wger/utils/generic_views.py
@@ -39,11 +39,6 @@ from crispy_forms.layout import (
)
# wger
-from wger.utils.constants import (
- HTML_ATTRIBUTES_WHITELIST,
- HTML_STYLES_WHITELIST,
- HTML_TAG_WHITELIST,
-)
from wger.utils.markdown import sanitize_html
diff --git a/wger/utils/markdown.py b/wger/utils/markdown.py
index ec1c01460..161e3cc2a 100644
--- a/wger/utils/markdown.py
+++ b/wger/utils/markdown.py
@@ -15,11 +15,13 @@
# You should have received a copy of the GNU Affero General Public License
# Third Party
-# Third party
import nh3
from markdown_it import MarkdownIt
+ALLOWED_TAGS = {'b', 'strong', 'i', 'em', 'ul', 'ol', 'li', 'p'}
+
+
def render_markdown(text):
"""
Renders markdown text to HTML and sanitizes it to allow only basic markup.
@@ -31,12 +33,7 @@ def render_markdown(text):
md = MarkdownIt('commonmark', {'breaks': True, 'html': True})
raw_html = md.render(text)
- # Sanitize HTML
- ALLOWED_TAGS = {'b', 'strong', 'i', 'em', 'ul', 'ol', 'li', 'p'}
-
- clean_html = nh3.clean(raw_html, tags=ALLOWED_TAGS, attributes={})
-
- return clean_html
+ return nh3.clean(raw_html, tags=ALLOWED_TAGS, attributes={})
def sanitize_html(text):
@@ -46,5 +43,4 @@ def sanitize_html(text):
if not text:
return ''
- ALLOWED_TAGS = {'b', 'strong', 'i', 'em', 'ul', 'ol', 'li', 'p'}
return nh3.clean(text, tags=ALLOWED_TAGS, attributes={})