'''
Created on Oct 17, 2016

@author: henrylevy
'''
import tornado.gen
from DataObjects.User import User
from Controller.UserHandlers.AbstractUserHandler import AbstractUserHandler
from FunctionalUtils import createSalt, saltPassword

SUCCESS = '200'
FAILURE = '401'
PARTIAL = '206'
    
class SignUpHandler(AbstractUserHandler):
    '''
    -Requests are posted here when a user is creating an account.
    -All arguments are submitted as strings.
    '''
    
    @tornado.gen.coroutine
    def post(self):
        '''
        -Each argument is specified in URL format.
        -If an argument is not specified (except picture), then the creation will fail.
        -writes 'success' if everything worked, writes 'partial' if only
            picture could not be added, writes 'failure' if user could not be created
        args:
            email->str
            password->str
            first->str
            last->str
            phone->str without delimiters, length not checked, nor validity nor uniqueness of number
            profilePic->B64 string (if not specified default is used)
            seeker->str--0 for false, 1 for true
            owner->same as seeker
        returns:
            success: 200
            failure: 401
            partial: 206
        '''
        result = SUCCESS
        salt = createSalt()
        try:
            args = {'email'          : self.get_argument("email", ""),
                    'saltedPassword' : saltPassword(self.get_argument("password", ""), salt),
                    'salt'           : salt,
                    'firstName'      : self.get_argument("first", ""),
                    'lastName'       : self.get_argument("last", ""),
                    'phone'          : self.get_argument("phone", ""),
                    'isSeeker'       : self.get_argument("seeker", ""),
                    'isOwner'        : self.get_argument("owner", "")}
            user = User(**args)
            self.db.insertIntoTable(user)
            print 'inserted'
            userId = self.get_argument("email", "")
            profilePic = self.get_argument("profilePic", "")
            verificationPic = self.get_argument("verificationPhoto","")
            if not userId: result = FAILURE
            elif profilePic: # profile picture support not implemented
                try:
                    print 'submitting profile picture'
                    self.db.submitPicture(userId, profilePic)
                except:
                    print 'profile exception'
                    result = PARTIAL
            if verificationPic:
                try:
                    print 'print submitting verification'
                    self.db.submitVerification(userId, verificationPic)
                except Exception as e:
                    print e
                    print 'verification exception'
                    result = PARTIAL
            if userId:
                self.setCurrentUser(userId)
        except:
            print 'exception'
            result = FAILURE
        print 'writing result'
        self.write(result)

    @tornado.gen.coroutine
    def setCurrentUser(self, user):
        '''
        -set secure cookie for future requests
        '''
        if user:
            self.set_secure_cookie("user", user, expires_days=None)
        else:
            self.clear_cookie("user")
            

0270f15c1fbba954b3690d86cbe5e42e1f895851
'''
Created on Oct 17, 2016

@author: henrylevy
'''
import tornado.gen
from DataObjects.User import User
from Controller.UserHandlers.AbstractUserHandler import AbstractUserHandler
from FunctionalUtils import createSalt, saltPassword

SUCCESS = '200'
FAILURE = '401'
PARTIAL = '206'
    
class SignUpHandler(AbstractUserHandler):
    '''
    -Requests are posted here when a user is creating an account.
    -All arguments are submitted as strings.
    '''
    
    @tornado.gen.coroutine
    def post(self):
        '''
        -Each argument is specified in URL format.
        -If an argument is not specified (except picture), then the creation will fail.
        -writes 'success' if everything worked, writes 'partial' if only
            picture could not be added, writes 'failure' if user could not be created
        args:
            email->str
            password->str
            first->str
            last->str
            phone->str without delimiters, length not checked, nor validity nor uniqueness of number
            profilePic->B64 string (if not specified default is used)
            seeker->str--0 for false, 1 for true
            owner->same as seeker
        returns:
            success: 200
            failure: 401
            partial: 206
        '''
        result = SUCCESS
        salt = createSalt()
        try:
            args = {'email'          : self.get_argument("email", ""),
                    'saltedPassword' : saltPassword(self.get_argument("password", ""), salt),
                    'salt'           : salt,
                    'firstName'      : self.get_argument("first", ""),
                    'lastName'       : self.get_argument("last", ""),
                    'phone'          : self.get_argument("phone", ""),
                    'isSeeker'       : self.get_argument("seeker", ""),
                    'isOwner'        : self.get_argument("owner", "")}
            user = User(**args)
            self.db.insertIntoTable(user)
            print 'inserted'
            userId = self.get_argument("email", "")
            profilePic = self.get_argument("profilePic", "")
            verificationPic = self.get_argument("verificationPhoto","")
            if not userId: result = FAILURE
            elif profilePic: # profile picture support not implemented
                try:
                    print 'submitting profile picture'
                    self.db.submitPicture(userId, profilePic)
                except:
                    print 'profile exception'
                    result = PARTIAL
            if verificationPic:
                try:
                    print 'print submitting verification'
                    self.db.submitVerification(userId, verificationPic)
                except Exception as e:
                    print e
                    print 'verification exception'
                    result = PARTIAL
            if userId:
                self.setCurrentUser(userId)
        except:
            print 'exception'
            result = FAILURE
        print 'writing result'
        self.write(result)

    @tornado.gen.coroutine
    def setCurrentUser(self, user):
        '''
        -set secure cookie for future requests
        '''
        if user:
            self.set_secure_cookie("user", user, expires_days=None)
        else:
            self.clear_cookie("user")
            
