Skip to content

Commit

Permalink
replace current date in a soup for snapshot, given a specified format
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 25, 2024
1 parent 0426e9b commit 47b40db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lacommunaute/utils/testing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
from datetime import datetime

from bs4 import BeautifulSoup
from django.db import connection
Expand All @@ -19,7 +20,14 @@ def disable(self):
setattr(self._module, key, value)


def parse_response_to_soup(response, selector=None, no_html_body=False, replace_in_href=None, replace_img_src=False):
def parse_response_to_soup(
response,
selector=None,
no_html_body=False,
replace_in_href=None,
replace_img_src=False,
replace_current_date_format=False,
):
soup = BeautifulSoup(response.content, "html5lib", from_encoding=response.charset or "utf-8")
if no_html_body:
# If the provided HTML does not contain <html><body> tags
Expand Down Expand Up @@ -50,6 +58,12 @@ def parse_response_to_soup(response, selector=None, no_html_body=False, replace_
for attr in ["src"]:
for links in soup.find_all("img", attrs={attr: True}):
links.attrs.update({attr: "[img src]"})
if replace_current_date_format:
current_date_str = datetime.now().strftime(replace_current_date_format)
for text_node in soup.find_all(string=True):
if current_date_str in text_node:
new_text = text_node.replace(current_date_str, "[CURRENT DATE]")
text_node.replace_with(new_text)
return soup


Expand Down
7 changes: 7 additions & 0 deletions lacommunaute/utils/tests/tests_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,13 @@ def test_replace_img_src(self):
soup = parse_response_to_soup(response, replace_img_src=True)
assert str(soup) == '<html><head></head><body><img src="[img src]"/></body></html>'

def test_replace_current_date(self):
response = HttpResponse(
f'<html><head></head><body><div>Today is {datetime.now().strftime("%d/%m/%Y")}</div></body></html>'
)
soup = parse_response_to_soup(response, replace_current_date_format="%d/%m/%Y")
assert str(soup) == "<html><head></head><body><div>Today is [CURRENT DATE]</div></body></html>"

def test_replace_in_href_mixing_tuple_and_object(self):
topic = TopicFactory()
response = HttpResponse(
Expand Down

0 comments on commit 47b40db

Please sign in to comment.