"""
open/dulcinea/lib/ui/user/util.qpy
"""
from dulcinea.misc import get_misc_db
from dulcinea.ui.user.motd import format_motd
from dulcinea.ui.util import get_site_url, none_quote
from dulcinea.ui.util import safe_respond, respond
from dulcinea.user import get_matching_user
from qp.fill.form import Form
from qp.fill.html import href, url_quote
from qp.fill.widget import HiddenWidget
from qp.fill.widget import StringWidget, PasswordWidget, SubmitWidget
from qp.pub.common import get_user, get_request, get_session, get_publisher
from qp.pub.common import redirect, get_path, not_found, get_hit
from qp.pub.common import get_config_value, complete_path, get_users
from qpy import stringify
def get_user_id_hint:xml():
'
Enter your user ID'
if get_config_value('allow_email_login'):
' or email address'
'.'
if (not get_user() and
get_config_value('allow_anonymous_registration')):
" If you do not have a user account, %s." % (
href(complete_path('/user/register'), 'register here'))
def get_password_hint:xml():
if get_publisher().is_email_enabled():
"
Forgot your password? "
"We can %s." % href(complete_path('/user/forgot'),
'mail you a new one')
def get_signin_hint:xml():
if get_publisher().format_login_agreement():
"subject to agreement below"
def _get_href(path):
"""
If the site offers an https_address, and the current scheme
is not https, this returns a full url using https as the scheme
with the given path.
Otherwise, this just returns the path as it is given.
"""
request = get_request()
https_address = get_config_value('https_address')
if https_address and request.get_scheme() != 'https':
return get_publisher().complete_url(path, secure=True)
return path
def ensure_signed_in(title="Please sign in.", realm=None):
https_address = get_config_value('https_address')
if https_address and get_request().get_scheme() != 'https':
redirect(_get_href(get_path()))
if not get_user():
get_hit().get_info()['sign_in_page'] = True
form = Form(use_tokens=False,
action=_get_href(get_path()),
**{'class':'signin quixote qp'})
form.add(StringWidget, 'user_id',
title='User',
value=get_session().get_owner().get_id(),
size=30, required=1,
hint=get_user_id_hint(), tabindex='1')
form.add(PasswordWidget, 'password',
title='Password', size=30, required=1,
hint=get_password_hint(), tabindex='2')
form.add(SubmitWidget, 'signin', 'Sign in',
hint=get_signin_hint(), tabindex='3')
login_agreement = get_publisher().format_login_agreement()
if not form.is_submitted() or form.has_errors():
safe_respond(title, '', form.render(), login_agreement)
if get_config_value('allow_email_login'):
user = get_matching_user(form['user_id'])
else:
user = get_users().get(form['user_id'])
if not user or user.is_disabled():
form.set_error('user_id', 'User %s not found' % form['user_id'])
elif not user.has_password(form.get('password'), realm=realm):
form.set_error('password', 'That password was wrong.')
form.get_widget('password').set_value(None)
if form.has_errors():
safe_respond(title, title, form.render(), login_agreement)
get_session().set_authenticated(user)
user.record_login(get_request().get_environ('REMOTE_ADDR'))
if get_misc_db() and get_misc_db().get_motd():
form = Form(use_tokens=False)
form.add_submit('ok', 'OK')
if not form.get('ok'):
respond('Signin Message',
format_motd(get_misc_db().get_motd()),
form.render())
del get_hit().get_info()['sign_in_page']
redirect("")
def allow_tiny_signin(user):
return user.is_admin()
def signin_link:xml():
'