# Path: indivo_client_py/lib/client.py
# class IndivoClient(APIConnector):
#     def create_account(self, user):
#         USER_EMAIL = 'user_email'
#         ACCOUNT_ID = 'account_id'
#         if not (user.has_key(USER_EMAIL) or user.has_key(ACCOUNT_ID)):
#             raise APIConnectorError("No user email or account id when posting account")
#         
#         account_id = None
#         if user.has_key(USER_EMAIL):
#             account_id = user[USER_EMAIL]
#         elif user.has_key(ACCOUNT_ID):
#             account_id = user[ACCOUNT_ID]
#         
#         if account_id:
#             primary_secret = user.get('primary_secret_p', 0)
#             secondary_secret = user.get('secondary_secret_p', 0)
#             password = user.get('user_pass', '')
#             
#             # There is a difference between app_type chrome without auth and admin
#             return self.api.call(self.ds.app_info, {'account_id': account_id,
#                                               'primary_secret_p': primary_secret,
#                                             'secondary_secret_p': secondary_secret,
#                                                       'password': password})
#         return False
#     
#     def set_record_id(self, id):
#         self.record_id, self.ds.record_id = id, id
#     
#     def set_app_id(self, id):
#         self.app_id, self.ds.app_id = id, id
#     
#     def create_session(self, user):
#         if user.has_key('username') and user.has_key('user_pass'):
#             chrome_auth = {'username' : user['username'], 'password' : user['user_pass']}
#             chrome_token = self.api.call(self.ds.app_info, chrome_auth)['prd']
#             if isinstance(chrome_token, dict):
#                 self.ds.app_info.update(chrome_token)
#                 return chrome_token
#         return False
#     
#     def update_token(self, oauth_token={}):
#         if oauth_token \
#             and isinstance(oauth_token, dict) \
#             and oauth_token.has_key(OAUTH_TOKEN) \
#             and oauth_token.has_key(OAUTH_TOKEN_SECRET):
#             self.ds.app_info[OAUTH_TOKEN] = oauth_token[OAUTH_TOKEN]
#             self.ds.app_info[OAUTH_TOKEN_SECRET] = oauth_token[OAUTH_TOKEN_SECRET]
#     
#     def get_surl_credentials(self):
#         """Requires there to be a token and secret set
#         
#         produces a token and secret dictionary for SURL (signing URLs).
#         """
#         
#         if self.ds.app_info.has_key(OAUTH_TOKEN) and \
#                 self.ds.app_info.has_key(OAUTH_TOKEN_SECRET):
#             token = self.ds.app_info[OAUTH_TOKEN]
#             secret = base64.b64encode(hmac.new(self.ds.app_info[OAUTH_TOKEN_SECRET], "SURL-SECRET", hashlib.sha1).digest())
#             return {'token' : token, 'secret' : secret}
#         return False


from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseForbidden, HttpRequest
from django.contrib.auth.models import User
from django.core.exceptions import *
from django.core.urlresolvers import reverse
from django.core import serializers
from django.db import transaction
from django.conf import settings
from django.utils import simplejson
from indivo_client_py.oauth import oauth
from django.db import transaction
from models import SmartConnectToken
from smart_ui_server import utils
from indivo_client_py.lib.client import IndivoClient
import xml.etree.ElementTree as ET
import urllib
import re
import httplib
import urllib
import urllib2
import urlparse
import time
import logging
            "account_secret": account_secret,
            "new_password": request.POST.get('new_password', None)
        }
        ret = api.call("POST", "/users/reset_password", options={'data': data})
    except Exception, e:
        error_msg = 'Failed to reset password:<br />%s<br />Please try again' % str(e)

    # display error
    if error_msg is not None:
        return utils.render_template('ui/reset_password', {
            'ACCOUNT': account_email,
            'ACCOUNT_SECRET': account_secret,
            'ERROR': error_msg
        })

    # success, prompt to login
    return utils.render_template(LOGIN_PAGE, {
        'MESSAGE': "Account password has been reset. Please log in below.",
        'ACCOUNT': account_email
    })


##
##  Utilities
##
def _interpolate_url_template(url, variables):
    """ Interpolates variables into a url which has placeholders enclosed by
    curly brackets """

    new_url = url

# Path: indivo_client_py/lib/client.py
# class IndivoClient(APIConnector):
#     def create_account(self, user):
#         USER_EMAIL = 'user_email'
#         ACCOUNT_ID = 'account_id'
#         if not (user.has_key(USER_EMAIL) or user.has_key(ACCOUNT_ID)):
#             raise APIConnectorError("No user email or account id when posting account")
#         
#         account_id = None
#         if user.has_key(USER_EMAIL):
#             account_id = user[USER_EMAIL]
#         elif user.has_key(ACCOUNT_ID):
#             account_id = user[ACCOUNT_ID]
#         
#         if account_id:
#             primary_secret = user.get('primary_secret_p', 0)
#             secondary_secret = user.get('secondary_secret_p', 0)
#             password = user.get('user_pass', '')
#             
#             # There is a difference between app_type chrome without auth and admin
#             return self.api.call(self.ds.app_info, {'account_id': account_id,
#                                               'primary_secret_p': primary_secret,
#                                             'secondary_secret_p': secondary_secret,
#                                                       'password': password})
#         return False
#     
#     def set_record_id(self, id):
#         self.record_id, self.ds.record_id = id, id
#     
#     def set_app_id(self, id):
#         self.app_id, self.ds.app_id = id, id
#     
#     def create_session(self, user):
#         if user.has_key('username') and user.has_key('user_pass'):
#             chrome_auth = {'username' : user['username'], 'password' : user['user_pass']}
#             chrome_token = self.api.call(self.ds.app_info, chrome_auth)['prd']
#             if isinstance(chrome_token, dict):
#                 self.ds.app_info.update(chrome_token)
#                 return chrome_token
#         return False
#     
#     def update_token(self, oauth_token={}):
#         if oauth_token \
#             and isinstance(oauth_token, dict) \
#             and oauth_token.has_key(OAUTH_TOKEN) \
#             and oauth_token.has_key(OAUTH_TOKEN_SECRET):
#             self.ds.app_info[OAUTH_TOKEN] = oauth_token[OAUTH_TOKEN]
#             self.ds.app_info[OAUTH_TOKEN_SECRET] = oauth_token[OAUTH_TOKEN_SECRET]
#     
#     def get_surl_credentials(self):
#         """Requires there to be a token and secret set
#         
#         produces a token and secret dictionary for SURL (signing URLs).
#         """
#         
#         if self.ds.app_info.has_key(OAUTH_TOKEN) and \
#                 self.ds.app_info.has_key(OAUTH_TOKEN_SECRET):
#             token = self.ds.app_info[OAUTH_TOKEN]
#             secret = base64.b64encode(hmac.new(self.ds.app_info[OAUTH_TOKEN_SECRET], "SURL-SECRET", hashlib.sha1).digest())
#             return {'token' : token, 'secret' : secret}
#         return False


# Path: ui/views.py
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseForbidden, HttpRequest
from django.contrib.auth.models import User
from django.core.exceptions import *
from django.core.urlresolvers import reverse
from django.core import serializers
from django.db import transaction
from django.conf import settings
from django.utils import simplejson
from indivo_client_py.oauth import oauth
from django.db import transaction
from models import SmartConnectToken
from smart_ui_server import utils
from indivo_client_py.lib.client import IndivoClient
import xml.etree.ElementTree as ET
import urllib
import re
import httplib
import urllib
import urllib2
import urlparse
import time
import logging

            "account_secret": account_secret,
            "new_password": request.POST.get('new_password', None)
        }
        ret = api.call("POST", "/users/reset_password", options={'data': data})
    except Exception, e:
        error_msg = 'Failed to reset password:<br />%s<br />Please try again' % str(e)

    # display error
    if error_msg is not None:
        return utils.render_template('ui/reset_password', {
            'ACCOUNT': account_email,
            'ACCOUNT_SECRET': account_secret,
            'ERROR': error_msg
        })

    # success, prompt to login
    return utils.render_template(LOGIN_PAGE, {
        'MESSAGE': "Account password has been reset. Please log in below.",
        'ACCOUNT': account_email
    })


##
##  Utilities
##
def _interpolate_url_template(url, variables):
    """ Interpolates variables into a url which has placeholders enclosed by
    curly brackets """

    new_url = url

    for key in variables: