command to remove relative news description images
This commit is contained in:
parent
6a9356aaa6
commit
351d492012
17
apps/news/management/commands/rm_empty_images.py
Normal file
17
apps/news/management/commands/rm_empty_images.py
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
|
from news.models import News
|
||||||
|
import re
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
help = 'Removes empty img html tags from news description'
|
||||||
|
|
||||||
|
relative_img_regex = re.compile(r'\<img.+src=(?!https?:\/\/)([^\/].+?)[\"|\']>', re.I)
|
||||||
|
|
||||||
|
def handle(self, *args, **kwargs):
|
||||||
|
for news in News.objects.all():
|
||||||
|
if isinstance(news.description, dict):
|
||||||
|
news.description = {locale: self.relative_img_regex.sub('', rich_text)
|
||||||
|
for locale, rich_text in news.description.items()}
|
||||||
|
self.stdout.write(self.style.WARNING(f'Replaced {news} empty img html tags...\n'))
|
||||||
|
news.save()
|
||||||
Loading…
Reference in New Issue
Block a user