Skip to content

Commit

Permalink
modify config and user
Browse files Browse the repository at this point in the history
  • Loading branch information
CAOLINAN committed May 22, 2018
1 parent ec1e8d2 commit e925ab1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
22 changes: 22 additions & 0 deletions ulordapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,34 @@


class Config(dict):
"""
config class.Based on the dict.Update key-value,including subdict.
"""
def __init__(self, *args, **kwargs):
"""
config init
:param key-value: config key and it's value
:type key-value : str
"""
dict.__init__(self, *args, **kwargs)
self._ensure_subconfig()
self.log = logging.getLogger('Config:')

def _ensure_subconfig(self):
"""
set sub dict as config class
"""
for key in self:
obj = self[key]
if isinstance(obj, dict) and not isinstance(obj, Config):
setattr(self, key, Config(obj))

def save(self):
"""
save config to the config file.Just for the config,not support for it's sub config.
"""
# data ={k: unicode(v).encode("utf-8") for k,v in self.iteritems()}
# Just think about config.maybe need to think about other instance.
if self.has_key('baseconfig') and self['baseconfig'].has_key('config_file'):
Expand All @@ -46,6 +62,12 @@ def save(self):
json.dump(self, target, encoding='utf-8', ensure_ascii=False, indent=2, sort_keys=True)

def read(self, init=True):
"""
read config from config file and update config.
:param init: check if the config is first save.Default is True, will save the config file.
:type init: bool
"""
# read and config from self['baseconfig']['config_file']
if self.has_key('baseconfig') and self['baseconfig'].has_key('config_file') and \
os.path.isfile(self.get('baseconfig').get('config_file')):
Expand Down
78 changes: 66 additions & 12 deletions ulordapi/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ulordapi.udfs import udfs
from ulordapi.config import config, ulordconfig, webconfig, dbconfig
from ulordapi.manage import db, User, Resource, Tag, create
from ulordapi.errcode import _errcodes
from ulordapi.errcode import _errcodes, return_result


class Developer():
Expand Down Expand Up @@ -85,7 +85,7 @@ def udfs_download(self, udfshashs):
})
else:
result.update({
udfshash: "not a udfshash"
udfshash: 0 # "not a udfshash"
})
return result

Expand Down Expand Up @@ -129,7 +129,7 @@ def udfs_cat(self, udfshashs):
})
else:
result.update({
udfshash: "not a udfshash"
udfshash: 0 # "not a udfshash"
})
return result

Expand Down Expand Up @@ -213,6 +213,7 @@ def __init__(self, appkey, secret):
def get_purearg(self, arg):
"""
check if the arg is encrypted.If encrypted return decrypted arg,else return arg.
:param arg: arg need to be checked
:type arg: str
:return: decrypted arg or arg
Expand All @@ -228,9 +229,10 @@ def get_purearg(self, arg):
return arg

# up functions
def user_regist(self, username, password, cellphone=None, email=None, wallet=None, pay_password=None, *encryption):
def user_regist(self, username, password, cellphone=None, email=None, wallet=None, pay_password=None, encryption=[]):
"""
user regist
:param username: user name
:type username: str
:param password: user password
Expand Down Expand Up @@ -292,9 +294,10 @@ def user_regist(self, username, password, cellphone=None, email=None, wallet=Non
# return return_result(0, result={"token": user.token})
return user.token

def user_login(self, username, password, *encryption):
def user_login(self, username, password, encryption=[]):
"""
user login
:param username: user name
:type username: str
:param password: user password
Expand All @@ -316,12 +319,13 @@ def user_login(self, username, password, *encryption):
login_user.token = str(uuid1())
login_user.timestamp = int(time.time()) + webconfig.get('token_expired')
db.session.commit()
# return return_result(0, result={"token": login_user.token})
return login_user.token
return return_result(0, result={"token": login_user.token})
# return login_user.token

def user_logout(self, token=None, username=None):
"""
user logout.Change user's timestamp
:param token: user token
:type token: str
:param username: user name
Expand All @@ -341,6 +345,39 @@ def user_logout(self, token=None, username=None):
else:
return _errcodes.get(60002)

def user_activity(self, token=None, username=None):
"""
activity.App developer send some ulords to the user.
:param token: user token
:type token: str
:param username: user name
:type username: str
:return: True or False.
"""
login_user = None
if token:
login_user = User.query.filter_by(token=token).first()
if int(login_user.timestamp) < time.time():
return _errcodes.get(60104)
elif username:
login_user = User.query.filter_by(username=username).first()
if login_user:
credit_result = self.ulord.paytouser(login_user.wallet)
if credit_result.get('errcode') != 0:
return credit_result
else:
login_user.activity = ulordconfig.get('amount')
return {
"errcode": 0,
"reason": "success",
"result":{
"amount": ulordconfig.get('amount'),
}
}
else:
return _errcodes.get(60002)

def user_publish(self, title, udfshash, amount, tags, description, usercondition):
"""
user publish resource
Expand Down Expand Up @@ -445,7 +482,7 @@ def user_resouce_views(self, title):
db.commit()
return resources.views

def user_pay_resources(self, payer, claim_id, pay_password):
def user_pay_resources(self, payer, claim_id, pay_password, encryption=[]):
"""
user pay resource
Expand All @@ -457,6 +494,13 @@ def user_pay_resources(self, payer, claim_id, pay_password):
:type pay_password: str
:return: errcode.You can query from the errcode.
"""
if encryption:
if encryption[0]:
payer = self.get_purearg(payer)
if encryption[1]:
claim_id = self.get_purearg(claim_id)
if encryption[2]:
pay_password = self.get_purearg(pay_password)
return self.ulord.transaction(payer, claim_id, pay_password)

def user_pay_ads(self, wallet, claim_id, pay_password):
Expand All @@ -470,15 +514,28 @@ def user_pay_ads(self, wallet, claim_id, pay_password):
"""
return self.ulord.transaction(wallet, claim_id, pay_password, True)

def create_database(self):
def create_database(self, path=None):
"""
create database
:param path: database path.Warining:It'a a dir
:type path: str
:return: True/False
"""
# check if the database is exited
if dbconfig.get('IsCreated'):
self.log.info("DB has created!")
else:
self.log.info("Creating DB...")
if path:
try:
os.stat(path)
except:
os.mkdir(path)
dbconfig.update({
'SQLALCHEMY_DATABASE_URI':'sqlite:///{}/sqlite.db'.format(path)
})
config.save()
create()
dbconfig.update({
'IsCreated': True
Expand Down Expand Up @@ -513,9 +570,6 @@ def query(self, sql):
return result


def test():
print("Hello world!")

if __name__ == '__main__':
develop = Developer()
result = develop.help()
Expand Down

0 comments on commit e925ab1

Please sign in to comment.