def setUp(self): self.config = testing.setUp() from sqlalchemy import create_engine engine = create_engine('sqlite://') from .models import ( Base, MyModel, ) session.configure(bind=engine) Base.metadata.create_all(engine) with transaction.manager: model = MyModel(name='one', value=55) session.add(model)
def create_signup(request): data = json.loads(request.body.decode('UTF-8'))['data']['attributes'] signup = Signup() signup.orientation = data['orientation'] signup.gender = data['gender'] signup.country = data['country'] signup.city = data['city'] signup.email = data['email'] signup.birthdate = datetime.date(int(data['year']), int(data['month']), int(data['day'])) signup.secret_key = ''.join(random.choice( string.ascii_letters + string.digits) for _ in range(20)) session.add(signup) session.flush() # send activation email mailer = get_mailer(request) body = """ Your account on Passport Date is almost active! Click on the link below to get started: http://localhost/activate?id=%s&k=%s """ % (signup.id, signup.secret_key) message = Message( subject="Validate your Passport Date account", sender="*****@*****.**", recipients=[signup.email], body=body, ) mailer.send(message) # handle response request.response.status = '201 Created' request.response.content_type = 'application/vnd.api+json' request.response.headers['Location'] = 'http://localhost/api/1/signups/%s' % signup.id return { "data": { "type": "signups", "id": str(signup.id), "attributes": data, }, }
def validate_signup(request): signup = session.query(Signup).filter(Signup.id==request.GET['id'])[0] if signup.secret_key != request.GET['k']: # probably just a 200 and an error message is OK? request.response.status = '400 Bad Request' return 'foo!' if not session.query(Profile).filter(Profile.id==request.GET['id']): profile = Profile() profile.id = str(signup.id) profile.orientation = signup.orientation profile.gender = signup.gender profile.country = signup.country profile.city = signup.city profile.birthdate = signup.birthdate profile.email = signup.email session.add(profile) session.flush() # handle response request.response.status = '200 OK' request.response.content_type = 'application/vnd.api+json' request.response.headers['Location'] = 'http://localhost/api/1/signups/%s' % signup.id return { "data": { "type": "signups", "id": str(signup.id), "attributes": { 'orientation':signup.orientation, 'gender':signup.gender, 'country':signup.country, 'city':signup.city, 'birthdate':signup.birthdate.isoformat(), 'email':signup.email, }, }, }