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.
This commit is contained in:
@@ -43,11 +43,40 @@ def attach_project_pictures(project, api):
|
|||||||
This function should be moved in the API, attached to a new Project object.
|
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_square = get_file(project.picture_square, api=api)
|
||||||
project.picture_header = get_file(project.picture_header, api=api)
|
project.picture_header = get_file(project.picture_header, api=api)
|
||||||
project.picture_16_9 = get_file(project.picture_16_9, 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], *,
|
def mass_attach_project_pictures(projects: typing.Iterable[pillarsdk.Project], *,
|
||||||
api, header=True, square=True):
|
api, header=True, square=True):
|
||||||
"""Attach file object to all projects in the list.
|
"""Attach file object to all projects in the list.
|
||||||
|
@@ -1,5 +1,3 @@
|
|||||||
# -*- encoding: utf-8 -*-
|
|
||||||
|
|
||||||
import unittest
|
import unittest
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
@@ -120,3 +118,40 @@ class EvePaginationTest(unittest.TestCase):
|
|||||||
self.assertEqual(2, lpi({'total': 10, 'max_results': 5}))
|
self.assertEqual(2, lpi({'total': 10, 'max_results': 5}))
|
||||||
self.assertEqual(3, lpi({'total': 11, 'max_results': 5}))
|
self.assertEqual(3, lpi({'total': 11, 'max_results': 5}))
|
||||||
self.assertEqual(404129352, lpi({'total': 2828905463, 'max_results': 7}))
|
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)
|
||||||
|
Reference in New Issue
Block a user