def user_scope_for_token(raw_token): if not raw_token: return None token_cache_key = CACHE_KEY_FORMAT % raw_token token = cache.get(token_cache_key) if not token: try: token = OAuthAccessToken.objects.get(token=raw_token, expire_time__isnull=True) except OAuthAccessToken.DoesNotExist: token = None if token: cache.set(token_cache_key, token) if token: if token.last_used_time < datetime.now() - timedelta(hours=1): OAuthAccessToken.objects.filter(id=token.id).update(last_used_time=datetime.now()) cache.delete(token_cache_key) user = User.get_cached(token.user_id) if not user: # User account was deleted. token.invalidate_cache() return None, None return user, token.scope return None, None
def user_scope_for_token(raw_token): if not raw_token: return None token_cache_key = CACHE_KEY_FORMAT % raw_token token = cache.get(token_cache_key) if not token: try: token = OAuthAccessToken.objects.get(token=raw_token, expire_time__isnull=True) except OAuthAccessToken.DoesNotExist: token = None if token: cache.set(token_cache_key, token) if token: if token.last_used_time < datetime.now() - timedelta(hours=1): OAuthAccessToken.objects.filter(id=token.id).update( last_used_time=datetime.now()) cache.delete(token_cache_key) user = User.get_cached(token.user_id) if not user: # User account was deleted. token.invalidate_cache() return None, None return user, token.scope return None, None
def user_by_id(request, user_id=None): user = User.find_by_encrypted_id(user_id) if not user: return not_found() return api_response({ 'user': user.to_minimal_dict() })
def _build_response(): user = User.get_cached(request.user.id) flags = {} for flag in users.USER_EDITABLE_USER_FLAGS: flags[flag] = bool(getattr(user.flags, flag)) return api_response({ 'user': user.to_dict(), 'emails': [email.to_dict() for email in user.emails_set.all()], 'settings': flags })
def send_welcome_email(user_id): user = User.get_cached(user_id) if not user: # Deleted, rollbacked transaction, etc. return unsubscribe_url = generate_user_unsubscribe_url(user, 'unsubscribed_from_email') verify_url = None if user.flags.has_unverified_email: verify_url = verification_url_for_user_email(user, user.email) email = emails.create_welcome_email(user, verify_url, unsubscribe_url) emails.send_all([email])
def unsubscribe_with_token(token): user_dict = crypto_hack.decrypt_object(token, settings.UNSUBSCRIBE_URL_SECRET) if not user_dict: return False user = User.get_cached(user_dict['user_id']) flag = user_dict['flag_name'] if flag not in USER_EDITABLE_USER_FLAGS: logging.warn('WTF? uneditable flag: %s for user: %s', flag, user.id) return False if flag.startswith('enable') or flag.endswith('optin'): unsubscribed_value = False else: unsubscribed_value = True update_user_flags(user, {flag: unsubscribed_value}) return True
def create_user(first_name, last_name, email=None, verified_email=False, password=None): if not email: raise ValueError('No email provided') user = User() user.first_name = first_name user.last_name = last_name if not password: user.set_unusable_password() else: user.set_password(password) user.save() if email: success = associate_user_with_email(user, email, verified=verified_email, send_email=False) if not success: user.delete() return None send_welcome_email.apply_async(args=[user.id], countdown=60.0) return user
def user_by_token(token_string): token = token_by_token(token_string) if not token: return None return User.get_cached(token.user_id)
def user_by_id(request, user_id=None): user = User.find_by_encrypted_id(user_id) if not user: return not_found() return api_response({'user': user.to_minimal_dict()})