0001-pwa-import-export-pwa-application-icon-44833.patch
combo/apps/pwa/models.py | ||
---|---|---|
76 | 76 |
return cls.objects.first() or cls() |
77 | 77 | |
78 | 78 |
@classmethod |
79 | 79 |
def export_for_json(cls): |
80 | 80 |
obj = cls.singleton() |
81 | 81 |
if not obj.id: |
82 | 82 |
return {} |
83 | 83 |
serialized_settings = json.loads(serializers.serialize('json', [obj])) |
84 |
return serialized_settings[0].get('fields') |
|
84 |
result = serialized_settings[0].get('fields') |
|
85 |
if obj.application_icon: |
|
86 |
result['icon:base64'] = force_text(base64.encodebytes(obj.application_icon.read())) |
|
87 |
return result |
|
85 | 88 | |
86 | 89 |
@classmethod |
87 | 90 |
def load_serialized_settings(cls, json_settings): |
88 | 91 |
if not json_settings: |
89 | 92 |
return |
90 | 93 | |
91 | 94 |
obj = cls.singleton() |
95 |
decoded_icon = None |
|
96 |
if json_settings.get('icon:base64'): |
|
97 |
decoded_icon = base64.decodebytes(force_bytes(json_settings['icon:base64'])) |
|
98 |
del json_settings['icon:base64'] |
|
92 | 99 |
for attr in json_settings: |
93 | 100 |
setattr(obj, attr, json_settings[attr]) |
94 | 101 |
obj.save() |
102 |
if decoded_icon: |
|
103 |
if not default_storage.exists(obj.application_icon.name) or obj.application_icon.read() != decoded_icon: |
|
104 |
# save new file |
|
105 |
path = obj.application_icon.name |
|
106 |
if path.startswith('pwa/'): |
|
107 |
path = path[len('pwa/'):] |
|
108 |
obj.application_icon.save(path, ContentFile(decoded_icon)) |
|
95 | 109 | |
96 | 110 |
@classmethod |
97 | 111 |
def get_default_application_name(cls): |
98 | 112 |
return settings.TEMPLATE_VARS.get('global_title') or 'Compte Citoyen' |
99 | 113 | |
100 | 114 |
def get_application_name(self): |
101 | 115 |
return self.application_name or self.get_default_application_name() |
102 | 116 |
tests/test_import_export.py | ||
---|---|---|
256 | 256 |
assert Asset.objects.count() == 2 |
257 | 257 | |
258 | 258 |
def test_import_export_pwa_settings(app): |
259 | 259 |
output = get_output_of_command('export_site') |
260 | 260 |
pwa_settings = PwaSettings.singleton() |
261 | 261 |
pwa_settings.offline_text = 'Hello world' |
262 | 262 |
pwa_settings.offline_retry_button = False |
263 | 263 |
pwa_settings.save() |
264 | ||
265 |
# check exort with no application icon |
|
266 |
output = get_output_of_command('export_site') |
|
267 | ||
268 |
pwa_settings.application_icon = File(BytesIO(b'te\30st'), 'test.png') |
|
269 |
pwa_settings.save() |
|
264 | 270 |
output = get_output_of_command('export_site') |
265 | 271 |
import_site(data={}, clean=True) |
266 | 272 |
assert PwaSettings.objects.all().count() == 0 |
267 | 273 | |
268 | 274 |
import_site(data=json.loads(output)) |
269 | 275 |
assert PwaSettings.singleton().offline_retry_button is False |
270 | 276 |
assert PwaSettings.singleton().offline_text == 'Hello world' |
277 |
# check identical file was not touched |
|
278 |
assert os.path.basename(PwaSettings.objects.get().application_icon.file.name) == 'test.png' |
|
279 |
assert PwaSettings.objects.get().application_icon.read() == b'te\30st' |
|
280 | ||
281 |
# check with a change in icon file content |
|
282 |
data = json.loads(output) |
|
283 |
data['pwa']['settings']['icon:base64'] = force_text(base64.encodebytes(b'TEST')) |
|
284 |
import_site(data=data) |
|
285 |
assert PwaSettings.objects.get().application_icon.read() == b'TEST' |
|
286 | ||
287 |
# check with a change in icon file name |
|
288 |
data = json.loads(output) |
|
289 |
data['pwa']['settings']['icon:base64'] = force_text(base64.encodebytes(b'TEST2')) |
|
290 |
data['pwa']['settings']['application_icon'] = 'pwa/test2.png' |
|
291 |
import_site(data=data) |
|
292 |
assert os.path.basename(PwaSettings.objects.get().application_icon.file.name) == 'test2.png' |
|
293 |
assert PwaSettings.objects.get().application_icon.read() == b'TEST2' |
|
271 | 294 | |
272 | 295 |
def test_import_export_pwa_navigation(app, some_data): |
273 | 296 |
page = Page.objects.get(slug='one') |
274 | 297 |
entry1 = PwaNavigationEntry(label='a', url='/', order=0) |
275 | 298 |
entry2 = PwaNavigationEntry(link_page=page, order=1, icon=File(BytesIO(b'te\30st'), 'test.png')) |
276 | 299 |
entry1.save() |
277 | 300 |
entry2.save() |
278 | 301 |
output = get_output_of_command('export_site') |
279 |
- |