forked from django-cms/django-filer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
75 lines (59 loc) · 2.51 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from filer.models.clipboardmodels import Clipboard, ClipboardItem
from filer.models.foldermodels import Folder
from filer.utils.compatibility import PILImage, PILImageDraw
def create_superuser():
from django.contrib.auth import get_user_model
User = get_user_model()
superuser = User.objects.create_superuser('admin',
'secret')
return superuser
def create_folder_structure(depth=2, sibling=2, parent=None):
"""
This method creates a folder structure of the specified depth.
* depth: is an integer (default=2)
* sibling: is an integer (default=2)
* parent: is the folder instance of the parent.
"""
if depth > 0 and sibling > 0:
depth_range = list(range(1, depth + 1))
depth_range.reverse()
for d in depth_range:
for s in range(1, sibling + 1):
name = "folder: {} -- {}".format(str(d), str(s))
folder = Folder(name=name, parent=parent)
folder.save()
create_folder_structure(depth=d - 1, sibling=sibling, parent=folder)
def create_clipboard_item(user, file_obj):
clipboard, was_clipboard_created = Clipboard.objects.get_or_create(user=user)
clipboard_item = ClipboardItem(clipboard=clipboard, file=file_obj)
return clipboard_item
def create_image(mode='RGB', size=(800, 600)):
image = PILImage.new(mode, size)
draw = PILImageDraw.Draw(image)
x_bit, y_bit = size[0] // 10, size[1] // 10
draw.rectangle((x_bit, y_bit * 2, x_bit * 7, y_bit * 3), 'red')
draw.rectangle((x_bit * 2, y_bit, x_bit * 3, y_bit * 8), 'red')
return image
class SettingsOverride:
"""
Overrides Django settings within a context and resets them to their initial
values on exit.
Example:
with SettingsOverride(DEBUG=True):
# do something
"""
def __init__(self, settings_module, **overrides):
self.settings_module = settings_module
self.overrides = overrides
def __enter__(self):
self.old = {}
for key, value in list(self.overrides.items()):
self.old[key] = getattr(self.settings_module, key, None)
setattr(self.settings_module, key, value)
def __exit__(self, _type, value, traceback):
for key, value in list(self.old.items()):
if value is not None:
setattr(self.settings_module, key, value)
else:
delattr(self.settings_module, key)