这是我尝试添加用户的实现。获取错误的部分是* args命令。链接到我的注册页面是here。
def addUser(self, session, *args, **kwargs):
try:
print person_models.Player
print person_models.Player.Email
existingPerson = session.query(person_models.Player).filter(person_models.Player.Email == args['Email']).first()
print existingPerson
if existingPerson is not None:
return {'error':"User already exists"}
print person_models
print type(person_models)
person_model = getattr(self, person_models)
print person_model
person = person_model(*args)
session.add(person)
session.commit()
return {"response": "User added"}
except Exception as e:
return {'error':str(e)}
我的目标是以下一行
existingPerson = session.query(person_models.Player).filter(person_models.Player.Email == args['Email']).first()
是检查电子邮件是否已经存在,如果是,则说“嘿,这封电子邮件已经存在!”
这是我的帖子功能
@jsonDbRest
def POST(self, *args, **kwargs):
session = cherrypy.request.db
print session
pc = PersonController()
print pc
response = pc.addUser(session, *args, **kwargs)
return {"results": response}
这是我的My model.py
from sqlalchemy import Column,String,VARCHAR,NUMERIC,INTEGER,ForeignKey,Boolean,DateTime,FLOAT
from sqlalchemy.orm import relationship
from sqlalchemy import event
from sqlalchemy.orm import validates
import time
import datetime
import uuid
from db import JsonBase
#Local imports
def time_mills_int():
return int(time.time()*1000)
class Player(JsonBase):
def __init__(self,*args,**kwargs):
super(Player,self).__init__(*args,**kwargs)
__tablename__ = 'Player'
Address = Column(String(255))
Citizenship = Column(String(50))
Committed = Column(String(255))
CountryPlayingIn = Column(String(50))
DateOfBirth = Column(DateTime)
Email = Column(String(255), primary_key=True)
EmailConfidential = Column(INTEGER())
FieldOfStudy = Column(String(255))
Gender = Column(String(1))
Graduation = Column(String(15))
Level = Column(String(15))
Links = Column(String(255))
Name = Column(String(50))
ParentApproval = Column(INTEGER())
Password = Column(String(100), nullable=False)
PhoneNumber = Column(String(30))
priPosition = Column(String(30))
secPosition = Column(String(30))
Sport = Column(String(25))
Views = Column(String(255))
Videos = Column(String(255))
ACT = Column(String(15))
GPA = Column(FLOAT())
SAT = Column(String(15))
TOEFL = Column(String(15))
__mapper_args__={
'polymorphic_identity':'Player'
}
class Coach(JsonBase):
def __init__(self,*args,**kwargs):
super(Coach,self).__init__(*args,**kwargs)
__tablename__ = 'Coach'
Address = Column(String(255))
Citizenship = Column(String(50), nullable=False)
DateOfBirth = Column(DateTime, nullable=False)
Email = Column(String(255), primary_key=True)
EmailConfidential = Column(INTEGER(), nullable=False)
Gender = Column(String(1), nullable=False)
Name = Column(String(50), nullable=False)
Password = Column(String(100), nullable=False)
PhoneNumber = Column(String(30), nullable=False)
Sport = Column(String(25))
Verified = Column(INTEGER(), nullable=False)
Views = Column(String(255))
__mapper_args__={
'polymorphic_identity':'Coach'
}
这是我的jsonDbRest(func)
import cherrypy
import json
from engine import create_session
def jsonDbRest(func):
def wrapper(self, *args, **kwargs):
cherrypy.request.db = create_session()
if 'Content-Length' in cherrypy.request.headers:
length = cherrypy.request.headers['Content-Length']
bodytext = cherrypy.request.body.fp.read(int(length))
else:
bodytext = ""
if bodytext != "":
jsonData = json.loads(bodytext)
else:
jsonData = None
cherrypy.request.json = jsonData
func_data = func(self, *args, **kwargs)
data = {"method":func.__name__, "input":jsonData,"kwargs":kwargs}
if func_data is not None:
data.update(func_data)
cherrypy.response.headers['Content-Type'] = 'application/json'
if "error" in data:
cherrypy.response.status = "400"
else:
cherrypy.response.status = "200"
cherrypy.request.db.close()
return json.dumps(data, indent=4)
return wrapper