def reset_password(request): email = request.POST['email'] try: profile = session.query(Profile).filter(Profile.email==email)[0] except IndexError: request.response.status = '404 Not Found' return {'message':'Email not found','code':'1'} profile.password_reset_key = ''.join(random.choice( string.ascii_letters + string.digits) for _ in range(20)) now = datetime.datetime.now() profile.password_reset_date = datetime.date(now.year, now.month, now.day) session.flush() mailer = get_mailer(request) body = """ Someone requested a password reset for your account. If this was you, then you can click on this link to change your password: http://localhost/set_new_password?id=%s&k=%s&email=%s """ % (profile.id, profile.password_reset_key, email) message = Message( subject="Passpord Date password reset request", sender="*****@*****.**", recipients=[profile.email], body=body, ) mailer.send(message) return {'message':'Password request sent. Check your email.', 'code':'0'}
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, }, }, }