本文整理汇总了Python中models.User.by_id方法的典型用法代码示例。如果您正苦于以下问题:Python User.by_id方法的具体用法?Python User.by_id怎么用?Python User.by_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.User
的用法示例。
在下文中一共展示了User.by_id方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def post(self):
"""
Post method to like and unlike blogs
"""
if self.user:
if self.request.get("like_blog"):
_blog_id = self.request.get("like_blog")
user = self.user
user_id = user.key.id()
_user = User.by_id(int(user_id))
_blog = Blog.get_by_id(int(_blog_id))
like = Like(user=user.key, blog=_blog.key)
like.like = 1
like_key = like.put()
blog = _blog.key.get()
blog.likes.append(like_key)
blog.put()
self.redirect('/')
if self.request.get("dislike"):
_blog_id = self.request.get("dislike")
user = self.user
user_id = user.key.id()
_user = User.by_id(int(user_id))
_blog = Blog.get_by_id(int(_blog_id))
like = Like.query(Like.user == user.key, Like.blog == _blog.key).get()
like_key = like.key
blog = _blog.key.get()
blog.likes.remove(like_key)
blog.put()
like_key.delete()
self.redirect('/')
else:
self.redirect('/login')
示例2: post_avatar
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def post_avatar(self, *args, **kwargs):
'''
Saves avatar - Reads file header an only allows approved formats
'''
user = User.by_id(self.session['user_id'])
if 'avatar' in self.request.files:
if len(self.request.files['avatar'][0]['body']) < (1024 * 1024):
if user.avatar == "default_avatar.jpeg":
user.avatar = unicode(uuid4()) + u".jpeg"
ext = imghdr.what(
"", h=self.request.files['avatar'][0]['body']
)
avatar_path = str(self.application.settings['avatar_dir'] + '/' + user.avatar)
if ext in ['png', 'jpeg', 'gif', 'bmp']:
if os.path.exists(avatar_path):
os.unlink(avatar_path)
user.avatar = unicode(user.avatar[:user.avatar.rfind('.')] + "." + ext)
file_path = str(self.application.settings['avatar_dir'] + '/' + user.avatar)
avatar = open(file_path, 'wb')
avatar.write(self.request.files['avatar'][0]['body'])
avatar.close()
dbsession.add(user)
dbsession.flush()
self.render_page(success=["Successfully changed avatar"])
else:
self.render_page(
errors=["Invalid image format, avatar must be: .png .jpeg .gif or .bmp"]
)
else:
self.render_page(errors=["The image is too large"])
else:
self.render_page(errors=["Please provide an image"])
示例3: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user-id')
self.user = uid and User.by_id(int(uid))
if self.request.url.endswith('.json'):
self.format = 'json'
else:
self.format = 'html'
示例4: render
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def render(self, *args, **kwargs):
""" Renders the top menu """
if self.handler.session is not None:
user = User.by_id(self.handler.session["user_id"])
if self.handler.session["menu"] == "user":
return self.render_string("menu/user.html", user=user)
elif self.handler.session["menu"] == "admin":
return self.render_string("menu/admin.html", handle=user.handle)
return self.render_string("menu/public.html")
示例5: do_id
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def do_id(self, user_id):
'''
Pull user based on id.
Usage: id <user_id>
'''
user = User.by_id(user_id)
if user is None:
print(WARN + str("%s user not found in database." % username))
else:
print INFO + repr(user)
示例6: do_id
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def do_id(self, user_id):
"""
Pull user based on id.
Usage: id <user_id>
"""
user = User.by_id(user_id)
if user is None:
print (WARN + "'%s' user not found in database." % username)
else:
print INFO + repr(user)
示例7: __crack__
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def __crack__(self, job, weapon_system):
'''
Does the actual password cracking, before calling this function you should
ensure the weapon system is online and not busy
'''
results = None
user = User.by_id(job.user_id)
if user is None:
logging.error("Invalid job passed to dispatcher (no user with id %d)." % (
job.user_id,
))
elif job == None:
logging.error("Invalid job passed to dispatcher (job is None).")
else:
job.started = datetime.now()
algorithm = Algorithm.by_id(job.algorithm_id)
try:
ssh_keyfile = NamedTemporaryFile()
ssh_keyfile.write(weapon_system.ssh_key)
ssh_keyfile.seek(0)
ssh_context = SshContext(
weapon_system.ip_address,
user=weapon_system.ssh_user,
keyfile=ssh_keyfile.name,
)
rpc_connection = rpyc.ssh_connect(
ssh_context,
weapon_system.service_port,
)
hashes = job.to_list()
logging.info("Sending %s job to %s for cracking." % (
job.job_name,
weapon_system.weapon_system_name,
))
job.status = u"IN_PROGRESS"
dbsession.add(job)
dbsession.flush()
results = rpc_connection.root.exposed_crack_list(
job.id,
job.to_list(),
algorithm.algorithm_name,
)
except:
logging.exception("Connection to remote weapon system failed, check parameters.")
finally:
ssh_keyfile.close()
if results is not None:
job.save_results(results)
else:
logging.warn("No results returned from weapon system.")
job.status = u"COMPLETED"
job.finished = datetime.now()
dbsession.add(job)
dbsession.flush()
self.__next__()
示例8: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
"""
Initialize method returns user
:param a:
:type a:
:param kw:
:type kw:
"""
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
self.user = uid and User.by_id(int(uid))
示例9: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
if not uid:
self.user = None
else :
self.user = User.by_id(int(uid))
#------------for .json
if self.request.url.endswith('.json'):
self.format='json'
else:
self.format='html'
示例10: get
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def get(self):
"""
Get method to render blog.html
"""
if self.user:
user_key = self.user
user_id = user_key.key.id()
_user = User.by_id(int(user_id))
_blogs = Blog.query_blogs().fetch()
likes = Like.query().fetch()
self.render('blog.html', blogs=_blogs, user=_user, likes=likes)
else:
_blogs = Blog.query_blogs().fetch()
self.render('blog.html', blogs=_blogs, user=None, likes=None)
示例11: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
"""
Runs on every request.
Checks that user is logged in.
"""
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('userId')
print('INITIALIZE!')
print(uid)
self.user = uid and User.by_id(int(uid))
if self.request.url.endswith('.json'):
self.format = 'json'
else:
self.format = 'html'
示例12: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
"""
Checks if user id and username is valid
As well as format to JSON for API
"""
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
self.user = uid and User.by_id(int(uid))
if self.request.url.endswith(".json"):
self.format = "json"
else:
self.format = "html"
def notfound(self):
self.error(404)
self.write('<h1>404: Oopsy</h1> Sorry, my friend page does not exist')
示例13: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
'''
By default, each instance of the Handler class will have the attributes
request, response and app (that's why one can use e.g. self.request.get();
self.request is a request object) because it inherits these attributes from
webapp2.RequestHandler. Whenever the handler is called (i.e. when you receive
a request for that URL), a new instance is created and initialized with the
appropriate values for the attributes.
The initialize() method is called every time a new instance of NewPost is
created (which happens on each http request to the web server) and as you can
see it retrieves the user_id from the cookie (if present), retrives the
corresponding User from the db and stores it instance variable user (self.user)
so it can be easily accessed by the rest of the code.
'''
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
'''
If uid has any value that evaluates to True, call the method
User.by_id(int(uid)) and assign the return value (which will be a User object)
to self.user. If uid has any value that evaluates to False, assign that value
to self.user. In that case, it won't even try to call User.by_id(int(uid)).
'''
self.user = uid and User.by_id(int(uid)) # assign a User object to self.user
示例14: __crack__
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def __crack__(self, job, weapon_system):
'''
Does the actual password cracking, before calling this function you should
ensure the weapon system is online and not busy
'''
user = User.by_id(job.user_id)
if user == None or job == None:
logging.error("Invalid job passed to dispatcher.")
else:
job.status = u"IN_PROGRESS"
dbsession.add(job)
dbsession.flush()
algo = job.hashes[0].algorithm
job.started = datetime.now()
try:
ssh_keyfile = NamedTemporaryFile()
ssh_keyfile.write(weapon_system.ssh_key)
ssh_keyfile.seek(0)
ssh_context = SshContext(weapon_system.ip_address,
user=weapon_system.ssh_user, keyfile=ssh_keyfile.name)
rpc_connection = rpyc.ssh_connect(
ssh_context, self.service_port)
hashes = job.to_list()
results = rpc_connection.root.exposed_crack_list(
job.id, job.to_list(), algo, weapon_system.cpu_count)
except:
logging.exception("Connection to remote weapon system failed, check parameters")
finally:
ssh_keyfile.close()
if results != None:
job.save_results(results)
job.completed = True
job.finished = datetime.now()
dbsession.add(job)
dbsession.flush()
self.__next__()
示例15: initialize
# 需要导入模块: from models import User [as 别名]
# 或者: from models.User import by_id [as 别名]
def initialize(self, *a, **kw):
webapp2.RequestHandler.initialize(self, *a, **kw)
uid = self.read_secure_cookie('user_id')
self.user = uid and User.by_id(int(uid))