""" 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(): '' def signout_link:xml(): signout = complete_path('/user/logout') '
' % signout '
' def ensure_admin_access(): get_publisher().ensure_signed_in() if not get_user().is_admin(): not_found() def ensure_create_users_access(): if not can_create_users(): not_found() def can_create_users(): return get_user().is_granted('create-users') def user_admin_path(user, full=False): if full: path = get_site_url() else: path = complete_path('/') if user: path += 'profile/' + url_quote(stringify(user.get_key())) + '/' return path def allow_act_as(): return get_user().is_granted('act-as') def act_as_path(user): return user_admin_path(user) + 'act_as?url=' + get_path() def un_act_as_path(): return user_admin_path(get_user()) + 'un_act_as?url=' + get_path() def format_act_as:xml(user): '' % ( act_as_path(user), user) def format_act_as_css:str(): """\ a.actas { white-space: nowrap; text-decoration: none; margin-left: 0.4ex; } span.actas { white-space: nowrap; text-decoration: none; } @media print { a.actas { display: none; } } """ def format_user:xml(user, name=1, email=1): #(user : Contact, # name : boolean = 1 # email : boolean = 1) -> xml if not user: none_quote() else: user_id = user.get_id() real_user = get_user() if real_user and real_user.is_admin(): '' href(user_admin_path(user), user_id) if real_user.is_granted('act-as'): format_act_as(user) '' else: user_id name_email = [] if name: name_email.append(none_quote(user.format_realname())) if email: email = user.get_email() if email: name_email.append(href('mailto:' + email, email)) else: name_email.append(none_quote()) name_email = [w for w in name_email if w] if name_email: ' (' ', '.join(name_email) ')' def format_signin_css:str(): """\ /* form.signin { float: left; } */ form.signin div.qpform div.widget { float: none; } form.signin div.widget { margin-bottom: 0; float: none; } aform.signin div.qpform, aform.signin div.qpform div.widget, aform.signin div.qpform div.submit { margin-bottom: 0; float: none; } form.signin div.widget div.hint { /* inline hints. */ padding-left: 1em; display: inline; } form.signin br.widget { display: none; } form.tiny_signin { float: right; } form.tiny_signin div, form.tiny_signin input { margin: 0; padding: 0; font-weight: normal; display: inline; background: inherit; white-space: nowrap; } form.tiny_signin { margin: 0; } form.tiny_signin div.qpform div.widget { background: inherit; } form.tiny_signin div.qpform div.widget div.title { font-weight: normal; } form.tiny_signin div.content input { font-size: xx-small; vertical-align: top; } div.signin_link { float: right; text-align: right; font-size: small; padding-left: 1em; padding-right: 1em; } div.signin_link a { text-decoration: none; padding: 2px; } div.signin_link a:hover { text-decoration: underline; } form.signout { display: inline; } """