From c396c7d37137678a4a0c8a238f3de27df7f825fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 22 May 2019 10:14:19 +0200 Subject: [PATCH] Allow web projects to un-attach project pictures This makes it possible to PUT a project after attach_project_pictures() has been called on it (which embeds the picture file documents). This will be used in SVNman. --- pillar/web/utils/__init__.py | 29 +++++++++++++++++++++++++++ tests/test_web/test_utils.py | 39 ++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/pillar/web/utils/__init__.py b/pillar/web/utils/__init__.py index 12ba57c1..2d000bd6 100644 --- a/pillar/web/utils/__init__.py +++ b/pillar/web/utils/__init__.py @@ -43,11 +43,40 @@ def attach_project_pictures(project, api): This function should be moved in the API, attached to a new Project object. """ + # When adding to the list of pictures dealt with here, make sure + # you update unattach_project_pictures() too. project.picture_square = get_file(project.picture_square, api=api) project.picture_header = get_file(project.picture_header, api=api) project.picture_16_9 = get_file(project.picture_16_9, api=api) +def unattach_project_pictures(project: dict): + """Reverts the operation of 'attach_project_pictures'. + + This makes it possible to PUT the project again. + """ + + def unattach(property_name: str): + picture_info = project.get(property_name, None) + if not picture_info: + project.pop(property_name, None) + return + + if not isinstance(picture_info, dict): + # Assume it's already is an ID. + return + + try: + picture_id = picture_info['_id'] + project[property_name] = picture_id + except KeyError: + return + + unattach('picture_square') + unattach('picture_header') + unattach('picture_16_9') + + def mass_attach_project_pictures(projects: typing.Iterable[pillarsdk.Project], *, api, header=True, square=True): """Attach file object to all projects in the list. diff --git a/tests/test_web/test_utils.py b/tests/test_web/test_utils.py index f14f3642..28827780 100644 --- a/tests/test_web/test_utils.py +++ b/tests/test_web/test_utils.py @@ -1,5 +1,3 @@ -# -*- encoding: utf-8 -*- - import unittest import datetime @@ -120,3 +118,40 @@ class EvePaginationTest(unittest.TestCase): self.assertEqual(2, lpi({'total': 10, 'max_results': 5})) self.assertEqual(3, lpi({'total': 11, 'max_results': 5})) self.assertEqual(404129352, lpi({'total': 2828905463, 'max_results': 7})) + + +class UnattachPicturesTest(unittest.TestCase): + def test_unattach_pictures(self): + project = { + 'picture_square': {'_id': 'PICTURE_SQUARE_ID', 'je': 'moeder'}, + 'picture_header': 'PICTURE_HEADER_ID', + 'picture_16_9': {}, + '_id': 'PROJECT_ID', + 'name': 'Op je Hoofd™', + } + from pillar.web.utils import unattach_project_pictures + + unattach_project_pictures(project) + + self.assertEqual({ + 'picture_square': 'PICTURE_SQUARE_ID', + 'picture_header': 'PICTURE_HEADER_ID', + '_id': 'PROJECT_ID', + 'name': 'Op je Hoofd™', + }, project) + + def test_missing_pictures(self): + project = { + 'picture_square': None, + 'picture_16_9': {}, + '_id': 'PROJECT_ID', + 'name': 'Op je Hoofd™', + } + from pillar.web.utils import unattach_project_pictures + + unattach_project_pictures(project) + + self.assertEqual({ + '_id': 'PROJECT_ID', + 'name': 'Op je Hoofd™', + }, project)