Skip to content

Commit 63c94a5

Browse files
author
Ahtesham Quraish
committed
fix the image stregth issue
1 parent a4b5e39 commit 63c94a5

File tree

5 files changed

+156
-135
lines changed

5 files changed

+156
-135
lines changed

articles/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from main.models import TimestampedModel
77
from profiles.utils import article_image_upload_uri
8+
from django.utils.text import slugify
89

910

1011
class Article(TimestampedModel):
@@ -23,6 +24,13 @@ class Article(TimestampedModel):
2324
slug = models.SlugField(max_length=255, unique=True, blank=True, null=True)
2425
is_published = models.BooleanField(default=False)
2526

27+
def save(self, *args, **kwargs):
28+
previous = Article.objects.get(pk=self.pk) if self.pk else None
29+
was_published = getattr(previous, "is_published", None)
30+
if not was_published:
31+
self.slug = slugify(self.title)
32+
super().save(*args, **kwargs)
33+
2634

2735
class ArticleImageUpload(models.Model):
2836
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

articles/views_test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import pytest
44
from rest_framework.reverse import reverse
5+
import pytest
6+
from rest_framework.reverse import reverse
7+
8+
from articles.models import Article
59

610
from main.factories import UserFactory
711

@@ -30,3 +34,71 @@ def test_article_permissions(client, is_staff):
3034
resp = client.get(url)
3135
resp.json()
3236
assert resp.status_code == 200 if is_staff else 403
37+
38+
39+
def test_retrieve_article_by_id(client, user):
40+
"""Should retrieve published article by numeric ID"""
41+
article = Article.objects.create(
42+
title="Test Article",
43+
content={},
44+
is_published=True,
45+
user=user,
46+
)
47+
48+
url = reverse(
49+
"articles:v1:article-detail-by-id-or-slug",
50+
kwargs={"identifier": str(article.id)},
51+
)
52+
53+
resp = client.get(url)
54+
data = resp.json()
55+
56+
assert resp.status_code == 200
57+
assert data["id"] == article.id
58+
assert data["title"] == "Test Article"
59+
60+
61+
def test_retrieve_article_by_slug(client, user):
62+
"""Should retrieve published article by slug"""
63+
article = Article.objects.create(
64+
title="Slug Article",
65+
content={},
66+
is_published=True,
67+
user=user,
68+
)
69+
70+
url = reverse(
71+
"articles:v1:article-detail-by-id-or-slug",
72+
kwargs={"identifier": article.slug},
73+
)
74+
75+
resp = client.get(url)
76+
data = resp.json()
77+
78+
assert resp.status_code == 200
79+
assert data["slug"] == article.slug
80+
assert data["title"] == "Slug Article"
81+
82+
83+
def test_staff_can_access_unpublished_article(client):
84+
"""Staff should be able to see unpublished articles"""
85+
staff_user = UserFactory.create(is_staff=True)
86+
client.force_login(staff_user)
87+
88+
article = Article.objects.create(
89+
title="Draft Article",
90+
content={},
91+
is_published=False,
92+
user=staff_user,
93+
)
94+
95+
url = reverse(
96+
"articles:v1:article-detail-by-id-or-slug",
97+
kwargs={"identifier": str(article.id)},
98+
)
99+
100+
resp = client.get(url)
101+
data = resp.json()
102+
103+
assert resp.status_code == 200
104+
assert data["id"] == article.id

frontends/ol-components/src/components/TiptapEditor/ArticleEditor.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import { Alert, Button, ButtonLink } from "@mitodl/smoot-design"
5454
import Typography from "@mui/material/Typography"
5555
import { useUserHasPermission, Permission } from "api/hooks/user"
5656
import { BannerNode } from "./extensions/node/Banner/BannerNode"
57-
import { extractFirstH1Title, slugify } from "./extensions/lib/utils"
5857
import {
5958
HEADER_HEIGHT,
6059
HEADER_HEIGHT_MD,
@@ -147,9 +146,6 @@ const ArticleEditor = ({ onSave, readOnly, article }: ArticleEditorProps) => {
147146
const [touched, setTouched] = useState(false)
148147

149148
const handleSave = (publish: boolean) => {
150-
const title = extractFirstH1Title(content, 1)
151-
const slug = slugify(title ?? "")
152-
153149
if (!title) return
154150
if (article) {
155151
updateArticle(
@@ -158,7 +154,6 @@ const ArticleEditor = ({ onSave, readOnly, article }: ArticleEditorProps) => {
158154
title: title.trim(),
159155
content,
160156
is_published: publish,
161-
slug: !article.is_published ? slug : article.slug || "",
162157
},
163158
{
164159
onSuccess: onSave,
@@ -170,7 +165,6 @@ const ArticleEditor = ({ onSave, readOnly, article }: ArticleEditorProps) => {
170165
title: title.trim(),
171166
content,
172167
is_published: publish,
173-
slug: publish ? slug : "",
174168
},
175169
{
176170
onSuccess: onSave,

frontends/ol-components/src/components/TiptapEditor/extensions/lib/utils.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)