set user object id after created

This commit is contained in:
CMGS 2016-08-23 10:52:35 +08:00
parent 8e58999f5a
commit 4a3c4b53cc

View File

@ -83,10 +83,10 @@ class User(db.Model):
def is_authenticated(self): def is_authenticated(self):
return True return True
def is_active(self): def is_active(self):
return True return True
def is_anonymous(self): def is_anonymous(self):
return False return False
@ -111,7 +111,7 @@ class User(db.Model):
pw = plain_text_password if plain_text_password else self.plain_text_password pw = plain_text_password if plain_text_password else self.plain_text_password
return bcrypt.hashpw(pw, bcrypt.gensalt()) return bcrypt.hashpw(pw, bcrypt.gensalt())
def check_password(self, hashed_password): def check_password(self, hashed_password):
# Check hased password. Useing bcrypt, the salt is saved into the hash itself # Check hased password. Useing bcrypt, the salt is saved into the hash itself
return bcrypt.checkpw(self.plain_text_password, hashed_password) return bcrypt.checkpw(self.plain_text_password, hashed_password)
@ -203,7 +203,7 @@ class User(db.Model):
ldap_username = result[0][0][0] ldap_username = result[0][0][0]
l.simple_bind_s(ldap_username, self.password) l.simple_bind_s(ldap_username, self.password)
logging.info('User "%s" logged in successfully' % self.username) logging.info('User "%s" logged in successfully' % self.username)
# create user if not exist in the db # create user if not exist in the db
if User.query.filter(User.username == self.username).first() == None: if User.query.filter(User.username == self.username).first() == None:
try: try:
@ -220,7 +220,7 @@ class User(db.Model):
if User.query.count() == 0: if User.query.count() == 0:
self.role_id = Role.query.filter_by(name='Administrator').first().id self.role_id = Role.query.filter_by(name='Administrator').first().id
else: else:
self.role_id = Role.query.filter_by(name='User').first().id self.role_id = Role.query.filter_by(name='User').first().id
self.create_user() self.create_user()
logging.info('Created user "%s" in the DB' % self.username) logging.info('Created user "%s" in the DB' % self.username)
@ -268,6 +268,7 @@ class User(db.Model):
user = User(username=self.username, firstname=self.firstname, lastname=self.lastname, role_id=self.role_id, email=self.email, password=self.get_hashed_password(self.plain_text_password)) user = User(username=self.username, firstname=self.firstname, lastname=self.lastname, role_id=self.role_id, email=self.email, password=self.get_hashed_password(self.plain_text_password))
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
self.id = user.id
return True return True
except Exception, e: except Exception, e:
raise raise
@ -338,7 +339,7 @@ class User(db.Model):
Revoke all privielges from a user Revoke all privielges from a user
""" """
user = User.query.filter(User.username == self.username).first() user = User.query.filter(User.username == self.username).first()
if user: if user:
user_id = user.id user_id = user.id
try: try:
@ -385,8 +386,8 @@ class Role(db.Model):
self.id = id self.id = id
self.name = name self.name = name
self.description = description self.description = description
# allow database autoincrement to do its own ID assignments # allow database autoincrement to do its own ID assignments
def __init__(self, name=None, description=None): def __init__(self, name=None, description=None):
self.id = None self.id = None
self.name = name self.name = name
@ -402,18 +403,18 @@ class DomainSetting(db.Model):
domain = db.relationship('Domain', back_populates='settings') domain = db.relationship('Domain', back_populates='settings')
setting = db.Column(db.String(255), nullable = False) setting = db.Column(db.String(255), nullable = False)
value = db.Column(db.String(255)) value = db.Column(db.String(255))
def __init__(self, id=None, setting=None, value=None): def __init__(self, id=None, setting=None, value=None):
self.id = id self.id = id
self.setting = setting self.setting = setting
self.value = value self.value = value
def __repr__(self): def __repr__(self):
return '<DomainSetting %r for $d>' % (setting, self.domain.name) return '<DomainSetting %r for $d>' % (setting, self.domain.name)
def __eq__(self, other): def __eq__(self, other):
return self.setting == other.setting return self.setting == other.setting
def set(self, value): def set(self, value):
try: try:
self.value = value self.value = value
@ -448,7 +449,7 @@ class Domain(db.Model):
def __repr__(self): def __repr__(self):
return '<Domain %r>' % (self.name) return '<Domain %r>' % (self.name)
def add_setting(self, setting, value): def add_setting(self, setting, value):
try: try:
self.settings.append(DomainSetting(setting=setting, value=value)) self.settings.append(DomainSetting(setting=setting, value=value))
@ -487,7 +488,7 @@ class Domain(db.Model):
Return domain id Return domain id
""" """
domain = Domain.query.filter(Domain.name==name).first() domain = Domain.query.filter(Domain.name==name).first()
return domain.id return domain.id
def update(self): def update(self):
""" """
@ -638,10 +639,10 @@ class Domain(db.Model):
""" """
domain_id = self.get_id_by_name(self.name) domain_id = self.get_id_by_name(self.name)
domain_user_ids = self.get_user() domain_user_ids = self.get_user()
new_user_ids = [u.id for u in User.query.filter(User.username.in_(new_user_list)).all()] if new_user_list else [] new_user_ids = [u.id for u in User.query.filter(User.username.in_(new_user_list)).all()] if new_user_list else []
removed_ids = list(set(domain_user_ids).difference(new_user_ids)) removed_ids = list(set(domain_user_ids).difference(new_user_ids))
added_ids = list(set(new_user_ids).difference(domain_user_ids)) added_ids = list(set(new_user_ids).difference(domain_user_ids))
@ -816,7 +817,7 @@ class Record(object):
""" """
# get list of current records we have in powerdns # get list of current records we have in powerdns
current_records = self.get_record_data(domain_name)['records'] current_records = self.get_record_data(domain_name)['records']
# convert them to list of list (just has [name, type]) instead of list of hash # convert them to list of list (just has [name, type]) instead of list of hash
# to compare easier # to compare easier
list_current_records = [[x['name'],x['type']] for x in current_records] list_current_records = [[x['name'],x['type']] for x in current_records]
@ -912,9 +913,9 @@ class Record(object):
"disabled": temp_disabled "disabled": temp_disabled
}) })
final_records.append(new_record) final_records.append(new_record)
else: else:
final_records.append({ final_records.append({
"name": key[0], "name": key[0],
"type": key[1], "type": key[1],
@ -962,11 +963,11 @@ class Record(object):
"name": self.name, "name": self.name,
"type": self.type, "type": self.type,
"changetype": "DELETE", "changetype": "DELETE",
"records": [ "records": [
{ {
"name": self.name, "name": self.name,
"type": self.type "type": self.type
} }
] ]
} }
] ]
@ -1070,7 +1071,7 @@ class Server(object):
""" """
headers = {} headers = {}
headers['X-API-Key'] = PDNS_API_KEY headers['X-API-Key'] = PDNS_API_KEY
try: try:
jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/%s/config' % self.server_id), headers=headers, method='GET') jdata = utils.fetch_json(urlparse.urljoin(PDNS_STATS_URL, API_EXTENDED_URL + '/servers/%s/config' % self.server_id), headers=headers, method='GET')
return jdata return jdata
@ -1146,12 +1147,12 @@ class Setting(db.Model):
self.id = id self.id = id
self.name = name self.name = name
self.value = value self.value = value
# allow database autoincrement to do its own ID assignments # allow database autoincrement to do its own ID assignments
def __init__(self, name=None, value=None): def __init__(self, name=None, value=None):
self.id = None self.id = None
self.name = name self.name = name
self.value = value self.value = value
def set_mainteance(self, mode): def set_mainteance(self, mode):
""" """
@ -1195,7 +1196,7 @@ class Setting(db.Model):
logging.debug(traceback.format_exec()) logging.debug(traceback.format_exec())
db.session.rollback() db.session.rollback()
return False return False
def set(self, setting, value): def set(self, setting, value):
setting = str(setting) setting = str(setting)
new_value = str(value) new_value = str(value)