Hello
There is a program in which the login frame and the main window of the program. I can’t figure out how to pass the request def adoffice the conn value from the Connectad class to the class
class App(Tk):
def __init__(self):
Tk.__init__(self)
self._frame = None
self.switch_frame(Connectad)
def switch_frame(self, frame_class):
"""Destroys current frame and replaces it with a new one."""
new_frame = frame_class(self)
if self._frame is not None:
self._frame.destroy()
self._frame = new_frame
self._frame.pack()
class Connectad(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
Label(self,text='Логин:').grid(row=0)
self.login_field = Entry(self,width=30)
self.login_field.grid(row=0,column=1)
Label(self,text='Пароль:').grid(row=1)
self.pw_field = Entry(self,show = '*', width=30)
self.pw_field.grid(row=1,column=1)
self.notlogon = Label(self,text='Не верный логин или пароль',fg='red', font=("Arial", 14))
self.notlogon.grid_remove
send_button = Button(self, text='Отправить',bg='#4a7abc',
fg='yellow',
activebackground='green',
activeforeground='white',command=self.connad).grid(row=3, column=2, sticky=W, pady=4)
def validationlogin(self): #Проверка логина и пароля на пустые поля
pw = self.pw_field.get()
login = self.login_field.get()
msg = ''
if len(pw) == 0 or len(login) == 0:
msg = 'Логин или пароль не могут быть пустыми'
mb.showinfo('message', msg)
else:
self.connad()
def connad(self): # Подключение к AD
self.AD_SERVER = 'dc.world.local'
self.AD_USER = self.login_field.get()
self.AD_PASSWORD = self.pw_field.get()
self.AD_SEARCH_TREE = 'dc=world,dc=local'
self.AD_SEARCH_OU = 'ou=users,dc=world,local'
self.server = Server(self.AD_SERVER, use_ssl=True)
self.conn = Connection(self.server,user=self.AD_USER,password=self.AD_PASSWORD)
print(self.conn.bind)
if not self.conn.bind():
print('False')
elif self.conn.bind():
self.parent.switch_frame(Query)
class Query(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
#self.wind = window
#self.wind.title('Создание пользователя')
self.message = Label(self,text = '', fg = 'green').grid(row = 3, column = 0, columnspan = 2, sticky = W + E)
# таблица слов и значений
columns = ("#1", "#2", "#3", "#4", "#5")
self.tree = ttk.Treeview(self,height = 20, columns = columns)
self.tree.grid(row = 4, column = 0, columnspan = 4)
self.tree.heading('#0', text = 'Фамилия', anchor = CENTER)
self.tree.heading('#1', text = 'Имя', anchor = CENTER)
self.tree.heading('#2', text = 'Отчество', anchor = CENTER)
self.tree.heading('#3', text = 'Логин', anchor = CENTER)
self.tree.heading('#4', text = 'Отдел', anchor = CENTER)
self.tree.heading('#5', text = 'Заявитель', anchor = CENTER)
# кнопки редактирования записей
ttk.Button(self,text = 'Принять', command = self.adoffice).grid(row = 5, column = 2, sticky = W + E)
ttk.Button(self,text = 'Изменить', command = self.edit_word).grid(row = 5, column = 1, sticky = W + E)
ttk.Button(self,text = 'Обновить', command = self.get_words).grid(row = 5, column = 0, sticky = W + E)
#ttk.Button(text = 'Выход', command = self.wind.destroy).grid(row = 5, column = 3, sticky = W + E)
# заполнение таблицы
self.get_words()
def mailoffice(self,cn,otdel): # Создание почты Офиса
psconn = subprocess.run(['powershell', '$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://mail01/PowerShell/ -Authentication Kerberos;', 'Import-PSSession $Session -DisableNameChecking;',f'Enable-Mailbox -Identity {cn} -Database BykIT2021;', 'Remove-PSSession $Session;'])
def adoffice(self): # Создание пользователя офис
famaly = self.tree.item(self.tree.selection())['text']
nameuser = self.tree.item(self.tree.selection())['values'][0]
nameuser2 = self.tree.item(self.tree.selection())['values'][1]
cn = self.tree.item(self.tree.selection())['values'][2]
otdel = self.tree.item(self.tree.selection())['values'][3]
conn.search('DC=world,DC=local',f'(&(objectclass=user)(sAMAccountName={cn}))', attributes=['sAMAccountName'])
print(conn.entries)
if not conn.entries:
conn.add(f'CN={famaly} {nameuser} {nameuser2},OU={otdel},OU=USERS,DC=world,DC=local', 'user',
{'cn': f'{famaly} {nameuser} {nameuser2}',
'givenName': {nameuser},
'sn': {famaly},
'department': {otdel},
'company': 'world',
'initials': {nameuser2[0]},
'info': {gp.genpass},
'displayName': f'{famaly} {nameuser} {nameuser2}',
'sAMAccountName': {cn},
'userPrincipalName': f'{cn}@world.local',
f'telephoneNumber': '0001'
})
conn.extend.microsoft.unlock_account(f'CN={famaly} {nameuser} {nameuser2},OU={otdel},OU=USERS,DC=world,DC=local')
conn.extend.microsoft.modify_password(f'CN={famaly} {nameuser} {nameuser2},OU={otdel},OU=USERS,DC=world,DC=local', new_password=gp.genpass, old_password=None)
conn.modify(f'CN={famaly} {nameuser} {nameuser2},OU={otdel},OU=Users,DC=world,DC=local', {'userAccountControl': (MODIFY_REPLACE, [512])})
self.mailoffice(cn,otdel)
self.close_request(cn)