Skip to content

Commit 64d218a

Browse files
committed
Playwright task updates
- Fix logger.error usages - Add environment variable to switch playwright debug
1 parent e7a814e commit 64d218a

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

api/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,5 +1044,5 @@ class ExportTokenAdmin(admin.ModelAdmin):
10441044
admin.site.register(models.CountryOfFieldReportToReview, CountryOfFieldReportToReviewAdmin)
10451045
# admin.site.register(Revision, RevisionAdmin)
10461046

1047-
admin.site.site_url = "https://" + settings.FRONTEND_URL
1047+
admin.site.site_url = settings.GO_WEB_URL
10481048
admin.widgets.RelatedFieldWidgetWrapper.template_name = "related_widget_wrapper.html"

api/tasks.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
from playwright.sync_api import sync_playwright
1313
from rest_framework.authtoken.models import Token
1414

15+
from main.utils import logger_context
16+
1517
from .logger import logger
1618
from .models import Export
17-
18-
# from .utils import DebugPlaywright
19+
from .utils import DebugPlaywright
1920

2021

2122
def build_storage_state(tmp_dir, user, token):
@@ -51,9 +52,10 @@ def build_storage_state(tmp_dir, user, token):
5152

5253
@shared_task
5354
def generate_url(url, export_id, user, title):
54-
export = Export.objects.filter(id=export_id).first()
55-
user = User.objects.filter(id=user).first()
55+
export = Export.objects.get(id=export_id)
56+
user = User.objects.get(id=user)
5657
token = Token.objects.filter(user=user).last()
58+
5759
footer_template = """
5860
<div class="footer" style="width: 100%;font-size: 8px;color: #FEFEFE; bottom: 10px; position: absolute;">
5961
<div style="float: left; margin-top: 10px; margin-left: 40px;">
@@ -80,6 +82,7 @@ def generate_url(url, export_id, user, title):
8082
</div>
8183
</div>
8284
""" # noqa: E501
85+
8386
try:
8487
with tempfile.TemporaryDirectory() as tmp_dir:
8588
with sync_playwright() as p:
@@ -103,7 +106,8 @@ def generate_url(url, export_id, user, title):
103106
)
104107
context = browser.new_context(storage_state=storage_state)
105108
page = context.new_page()
106-
# DebugPlaywright.debug(page)
109+
if settings.DEBUG_PLAYWRIGHT:
110+
DebugPlaywright.debug(page)
107111
timeout = 300000
108112
page.goto(url, timeout=timeout)
109113
time.sleep(5)
@@ -131,7 +135,11 @@ def generate_url(url, export_id, user, title):
131135
"completed_at",
132136
]
133137
)
134-
except Exception as e:
135-
logger.error(e)
138+
except Exception:
139+
logger.error(
140+
"Failed to export PDF",
141+
exc_info=True,
142+
extra=logger_context(dict(export_id=export.pk)),
143+
)
136144
export.status = Export.ExportStatus.ERRORED
137145
export.save(update_fields=["status"])

main/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
FDRS_CREDENTIAL=(str, None),
8383
HPC_CREDENTIAL=(str, None),
8484
APPLICATION_INSIGHTS_INSTRUMENTATION_KEY=(str, None),
85+
DEBUG_PLAYWRIGHT=(bool, False),
8586
# Pytest (Only required when running tests)
8687
PYTEST_XDIST_WORKER=(str, None),
8788
# Elastic-Cache
@@ -180,6 +181,7 @@ def parse_domain(*env_keys: str) -> str:
180181

181182
SECRET_KEY = env("DJANGO_SECRET_KEY")
182183
DEBUG = env("DJANGO_DEBUG")
184+
DEBUG_PLAYWRIGHT = env("DEBUG_PLAYWRIGHT")
183185
GO_ENVIRONMENT = env("GO_ENVIRONMENT")
184186

185187
# See if we are inside a test environment

0 commit comments

Comments
 (0)