|
1
|
|
|
# coding: utf8 |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
|
5
|
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
|
7
|
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
|
10
|
|
|
the License at |
|
11
|
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
17
|
|
|
License for the specific language governing permissions and limitations under |
|
18
|
|
|
the License. |
|
19
|
|
|
""" |
|
20
|
|
|
|
|
21
|
|
|
import shutil |
|
22
|
|
|
import tempfile |
|
23
|
|
|
from django.conf import settings |
|
24
|
|
|
from django.test import override_settings |
|
25
|
|
|
|
|
26
|
|
|
from lxml.builder import E |
|
27
|
|
|
|
|
28
|
|
|
class temporary_media_root(override_settings): |
|
29
|
|
|
"""Temporarily override settings.MEDIA_ROOT with a temporary directory. |
|
30
|
|
|
|
|
31
|
|
|
The temporary directory is automatically created and destroyed. |
|
32
|
|
|
|
|
33
|
|
|
Use this function as a context manager: |
|
34
|
|
|
|
|
35
|
|
|
>>> from django_downloadview.test import temporary_media_root |
|
36
|
|
|
>>> from django.conf import settings # NoQA |
|
37
|
|
|
>>> global_media_root = settings.MEDIA_ROOT |
|
38
|
|
|
>>> with temporary_media_root(): |
|
39
|
|
|
... global_media_root == settings.MEDIA_ROOT |
|
40
|
|
|
False |
|
41
|
|
|
>>> global_media_root == settings.MEDIA_ROOT |
|
42
|
|
|
True |
|
43
|
|
|
|
|
44
|
|
|
Or as a decorator: |
|
45
|
|
|
|
|
46
|
|
|
>>> @temporary_media_root() |
|
47
|
|
|
... def use_temporary_media_root(): |
|
48
|
|
|
... return settings.MEDIA_ROOT |
|
49
|
|
|
>>> tmp_media_root = use_temporary_media_root() |
|
50
|
|
|
>>> global_media_root == tmp_media_root |
|
51
|
|
|
False |
|
52
|
|
|
>>> global_media_root == settings.MEDIA_ROOT |
|
53
|
|
|
True |
|
54
|
|
|
|
|
55
|
|
|
""" |
|
56
|
|
|
|
|
57
|
|
|
def enable(self): |
|
58
|
|
|
"""Create a temporary directory and use it to override |
|
59
|
|
|
settings.MEDIA_ROOT.""" |
|
60
|
|
|
tmp_dir = tempfile.mkdtemp() |
|
61
|
|
|
self.options['MEDIA_ROOT'] = tmp_dir |
|
62
|
|
|
super(temporary_media_root, self).enable() |
|
63
|
|
|
|
|
64
|
|
|
def disable(self): |
|
65
|
|
|
"""Remove directory settings.MEDIA_ROOT then restore original |
|
66
|
|
|
setting.""" |
|
67
|
|
|
shutil.rmtree(settings.MEDIA_ROOT) |
|
68
|
|
|
super(temporary_media_root, self).disable() |
|
69
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
def create_app_xml(**kwargs): |
|
72
|
|
|
events = kwargs.pop('events', []) |
|
73
|
|
|
app = dict(**kwargs) |
|
74
|
|
|
app = E.app(app) |
|
75
|
|
|
if type(events) is not list: |
|
76
|
|
|
events = [events] |
|
77
|
|
|
for event in events: |
|
78
|
|
|
e = E.event(event) |
|
79
|
|
|
app.append(e) |
|
80
|
|
|
return app |
|
81
|
|
|
|