Skip to content

Commit

Permalink
Use a cache directory instead of temporary files
Browse files Browse the repository at this point in the history
Temporary files behave differently on Windows from how they do on Unix:
https://stackoverflow.com/a/23212515 which was causing permission errors
when creating and writing docx files. To avoid this, we will use a
regular cache directory. However, note that files no longer get cleaned
up automatically (issue #27).
  • Loading branch information
jftsang committed Sep 2, 2023
1 parent 066518d commit dd58946
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
appdirs~=1.4.4
attrs~=21.2.0
cattrs~=22.1.0
docxtpl~=0.15.2
Expand Down
9 changes: 7 additions & 2 deletions tests/test_pypew.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from datetime import date
from pathlib import Path
from unittest.mock import patch
from urllib.parse import urlencode

Expand All @@ -15,6 +16,10 @@
from utils import advent


def m_create_docx_impl(path):
Path(path).touch()


class TestDates(unittest.TestCase):
@parameterized.expand([
# Christmas Day in 2021 was a Saturday
Expand Down Expand Up @@ -159,7 +164,7 @@ def test_feast_detail_view_handles_not_found(self):
)
self.assertEqual(r.status_code, 404)

@patch('pypew.views.feast_views.Feast.create_docx')
@patch('pypew.views.feast_views.Feast.create_docx', side_effect=m_create_docx_impl)
def test_feast_docx_view(self, m_create_docx):
r = self.client.get(
url_for('feast_docx_view', slug='christmas-day')
Expand All @@ -175,7 +180,7 @@ def test_feast_docx_view(self, m_create_docx):
r.headers['Content-Type'],
)

@patch('pypew.views.pew_sheet_views.Service.create_docx')
@patch('pypew.views.pew_sheet_views.Service.create_docx', side_effect=m_create_docx_impl)
def test_pew_sheet_docx_view(self, m_create_docx):
r = self.client.get(
url_for('pew_sheet_docx_view') + '?' + urlencode(
Expand Down
5 changes: 5 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from functools import lru_cache
from typing import Optional

from appdirs import AppDirs

try:
import pandas as pd
except ImportError:
Expand All @@ -14,6 +16,9 @@ class NoPandasError(RuntimeError):
pass


cache_dir = AppDirs("pypew").user_cache_dir
os.makedirs(cache_dir, exist_ok=True)

logger = logging.getLogger("pypew")
logger.setLevel(logging.INFO)

Expand Down
16 changes: 9 additions & 7 deletions views/feast_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
from tempfile import NamedTemporaryFile
import os
import uuid
from tempfile import TemporaryDirectory

import cattrs
from flask import (flash, make_response, render_template, send_file,
Expand All @@ -8,7 +10,7 @@
from filters import english_date
from models import Feast
from models_base import NotFoundError, get
from utils import str2date
from utils import str2date, cache_dir

__all__ = ['feast_index_view', 'feast_index_api', 'feast_date_api',
'feast_upcoming_api', 'feast_detail_view', 'feast_detail_api',
Expand Down Expand Up @@ -89,8 +91,8 @@ def feast_docx_view(slug):
return make_response(feast_index_view(), 404)

filename = f'{feast.name}.docx'
with NamedTemporaryFile() as tf:
feast.create_docx(path=tf.name)
return send_file(
tf.name, as_attachment=True, attachment_filename=filename
)
temp_docx = os.path.join(cache_dir, f"feast_{str(uuid.uuid4())}.docx")
feast.create_docx(path=temp_docx)
return send_file(
temp_docx, as_attachment=True, attachment_filename=filename
)
17 changes: 8 additions & 9 deletions views/pew_sheet_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from tempfile import NamedTemporaryFile
import uuid
from urllib.parse import parse_qs, urlencode

import dotenv
Expand All @@ -9,7 +9,7 @@

from forms import PewSheetForm
from models import Service, Feast
from utils import logger
from utils import logger, cache_dir

__all__ = ['pew_sheet_create_view', 'pew_sheet_clear_history_endpoint', 'pew_sheet_docx_view']

Expand Down Expand Up @@ -79,10 +79,9 @@ def pew_sheet_docx_view():

datestamp = service.date.strftime("%Y-%m-%d")

with NamedTemporaryFile() as tf:
service.create_docx(tf.name)
return send_file(
tf.name,
as_attachment=True,
attachment_filename=f'{datestamp} {service.title}.docx'
)
filename = f'{datestamp} {service.title}.docx'
temp_docx = os.path.join(cache_dir, f"pew_sheet_{str(uuid.uuid4())}.docx")
service.create_docx(temp_docx)
return send_file(
temp_docx, as_attachment=True, attachment_filename=filename
)

0 comments on commit dd58946

Please sign in to comment.