-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
212 lines (174 loc) · 6.72 KB
/
utils.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Copyright (C) 2008-2009 Open Society Institute
# Thomas Moroz: tmoroz@sorosny.org
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License Version 2 as published
# by the Free Software Foundation. You may not use, modify or distribute
# this program under any other version of the GNU General Public License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import calendar
import copy
import random
import time
from hashlib import sha256 as sha
from zope.component import queryAdapter
from zope.component import queryMultiAdapter
from zope.component import queryUtility
from pyramid.interfaces import ISettings
from pyramid.traversal import find_root
from pyramid.traversal import find_interface
from repoze.lemonade.content import get_content_type
from karl.models.interfaces import ICatalogSearch
from karl.models.interfaces import ICommunity
from karl.models.interfaces import ISite
from karl.models.interfaces import IIntranets
from karl.models.interfaces import IIntranet
from karl.models.interfaces import IAttachmentPolicy
from karl.models.interfaces import IPeopleDirectory
from karl.models.tempfolder import TempFolder
from karl.views.interfaces import IFolderAddables
from karl.views.interfaces import ILayoutProvider
try:
random = random.SystemRandom()
using_sysrandom = True
except NotImplementedError:
using_sysrandom = False
def find_site(context):
site = find_interface(context, ISite)
if site is None:
# for unittesting convenience
site = find_root(context)
return site
def find_users(context):
return getattr(find_site(context), 'users', None)
def find_catalog(context):
return getattr(find_site(context), 'catalog', None)
def find_events(context):
return getattr(find_site(context), 'events', None)
def find_tags(context):
return getattr(find_site(context), 'tags', None)
def find_profiles(context):
return find_site(context).get('profiles')
def find_community(context):
return find_interface(context, ICommunity)
def find_communities(context):
return find_site(context).get('communities')
def find_intranet(context):
# Find the ancestor that has IIntranet, e.g. /osi/someoffice
return find_interface(context, IIntranet)
def find_intranets(context):
# Find the community in the site with IIntranets
site = find_site(context)
for v in site.values():
if IIntranets.providedBy(v):
return v
return []
def find_peopledirectory(context):
site = find_site(context)
people = site.get('people', None)
if people is not None and not IPeopleDirectory.providedBy(people):
# wrong kind of people directory
return None
return people
def find_peopledirectory_catalog(context):
site = find_site(context)
people = site.get('people', None)
if not people:
return None
return getattr(people, 'catalog', None)
def get_setting(context, setting_name, default=None):
# Grab a setting from ISettings. (context is ignored.)
settings = queryUtility(ISettings)
if settings is not None:
return settings.get(setting_name, default)
return default
def get_settings():
return queryUtility(ISettings)
def get_content_type_name(resource):
content_iface = get_content_type(resource)
return content_iface.getTaggedValue('name')
def get_content_type_name_and_icon(resource):
content_iface = get_content_type(resource)
return (content_iface.getTaggedValue('name'),
content_iface.queryTaggedValue('icon', 'blue-document.png'))
def debugsearch(context, **kw):
searcher = ICatalogSearch(context)
kw['use_cache'] = False
num, docids, resolver = searcher(**kw)
L = []
for docid in docids:
L.append(resolver(docid))
return num, L
def get_session(context, request):
site = find_site(context)
session = site.sessions.get(request.environ['repoze.browserid'])
return session
_MAX_32BIT_INT = int((1<<31) - 1)
def docid_to_hex(docid):
return '%08X' % (_MAX_32BIT_INT + docid)
def hex_to_docid(hex):
return int('%s' % hex, 16) - _MAX_32BIT_INT
def asbool(s):
s = str(s).strip()
return s.lower() in ('t', 'true', 'y', 'yes', 'on', '1')
def coarse_datetime_repr(date):
"""Convert a datetime to an integer with 100 second granularity.
The granularity reduces the number of index entries in the
catalog.
"""
timetime = calendar.timegm(date.timetuple())
return int(timetime) // 100
def support_attachments(context):
"""Return true if the given object should support attachments"""
adapter = queryAdapter(context, IAttachmentPolicy)
if adapter:
return adapter.support()
else:
# support attachments by default
return True
class PersistentBBB(object):
""" A descriptor which fixes up old persistent instances with a
default value as a 'write on read' operation. This is usually not
useful if the default value isn't mutable, and arguably 'write on
read' behavior is evil. Note that this descriptor returns the
value after *replacing itself* on the instance with the value."""
def __init__(self, name, val):
self.name = name
self.val = val
def __get__(self, inst, cls):
setattr(inst, self.name, copy.deepcopy(self.val))
return getattr(inst, self.name)
def get_layout_provider(context, request):
from karl.content.views.adapters import DefaultLayoutProvider
return queryMultiAdapter((context, request), ILayoutProvider,
default=DefaultLayoutProvider(context,request))
def get_folder_addables(context, request):
from karl.content.views.adapters import DefaultFolderAddables
return queryMultiAdapter((context, request), IFolderAddables,
default=DefaultFolderAddables(context,request))
def find_tempfolder(context):
root = find_root(context)
if not 'TEMP' in root:
root['TEMP'] = TempFolder()
return root['TEMP']
def find_repo(context):
return getattr(find_site(context), 'repo', None)
SECRET = random.randint(0, 1000000)
def safe_random():
if not using_sysrandom:
random.seed(
sha(
"%s%s%s" % (
random.getstate(),
time.time(),
SECRET)
).digest())
return random.random()