本文整理汇总了Python中tiddlyweb.model.bag.Bag类的典型用法代码示例。如果您正苦于以下问题:Python Bag类的具体用法?Python Bag怎么用?Python Bag使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Bag类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: retrieve_from_store
def retrieve_from_store(email):
"""
get the tiddler requested by the email from the store
and return it as an email
"""
store = get_store(config)
tiddler_title = clean_subject(email["subject"])
tiddler = Tiddler(tiddler_title)
bag = determine_bag(email["to"])
tiddler.bag = bag
try:
tiddler = store.get(tiddler)
response_text = tiddler.text
except NoTiddlerError:
# Tiddler not found. Return a list of all tiddlers
bag = Bag(bag)
bag = store.get(bag)
response_text = "The following tiddlers are in %s:\n" % email["to"].split("@")[1]
tiddlers = bag.gen_tiddlers()
tiddlers = [tiddler for tiddler in tiddlers]
response_text += "\n".join([tiddler.title for tiddler in tiddlers])
response_email = {"from": email["to"], "to": email["from"], "subject": tiddler.title, "body": response_text}
return response_email
示例2: _create_bag
def _create_bag(environ):
"""Take the form input and turn it into a bag."""
query_data = _flatten_form_data(environ['tiddlyweb.query'])
logging.debug(query_data)
store = environ['tiddlyweb.store']
try:
new_bag_name = query_data['bag_name']
if _bag_exists(store, new_bag_name):
raise HTTP409('That bag may not be created.')
new_bag = Bag(new_bag_name)
username = environ['tiddlyweb.usersign']['name']
new_bag.policy.owner = username
new_bag.policy.manage = [username]
new_bag.desc = query_data.get('bag_desc', '')
for policy_type in ('read', 'write', 'create', 'delete'):
texted = query_data.get(policy_type + '_text', None)
logging.debug('texted: %s' % texted)
if texted:
new_bag.policy.__setattr__(policy_type, [x.lstrip().rstrip() for x in texted.split(',')])
else:
set = query_data[policy_type]
new_bag.policy.__setattr__(policy_type, _policy_form_to_entry(username, set))
store.put(new_bag)
except KeyError, exc:
raise HTTP400('something went wrong processing for: %s' % exc)
示例3: setup_module
def setup_module(module):
module.TMPDIR = tempfile.mkdtemp()
_initialize_app(TMPDIR)
module.ADMIN_COOKIE = make_cookie('tiddlyweb_user', 'admin',
mac_key=CONFIG['secret'])
module.STORE = get_store(CONFIG)
# register admin user
data = {
'username': 'admin',
'password': 'secret',
'password_confirmation': 'secret'
}
response, content = _req('POST', '/register', urlencode(data),
headers={ 'Content-Type': 'application/x-www-form-urlencoded' })
bag = Bag('alpha')
bag.policy = Policy(read=['admin'], write=['admin'], create=['admin'],
delete=['admin'], manage=['admin'])
STORE.put(bag)
bag = Bag('bravo')
STORE.put(bag)
bag = Bag('charlie')
bag.policy = Policy(read=['nobody'], write=['nobody'], create=['nobody'],
delete=['nobody'], manage=['nobody'])
STORE.put(bag)
tiddler = Tiddler('index', 'bravo')
tiddler.text = 'lorem ipsum\ndolor *sit* amet'
tiddler.type = 'text/x-markdown'
STORE.put(tiddler)
示例4: test_get_revision
def test_get_revision():
"""
Test we are able to retrieve a particular revision.
"""
bagone = Bag('bagone')
bagone.add_tiddlers(tiddlers)
store.put(bagone)
store.put(bagone)
tiddler = Tiddler('RevisionTiddler')
tiddler.text='how now 1'
tiddler.bag = 'bagone'
store.put(tiddler)
tiddler.text = 'how now 2'
store.put(tiddler)
tiddler.text = 'how now 3'
store.put(tiddler)
tiddler = Tiddler(title='RevisionTiddler', bag='bagone')
tiddler = store.get(tiddler)
assert tiddler.text == 'how now 3'
assert tiddler.revision == 3
tiddler = Tiddler(title='RevisionTiddler', bag='bagone')
tiddler.revision = 2
tiddler = store.get(tiddler)
assert tiddler.text == 'how now 2'
assert tiddler.revision == 2
revisions = store.list_tiddler_revisions(tiddler)
assert len(revisions) == 3
assert revisions[0] == 3
示例5: remove_user
def remove_user(environ, space_name, user_name):
"""
remove user_name from space_name while
checking the policy allows the logged
in user to do it
"""
store = environ['tiddlyweb.store']
user = User(user_name)
user = store.get(user)
logged_in_user = environ['tiddlyweb.usersign']
space_definition = environ['tiddlyweb.config']['space']
space = []
for name, values in space_definition['bags'].iteritems():
bag = Bag(name.replace('SPACE_NAME', space_name))
bag = store.get(bag)
bag.policy.allows(logged_in_user, 'manage')
bag.policy = remove_from_policy(user, bag.policy)
space.append(bag)
for name, values in space_definition['recipes'].iteritems():
recipe = Recipe(name.replace('SPACE_NAME', space_name))
recipe = store.get(recipe)
recipe.policy.allows(logged_in_user, 'manage')
recipe.policy = remove_from_policy(user, recipe.policy)
space.append(recipe)
for thing in space:
store.put(thing)
示例6: create_wiki
def create_wiki(environ, name, mode='private', username=None, desc='',
validate=True):
"""
Create a wiki with the name, name.
For now a wiki is just a bag a policy.
"""
store = environ['tiddlyweb.store']
if username is None:
username = environ['tiddlyweb.usersign']['name']
bag = Bag(name)
# We want this get to fail.
try:
store.get(bag)
return False
except NoBagError:
pass
try:
bag.policy = WIKI_MODES[mode](username)
except KeyError:
bag.policy = WIKI_MODES['private'](username)
bag.desc = desc
if validate:
validate_bag(bag, environ)
store.put(bag)
return bag
示例7: test_validate_bag_desc
def test_validate_bag_desc():
bag = Bag('barney')
bag.desc = '<script>alert("foo");</script>'
validate_bag(bag)
assert bag.desc == '<script>alert("foo");</script>'
示例8: test_post_no_title
def test_post_no_title():
"""
post a tiddler with no title set
and make sure it gets into the store
"""
store = setup_store()
setup_web()
http = httplib2.Http()
#post some fields to a tiddler
response = http.request('http://test_domain:8001/bags/foo/tiddlers',
method='POST',
headers={'Content-type': 'application/x-www-form-urlencoded'},
body='text=Hi%20There')[0]
assert response.status == 204
#now find the tiddler just entered and check it
bag = Bag('foo')
bag = store.get(bag)
tiddlers = bag.list_tiddlers()
assert len(tiddlers) == 1
tiddler = store.get(tiddlers[0])
assert tiddler.title != ''
assert tiddler.text == 'Hi There'
示例9: tiddler_as
def tiddler_as(self, tiddler):
"""
entry point for a single tiddler. Sets some variables
and passes the tiddler into list_tiddlers for turning
into HTML
"""
if 'tiddler' not in self.environ['tiddlyweb.recipe_template']:
self.environ['tiddlyweb.recipe_template']['tiddler'] = tiddler.title
if 'bag' not in self.environ['tiddlyweb.recipe_template']:
self.environ['tiddlyweb.recipe_template']['bag'] = tiddler.bag
if tiddler.recipe and 'recipe' not in self.environ['tiddlyweb.recipe_template']:
self.environ['tiddlyweb.recipe_template']['recipe'] = tiddler.recipe
bag = Bag('tmpbag',tmpbag=True)
bag.add_tiddler(tiddler)
self.plugin_name = self.set_plugin_name('single_tiddler')
if self.plugin_name not in self.environ['tiddlyweb.config']['tw_pages_serializers']:
content = self.pass_through_external_serializer(self.plugin_name, tiddler)
return content
self.page_title = self.environ['tiddlyweb.config']['tw_pages_serializers'][self.plugin_name]['title'] or tiddler.title
return self.list_tiddlers(bag)
示例10: _bag_list
def _bag_list(self, tiddlers):
if '/feedbag' in self.environ['selector.matches'][0]:
representation_link = '%s/feedbag' % (self._server_prefix())
bag = Bag('feedbag')
bag.policy.manage = ["NONE"]
bag.policy.delete = ["NONE"]
bag.desc = 'Recent Public Stuff'
else:
name = self.environ['wsgiorg.routing_args'][1]['bag_name']
name = urllib.unquote(name)
name = name.decode('utf-8')
representation_link = '%s/bags/%s/tiddlers' % (
self._server_prefix(), encode_name(name))
bag = self.environ['tiddlyweb.store'].get(Bag(name))
representations = self._tiddler_list_header(representation_link)
user_object = get_user_object(self.environ)
publicity = ''
try:
bag.policy.allows(user_object, 'manage')
policy = bag.policy
publicity = determine_publicity(user_object, policy)
except (UserRequiredError, ForbiddenError):
policy = None
try:
bag.policy.allows(user_object, 'delete')
delete = True
except (UserRequiredError, ForbiddenError):
delete = False
data = {'title': 'TiddlyHoster Bag %s' % bag.name, 'policy': policy,
'publicity': publicity, 'delete': delete,
'bag': bag, 'tiddlers': tiddlers, 'representations': representations}
del self.environ['tiddlyweb.title']
return send_template(self.environ, 'baglist.html', data)
示例11: tiddler_as
def tiddler_as(self, tiddler):
logging.debug("###################################\nDOING TIDDLER_AS\n\n\n###################################")
"""
Take the single tiddler provided and inject it into
a TiddlyWiki.
"""
environ = self.environ
store = self.environ["tiddlyweb.store"]
try:
recipe_name =tiddler.recipe
tiddler_as_name = tiddler.title
resource = Recipe(recipe_name)
resource= store.get(resource)
tiddlers = control.get_tiddlers_from_recipe(resource)
bag =Bag("tmp",tmpbag=True)
logging.debug("have tiddlers %s"%tiddlers)
for a_tiddler in tiddlers:
a_tiddler.recipe = recipe_name
a_tiddler = store.get(a_tiddler)
if a_tiddler.title == "DefaultTiddlers":
a_tiddler.text = "[[%s]]"%tiddler_as_name
logging.debug("tiddler_as overriding DefaultTiddlers")
bag.add_tiddler(a_tiddler)
except AttributeError:
resource = Bag(tiddler.bag)
bag = store.get(resource)
self._prepare_twp(bag)
return self.build_non_js_version(bag,default=[tiddler])
示例12: test_post_fields
def test_post_fields():
"""
post some fields to a tiddler
"""
store = setup_store()
setup_web()
http = httplib2.Http()
#make sure there is nothing in bag 'foo'
bag = Bag('foo')
bag = store.get(bag)
assert len(bag.list_tiddlers()) == 0
response = http.request('http://test_domain:8001/bags/foo/tiddlers',
method='POST',
headers={'Content-type': 'application/x-www-form-urlencoded'},
body='title=HelloWorld&field1=foo&field2=bar')[0]
assert response.status == 204
#now find the tiddler just entered
tiddler = Tiddler('HelloWorld', 'foo')
try:
store.get(tiddler)
except NoTiddlerError:
raise AssertionError('tiddler was not put into store')
#and check the fields
assert tiddler.title == 'HelloWorld'
assert len(tiddler.fields) == 2
assert tiddler.fields['field1'] == 'foo'
assert tiddler.fields['field2'] == 'bar'
示例13: test_filter_bag_by_filter
def test_filter_bag_by_filter():
"""
Confirm a bag will properly filter.
"""
bagfour = Bag('bagfour')
store.put(bagfour)
bagfour.store = store
for tiddler in tiddlers:
tiddler.bag = 'bagfour'
store.put(tiddler)
filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour,
'select=title:TiddlerOne', environ=environ))
assert len(filtered_tiddlers) == 1
assert filtered_tiddlers[0].title == 'TiddlerOne'
filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour,
'select=tag:tagone', environ=environ))
assert len(filtered_tiddlers) == 2
filters, thing = parse_for_filters(
'select=tag:tagone;select=title:TiddlerThree', environ=environ)
filtered_tiddlers = list(control._filter_tiddlers_from_bag(bagfour,
filters, environ=environ))
assert len(filtered_tiddlers) == 1
assert filtered_tiddlers[0].title == 'TiddlerThree'
示例14: _ensure_bags
def _ensure_bags(self, environ, username):
# for privacy in ["public", "protected", "private"]:
for privacy in ["private", "protected"]:
only_user = ["admin", username]
if privacy=="public":
readPerms = []
writePerms = []
elif privacy=="protected":
readPerms = []
writePerms = only_user
elif privacy=="private":
readPerms = only_user
writePerms = only_user
bag = Bag(privacy+"-"+username)
store = environ['tiddlyweb.store']
try:
store.get(bag)
return
except NoBagError:
bag.desc = 'tiddlyguv private user bag'
bag.policy.owner = username
bag.policy.manage = ["admin", username]
bag.policy.read = readPerms
bag.policy.write = writePerms
bag.policy.create = ["admin", username]
bag.policy.delete = ["admin", username]
store.put(bag)
示例15: _process_choices
def _process_choices(environ, start_response, form):
store = environ['tiddlyweb.store']
user = environ['tiddlyweb.usersign']
tmp_bag = form['tmp_bag'][0]
bag = form['target_bag'][0]
if bag:
bag = Bag(bag)
try:
bag.skinny = True
bag = store.get(bag)
except NoBagError:
return _send_wimport(environ, start_response,
'chosen bag does not exist')
else:
bag = form['new_bag'][0]
bag = _make_bag(environ, bag)
try:
bag.policy.allows(user, 'write')
except (ForbiddenError, UserRequiredError):
return _send_wimport(environ, start_response,
'you may not write to that bag')
tiddler_titles = form['tiddler']
for title in tiddler_titles:
tiddler = Tiddler(title.decode('utf-8', 'ignore'), tmp_bag)
tiddler = store.get(tiddler)
tiddler.bag = bag.name
store.put(tiddler)
tmp_bag = Bag(tmp_bag)
store.delete(tmp_bag)
bagurl = bag_url(environ, bag) + '/tiddlers'
raise HTTP302(bagurl)