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

Limit snippet characters #379

Merged
merged 33 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
9689486
attempt to customise draft checkbox styling
freedompraise Nov 8, 2023
b1decca
Add snippet validator to Post model
freedompraise Nov 8, 2023
f71ad1f
Add CKEditor5Field to Post model for snippet field
freedompraise Nov 8, 2023
d439eaa
Increase max length of snippet validator to 400.
freedompraise Nov 8, 2023
231e334
Revert "attempt to customise draft checkbox styling"
freedompraise Nov 8, 2023
b80a4c8
Make error message red
Nov 9, 2023
be4ed71
Merge branch 'master' into limit-snippet-characters
Nov 9, 2023
338942f
Merge branch 'master' into limit-snippet-characters
freedompraise Nov 10, 2023
a68e2f5
Add test for valid post snippet
freedompraise Nov 10, 2023
e89c141
Merge branch 'limit-snippet-characters' of https://github.com/jsolly/…
freedompraise Nov 10, 2023
912b70c
refactor snippet_validator function
freedompraise Nov 11, 2023
a97ca38
Add snippet validation test to test_models.py
freedompraise Nov 11, 2023
bcad1e4
Add snippet validation test to test_utils.py
freedompraise Nov 11, 2023
e5f305d
Add Django validation for snippet in test_utils.py
freedompraise Nov 11, 2023
254e373
Remove unused function link_media_pattern from
freedompraise Nov 11, 2023
4e53a4b
Merge branch 'master' of https://github.com/jsolly/blogthedata into l…
freedompraise Nov 11, 2023
d5b9f0e
Add similarity computation and cleanup functions
freedompraise Nov 11, 2023
c2eedbd
Merge branch 'master' of https://github.com/jsolly/blogthedata into l…
freedompraise Nov 13, 2023
726e831
Fix snippet_validator import in models.py
freedompraise Nov 13, 2023
a7a3ca0
fix migration errors
freedompraise Nov 13, 2023
02aba83
Refactor imports in test_utils.py
freedompraise Nov 13, 2023
bd038ac
Refactor validator test to new file
Nov 13, 2023
746c525
dependency injection, explicit True return
Nov 13, 2023
99665e0
Add snippet validation to forms.py
Nov 13, 2023
bacbccc
Remove unused migration (moved validation logic to forms.py)
Nov 13, 2023
d7e9d72
Go back to A11y red color
Nov 13, 2023
9dcf6c0
Add failing test
Nov 13, 2023
721ca8f
Remove unused import and validator in models.py
freedompraise Nov 13, 2023
158d763
Update link and media pattern in validators.py
freedompraise Nov 14, 2023
f7adc64
Add link and media removal to snippet_validator
freedompraise Nov 16, 2023
5875051
Add beautifulSoup4 to requirements.txt
freedompraise Nov 16, 2023
26982e2
Refactor snippet_validator function and add test
freedompraise Nov 16, 2023
7d70a08
Refactor link and media regex in snippet_validator
freedompraise Nov 16, 2023
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
21 changes: 21 additions & 0 deletions app/blog/migrations/0042_alter_post_snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.5 on 2023-11-08 16:00

import blog.utils
from django.db import migrations
import django_ckeditor_5.fields


class Migration(migrations.Migration):
dependencies = [
("blog", "0041_disallow_null_values_date_updated"),
]

operations = [
migrations.AlterField(
model_name="post",
name="snippet",
field=django_ckeditor_5.fields.CKEditor5Field(
blank=True, null=True, validators=[blog.utils.snippet_validator]
),
),
]
5 changes: 4 additions & 1 deletion app/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.utils.text import slugify
from django.contrib.auth.models import User
from django_ckeditor_5.fields import CKEditor5Field
from .utils import snippet_validator

# import logging
from django_resized import ResizedImageField
Expand Down Expand Up @@ -70,7 +71,9 @@ class Post(models.Model):
metaimg_alt_txt = models.CharField(max_length=500, default="John Solly Headshot")
metaimg_attribution = models.CharField(max_length=500, blank=True, null=True)
content = CKEditor5Field(blank=True, null=True, config_name="extends")
snippet = CKEditor5Field(blank=True, null=True, config_name="extends")
snippet = CKEditor5Field(
blank=True, null=True, config_name="extends", validators=[snippet_validator]
)
date_posted = models.DateTimeField(default=timezone.now)
date_updated = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User, on_delete=models.CASCADE)
Expand Down
15 changes: 15 additions & 0 deletions app/blog/utils.py
freedompraise marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,18 @@ def answer_question(
model=model,
)
return response["choices"][0]["text"].strip()


from django.core.exceptions import ValidationError
import re


def snippet_validator(value):
freedompraise marked this conversation as resolved.
Show resolved Hide resolved
link_media_regex = r"<a.*?/a>|<img.*?/img>|<video.*?/video>|<audio.*?/audio>"
value_without_links_media = re.sub(link_media_regex, "", value, flags=re.IGNORECASE)
max_length = 400
if len(value_without_links_media) > max_length:
# Raise a validation error
raise ValidationError(
f"The snippet cannot have more than {max_length} characters (excluding links and media)."
)
50 changes: 26 additions & 24 deletions app/staticfiles/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,29 @@ label[for="id_draft"] {
Typeography
============= */

:is(
h1,
h2,
h3,
h4,
h5,
h6,
strong,
.list-group-item,
svg,
table,
p,
blockquote,
li,
legend,
small,
.word-count,
label,

) {
color: var(--secondary-color);
}

.category-title {
font-weight: 300;
letter-spacing: 0.1rem;
Expand Down Expand Up @@ -746,8 +769,9 @@ a[href^="http"]::after {
margin: auto;
}

.errorlist {
color: var(--a11-red);
.errorlist li {
color: red;
list-style-type: none;
}

a.hidden-link {
Expand All @@ -758,28 +782,6 @@ a.hidden-link {
:is(h1, h2, h3) {
line-height: 1.1;
}
:is(
h1,
h2,
h3,
h4,
h5,
h6,
strong,
.list-group-item,
svg,
table,
p,
blockquote,
li,
legend,
small,
.word-count,
label,

) {
color: var(--secondary-color);
}

hr {
border-color: #e0e0e0;
Expand Down