本文整理汇总了Python中mongoalchemy.session.Session类的典型用法代码示例。如果您正苦于以下问题:Python Session类的具体用法?Python Session怎么用?Python Session使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Session类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cache_miss
def test_cache_miss():
s_nocache = Session.connect("unit-testing", cache_size=10)
t = TExtra(i=4)
s_nocache.insert(t)
s = Session.connect("unit-testing", cache_size=10)
s.add_to_session(t)
t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id).one()
示例2: test_dereference_doc
def test_dereference_doc():
class A(Document):
x = IntField()
s = Session.connect('unit-testing', cache_size=0)
s.clear_collection(A)
a = A(x=5)
s.insert(a)
dbaref = DBRef(collection='A', id=a.mongo_id, database='unit-testing')
s2 = Session.connect('unit-testing2', cache_size=0)
assert s2.dereference(a).x == 5
示例3: test_dereference
def test_dereference():
class A(Document):
x = IntField()
s = Session.connect("unit-testing", cache_size=0)
s.clear_collection(A)
a = A(x=5)
s.insert(a)
dbaref = DBRef(collection="A", id=a.mongo_id, database="unit-testing")
s2 = Session.connect("unit-testing2", cache_size=0)
assert s2.dereference(dbaref).x == 5
示例4: test_update_ignore_extras
def test_update_ignore_extras():
s = Session.connect('unit-testing')
s.clear_collection(TExtra)
# Create Object
t = TExtra(i=1, j='test', k='test2')
s.save(t)
# Retrieve Object
t = s.query(TExtra).one()
assert t.i == 1
assert t.get_extra_fields()['j'] == 'test'
assert t.get_extra_fields()['k'] == 'test2'
# Update Object
t.i = 5
del t.get_extra_fields()['j'] # delete an extra field
t.get_extra_fields()['k'] = 'changed' # change an extra field
t.get_extra_fields()['l'] = 'added' # new extra field
s.update(t)
# Retrieve Object
t_new = s.query(TExtra).one()
assert 'j' not in t_new.get_extra_fields()
assert t_new.get_extra_fields()['k'] == 'changed'
assert t_new.get_extra_fields()['l'] == 'added'
assert t_new.i == 5
示例5: login
def login(request):
basept = get_renderer('templates/base.pt').implementation()
login_url = resource_url(request.context, request, 'login')
referrer = request.url
if referrer == login_url:
referrer = request.application_url
came_from = request.params.get('came_from', referrer)
message = ''
login = ''
password = ''
if 'form.submitted' in request.params:
login = request.params['login']
password = request.params['password']
with Session.connect(request.registry.settings['db_name'] ) as s:
try:
user = s.query(User).filter_by(email=login).one()
except:
user = None
if user is not None and user.password == password:
headers = remember(request, login)
return HTTPFound(location = came_from,
headers = headers)
message = 'Failed login'
return dict(
message = message,
url = request.application_url + '/login',
came_from = came_from,
login = login,
password = password,
base_pt = basept
)
示例6: test_cache_max
def test_cache_max():
# not a great test, but gets coverage
s = Session.connect("unit-testing", cache_size=3)
for i in range(0, 10):
t = TExtra(i=4)
s.insert(t)
assert len(s.cache) == 3
示例7: test_update_ignore_extras
def test_update_ignore_extras():
s = Session.connect("unit-testing")
s.clear_collection(TExtra)
# Create Object
t = TExtra(i=1, j="test", k="test2")
s.insert(t)
# Retrieve Object
t = s.query(TExtra).one()
assert t.i == 1
assert t.get_extra_fields()["j"] == "test"
assert t.get_extra_fields()["k"] == "test2"
# Update Object
t.i = 5
del t.get_extra_fields()["j"] # delete an extra field
t.get_extra_fields()["k"] = "changed" # change an extra field
t.get_extra_fields()["l"] = "added" # new extra field
s.update(t)
# Retrieve Object
t_new = s.query(TExtra).one()
assert "j" not in t_new.get_extra_fields()
assert t_new.get_extra_fields()["k"] == "changed"
assert t_new.get_extra_fields()["l"] == "added"
assert t_new.i == 5
示例8: test_regex
def test_regex():
class Spell(Document):
name = StringField()
s = Session.connect('unit-testing')
s.clear_collection(Spell)
s.save(Spell(name='Wingardium Leviosa'))
s.save(Spell(name='abracadabra'))
s.save(Spell(name='ab.*ra.ca.da.*bra'))
s.save(Spell(name='Alacazam'))
# check ignore case True
q = s.query(Spell).filter(Spell.name.startswith('wingardium', ignore_case=True))
assert q.first().name == 'Wingardium Leviosa'
# check ignore case False (default)
xs = s.query(Spell).filter(Spell.name.startswith('wingardium')).all()
assert len(xs) == 0
# check regex-free startswith and endswith
assert len(s.query(Spell).filter(Spell.name.startswith('ab.*ra.ca')).all()) == 1
assert len(s.query(Spell).filter(Spell.name.endswith('da.*bra')).all()) == 1
# check regex
assert len(s.query(Spell).filter(Spell.name.regex(r'^[Aa]\w*[am]$')).all()) == 2
# check regex with options
assert len(s.query(Spell).filter(Spell.name.regex(r'^[a]\w*[am]$', options='i')).all()) == 2
示例9: admin
def admin(request):
message = ""
if "form.submitted" in request.params:
login = request.params["login"]
password = request.params["password"]
max_keys = request.params["max_keys"]
try:
max_keys = int(max_keys)
except:
max_keys = -1
with Session.connect(request.registry.settings["db_name"]) as s:
try:
user = s.query(User).filter_by(email=login).one()
except:
user = User(
email=login,
password=password,
max_keys=max_keys,
groups=["admin"] if request.params.get("admin") else [],
scritti=[],
bloccati=[],
)
s.insert(user)
message = "user created"
return dict(message=message)
示例10: test_update_list
def test_update_list():
s = Session.connect("unit-testing")
s.clear_collection(TIntListDoc)
tIntList = TIntListDoc(intlist=[1, 2])
s.insert(tIntList)
# pull out of db
tFetched = s.query(TIntListDoc).one()
assert sorted([1, 2]) == sorted(tFetched.intlist)
# append to list, update
l = tFetched.intlist
l.append(3)
s.update(tFetched)
# pull out of db
tFetched = s.query(TIntListDoc).one()
assert sorted([1, 2, 3]) == sorted(tFetched.intlist)
tFetched.intlist.remove(1)
s.update(tFetched)
tFetched = s.query(TIntListDoc).one()
assert sorted([2, 3]) == sorted(tFetched.intlist)
示例11: test_tz_aware
def test_tz_aware():
import pytz
from mongoalchemy.session import Session
from mongoalchemy.document import Document
# doc
class DocTZ(Document):
time = DateTimeField(use_tz=True)
class DocNoTZ(Document):
time = DateTimeField(use_tz=False)
# timezone -- choose one where the author doesn't live
eastern = pytz.timezone('Australia/Melbourne')
utc = pytz.utc
# session
s = Session.connect('unit-testing', timezone=eastern)
s.clear_collection(DocTZ)
s.clear_collection(DocNoTZ)
# time
local = eastern.localize(datetime.now())
local = local.replace(microsecond=0)
doc = DocTZ(time=local)
s.insert(doc)
doc = s.query(DocTZ).one()
assert doc.time == local, (doc.time, local)
# do the no timezone case for code coverage
s.insert(DocNoTZ(time=datetime(2012, 1, 1)))
obj = s.query(DocNoTZ).one()
示例12: test_geo
def test_geo():
class Place(Document):
config_collection_name = 'places4'
loc = GeoField()
val = IntField()
index = Index().geo2d('loc', min=-100, max=100)
s = Session.connect('unit-testing')
s.clear_collection(Place)
s.insert(Place(loc=(1,1), val=2))
s.insert(Place(loc=(5,5), val=4))
s.insert(Place(loc=(30,30 ), val=5))
x = s.query(Place).filter(Place.loc.near(0, 1))
assert x.first().val == 2, x.query
xs = s.query(Place).filter(Place.loc.near(1, 1, max_distance=2)).all()
assert len(xs) == 1, xs
xs = s.query(Place).filter(Place.loc.near_sphere(1, 1, max_distance=50)).all()
assert len(xs) == 3
q = s.query(Place).filter(Place.loc.within_box([-2, -2], [2, 2]))
assert len(q.all()) == 1, q.query
q = s.query(Place).filter(Place.loc.within_radius(0, 0, 2))
assert len(q.all()) == 1, q.query
q = s.query(Place).filter(Place.loc.within_polygon(
[[-2, 0], [2, 0], [0, 2], [0, -2]]
))
assert len(q.all()) == 1, q.query
q = s.query(Place).filter(Place.loc.within_radius_sphere(30, 30, 0.0001))
assert len(q.all()) == 1, q.all()
示例13: test_bad_dereference
def test_bad_dereference():
class A(Document):
x = IntField()
s = Session.connect("unit-testing", cache_size=0)
s.clear_collection(A)
dbaref = DBRef(collection="A", id=ObjectId(), database="unit-testing")
s.dereference(dbaref)
示例14: middleware
async def middleware(request):
if request.path.startswith('/api/'):
request.db_session = Session.connect(
config.get("mongo_database_name")
)
response = await handler(request)
return response
示例15: test_cache
def test_cache():
s = Session.connect("unit-testing", cache_size=10)
t = TExtra(i=4)
s.insert(t)
s.insert(t)
t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id).one()
assert id(t) == id(t2)
assert id(s.refresh(t)) != t2