本文整理汇总了Python中app.users.models.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: oauth_callback
def oauth_callback(provider):
if not current_user.is_anonymous():
return redirect(url_for('simple_page.index'))
oauth = OAuthSignIn.get_provider(provider)
social_id, username, email = oauth.callback()
if social_id is None:
flash(getttext(u'Authentication failed'), 'danger')
return redirect(url_for('simple_page.index'))
# check if user exists and if no creates new
user = User.query.filter_by(social_id=social_id).first()
if user is None:
user = User(
username=username,
password='',
email=email,
social_id=social_id
)
db.session.add(user)
db.session.commit()
login_user(user, remember=True)
user.update_login_info()
return redirect(url_for('simple_page.index'))
示例2: create
def create():
args = ['username', 'realname', 'email', 'pw', 'phone']
result = ''
try:
for i in args:
assert i in request.args, 'missing ' + i
assert len(list(graph_db.find("User","username",request.args['username']))) == 0, "user already exists"
assert len(list(graph_db.find("User","email",request.args['email']))) == 0, "email already exists"
u = User(username=request.args['username'],
realname=request.args['realname'],
email=request.args['email'],
password=hash_pw(request.args['pw']),
phone=request.args['phone'])
while True:
u.userid = randint(app.config['UID_START'],app.config['UID_END'])
if len(list(graph_db.find("User","id",u.userid))) == 0: break
u.save()
result = "user %s created" % u.userid
except AssertionError, e:
result = str(e)
示例3: after_login
def after_login(resp):
if resp.email is None or resp.email == "":
flash(gettext('Invalid login. Please try again.'))
return redirect(url_for('login_view'))
user = User.query.filter_by(email = resp.email).first()
userdetails = UserDetails(user=user)
if user is None:
nickname = resp.nickname
if nickname is None or nickname == "":
nickname = resp.email.split('@')[0]
nickname = User.make_valid_nickname(nickname)
nickname = User.make_unique_nickname(nickname)
user = User(nickname = nickname, email = resp.email, role = ROLE_USER)
db.session.add(user)
db.session.add(userdetails)
db.session.commit()
# make the user follow him/herself
db.session.add(user.follow(user))
db.session.commit()
userdetails = UserDetails.query.filter_by(user=user).first()
if userdetails is None:
userdetails = UserDetails(user=user)
db.session.add(userdetails)
db.session.commit()
remember_me = False
if 'remember_me' in session:
remember_me = session['remember_me']
session.pop('remember_me', None)
login_user(user, remember = True)
return redirect(request.args.get('next') or url_for('main_flow.index'))
示例4: test_invalid_password_is_rejected
def test_invalid_password_is_rejected(self):
User.create(name="Joe", email="[email protected]", password="12345")
with self.client:
self.client.post("/login/", data={"name": "Joe", "password": "****"})
self.assertTrue(current_user.is_anonymous())
示例5: create_test_user
def create_test_user():
user = User()
user.vk_id = "24076752"
user.access_token = "GsPlUf1RVH8AAAAAAAAAAQIpLzXXVtywxpD90ySk8XbSsFO5DoU0dhOlGU4qU5Fc"
user.expires = datetime.datetime.now() + datetime.timedelta(days=100)
db.session.add(user)
db.session.commit()
示例6: login
def login():
if request.method == 'POST':
user = User(name=request.form['username'], passwd = request.form['passwd'])
if user.is_active():
login_user(user)
redirect(url_for('frontend.home'))
redirect(url_for('frontend.login'))
示例7: setup_user
def setup_user(oauth_provider, oauth_id, display_name, image_url):
key = dict(
oauth_id = unicode(oauth_id),
oauth_provider = oauth_provider
)
slugs = set(u.display_slug for u in User.query.all())
user = User.query.filter_by(**key).first()
if not user:
user = User(**key)
db.session.add(user)
user.display_slug = slugify_unique(display_name, slugs)
user.display_name = display_name
user.oauth_token, user.oauth_secret = session['oauth']
# Load image
s = StringIO()
im = im_open(StringIO(req_get(image_url).content))
im.thumbnail((128, 128), ANTIALIAS)
im.save(s, 'JPEG')
user.image = s.getvalue()
db.session.commit()
login_user(user, remember = True)
# Why do i need this?
del session['oauth']
return redirect('/static/auth_recv.html')
示例8: register
def register():
"""
Registration Form
"""
form = RegisterForm(request.form)
if form.validate_on_submit():
# create an user instance not yet stored in the database
user = User(username=form.name.data,
email=form.email.data,
password=generate_password_hash(form.password.data)
)
#current_user=True)
# Insert the record in our database and commit it
try:
user.save()
except errors.NotUniqueError:
return render_template("users/register.html",
form=form,
duplicate=True,
user=None)
except:
raise
# Log the user in, as he now has an id
session['user_id'] = str(user.id)
session['user'] = user.username
print "session user_id: %s" % (str(user.id))
# flash will display a message to the user
#flash('Thanks for registering')
# redirect user to the 'home' method of the user module.
return redirect(url_for('users.home'))
return render_template("users/register.html", form=form, user=None)
示例9: register
def register():
email = request.form['email']
username = request.form['username']
password = request.form['password'].encode('utf8')
salt = generate_random_salt()
password_hash = generate_password_hash(password, salt)
# Check if email already exist in database
# if account does not exist create account in database
if User.objects(email=email).first() == None:
user = User(email=email)
user.accounts['internal'] = {"username":username, "password_hash":password_hash, \
"salt":salt}
user.save()
ret = json_util.dumps({"message":"account created", "status":"success"})
resp = Response(response=ret,
status=201,
mimetype="application/json")
return resp
else:
ret = json_util.dumps({"message":"Email already exist in database"})
resp = Response(response=ret,
status=200,
mimetype="application/json")
return resp
示例10: test_users_can_logout
def test_users_can_logout(self):
User.create(name="Joe", email="[email protected]", password="12345")
with self.client:
self.client.post("/login/", data={"name": "Joe", "password": "12345"})
self.client.get("/logout/")
self.assertTrue(current_user.is_anonymous())
示例11: add_admin
def add_admin():
"""Adds admin to database"""
admin = User.query.filter_by(username="admin").first()
if admin is None:
new_admin = User("admin", generate_password_hash("supertajnehaslo"), "[email protected]", social_id=None)
new_admin.role = USER.ADMIN
new_admin.confirmed = True
db.session.add(new_admin)
db.session.commit()
示例12: test_users_can_login
def test_users_can_login(self):
User.create(name="Joe", email="[email protected]", password="12345")
with self.client:
response = self.client.post("/login/", data={"name": "Joe", "password": "12345"})
self.assert_redirects(response, url_for("index"))
self.assertTrue(current_user.name == "Joe")
self.assertFalse(current_user.is_anonymous())
示例13: add_users
def add_users():
form = AddUserForms(request.form)
all_users = User.query.all()
form.select_role.choices = [(role.id, role.name) ## Loads up choices
for role in Role.query.all()]
## load roles that had supervisor roles in it:
form.select_supervisor.choices = [(user.username, user.username)
for user in User.query.all() if user.is_supervisor]
print form.select_role.choices
if form.validate_on_submit():
password = generate_password_hash('seer')
u = User (form.fname.data,form.midname.data,form.lname.data, ## Load user
form.email.data,form.username.data,password)
print form.select_role.data
for i in form.select_role.data: ## Loads up the selected choices of the user
print i
u.role.append(Role.query.get(i))
print u.role
##Checks if the user is a supervisor
if form.check_if_supervisor.data:
u.is_supervisor = True
## Loads up the supervisor of the user, and loads up the user to its supervisor
u.supervisor = form.select_supervisor.data
print form.select_supervisor.data
supervisor = User.query.filter_by(username=form.select_supervisor.data).first()
if supervisor.supervisee is None or '':
supervisor.supervisee = u.username
print supervisor.supervisee
else:
supervisor.supervisee = supervisor.supervisee + ' ' + u.username
print supervisor.supervisee
db_session.add(u)
db_session.add(supervisor)
db_session.commit()
us = UserStatistics(userId=u.id)
us.sl = form.number_of_sick_leaves.data
us.vl = form.number_of_vacation_leaves.data
us.offset = 0
db_session.add(us)
db_session.commit()
flash('User Created')
return redirect(url_for('Users.add_users'))
return render_template('admin/add_user.html', form = form)
示例14: test_make_unique_name
def test_make_unique_name(self):
user = User(name='john', email='[email protected]')
db.session.add(user)
db.session.commit()
name = User.make_unique_name('john')
self.assertEqual(name, 'john2')
# use the new name
user = User(name=name, email='[email protected]')
db.session.add(user)
db.session.commit()
new_name = User.make_unique_name('john')
self.assertEqual(new_name, 'john3')
示例15: register_view
def register_view():
form = RegistrationForm(request.form)
if form.validate_on_submit():
user = User()
form.populate_obj(user)
user.initials = user.initials.upper()
user.generate_password_hash(user.password)
session.add(user)
session.commit()
login_user(user)
return redirect(url_for('index'))
return render_template('users/register.html', form=form)