def _PUT(self, *param, **params): user_id = param[0] if not validates_param_id(self, user_id): self.logger.debug("Failed to update account. the value of parameter is invalid.") return web.notfound(self.view.alert) if not validates_user(self): self.logger.debug("Failed to update account. the value of input is invalid.") return web.badrequest(self.view.alert) user = findby1(self.orm, user_id) if not user: self.logger.debug("Failed to update account. No such account - id=%s" % user_id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if int(user_id) != cmp_user.id: self.logger.debug("Failed to update account. The same mail address '%s' already exist - user='******'" % (self.input.email, cmp_user.nickname)) return web.conflict(web.ctx.path) user.nickname = self.input.nickname user.email = self.input.email user.languages = self.input.languages if not is_empty(self.input.new_password): if compare_password(self, user) == False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) user.password = password user.salt = salt update(self.orm, user) return web.seeother(web.ctx.path)
def _POST(self, *param, **params): if not validates_user(self): self.logger.debug("Failed to create account. the values of input are invalid.") return web.badrequest(self.view.alert) user = findby1email(self.orm, self.input.email) if user: self.logger.debug("Failed to create account. The same mail address '%s' already exist - user='******'" % (self.input.email, user.nickname)) return web.conflict(web.ctx.path) (password, salt) = sha1encrypt(self.input.new_password) new_user = new(self.input.email, password, salt, self.input.nickname, self.input.languages ) save(self.orm, new_user) return web.created(None)
def _PUT(self, *param, **params): user_id = param[0] if not validates_param_id(self, user_id): self.logger.debug( "Failed to update account. the value of parameter is invalid.") return web.notfound(self.view.alert) if not validates_user(self): self.logger.debug( "Failed to update account. the value of input is invalid.") return web.badrequest(self.view.alert) user = findby1(self.orm, user_id) if not user: self.logger.debug( "Failed to update account. No such account - id=%s" % user_id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if int(user_id) != cmp_user.id: self.logger.debug( "Failed to update account. The same mail address '%s' already exist - user='******'" % (self.input.email, cmp_user.nickname)) return web.conflict(web.ctx.path) user.nickname = self.input.nickname user.email = self.input.email user.languages = self.input.languages if not is_empty(self.input.new_password): if compare_password(self, user) == False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) user.password = password user.salt = salt update(self.orm, user) return web.seeother(web.ctx.path)
def _PUT(self, *param, **params): if not validates_me(self): return web.badrequest(self.view.alert) if self.me.id != int(self.input.id): self.logger.info("Update account is failed, " "posted ID parameter is different from me ID " "- posted ID %s, me ID %s" % (self.input.id, self.me.id)) return web.badrequest(_('ID is wrong. Your ID is not %s.') % self.input.id) me = findby1(self.orm, self.input.id) if not me: self.logger.debug("Update account is failed, " "Did not exist account - id=%s" % self.input.id) return web.notfound() cmp_user = findby1email(self.orm, self.input.email) if not cmp_user is None: if me.id != cmp_user.id: self.logger.info("Update account is failed, " "Already exists mail address " "- %s, %s" % (me, cmp_user)) return web.conflict(web.ctx.path) if self.input.password: if compare_password(self, self.me) is False: return web.badrequest(self.view.alert) (password, salt) = sha1encrypt(self.input.new_password) me.password = password me.salt = salt me.email = self.input.email me.languages = self.input.languages me.nickname = self.input.nickname dba_update(self.orm, me) self.me = me return web.seeother(web.ctx.path)
class Init(Rest): def _GET(self, *param, **params): self.view.database_bind = karesansui.config['database.bind'] self.view.default_locale = karesansui.config['application.default.locale'] self.view.locales = DEFAULT_LANGS.keys() if karesansui_database_exists() is True: return web.tempredirect("/", absolute=False) if self.is_mode_input(): return True else: return True return True def _POST(self, *param, **params): if not validates_user(self): return web.badrequest(self.view.alert) engine = get_engine() metadata = get_metadata() session = get_session() try: metadata.drop_all() metadata.tables['machine2jobgroup'].create() metadata.create_all() except Exception, e: traceback.format_exc() raise Exception('Initializing/Updating a database error - %s' % ''.join(e.args)) (password, salt) = sha1encrypt(self.input.password) user = User(u"%s" % self.input.email, unicode(password), unicode(salt), u"%s" % self.input.nickname, u"%s" % self.input.languages, ) session.add(user) session.commit() # Tag Table set. tag = Tag(u"default") session.add(tag) session.commit() # Machine Table set. #user = session.query(User).filter(User.email == self.input.email).first() uuid = string_from_uuid(generate_uuid()) fqdn = socket.gethostname() notebook = Notebook(u"", u"") machine = Machine(user, user, u"%s" % uuid, u"%s" % fqdn, MACHINE_ATTRIBUTE['HOST'], MACHINE_HYPERVISOR['REAL'], notebook, [tag], u"%s" % fqdn, u'icon-guest1.png', False, None, ) session.add(machine) session.commit() session.close() return web.created(None)
#sys.exit() engine = get_engine() metadata = get_metadata() try: metadata.drop_all() metadata.tables['machine2jobgroup'].create() metadata.create_all() except Exception, e: traceback.format_exc() raise Exception('Initializing/Updating a database error - %s' % ''.join(e.args)) session = get_session() try: (password, salt) = sha1encrypt(u"%s" % password) user = session.query(User).filter(User.email == email).first() if user is None: # User Table set. new_user = User(u"%s" % email, unicode(password), unicode(salt), u"Administrator", u"%s" % lang, ) if string.atof(sqlalchemy.__version__[0:3]) >= 0.6: session.add(new_user) else: session.save(new_user)
convert_unicode=True, #assert_unicode='warn', # DEBUG echo=True, echo_pool=False ) metadata = sqlalchemy.MetaData(bind=engine) reload_mapper(metadata) metadata.drop_all() metadata.create_all() Session = sqlalchemy.orm.sessionmaker(bind=engine, autoflush=False) session = Session() # INSERT from karesansui.lib.crypt import sha1encrypt (password, salt) = sha1encrypt(u'password') from karesansui.lib.utils import uni_force _m_u = User(u'hoge@localhost', uni_force(password), unicode(salt, 'utf-8'), u'ja', u'ja_JP') session.add(_m_u) session.commit() # SELECT One u = session.query(User).filter(User.email == u'hoge@localhost').one() print _m_u.__repr__() # UPDATE _m_u.email = u'foo@localhost' session.add(_m_u) session.commit() # DELETE _m_u = session.query(User).filter(User.email == u'foo@localhost').one() session.delete(_m_u)