parent
cbd8bd2f4d
commit
d72aa6e6f0
Binary file not shown.
@ -0,0 +1,113 @@
|
||||
"""
|
||||
|
||||
This class handles user registration i.e :
|
||||
- The first time a user logs in (default plan i.e free)
|
||||
- change of plan
|
||||
- cancel plan
|
||||
|
||||
The class is
|
||||
"""
|
||||
from utils.transport import *
|
||||
import uuid
|
||||
import requests
|
||||
|
||||
class Register :
|
||||
def __init__(self,**args) :
|
||||
"""
|
||||
@param read_class read class for data store
|
||||
@param write_class write class for data store
|
||||
@param store_args data store arguments
|
||||
"""
|
||||
self.class_write = args['write']
|
||||
self.class_read = args['read']
|
||||
self.store_args = args['store']
|
||||
self.default_plan = args['default']
|
||||
self.factory = DataSourceFactory()
|
||||
self.uid = args['uid']
|
||||
|
||||
|
||||
def register(self,pid):
|
||||
if pid is None :
|
||||
pid = self.default_plan
|
||||
|
||||
if self.is_registered(pid) == False:
|
||||
plan_info = self.__signup(pid)
|
||||
return self.__update(pid,plan_info)
|
||||
else:
|
||||
#
|
||||
# If the user is already registered for this plan, there is nothing much to do
|
||||
# @TODO: Consider plan migration functionality {upgrade,downgrade}
|
||||
#
|
||||
return None
|
||||
|
||||
def __signup(self,pid):
|
||||
auid = str(uuid.uuid4())
|
||||
headers = {"uid":self.uid,"pid":pid,"auid":json.dumps([auid])}
|
||||
|
||||
url="https://the-phi.com/store/init/monitor"
|
||||
r = requests.post(url,headers=headers)
|
||||
|
||||
return json.loads(r.text)
|
||||
|
||||
def __update(self,pid,plan_info) :
|
||||
"""
|
||||
Once the signup function has been performed, there needs to be some minor updates
|
||||
To the document notably in terms of the
|
||||
"""
|
||||
auid = self.get_key()
|
||||
args = dict(self.store_args)
|
||||
args['uid'] = auid
|
||||
args['dbname'] = pid
|
||||
store = self.factory.instance(type=self.class_write,args=args)
|
||||
store.write(label='emails',row=[auid,self.uid])
|
||||
plan = {pid:plan_info}
|
||||
store.write(label='plan',data=plan)
|
||||
return self.get_info()
|
||||
#return {"key":auid,"name":pid,"info":plan_info}
|
||||
def get_info(self):
|
||||
try:
|
||||
plan = self.get_active_plan()
|
||||
|
||||
key = self.get_key()
|
||||
return {"key":key,"name":plan['name'],"info":[plan['metadata']]}
|
||||
except Exception,e:
|
||||
print e
|
||||
return None
|
||||
def get_key(self) :
|
||||
store = self.factory.instance(type=self.class_read,args=self.store_args)
|
||||
id = store.view('federation/uid_map',key=self.uid)
|
||||
args = dict(self.store_args)
|
||||
args['uid'] = id
|
||||
store = self.factory.instance(type=self.class_read,args=args)
|
||||
document= store.read()
|
||||
if 'emails' in document and len(document['emails']) > 1 :
|
||||
|
||||
key = [id for id in document['emails'] if '@' not in id]
|
||||
key = key[len(key)-1]
|
||||
else:
|
||||
key = None
|
||||
return key
|
||||
|
||||
def is_registered(self,pid=None):
|
||||
store = self.factory.instance(type=self.class_read,args=self.store_args)
|
||||
id = store.view('federation/uid_map',key=self.uid)
|
||||
|
||||
if id is not None and pid is None :
|
||||
return id != []
|
||||
elif id is None :
|
||||
return False
|
||||
else:
|
||||
#
|
||||
# We are given a plan, this implies answering the question :
|
||||
# Is the current user signed-up for the given plan
|
||||
#
|
||||
return store.view('plans/active',key=id) != []
|
||||
return False
|
||||
def get_active_plan(self):
|
||||
store = self.factory.instance(type=self.class_read,args=self.store_args)
|
||||
id = store.view('federation/uid_map',key=self.uid)
|
||||
return store.view('plans/active',key=id)
|
||||
def cancel(self,pid):
|
||||
pass
|
||||
def upgrade(self,pid):
|
||||
pass
|
Loading…
Reference in new issue