我正在使用 python 的滤锅库进行验证。在我的代码中有一个 colander.DateTime() 类型的 createdon 字段。当我为其提供 datetime.datetime.now() 的值时,它失败了,但异常说 createdon 字段的日期无效。可能是什么问题?
这是python模块的代码:
import colander
import htmllaundry
import pymongo
import random
import datetime
import hashlib
from pymongo import Connection
# Database Connection
HOST = "localhost"
PORT = 27017
DATABASE = "testdb"
conn = Connection(HOST, PORT)
db = conn[DATABASE]
# function to generate random string
def getRandomString(wordLen):
word = ''
for i in range(wordLen):
word += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
return word
# Colander classe for User object
class User(colander.MappingSchema):
username = colander.SchemaNode(colander.String(),validator=colander.Length(3,100))
email = colander.SchemaNode(colander.String(),validator=colander.All(colander.Email(),colander.Length(3,254)))
password = colander.SchemaNode(colander.String(),validator=colander.Length(8,100))
isactive = colander.SchemaNode(colander.Boolean())
code = colander.SchemaNode(colander.String(),validator=colander.Length(64,64))
name = colander.SchemaNode(colander.String(),validator=colander.Length(3,100))
picture = colander.SchemaNode(colander.String())
about = colander.SchemaNode(colander.String(),preparer=htmllaundry.sanitize,validator=colander.Length(0,1024))
ipaddress = colander.SchemaNode(colander.String())
createdon = colander.SchemaNode(colander.DateTime())
updatedon = colander.SchemaNode(colander.DateTime())
status = colander.SchemaNode(colander.Int(),validator=colander.Range(0,4)) #0->active, 1->Deleted, 2->Suspended, 3->Deactivated
# getUser(username)
def getUser(username):
user = db.users.find_one({"username" : username })
return user
# getUserByEmail(email)
def getUserByEmail(email):
user = db.users.find_one({"email" : email })
return user
# createUser(userdata) #accepts a dictionary argument
def createUser(userdata):
schema = User() # generate schema object for User Validation Class
try:
# set current date/time in createdon/updatedon
userdata['createdon'] = datetime.datetime.now()
userdata['updatedon'] = userdata['createdon']
# generate unique activation code, set isactive to False, and set status to 0
randomCode = getRandomString(64)
userdata['code'] = hashlib.sha256(randomCode).hexdigest()
userdata['isactive'] = False
userdata['status'] = 0
# validate and deserialize userdata
schema.deserialize(userdata)
# sha256 the password
userdata['password'] = hashlib.sha256(userdata['password']).hexdigest()
# save the userdata object in mongodb database
result = db.users.insert(userdata)
# return the result of database operation and final userdata object
return result, userdata
except colander.Invalid, e:
errors = e.asdict()
return errors, userdata
这是我在 test.py 中使用它的方式:
import usermodule
UserObject = {
'username':'anuj',
'email': 'anuj.kumar@gmail.com',
'password':'testpassword',
'name':'Anuj Kumar',
'picture':'/data/img/1.jpg',
'about':'Hacker & Designer, New Delhi',
'ipaddress':'127.0.0.1'
}
result, data = usermodule.createUser(UserObject)
print result
print data
我收到以下错误:
anuj@anuj-Vostro-1450:~/Projects/test$ python test.py
{'createdon': u'Invalid date', 'updatedon': u'Invalid date'}
{'username': 'anuj', 'picture': '/data/img/1.jpg', 'about': 'Hacker & Designer, New Delhi', 'code': 'd6450b49e760f96256886cb24c2d54e8e8033293c479ef3976e6cbeabbd9d1f1', 'name': 'Anuj Kumar', 'updatedon': datetime.datetime(2012, 9, 14, 16, 16, 32, 311705), 'createdon': datetime.datetime(2012, 9, 14, 16, 16, 32, 311705), 'status': 0, 'password': 'testpassword', 'ipaddress': '127.0.0.1', 'email': 'anuj.kumar@gmail.com', 'isactive': False}