- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
-6ren"> -我正在尝试使用 celery 通过 ajax 处理用户登录请求。 这是我的用户登录名: #Login the user using Ajax def ajax_loginOrRegisterUser(-6ren">
我正在尝试使用 celery 通过 ajax 处理用户登录请求。
这是我的用户登录名:
#Login the user using Ajax
def ajax_loginOrRegisterUser(request):
#Set a test cookie
request.session.set_test_cookie()
#Test if the request is POST, otherwise fail
if request.method == 'POST':
#Calls celery to do the heavy lifting
result = userLoginCheck.delay(request)
data = result.get(timeout=30)
#Return the response
return HttpResponse(data)
else:
#Log the System, send an alert message to the admin
logger = logging.getLogger('views.logger.login')
logger.error("Login User should not be GET")
logger.info("Unknown User returning Invalid")
#Send an invalid modal to the user
return HttpResponse("Invalid")
这很简单,基本上就是:
那么日志部分就比较复杂了:
#Celery should look at login user request, and make appropriate decisions
@shared_task
def userLoginCheck(request):
#Get logger
logger = logging.getLogger('views.logger.login')
#Set user name and type
username = ""
type = ""
#Try to get the user's username
try:
username = request.POST['username']
except:
logger.error("Error occured in Signing/Registering user, cannot find username")
logger.info("Unknown User returning Invalid")
return ("Invalid")
#See what type of request we are making, signin or register
try:
type = request.POST['type']
#Test if cookie works, otherwise inform the user, and fail login
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
#Try to see username, password, rememberMe is missing or not
try:
username = str(request.POST['username'])
password = request.POST['password']
rememberMe = request.POST['rememberMe']
#If it is a signin type
if(type == "signin"):
##############USER BASED AUTHENTICATION SYSTEM#####################
#Test for the user's public IP or best matched IP to see if it is banned
realIP = str(get_real_ip(request))
IP = str(get_ip(request))
#Get their realIP, and see if it matches, if they have over 10 accounts, then they are banned
try:
SusIP = suspiciousIP.objects.get(pk=realIP)
if(SusIP.count >= 10):
logger.info("User:" + str(username) + " has a banned real IP")
logger.info("User:" + str(username) + " returning Banned")
return ("Banned")
except:
pass
#Get their IP, and see if it matches, if they have over 10 accounts, then they are banned
try:
SusIP = suspiciousIP.objects.get(pk=IP)
if(SusIP.count >= 10):
logger.info("User:" + str(username) + " has a banned IP")
logger.info("User:" + str(username) + " returning Banned")
return ("Banned")
except:
pass
#Test the current user's blockedList
#2 conditions, if lockedTemp = True, then it is temporary locked until user has unlocked it
#by clicking the unlock email
#The other condition is whether tries > 5, and violations <= 5 (at 5th violations, the lockedTemp = true)
#then the user would need to wait until the time is over (900 seconds), which increases by each violations
try:
#Get the user's blocked list if available
userObject = blockedList.objects.get(pk=str(username))
currentDateTime = datetime.datetime.now()
objectDateTime = 0
#Try to see if the objectDateTime is available (meaning a date is there)
try:
objectDateTime = datetime.datetime.strptime(str(userObject.dateTime), "%Y-%m-%d %H:%M:%S.%f")
except:
pass
#Load the locked, tries, and violations
lockedTemp = userObject.lockedTemp
tries = userObject.tries
violations = userObject.violations
#if LockedTemp == True, then it means the user is currently locked, can only be unlocked by email
if(lockedTemp == "True"):
logger.info("User:" + str(username) + " is temp locked")
logger.info("User:" + str(username) + " returning tempLock")
return ("tempLock")
elif (tries >= 5 and violations <= 5 and lockedTemp != "True"):
#The user is not locked,
timeDifferenceSeconds = (currentDateTime - objectDateTime).total_seconds()
#if tries > 5, and violations < 5, and locked is false, then we test whether the time difference is > 900 seconds
if(timeDifferenceSeconds > (900 * violations)):
#If over > 900 seconds, then we will reset tries
userObject.tries = 0
userObject.save()
logger.info("User:" + str(username) + " is unlocked, with tries:" + str(tries) + ", violations:" + str(violations))
else:
#If < 900 seconds, then we will keep locking the user
logger.info("User:" + str(username) + " is currently locked, with tries:" + str(tries) + ", violations:" + str(violations))
logger.info("User:" + str(username) + " returning Locked")
return ("Locked")
except:
pass
##############USER BASED AUTHENTICATION SYSTEM#####################
##############SESSION BASED##################
#Initialize tries, to be used later on
# tries = "0"
# try:
# tries = request.session['tries']
# except:
# pass
# #If tries > 5 times
# if(int(tries) >= 5):
# timeNow = request.session['locked_time']
# timeDifferenceSeconds = (datetime.datetime.now() - datetime.datetime.strptime(timeNow, "%Y-%m-%d %H:%M:%S.%f")).total_seconds()
# #See if the difference is greater than 15 minutes, otherwise lock
# if(timeDifferenceSeconds > 900):
# request.session['tries'] = str(0)
# logger.info("User:" + str(username) + " is unlocked")
# else:
# logger.info("User:" + str(username) + " is currently locked")
# logger.info("User:" + str(username) + " returning Locked")
# return ("Locked")
##############SESSION BASED##################
#See if the user is activated yet (through email)
try:
userActivatedObject = userActivated.objects.get(pk=username)
if(userActivatedObject.activation == "True"):
logger.info("User:" + str(username) + " is activated")
else:
logger.info("User:" + str(username) + " is not activated yet")
return ("Activation")
except:
pass
logger.info("UserName:" + username + " is trying to login")
user = authenticate(username=username, password=password)
#If the user does exist
if user is not None:
#And is active
if user.is_active:
login(request, user)
logger.info("User:" + str(username) + "is active, and logged in")
#See if the user's remember me is checked, if it is not checked, then
#the session cookie will automatically expire when the browser closes
if(rememberMe == "true"):
request.session.set_expiry(86400)
logger.info("User:" + str(username) + " has set their expiration to 1 day")
else:
request.session.set_expiry(0)
logger.info("User:" + str(username) + " has set their expiration to expire when browser closes")
#See if the user is marked in blockedList, if true, then delete their row
try:
userObject = blockedList.objects.get(pk=username)
userObject.delete()
logger.info("User:" + str(username) + " is in blockedList, removing their entry")
except:
logger.info("User:" + str(username) + " is NOT in blockedList")
#See if the user's real IP is marked in suspicious IP, if true, then remove their entry
try:
SusIP = suspiciousIP.objects.get(pk=realIP)
SusIP.delete()
logger.info("User:" + str(username) + " is in suspicious real IP, removing their entry")
except:
logger.info("User:" + str(username) + " is NOT in real suspeciousIP")
#See if the user's IP is marked in suspicious IP, if true, then remove their entry
try:
SusIP = suspiciousIP.objects.get(pk=IP)
SusIP.delete()
logger.info("User:" + str(username) + " is in suspicious IP, removing their entry")
except:
logger.info("User:" + str(username) + " is NOT in suspeciousIP")
logger.info("User:" + str(username) + " returning index.html")
return ('index.html')
else:
logger.info("User:" + str(username) + "is not active, and will not be logged in")
logger.info("User:" + str(username) + " returning Invalid")
return ("Invalid")
else:
##############USER BASED AUTHENTICATION SYSTEM#####################
try:
#Get their current object, if exists, and increase their tries
userObject = blockedList.objects.get(pk=username)
userObject.tries += 1
userObject.save()
print "After:" + str(userObject.tries)
#If their tries >= 5, then lock them temporary, and increase violations
if(userObject.tries >= 5):
logger.info("User:" + str(username) + " is not valid, current tries:" + str(userObject.tries) + " and will be locked")
userObject.violations += 1
userObject.dateTime = str(datetime.datetime.now())
userObject.save()
#If violations >= 5, then we will tempLock, and can only be unlocked by email
if(userObject.violations >= 5):
print "I'm here"
logger.info("User:" + str(username) + " is not valid, will get TempLocked")
userObject.lockedTemp = "True"
userObject.dateTime = ""
userObject.tries = 0
userObject.violations = 0
userObject.save()
#Get their suspicious Real IPs, and increase them, or make a new one
try:
susIP = suspiciousIP.objects.get(pk=realIP)
susIP.count += 1
susIP.save()
logger.info("User:" + str(username) + " has a suspeciousIP:" + str(susIP.IP) + " current count:" + str(susIP.count))
except:
if (realIP is not None and realIP != "None"):
susIP = suspiciousIP(pk=realIP, count=1)
susIP.save()
logger.info("User:" + str(username) + " has a new suspeciousIP:" + str(realIP))
#Get their suspicious IPs, and increase them, or make a new one
try:
susIP = suspiciousIP.objects.get(pk=IP)
susIP.count += 1
susIP.save()
logger.info("User:" + str(username) + " has a suspeciousIP:" + str(susIP.IP) + " current count:" + str(susIP.count))
except:
if IP is not None:
susIP = suspiciousIP(pk=IP, count=1)
susIP.save()
logger.info("User:" + str(username) + " has a new suspeciousIP:" + str(IP))
logger.info("User:" + str(username) + " returning tempLock")
logger.info("User:" + str(username) + " returning tempLock")
return ("tempLock")
logger.info("User:" + str(username) + " returning Locked")
return ("Locked")
except:
#Invalid password typed, and the username that was typed is not found in our blockedList
#Then we will create a new one
#Setup an invalid password violation in the database
newUsername = str(username)
newRealIP = realIP
newIP = IP
newDateTime = ""
newTries = 1
newViolations = 0
newLockedTemp = "False"
#Create a user blockedList
newUserObject = blockedList(username=newUsername, realIP=newRealIP, IP = newIP,
dateTime = newDateTime, tries = newTries, violations = newViolations,
lockedTemp = newLockedTemp)
#Save the row
newUserObject.save()
logger.info("User:" + str(username) + " returning Invalid")
return ("Invalid")
##############USER BASED AUTHENTICATION SYSTEM#####################
##############SESSION BASED##################
#if the user fails in providing the correct username/password, increment tries
# try:
# tries = request.session['tries']
# num = int(tries)
# num += 1
# tries = str(num)
# request.session['tries'] = str(tries)
# except:
# tries = 0
# request.session['tries'] = str(tries)
# #If tries > 5, then we will lock
# if(int(tries) >= 5):
# logger.info("User:" + str(username) + " is not valid, current tries:" + str(tries) + " and will be locked")
# request.session['locked_time'] = str(datetime.datetime.now())
# logger.info("User:" + str(username) + " returning Locked")
# return ("Locked")
# else:
# logger.info("User:" + str(username) + " is not valid, current tries:" + str(tries))
# logger.info("User:" + str(username) + " returning Invalid")
# return ("Invalid")
##############SESSION BASED##################
elif(type == "register"):
#Get the logger
logger = logging.getLogger('views.logger.register')
#Test their session to see if they are already registered
registered = ""
try:
registered = request.session['registered']
except:
registered = "notRegisteredAlready"
#If not registered already, then register the user
if(registered == "notRegisteredAlready"):
try:
#Get the password verdict (strength of password), implemented by zxcvbn
passwordVerdict = ""
try:
passwordVerdict = request.POST['passwordVerdict']
except Exception as e:
#Cannot register the user, possibly failed at user.save(), where the user already existed
logger.info("Error:" + str(e.args))
logger.info("User:" + str(username) + " cannot register, problem with getting the verdict")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
#Test the user's verdict
if testVerdict(passwordVerdict):
#Log the System
logger.info("The new user:" + username + " passes password strength checking")
else:
#Log the System
logger.info("The new user:" + username + " password is too weak")
#Send an invalid modal to the user
return ("Invalid")
#Log the user
logger.info("User:" + str(username) + " checking for special formats")
if(checkSpecialCharacters(username) == True and checkHPEmail(username) == True and checkLengthUsername(username) == True and checkSpecialUsernames(username) == True):
#Log the user
logger.info("User:" + str(username) + " passed username policies")
logger.info("User:" + str(username) + " is going to register a new account")
#Create the user, and make a new activationObject, and set to false
user = User.objects.create_user(username, username, password)
user.backend='django.contrib.auth.backends.ModelBackend'
user.is_active = True
user.save()
#Send a registration email
sendRegistrationEmail(user)
#Update the session so it is registered
request.session['registered'] = "registeredAlready"
#Log the system
logger.info("User:" + str(username) + " has successfully registered")
logger.info("User:" + str(username) + " returning Valid")
#Return valid to jQuery
return ("ValidRegistration")
else:
#Log the user
logger.info("Test for username policies:" + str(username) + " failed")
#Return invalid to jQuery
return ("Invalid")
except Exception as e:
#Cannot register the user, possibly failed at user.save(), where the user already existed
logger.info("Error:" + str(e.args))
logger.info("User:" + str(username) + " cannot register, possibly the name already existed")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
else:
#The user has already registered an account, according to the session
logger.info("User:" + str(username) + " cannot register, possibly the name already registered")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
else:
#This else statement is only true when type = "unknown", where the user might have been fiddling with javascript
logger.info("User:" + username + " has an unknown type")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
except MultiValueDictKeyError:
#This exception comes from we cannot retrieve username or password from the user
logger.info("The user:" + str(username) + " have missing forms")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
except Exception as e:
#This exception comes from we a serious error AFTER we got the user's username and password
logger.error("Error occured in Signing/Register user:" + str(username))
logger.error("Error:" + str(e.args))
logger.error("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
else:
#This else statement is only true when the user does not support cookies
#Which in case I need
logger.info("The user:" + str(username) + " does not have cookies enabled")
logger.info("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
except Exception as e:
#This exception is only true when cannot identify the "type" which is from jQuery ajax
logger.error("Error:" + str(e.args))
logger.error("Error occured with user:" + str(username) + " cannot detect login/register type")
logger.error("User:" + str(username) + " returning Invalid")
#Send an invalid modal to the user
return ("Invalid")
它执行以下几个步骤:
如果用户不存在:
如果用户要注册:
我得到的错误是:
无法腌制:属性查找 cStringIO.StringO 失败
但是我不确定这个异常是在哪里引发的
有人知道为什么会发生这种情况,或者是否有办法找到发生此错误的位置?
最佳答案
关于Python Django - 无法腌制 <type 'cStringIO.StringO">,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25964486/
我对 Python-Django 和 web 开发还很陌生,我被困在这个使用 POST 创建新资源的特殊问题上。 我正在为 REST API 使用 Django REST 框架,我正在尝试创建一个新资
我已经使用 Django-storages 成功地将 Word 文档存储到 S3。 class Document(TitleSlugDescriptionModel, TimeStampedModel
我有 2 个关于模型代理的问题, 如何从模型对象创建代理对象? 如何从模型查询集创建代理查询集? 例如,假设我们定义了: from django.contrib.auth.models import
我想编写一个直接执行 HTTP 请求的单元测试(而不是使用 django.test.client.Client)。 如果您好奇为什么 - 那是因为我想测试我从 Django 应用程序公开的 Thrif
我为我的个人网站启动了一个 django 项目来学习 django。到目前为止,我已经将我的开发环境设置为我需要的一切,并遵循 this很棒的教程来创建一些基本的数据结构和模板。现在我想开始使用我之前
我已经阅读了很多关于如何在使用 Django 注册时添加额外字段的信息,例如 here 、 here 和 here 。代码片段是: forms.py(来自注册应用程序) class Registrat
我正在编写小型社交应用程序。功能之一是在网站标题中写入用户名。因此,例如,如果我登录并且我的名字是Oleg(用户名),那么我应该看到: Hello, Oleg | Click to edit prof
我有一个使用 Django 和 Django Rest 框架开发的应用程序。我想将 django-reversion 功能添加到我的应用程序中。 我已经尝试过http://django-reversi
我有一个简单的 HTML 表单,我没有使用 Django 表单,但现在我想添加一个选择。 选择最容易创建为 Django ChoiceField (与通过循环等手动创建选择相反),但是,如果没有在 D
我不明白为什么人们以两种方式编写外键,这样做的目的是什么?它们是相同还是不同? 我注意到有些人这样写: author = models.ForeignKey(Author, on_delete=mod
我想在我的 Django 应用程序中获取评论最多的十个帖子,但我做不到,因为我想不出合适的方法。 我目前正在使用 django 评论框架,并且我已经看到使用 aggregate or annotate
这对于 Django 1.2 仍然有效吗? Custom Filter in Django Admin on Django 1.3 or below 我已经尝试过,但管理类中的 list_filter
问题在于,当 django-compressor 编译为 .js 文件的 CoffeeScript 文件中引用 {{ STATIC_URL }} 时,它无法正确加载。 在我的 django 模板中,我
我正在尝试将一些字段从一个 django 模型移动到一个新模型。假设我有一个书籍模型: class Book(models.Model): title = models.CharField(max
我想在我的 Django 应用程序中获取评论最多的十个帖子,但我做不到,因为我想不出合适的方法。 我目前正在使用 django 评论框架,并且我已经看到使用 aggregate or annotate
目前我正在寻找在 Django 中实现访问控制。我已经阅读了有关内置权限的内容,但它并不关心每个对象的基础。例如,我想要“只有创建者可以删除自己的项目”之类的权限。所以我读到了 django-guar
嗨,我正在将我的 Django 模型的一个字段的值设置为其他模型的另一个字段的值。这个值应该是动态变化的。 这是我的第一个模型 class MainModel(AbstractBaseUser, Pe
我正在尝试为我的模型创建一个编辑表单。我没有使用模型表单,因为根据模型类型,用户可以使用不同的表单。 (例如,其中一个表单有 Tinymce 小部件,而另一个没有。) 有没有什么方法可以使用模型设置表
Django 模板中的搜索字段 如何在类似于此图像的 Django 模板中创建搜索字段 http://asciicasts.com/system/photos/1204/original/E354I0
根据 Django documentation ,如果 Django 安装激活了 AuthenticationMiddleware,HttpRequest 对象有一个“user”属性代表当前登录的用户
我是一名优秀的程序员,十分优秀!