Exemplo n.º 1
0
	def update_info(self, client, user, token):

		unit = Unit.get_by_user_key(self.current_user.key)
		if unit is None:
			unit = Unit(id='en', parent=self.current_user.key)
			logging.info('Create new Unit')

		try:
			user_store = client.get_user_store()
			en_user = user_store.getUser()
		except (EDAMUserException, EDAMSystemException) as e:
			logging.error('Evernote Error: %s %s, parm: %s' % (str(e.errorCode), EDAMErrorCode._VALUES_TO_NAMES[e.errorCode], e.parameter))
			return False

		unit.token = token
		unit.username = en_user.username
		unit.user_id = en_user.id

		#generate an initial id for the unit if alias is already used
		unit.alias = unit.username.lower()
		x = 0
		while Unit.query(Unit.alias==unit.alias).count(1) > 0 or helper.is_reserved_name(unit.alias):
			
			if x >= 10:
				logging.info('Failed to generate valid alias.')
				return False

			unit.alias = helper.code_generator(size=8, chars=string.ascii_lowercase + string.digits)
			x += 1
				
		logging.info('Generated alias is %s' % unit.alias)

		""" save Unit """
		try:
			unit.put()
		except:
			return False

		# update user information
		if user.en_name != en_user.name:
			user.en_name = en_user.name
			user.put()

		return True