本文整理汇总了Python中session.Session.query方法的典型用法代码示例。如果您正苦于以下问题:Python Session.query方法的具体用法?Python Session.query怎么用?Python Session.query使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类session.Session
的用法示例。
在下文中一共展示了Session.query方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RegistrationTest
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
class RegistrationTest(unittest.TestCase):
def setUp(self):
self.session = Session()
self.session.query(User).delete()
self.email = '[email protected]'
self.controller = RegistrationController(self.session)
self.token = self.controller.register(self.email, 'qwerty')
def test_registration(self):
self.assertEqual(self.session.query(User).filter(and_(User.email == self.email, User.confirmed_at == None)).count(), 1)
def test_confirmation(self):
self.controller.confirm(self.email, self.token)
self.assertEqual(self.session.query(User).filter(and_(User.email == self.email, User.confirmed_at != None)).count(), 1)
示例2: get_responses
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get_responses(self, id_=1, less_than=None, more_than=None):
session = Session()
bbs = session.query(BBS).filter(BBS.id == id_).one()
responses = session.query(Response).filter(Response.bbs_id == id_)
if less_than:
responses = responses.filter(Response.id < less_than)
if more_than:
responses = responses.filter(Response.id > more_than)
responses = responses.order_by(Response.id.desc()).limit(30).all()
responses = [res.toDict() for res in responses]
session.close()
return responses
示例3: get
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get(self):
cookie = self.request.get('cookie')
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
if not cookie:
error = json_error('vote', 'failure', 'cookie')
self.response.write(json.dumps(error))
else:
id = self.request.get('id')
if not id:
error = json_error('vote', 'failure', 'no id')
self.response.write(json.dumps(error))
else:
session = Session.query(Session.cookie == cookie).get()
if not session:
error = json_error('vote', 'failure', 'session')
self.response.write(error)
else:
key = ndb.Key(urlsafe=id)
submission = key.get()
if not submission:
error = json_error('vote', 'failure', 'no submission')
self.response.write(error)
else:
value = self.request.get('value')
if not value:
error = json_error('vote', 'failure', 'value')
self.response.write(error)
else:
value = int(value)
if value < -1 or value > 1:
error = json_error('vote', 'failure', 'value')
self.response.write(error)
else:
last_vote = Vote.query(ndb.AND(Vote.user == session.user, Vote.sub_id == id)).get()
if not last_vote:
vote_val = 0
else:
vote_val = last_vote.value
last_vote.key.delete()
vote_val = value - vote_val;
old_score = submission.rating
new_score = old_score + vote_val
submission.rating = new_score;
submission.put()
vote = Vote(sub_id = id, value = value, user = session.user)
vote.put()
response = vote_result('ok')
self.response.write(response)
示例4: AuthorizationTest
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
class AuthorizationTest(unittest.TestCase):
def setUp(self):
self.session = Session()
self.session.query(User).delete()
self.email = '[email protected]'
reg_controller = RegistrationController(self.session)
self.password = 'qwerty'
token = reg_controller.register(self.email, self.password)
reg_controller.confirm(self.email, token)
self.controller = AuthenticationController(self.session)
def test_login_attempts(self):
self.controller.authenticate(self.email, self.password)
user = self.session.query(User).first()
with self.assertRaises(self.controller.UserBlocked):
for _ in xrange(User.LOGIN_ATTEMPTS + 1):
self.controller.authenticate(self.email, '')
user = self.session.query(User).first()
self.assertGreater(user.blocked_to, datetime.datetime.now())
def test_authentication(self):
self.controller.authenticate(self.email, self.password)
user = self.session.query(User).first()
self.assert_(self.controller.authenticate(self.email, self.password))
示例5: get
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get(self):
cookie = self.request.get('cookie')
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
if not cookie:
error = json_error('failure', 'cookie')
self.response.write(json.dumps(error))
else:
session = Session.query(Session.cookie == cookie).get()
if not session:
error = json_error('failure', 'session')
self.response.write(error)
else:
name = self.request.get('name')
if not name:
error = json_error('failure', 'name')
self.response.write(json.dumps(error))
else:
submissions_number = 40
submissions = Submission.query(Submission.lowerName == name.lower()).fetch(submissions_number)
if not submissions:
error = json_error('failure', 'no result')
self.response.write(json.dumps(error))
else:
submissions_array = []
if(submissions_number <= len(submissions)):
for i in range(0, submissions_number):
submission = submissions[i]
json_submission = json_string(submission.key.urlsafe(), submission.name, submission.image, submission.rating)
submissions_array.append(json_submission)
else:
for i in range(0, len(submissions)):
submission = submissions[i]
json_submission = json_string(submission.key.urlsafe(), submission.name, submission.image, submission.rating)
submissions_array.append(json_submission)
response = json.dumps(submissions_array)
self.response.write(response)
示例6: get_by_name
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get_by_name(class_, name):
"""
Queries the database for one or more funders matching the given name.
If onlye on funder with a matching name exists, only that funder is
returned. If multiple funders with the given name exist, an array of
those funders is returned.
Returns none if no match is found.
"""
query = Session.query(class_).filter_by(name=name)
try:
return query.one()
except MultipleResultsFound:
return query.all()
except NoResultFound:
return None
示例7: get
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get(self):
self.response.headers["Content-Type"] = "application/json; charset=utf-8"
cookie = self.request.get("cookie")
if not cookie:
error = json_error("delete session", "failure", "cookie")
self.response.write(json.dumps(error))
else:
session = Session.query(Session.cookie == cookie).get()
if not session:
error = json_error("delete session", "failure", "no such session")
self.response.write(json.dumps(error))
else:
session.key.delete()
response = {"delete session": {"status": "ok"}}
self.response.write(json.dumps(response))
示例8: post
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def post(self):
subName = self.request.get('name')
subCategory = self.request.get('category')
subDescription = self.request.get('description')
subLocation = self.request.get('location')
subImage = str(self.request.get('image'))
subKeywords = self.request.get('keywords')
cookie = self.request.get('cookie')
subTfrom = self.request.get('from')
subTto = self.request.get('to')
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
# Name, Category, Location, Image and Cookie are the required fields.
if not cookie:
self.response.write(json_response(5, 0))
elif not subName:
self.response.write(json_response(1, 0))
elif not subCategory:
self.response.write(json_response(2, 0))
elif not subLocation:
self.response.write(json_response(3, 0))
elif not subImage:
self.response.write(json_response(4, 0))
else:
session = Session.query(Session.cookie == cookie).get()
if session:
submission = Submission(name = subName,lowerName = subName.lower(), category = subCategory, description = subDescription, location = subLocation, image = subImage,
keywords = subKeywords, rating = 0, submitter = session.user)
submission_key = submission.put()
# ndb.Key.urlsafe(), generates a url safe version of the Key
self.response.write(json_response(0, submission.key.urlsafe()))
else:
self.response.write(json_response(6, 0))
示例9: getProfileByUser
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def getProfileByUser(self, user):
session = Session()
days = session.query(Days).filter(Days.username == user).first()
return days
示例10: get
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get(class_, id):
"""
Queries the database and returns a class object with a matching id.
"""
return Session.query(class_).get(id)
示例11: get
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def get(self):
cookie = self.request.get('cookie')
self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
if not cookie:
error = json_error('retrieve', 'failure', 'cookie')
self.response.write(json.dumps(error))
else:
flag = self.request.get('flag')
if not flag:
error = json_error('retrieve', 'failure', 'no flag')
self.response.write(json.dumps(error))
else:
session = Session.query(Session.cookie == cookie).get()
if not session:
error = json_error('retrieve', 'failure', 'session')
self.response.write(error)
else:
# flag = 1 means a single request
if flag == '1':
try:
id = self.request.get('id');
key = ndb.Key(urlsafe=id)
if not id:
self.response.write(json.dumps(json_error('single request', 'failure', 'id')))
else:
submission = key.get()
if not submission:
error = json_error('single request', 'failure', 'no corresponding submission')
self.response.write(json.dumps(error))
else:
last_vote = Vote.query(ndb.AND(Vote.user == session.user, Vote.sub_id == submission.key.urlsafe())).get()
if not last_vote:
vote_val = 0
else:
vote_val = last_vote.value
string_submission = json_string(submission.key.urlsafe(), submission.name, submission.category, submission.description, submission.location,
submission.image, submission.keywords, submission.submitter,
submission.tfrom, submission.tto, submission.rating, vote_val)
response = json.dumps(string_submission)
self.response.write(response)
except:
self.response.write(json.dumps(json_error('single request', 'failure', 'id')))
# flag = 2 means that we are requesting submissions to display in what's new
elif flag == '2':
time_range = 1447786800
date = datetime.datetime.fromtimestamp(time_range/1e3)
submissions_number = 20
submissions = Submission.query(Submission.submitted >= date).fetch(submissions_number)
if not submissions:
error = json_error('what is new', 'failure', 'nothing in range')
self.response.write(json.dumps(error))
else:
submissions_array = []
if(submissions_number <= len(submissions)):
for i in range(0, submissions_number):
submission = submissions[i]
# what's new used for around you too. Server return location so that the app
# can use it to display submissions near user on the map
json_submission = json_array_with_location(submission.key.urlsafe(), submission.name,submission.image, submission.rating, submission.location)
submissions_array.append(json_submission)
else:
for i in range(0, len(submissions)):
submission = submissions[i]
json_submission = json_array_with_location(submission.key.urlsafe(), submission.name, submission.image, submission.rating, submission.location)
submissions_array.append(json_submission)
response = json.dumps(submissions_array)
self.response.write(response)
# flag = 4 means that we are requesting submissions for a specific category
elif flag == '4':
category = self.request.get('category')
submissions_number = 20
if not category:
error = json_error('retrieve category', 'failure', 'no category')
self.response.write(json.dumps(error))
else:
submissions = Submission.query(Submission.category == category).fetch(submissions_number)
#.........这里部分代码省略.........
示例12: getUserByName
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
def getUserByName(self, user):
session = Session()
fullUser = session.query(User).filter(User.username == user).first()
return fullUser
示例13: Session
# 需要导入模块: from session import Session [as 别名]
# 或者: from session.Session import query [as 别名]
import cfg
import re
from tasks import *
from models import BarcodeConversion, Manufacturer, ProductBarcode, Product, SupplierCatalogItem
from session import Session
session = Session(autocommit=True)
file_name = '/home/jdsteele/Desktop/bowser-inventory-20120130.txt'
#cache barcode_conversions
query = session.query(BarcodeConversion)
barcode_conversions = (query)
manufacturers = dict()
query = session.query(Manufacturer)
for manufacturer in query:
manufacturers[manufacturer.id] = manufacturer
task = BarcodeConversionTask()
with open(file_name) as f:
for line in f:
line = line.rstrip()
print line
obj = task.get(line)