From 1e03df132dc263baf3434aa6daf47b373e564377 Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Thu, 16 May 2024 12:36:44 +0100 Subject: wip: markdown implemented for description field but working on help_text --- pyblackbird_cc/resources/forms.py | 11 ++++++++++- .../migrations/0003_alter_resource_description.py | 18 ++++++++++++++++++ pyblackbird_cc/resources/models.py | 18 +++++++++++++++++- pyblackbird_cc/resources/templatetags/__init__.py | 0 .../resources/templatetags/markdown_extras.py | 10 ++++++++++ .../templates/resources/resource_detail.html | 5 +++-- .../templates/resources/resource_metadata_update.html | 3 +-- 7 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 pyblackbird_cc/resources/migrations/0003_alter_resource_description.py create mode 100644 pyblackbird_cc/resources/templatetags/__init__.py create mode 100644 pyblackbird_cc/resources/templatetags/markdown_extras.py (limited to 'pyblackbird_cc') diff --git a/pyblackbird_cc/resources/forms.py b/pyblackbird_cc/resources/forms.py index da27fd4..05d8a75 100644 --- a/pyblackbird_cc/resources/forms.py +++ b/pyblackbird_cc/resources/forms.py @@ -36,7 +36,8 @@ class ResourceCreateForm(forms.Form): description = forms.CharField( max_length=1000, widget=forms.Textarea, - help_text="This is your opportunity to clearly explain what your resource " + help_text=" You can (and should) use Markdown here. " + "This is your opportunity to clearly explain what your resource " "is all " "about! It’s worth remembering that you are using the space to " "communicate to two " @@ -132,6 +133,14 @@ class ResourceCreateForm(forms.Form): class ResourceUpdateMetadataForm(forms.ModelForm): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.helper = FormHelper(self) + self.helper.add_input(Submit("submit", "Submit")) + + error_css_class = "error" + required_css_class = "required" + class Meta: model = Resource fields = [ diff --git a/pyblackbird_cc/resources/migrations/0003_alter_resource_description.py b/pyblackbird_cc/resources/migrations/0003_alter_resource_description.py new file mode 100644 index 0000000..f0ed99c --- /dev/null +++ b/pyblackbird_cc/resources/migrations/0003_alter_resource_description.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.4 on 2024-05-16 11:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('resources', '0002_alter_resource_additional_resource_category_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='resource', + name='description', + field=models.TextField(help_text='\n Markdown acceptable here!This is your opportunity to clearly explain what\n your resource is all about! It’s worth remembering that you are using the space to\n communicate to two different audiences. Firstly, think about what fellow teachers\n would like to know, such as exactly what the resource contains and how it could be used in\n the classroom. Secondly, the words you include on this page are also talking to internal and\n external search engines. External search engines, like Google, show the first 155 characters\n of the resource description, so make sure you take advantage\n of these characters by using lots of relevant keywords as part of an enticing pitch.\n', max_length=5000), + ), + ] diff --git a/pyblackbird_cc/resources/models.py b/pyblackbird_cc/resources/models.py index 6a279dd..5a03454 100644 --- a/pyblackbird_cc/resources/models.py +++ b/pyblackbird_cc/resources/models.py @@ -16,6 +16,17 @@ AGE_RANGE_CHOICES = [ ("Age not applicable", "Age not applicable"), ] +DESC_HELP_TEXT = """ + Markdown acceptable here!This is your opportunity to clearly explain what + your resource is all about! It’s worth remembering that you are using the space to + communicate to two different audiences. Firstly, think about what fellow teachers + would like to know, such as exactly what the resource contains and how it could be used in + the classroom. Secondly, the words you include on this page are also talking to internal and + external search engines. External search engines, like Google, show the first 155 characters + of the resource description, so make sure you take advantage + of these characters by using lots of relevant keywords as part of an enticing pitch. +""" + # Create your models here. class Resource(models.Model): @@ -39,7 +50,12 @@ class Resource(models.Model): blank=True, related_name="additional_resource_category", ) - description = models.TextField(max_length=1000, null=False, blank=False) + description = models.TextField( + max_length=5000, + null=False, + blank=False, + help_text=DESC_HELP_TEXT, + ) age_range = models.CharField( max_length=20, null=False, diff --git a/pyblackbird_cc/resources/templatetags/__init__.py b/pyblackbird_cc/resources/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyblackbird_cc/resources/templatetags/markdown_extras.py b/pyblackbird_cc/resources/templatetags/markdown_extras.py new file mode 100644 index 0000000..ce98298 --- /dev/null +++ b/pyblackbird_cc/resources/templatetags/markdown_extras.py @@ -0,0 +1,10 @@ +import markdown as md +from django import template +from django.template.defaultfilters import stringfilter + +register = template.Library() + +@register.filter(is_safe=True) +@stringfilter +def markdown(value): + return md.markdown(value, extensions=["markdown.extensions.fenced_code"]) diff --git a/pyblackbird_cc/templates/resources/resource_detail.html b/pyblackbird_cc/templates/resources/resource_detail.html index 70c1b11..8391cca 100644 --- a/pyblackbird_cc/templates/resources/resource_detail.html +++ b/pyblackbird_cc/templates/resources/resource_detail.html @@ -1,5 +1,7 @@ {% extends 'base.html' %} +{% load markdown_extras %} + {% block content %}
@@ -71,8 +73,7 @@ {% endfor %}
-

What's included?

-
{{ resource.description }}
+
{{ resource.description | markdown | safe }}
diff --git a/pyblackbird_cc/templates/resources/resource_metadata_update.html b/pyblackbird_cc/templates/resources/resource_metadata_update.html index 4c318c7..2c6daa1 100644 --- a/pyblackbird_cc/templates/resources/resource_metadata_update.html +++ b/pyblackbird_cc/templates/resources/resource_metadata_update.html @@ -17,9 +17,8 @@
{% csrf_token %} - {{ form|crispy }} + {% crispy form form.helper %} {# now for the submit button #} -
-- cgit v1.2.3