File tree Expand file tree Collapse file tree 2 files changed +20
-10
lines changed Expand file tree Collapse file tree 2 files changed +20
-10
lines changed Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ def __call__(self, request):
14
14
def _set_logged_in_cookie (self , request , response ):
15
15
"""Set or update the 'logged-in' header based on authentication."""
16
16
cookie_name = "logged_in"
17
- if request .user .is_authenticated :
17
+ if hasattr ( request , "user" ) and request .user .is_authenticated :
18
18
response .set_cookie (cookie_name , "1" )
19
19
elif request .COOKIES .get (cookie_name ):
20
20
response .delete_cookie (cookie_name )
Original file line number Diff line number Diff line change 1
1
from typing import cast
2
+ import logging
2
3
3
4
from django import template
4
5
from django .conf import settings
7
8
from django .utils .safestring import mark_safe
8
9
9
10
register = template .Library ()
11
+ logger = logging .getLogger (__name__ )
10
12
11
13
12
14
@register .inclusion_tag ("components/patchwork/patchwork.html" )
@@ -26,16 +28,24 @@ def render_file_content(file_field: FileField) -> str:
26
28
"""Renders the content of a Filefield as a safe HTML string
27
29
and caches the result."""
28
30
29
- def get_file_content () -> str :
30
- with file_field .open () as f :
31
- return mark_safe (f .read ().decode ("utf-8" )) # noqa: S308
31
+ try :
32
32
33
- return cast (
34
- str ,
35
- cache .get_or_set (
36
- f"filefield-{ file_field .name } -{ file_field .size } " , get_file_content
37
- ),
38
- )
33
+ def get_file_content () -> str :
34
+ with file_field .open () as f :
35
+ return mark_safe (f .read ().decode ("utf-8" )) # noqa: S308
36
+
37
+ return cast (
38
+ str ,
39
+ cache .get_or_set (
40
+ f"filefield-{ file_field .name } -{ file_field .size } " , get_file_content
41
+ ),
42
+ )
43
+ except FileNotFoundError as exception :
44
+ # We silent the error here to prevent crashing a page for a missing svg
45
+ logger .warning ("An error was quietly ignored" )
46
+ logger .error (exception )
47
+
48
+ return ""
39
49
40
50
41
51
@register .inclusion_tag ("head/favicon.html" )
You can’t perform that action at this time.
0 commit comments