def create_new_signup_verification(self,
                                       account_id,
                                       email,
                                       code,
                                       resend_code,
                                       expiration_delta=None):
        '''
        writes signup verification to verification table
        
        @param account_id: the account id assigned to the pending user
        @param email: the email used by the registering user
        @param code : String the unique string that will publicly identify the verification
        @param expiration_delta : python timedelta , denotes duration of validity of the verification
        
        
        @author: vincent agudelo
        
        
        '''
        verification_id = None

        new_verification_object = None
        params = {
            'email': email,
            'account_id': account_id,
            'code': code,
            'resend_code': resend_code,
            'category': 'SIGNUP'
        }

        if expiration_delta:
            params['date_expiry'] = (
                datetime.now() +
                expiration_delta).strftime('%Y-%m-%d %H:%M:%S')

        try:

            verification_id = self.__insert_to_database(params)

            new_verification_object = Verification()
            new_verification_object.verification_id = verification_id
            new_verification_object.email = email
            new_verification_object.verification_category = 'SIGNUP'
            new_verification_object.account_id = account_id
            new_verification_object.code = code
            new_verification_object.resend_code = resend_code

        except Exception, e:

            raise VerificationError(
                'could not create verification record: %s' % e)
    def create_new_change_email_verification(self, account_object, new_email,
                                             code, expiration_delta,
                                             resend_code):

        verification_object = None

        params = {
            'email':
            new_email,
            'account_id':
            account_object.account_id,
            'code':
            code,
            'resend_code':
            resend_code,
            'category':
            self.valid_categories['change_email'],
            'date_expiry':
            (datetime.now() + expiration_delta).strftime('%Y-%m-%d %H:%M:%S')
        }

        try:

            verification_id = self.__insert_to_database(params)

            if verification_id:

                verification_object = Verification()

                verification_object.verification_id = verification_id
                verification_object.email = params['email']
                verification_object.verification_category = params['category']
                verification_object.account_id = params['account_id']
                verification_object.code = params['code']
                verification_object.resend_code = params['resend_code']

        except Exception, e:

            raise VerificationError(
                'could not create verification record: %s' % e)
    def get_verification(self,
                         verification_id=None,
                         code=None,
                         email=None,
                         mobile=None,
                         category=None,
                         account_id=None,
                         resend_code=None):
        '''
        retrieves verification information based on information in given object
        
        '''
        verification_object = None

        criteria = {}

        if verification_id:
            criteria['id'] = verification_id

        if code:
            criteria['code'] = code

        if account_id:
            criteria['account_id'] = account_id

        if category:
            criteria['category'] = category

        if mobile:
            criteria['mobile'] = mobile

        if email:
            criteria['email'] = email

        if resend_code:
            criteria['resend_code'] = resend_code

        if criteria:
            table_cols = [
                'id', 'email', 'account_id', 'code', 'mobile', 'category',
                'date_expiry', 'send_status', 'resend_code'
            ]

            try:
                result = self.dbconn.execute_select(table_name='verification',
                                                    conditions=criteria,
                                                    operator='AND',
                                                    table_cols=table_cols,
                                                    fetchall=False)

            except Exception, e:
                raise VerificationError('unable to retrieve verification: %s' %
                                        e)

            if result:

                verification_object = Verification()

                verification_object.verification_id = result['id']
                verification_object.email = result['email']
                verification_object.account_id = result['account_id']
                verification_object.code = result['code']
                verification_object.resend_code = result['resend_code']

                verification_object.mobile = result['mobile']
                verification_object.verification_category = result['category']

                # magically retured as datetime object
                verification_object.date_expiry = result['date_expiry']