本文整理汇总了Python中seahub.utils.normalize_cache_key函数的典型用法代码示例。如果您正苦于以下问题:Python normalize_cache_key函数的具体用法?Python normalize_cache_key怎么用?Python normalize_cache_key使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了normalize_cache_key函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: incr_login_failed_attempts
def incr_login_failed_attempts(username=None, ip=None):
"""Increase login failed attempts by 1 for both username and ip.
Arguments:
- `username`:
- `ip`:
Returns new value of failed attempts.
"""
timeout = settings.LOGIN_ATTEMPT_TIMEOUT
username_attempts = 1
ip_attempts = 1
if username:
cache_key = normalize_cache_key(username, prefix=LOGIN_ATTEMPT_PREFIX)
try:
username_attempts = cache.incr(cache_key)
except ValueError:
cache.set(cache_key, 1, timeout)
if ip:
cache_key = normalize_cache_key(ip, prefix=LOGIN_ATTEMPT_PREFIX)
try:
ip_attempts = cache.incr(cache_key)
except ValueError:
cache.set(cache_key, 1, timeout)
return max(username_attempts, ip_attempts)
示例2: clear_login_failed_attempts
def clear_login_failed_attempts(request, username):
"""Clear login failed attempts records.
Arguments:
- `request`:
"""
ip = get_remote_ip(request)
cache.delete(normalize_cache_key(username, prefix=LOGIN_ATTEMPT_PREFIX))
cache.delete(normalize_cache_key(ip, prefix=LOGIN_ATTEMPT_PREFIX))
p = Profile.objects.get_profile_by_user(username)
if p and p.login_id:
cache.delete(normalize_cache_key(p.login_id, prefix=LOGIN_ATTEMPT_PREFIX))
示例3: refresh_cache
def refresh_cache(username):
"""
Function to be called when change user nickname.
"""
profile = get_first_object_or_none(Profile.objects.filter(user=username))
nickname = profile.nickname if profile else username.split('@')[0]
contactemail = profile.contact_email if profile else ''
key = normalize_cache_key(username, NICKNAME_CACHE_PREFIX)
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
contact_key = normalize_cache_key(username, CONTACT_CACHE_PREFIX)
cache.set(contact_key, contactemail, CONTACT_CACHE_TIMEOUT)
示例4: _get_cache_key
def _get_cache_key(request, prefix):
"""Return cache key of certain ``prefix``. If user is logged in, use
username, otherwise use combination of request ip and user agent.
Arguments:
- `prefix`:
"""
if request.user.is_authenticated():
key = normalize_cache_key(request.user.username, 'SharedLink_')
else:
ip = get_remote_ip(request)
# Memcached key length limit is 250 chars, and user agent somethings may
# be long which will cause error.
agent = request.META.get('HTTP_USER_AGENT', '')[:150]
key = normalize_cache_key(ip + agent, 'SharedLink_')
return key
示例5: char2pinyin
def char2pinyin(value):
"""Convert Chinese character to pinyin."""
key = normalize_cache_key(value, 'CHAR2PINYIN_')
py = cache.get(key)
if not py:
py = cc.convert(value)
cache.set(key, py, 365 * 24 * 60 * 60)
return py
示例6: get_login_failed_attempts
def get_login_failed_attempts(username=None, ip=None):
"""Get login failed attempts base on username and ip.
If both username and ip are provided, return the max value.
Arguments:
- `username`:
- `ip`:
"""
if username is None and ip is None:
return 0
username_attempts = ip_attempts = 0
if username:
cache_key = normalize_cache_key(username, prefix=LOGIN_ATTEMPT_PREFIX)
username_attempts = cache.get(cache_key, 0)
if ip:
cache_key = normalize_cache_key(ip, prefix=LOGIN_ATTEMPT_PREFIX)
ip_attempts = cache.get(cache_key, 0)
return max(username_attempts, ip_attempts)
示例7: email2nickname
def email2nickname(value):
"""
Return nickname or short email.
"""
if not value:
return ''
key = normalize_cache_key(value, NICKNAME_CACHE_PREFIX)
nickname = cache.get(key)
if not nickname:
profile = get_first_object_or_none(Profile.objects.filter(user=value))
nickname = profile.nickname if profile else value.split('@')[0]
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
return nickname
示例8: ajax_get_link_audit_code
def ajax_get_link_audit_code(request):
"""
Generate a token, and record that token with email in cache, expires in
one hour, send token to that email address.
User provide token and email at share link page, if the token and email
are valid, record that email in session.
"""
content_type = 'application/json; charset=utf-8'
token = request.POST.get('token')
email = request.POST.get('email')
if not is_valid_email(email):
return HttpResponse(json.dumps({
'error': _('Email address is not valid')
}), status=400, content_type=content_type)
dfs = FileShare.objects.get_valid_file_link_by_token(token)
ufs = UploadLinkShare.objects.get_valid_upload_link_by_token(token)
fs = dfs if dfs else ufs
if fs is None:
return HttpResponse(json.dumps({
'error': _('Share link is not found')
}), status=400, content_type=content_type)
cache_key = normalize_cache_key(email, 'share_link_audit_')
timeout = 60 * 60 # one hour
code = gen_token(max_length=6)
cache.set(cache_key, code, timeout)
# send code to user via email
subject = _("Verification code for visiting share links")
c = {
'code': code,
}
try:
send_html_email_with_dj_template(
email, dj_template='share/audit_code_email.html',
context=c, subject=subject, priority=MAIL_PRIORITY.now
)
return HttpResponse(json.dumps({'success': True}), status=200,
content_type=content_type)
except Exception as e:
logger.error('Failed to send audit code via email to %s')
logger.error(e)
return HttpResponse(json.dumps({
"error": _("Failed to send a verification code, please try again later.")
}), status=500, content_type=content_type)
示例9: email2contact_email
def email2contact_email(value):
"""
Return contact_email if it exists and it's not an empty string,
otherwise return username(login email).
"""
if not value:
return ''
key = normalize_cache_key(value, CONTACT_CACHE_PREFIX)
contact_email = cache.get(key)
if contact_email and contact_email.strip():
return contact_email
contact_email = Profile.objects.get_contact_email_by_user(value)
cache.set(key, contact_email, CONTACT_CACHE_TIMEOUT)
return contact_email
示例10: _decorated
def _decorated(request, token, *args, **kwargs):
assert token is not None # Checked by URLconf
fileshare = FileShare.objects.get_valid_file_link_by_token(token) or \
FileShare.objects.get_valid_dir_link_by_token(token) or \
UploadLinkShare.objects.get_valid_upload_link_by_token(token)
if fileshare is None:
raise Http404
if not is_pro_version() or not settings.ENABLE_SHARE_LINK_AUDIT:
return func(request, fileshare, *args, **kwargs)
# no audit for authenticated user, since we've already got email address
if request.user.is_authenticated():
return func(request, fileshare, *args, **kwargs)
# anonymous user
if request.session.get('anonymous_email') is not None:
request.user.username = request.session.get('anonymous_email')
return func(request, fileshare, *args, **kwargs)
if request.method == 'GET':
return render_to_response('share/share_link_audit.html', {
'token': token,
}, context_instance=RequestContext(request))
elif request.method == 'POST':
code = request.POST.get('code', '')
email = request.POST.get('email', '')
cache_key = normalize_cache_key(email, 'share_link_audit_')
if code == cache.get(cache_key):
# code is correct, add this email to session so that he will
# not be asked again during this session, and clear this code.
request.session['anonymous_email'] = email
request.user.username = request.session.get('anonymous_email')
cache.delete(cache_key)
return func(request, fileshare, *args, **kwargs)
else:
return render_to_response('share/share_link_audit.html', {
'err_msg': 'Invalid token, please try again.',
'email': email,
'code': code,
'token': token,
}, context_instance=RequestContext(request))
else:
assert False, 'TODO'
示例11: group_id_to_name
def group_id_to_name(group_id):
group_id = str(group_id)
key = normalize_cache_key(group_id, GROUP_ID_CACHE_PREFIX)
cached_group_name = cache.get(key)
if cached_group_name:
return cached_group_name
group = ccnet_api.get_group(int(group_id))
if not group:
return ''
group_name = group.group_name
cache.set(key, group_name, GROUP_ID_CACHE_TIMEOUT)
return group_name
示例12: test_anonymous_user_post_correct_token
def test_anonymous_user_post_correct_token(self, mock_is_pro_version):
"""
Check that anonnymous user input email and correct verification code.
"""
mock_is_pro_version.return_value = True
code = gen_token(max_length=6)
email = '[email protected]'
cache_key = normalize_cache_key(email, 'share_link_audit_')
cache.set(cache_key, code, timeout=60)
assert cache.get(cache_key) == code
anon_req = self._anon_post_request(data={'code': code, 'email': email})
self.assertEqual(anon_req.session.get('anonymous_email'), None)
resp = self._fake_view_shared_file(anon_req, self.fs.token)
self.assertEqual(resp.status_code, 200)
self.assertEqual(anon_req.session.get('anonymous_email'), email) # email is set in session
assert cache.get(cache_key) is None # token is delete after used
示例13: email2id
def email2id(value):
"""
Return the user id of an email. User id can be 0(ldap user),
positive(registered user) or negtive(unregistered user).
"""
if not value:
return -1
key = normalize_cache_key(value, EMAIL_ID_CACHE_PREFIX)
user_id = cache.get(key)
if not user_id:
try:
user = User.objects.get(email=value)
user_id = user.id
except User.DoesNotExist:
user_id = -1
cache.set(key, user_id, EMAIL_ID_CACHE_TIMEOUT)
return user_id
示例14: email2nickname
def email2nickname(value):
"""
Return nickname if it exists and it's not an empty string,
otherwise return short email.
"""
if not value:
return ''
key = normalize_cache_key(value, NICKNAME_CACHE_PREFIX)
cached_nickname = cache.get(key)
if cached_nickname and cached_nickname.strip():
return cached_nickname.strip()
profile = get_first_object_or_none(Profile.objects.filter(user=value))
if profile is not None and profile.nickname and profile.nickname.strip():
nickname = profile.nickname.strip()
else:
nickname = value.split('@')[0]
cache.set(key, nickname, NICKNAME_CACHE_TIMEOUT)
return nickname
示例15: clean_email_id_cache
def clean_email_id_cache(sender, **kwargs):
from seahub.utils import normalize_cache_key
user = kwargs['user']
key = normalize_cache_key(user.email, EMAIL_ID_CACHE_PREFIX)
cache.set(key, user.id, EMAIL_ID_CACHE_TIMEOUT)