Projet

Général

Profil

0002-workflows-change-attachment-view-to-use-the-factored.patch

Frédéric Péters, 20 août 2015 11:56

Télécharger (4,33 ko)

Voir les différences:

Subject: [PATCH 2/4] workflows: change attachment view to use the factored
 file code path (#8031)

 tests/test_form_pages.py |  2 ++
 wcs/wf/attachment.py     | 54 +++++++++++++++++++-----------------------------
 wcs/workflows.py         |  3 +++
 3 files changed, 26 insertions(+), 33 deletions(-)
tests/test_form_pages.py
1033 1033

  
1034 1034
    resp = resp.follow() # back to form page
1035 1035
    resp = resp.click('test.txt')
1036
    assert resp.location.endswith('/test.txt')
1037
    resp = resp.follow()
1036 1038
    assert resp.content_type == 'text/plain'
1037 1039
    assert resp.body == 'foobar'
wcs/wf/attachment.py
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17

  
18
from quixote import get_response
19
from quixote.util import FileStream
18
from quixote import get_response, redirect
20 19

  
21 20
from wcs.workflows import *
22 21
from qommon.errors import *
23 22

  
24
from wcs.forms.common import FormStatusPage
23
from wcs.forms.common import FormStatusPage, FileDirectory
24

  
25
def lookup_wf_attachment(self, filename):
26
    # supports for URLs such as /$formdata/$id/files/attachment/test.txt
27
    if self.reference.split('-')[0] != 'attachment':
28
        return
29

  
30
    for evo in self.formdata.evolution:
31
        if evo.parts:
32
            for p in evo.parts:
33
                if not isinstance(p, AttachmentEvolutionPart):
34
                    continue
35
                if p.base_filename == filename:
36
                    return p
37

  
25 38

  
26 39
def form_attachment(self):
27 40
    self.check_receiver()
......
31 44
    except (KeyError, ValueError):
32 45
        raise TraversalError()
33 46

  
34
    found = False
35 47
    for evo in self.filled.evolution:
36 48
        if evo.parts:
37 49
            for p in evo.parts:
38 50
                if not isinstance(p, AttachmentEvolutionPart):
39 51
                    continue
40 52
                if os.path.basename(p.filename) == fn:
41
                    found = True
42
                    break
43
            if found:
44
                break
45

  
46
    if not found:
47
        raise TraversalError()
48

  
49
    try:
50
        fd = open(p.filename, 'rb')
51
    except:
52
        raise TraversalError()
53
    size = os.path.getsize(p.filename)
54

  
55
    response = get_response()
56
    if p.content_type:
57
        response.set_content_type(p.content_type)
58
    else:
59
        response.set_content_type('application/octet-stream')
60
    if p.charset:
61
        response.set_charset(p.charset)
62
    if p.base_filename:
63
        if p.content_type and p.content_type.startswith('image/'):
64
            response.set_header(
65
                'content-disposition', 'inline; filename="%s"' % p.base_filename)
66
        else:
67
            response.set_header(
68
                'content-disposition', 'attachment; filename="%s"' % p.base_filename)
53
                    return redirect(self.filled.get_url() + 'files/attachment/' + p.base_filename)
69 54

  
70
    return FileStream(fd, size)
55
    raise TraversalError()
71 56

  
72 57

  
73 58
class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
......
87 72
    def init(cls):
88 73
        FormStatusPage._q_extra_exports.append('attachment')
89 74
        FormStatusPage.attachment = form_attachment
75
        if not 'lookup_wf_attachment' in FileDirectory._lookup_methods:
76
            FileDirectory._lookup_methods.append('lookup_wf_attachment')
77
            FileDirectory.lookup_wf_attachment = lookup_wf_attachment
90 78
    init = classmethod(init)
91 79

  
92 80
    def render_as_line(self):
wcs/workflows.py
75 75
                upload.charset)
76 76
    from_upload = classmethod(from_upload)
77 77

  
78
    def get_file_pointer(self):
79
        return open(self.filename)
80

  
78 81
    def __getstate__(self):
79 82
        odict = self.__dict__.copy()
80 83
        if not odict.has_key('fp'):
81
-