aboutsummaryrefslogtreecommitdiff
path: root/middleware
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-06-06 20:14:28 -0400
committerBobby <[email protected]>2023-06-06 20:14:28 -0400
commit5de7630148dd9ef69a66ea269e0b8077628e51ca (patch)
tree4688c02eb479cc03133e41a39f825cf289719c35 /middleware
parentb71094f06d4dce6777daaa7ae01104b8251c4c72 (diff)
downloadthatcomputerscientist-5de7630148dd9ef69a66ea269e0b8077628e51ca.tar.xz
thatcomputerscientist-5de7630148dd9ef69a66ea269e0b8077628e51ca.zip
Fix code highlight for Japanese
Diffstat (limited to 'middleware')
-rw-r--r--middleware/translationMiddleware.py14
-rw-r--r--middleware/uuidmiddleware.py4
2 files changed, 14 insertions, 4 deletions
diff --git a/middleware/translationMiddleware.py b/middleware/translationMiddleware.py
index de143286..ba1eda1f 100644
--- a/middleware/translationMiddleware.py
+++ b/middleware/translationMiddleware.py
@@ -1,7 +1,9 @@
-from google.cloud import translate_v2 as translate
+import os
+
+from bs4 import BeautifulSoup
from django.conf import settings
-import os
from django.core.cache import cache
+from google.cloud import translate_v2 as translate
cred_path = os.path.join(settings.BASE_DIR, 'credentials-translate.json')
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cred_path
@@ -21,7 +23,13 @@ class TranslationMiddleware:
if 'text' not in content_type:
return response
- HTML_content = response.content.decode('utf-8')
+ HTML_content =response.content.decode('utf-8').strip()
+
+ # add no translate class to the 'highlight' class
+ soup = BeautifulSoup(HTML_content, 'html.parser')
+ for tag in soup.find_all(class_='highlight'):
+ tag['class'].append('notranslate')
+ HTML_content = str(soup)
HTML_content = HTML_content.replace(
"That Computer Scientist",
diff --git a/middleware/uuidmiddleware.py b/middleware/uuidmiddleware.py
index 859000c5..212e67b2 100644
--- a/middleware/uuidmiddleware.py
+++ b/middleware/uuidmiddleware.py
@@ -1,6 +1,8 @@
+import json
import uuid
+
import redis
-import json
+
redis_instance = redis.StrictRedis(host='localhost', port=6379, db=0)