Projet

Général

Profil

0001-add-json_attachment-properties-to-LazyFormData-27323.patch

Benjamin Dauvergne, 22 octobre 2018 12:05

Télécharger (3,04 ko)

Voir les différences:

Subject: [PATCH 1/3] add json_attachment properties to LazyFormData (#27323)

The following properties are added:
- as_json_attachment : use export_to_json without include files
- as_json_attachment_with_files : use export_to_json including files
 tests/test_workflows.py | 14 ++++++++++++++
 wcs/variables.py        | 18 ++++++++++++++++++
 2 files changed, 32 insertions(+)
tests/test_workflows.py
1
import json
1 2
import datetime
2 3
import os
3 4
import pytest
......
1211 1212
    assert emails.emails['foobar']['msg'].get_payload()[2].get_payload() == 'blah'
1212 1213
    assert len(emails.emails['foobar']['msg'].get_payload()) == 3
1213 1214

  
1215
    emails.empty()
1216
    sendmail.attachments = ['form.as_json_attachment', 'form.as_json_attachment_with_files']
1217
    sendmail.perform(formdata)
1218
    get_response().process_after_jobs()
1219
    assert emails.count() == 1
1220
    assert emails.emails['foobar']['msg'].is_multipart()
1221
    assert emails.emails['foobar']['msg'].get_content_subtype() == 'mixed'
1222
    assert emails.emails['foobar']['msg'].get_payload(0).get_content_type() == 'text/html'
1223
    assert emails.emails['foobar']['msg'].get_payload(1).get_content_type() == 'application/json'
1224
    payload1 = emails.emails['foobar']['msg'].get_payload(1)
1225
    payload2 = emails.emails['foobar']['msg'].get_payload(2)
1226
    assert json.loads(formdata.export_to_json(include_files=False)) == json.loads(payload1.get_payload(decode=True))
1227
    assert json.loads(formdata.export_to_json()) == json.loads(payload2.get_payload(decode=True))
1214 1228

  
1215 1229

  
1216 1230
def test_webservice_call(http_requests, pub):
wcs/variables.py
14 14
# You should have received a copy of the GNU General Public License
15 15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16 16

  
17
import base64
18

  
17 19
from quixote import get_publisher
18 20

  
19 21
import qommon.misc
......
248 250
    def evolution(self):
249 251
        return self.formdef.get_detailed_evolution(self.formdata)
250 252

  
253
    def __to_json_attachment(self, include_files=False, anonymise=False):
254
        return {
255
            'filename': 'form-%s.json' % self.formdata.id,
256
            'content_type': 'application/json',
257
            'b64_content': base64.b64encode(self.formdata.export_to_json(
258
                include_files=include_files, anonymise=anonymise)),
259
        }
260

  
261
    @property
262
    def as_json_attachment(self):
263
        return self.__to_json_attachment()
264

  
265
    @property
266
    def as_json_attachment_with_files(self):
267
        return self.__to_json_attachment(include_files=True)
268

  
251 269
    def __getitem__(self, key):
252 270
        try:
253 271
            return getattr(self, key)
254
-