self.models = []
self.options = {
'views': [],
'buttons': [],
'link_locations': []
}
self.actions = []
self.security_rules = []
self.manifest_data = {}
self.fields_data = {}
self.init_ui()
def init_ui(self):
# Canvas and scrollbar setup
self.canvas = tk.Canvas(self.root)
self.scrollbar = ttk.Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
self.scrollable_frame.bind(
"<Configure>",
lambda e: self.canvas.configure(
scrollregion=self.canvas.bbox("all")
)
)
self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.grid(row=0, column=0, sticky="nsew")
self.scrollbar.grid(row=0, column=1, sticky="ns")
# Frame per il modulo e il percorso
frame = ttk.Frame(self.scrollable_frame, padding="10")
frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
# Nome del modulo
ttk.Label(frame, text="Nome del modulo:").grid(column=0, row=0, sticky=tk.W)
self.module_name = ttk.Entry(frame, width=40)
self.module_name.grid(column=1, row=0, sticky=(tk.W, tk.E))
self.module_name.bind("<FocusOut>", self.validate_field)
# Tooltip e istruzioni
ttk.Label(frame, text="Inserisci il nome del tuo modulo.", foreground="grey").grid(column=2, row=0, sticky=tk.W)
# Percorso del modulo
ttk.Label(frame, text="Percorso del modulo:").grid(column=0, row=1, sticky=tk.W)
self.module_path = ttk.Entry(frame, width=40)
self.module_path.grid(column=1, row=1, sticky=(tk.W, tk.E))
self.module_path.bind("<FocusOut>", self.validate_field)
ttk.Button(frame, text="Sfoglia...", command=self.browse_path).grid(column=2, row=1, sticky=tk.E)
# Tooltip e istruzioni
ttk.Label(frame, text="Seleziona il percorso in cui il modulo sarà generato.", foreground="grey").grid(column=2, row=2, sticky=tk.W)
# Import da Excel
self.use_excel = tk.BooleanVar()
ttk.Checkbutton(frame, text="Importa da Excel", variable=self.use_excel, command=self.toggle_excel).grid(column=0, row=3, sticky=tk.W)
# Percorso del file Excel
self.excel_path = ttk.Entry(frame, width=40)
self.excel_path.grid(column=1, row=3, sticky=(tk.W, tk.E))
self.excel_browse_btn = ttk.Button(frame, text="Sfoglia...", command=self.browse_excel)
self.excel_browse_btn.grid(column=2, row=3, sticky=tk.E)
# Tooltip e istruzioni
ttk.Label(frame, text="Seleziona un file Excel contenente le informazioni su tabelle e campi.", foreground="grey").grid(column=2, row=4, sticky=tk.W)
# Sezione per definire le colonne Excel
frame_excel_columns = ttk.Frame(self.scrollable_frame, padding="10")
frame_excel_columns.grid(row=4, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(frame_excel_columns, text="Configurazione delle colonne del file Excel", foreground="grey").grid(column=0, row=0, columnspan=4, sticky=tk.W)
ttk.Label(frame_excel_columns, text="Colonna Tabella/Modulo:").grid(column=0, row=1, sticky=tk.W)
self.excel_col_table = ttk.Entry(frame_excel_columns, width=20)
self.excel_col_table.grid(column=1, row=1, sticky=tk.W)
ttk.Label(frame_excel_columns, text="Colonna Campo/Field:").grid(column=2, row=1, sticky=tk.W)
self.excel_col_field = ttk.Entry(frame_excel_columns, width=20)
self.excel_col_field.grid(column=3, row=1, sticky=tk.W)
ttk.Button(frame_excel_columns, text="Importa", command=self.load_excel_data).grid(column=0, row=2, columnspan=4, sticky=tk.W)
# Manifest Data
manifest_frame = ttk.Labelframe(self.scrollable_frame, text="Dati Manifesto", padding="10")
manifest_frame.grid(row=5, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(manifest_frame, text="Versione:").grid(column=0, row=0, sticky=tk.W)
self.manifest_version = ttk.Entry(manifest_frame, width=20)
self.manifest_version.grid(column=1, row=0, sticky=tk.W)
ttk.Label(manifest_frame, text="Sommario:").grid(column=0, row=1, sticky=tk.W)
self.manifest_summary = ttk.Entry(manifest_frame, width=20)
self.manifest_summary.grid(column=1, row=1, sticky=tk.W)
ttk.Label(manifest_frame, text="Categoria:").grid(column=0, row=2, sticky=tk.W)
self.manifest_category = ttk.Entry(manifest_frame, width=20)
self.manifest_category.grid(column=1, row=2, sticky=tk.W)
ttk.Label(manifest_frame, text="Autore:").grid(column=0, row=3, sticky=tk.W)
self.manifest_author = ttk.Entry(manifest_frame, width=20)
self.manifest_author.grid(column=1, row=3, sticky=tk.W)
ttk.Label(manifest_frame, text="Sito Web:").grid(column=0, row=4, sticky=tk.W)
self.manifest_website = ttk.Entry(manifest_frame, width=20)
self.manifest_website.grid(column=1, row=4, sticky=tk.W)
# Caricamento dei Dati dei Modelli e dei Campi da JSON
ttk.Button(manifest_frame, text="Carica JSON Modelli e Campi", command=self.load_field_data).grid(column=2, row=0, rowspan=5, sticky=tk.W)
# Frame per la gestione dei menu
menu_frame = ttk.Labelframe(self.scrollable_frame, text="Gestione Menu", padding="10")
menu_frame.grid(row=13, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(menu_frame, text="Gestisci i menu del modulo").grid(column=0, row=0, columnspan=3, sticky=tk.W)
self.menus_listbox = tk.Listbox(menu_frame, height=8)
self.menus_listbox.grid(column=0, row=1, columnspan=3, sticky=(tk.W, tk.E))
ttk.Button(menu_frame, text="Aggiungi Menu", command=self.add_menu).grid(column=0, row=2, sticky=tk.W)
ttk.Button(menu_frame, text="Modifica Menu", command=self.edit_menu).grid(column=1, row=2, sticky=tk.W)
ttk.Button(menu_frame, text="Cancella Menu", command=self.delete_menu).grid(column=2, row=2, sticky=tk.W)
# Frame per i modelli
models_frame = ttk.Labelframe(self.scrollable_frame, text="Modelli", padding="10")
models_frame.grid(row=6, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(models_frame, text="Elenco dei modelli e gestione dei campi. Puoi aggiungere, modificare e cancellare modelli.", foreground="grey").grid(column=0, row=0, columnspan=3, sticky=tk.W)
# Lista dei modelli
self.models_listbox = tk.Listbox(models_frame, height=8)
self.models_listbox.grid(column=0, row=1, columnspan=3, sticky=(tk.W, tk.E))
ttk.Button(models_frame, text="Aggiungi Modello", command=self.add_model).grid(column=0, row=2, sticky=tk.W)
ttk.Button(models_frame, text="Modifica Modello", command=self.edit_model).grid(column=1, row=2, sticky=tk.W)
ttk.Button(models_frame, text="Cancella Modello", command=self.delete_model).grid(column=2, row=2, sticky=tk.W)
# Frame per le opzioni
options_frame = ttk.Labelframe(self.scrollable_frame, text="Opzioni", padding="10")
options_frame.grid(row=7, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(options_frame, text="Seleziona i tipi di viste che vuoi includere nel modulo. Puoi anche aggiungere bottoni personalizzati.", foreground="grey").grid(column=0, row=0, columnspan=6, sticky=tk.W)
self.view_types = ["form", "tree", "search", "kanban", "calendar", "graph"]
self.view_vars = {view: tk.BooleanVar() for view in self.view_types}
for i, view in enumerate(self.view_types):
ttk.Checkbutton(options_frame, text=view.capitalize(), variable=self.view_vars[view]).grid(column=i, row=1, sticky=tk.W)
# Frame per i bottoni personalizzati
buttons_frame = ttk.Labelframe(self.scrollable_frame, text="Bottoni Personalizzati", padding="10")
buttons_frame.grid(row=8, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(buttons_frame, text="Gestisci i bottoni personalizzati per le viste.", foreground="grey").grid(column=0, row=0, columnspan=3, sticky=tk.W)
ttk.Button(buttons_frame, text="Aggiungi Bottone", command=self.add_button).grid(column=0, row=1, sticky=tk.W)
ttk.Button(buttons_frame, text="Modifica Bottone", command=self.edit_button).grid(column=1, row=1, sticky=tk.W)
ttk.Button(buttons_frame, text="Cancella Bottone", command=self.delete_button).grid(column=2, row=1, sticky=tk.W)
self.buttons_listbox = tk.Listbox(buttons_frame, height=4)
self.buttons_listbox.grid(column=0, row=2, columnspan=3, sticky=(tk.W, tk.E))
# Frame per il collegamento dei moduli
link_frame = ttk.Labelframe(self.scrollable_frame, text="Collegamenti", padding="10")
link_frame.grid(row=9, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(link_frame, text="Specifica dove collegare il nuovo modulo nelle viste esistenti.", foreground="grey").grid(column=0, row=0, columnspan=3, sticky=tk.W)
ttk.Button(link_frame, text="Aggiungi Collegamento", command=self.add_link).grid(column=0, row=1, sticky=tk.W)
ttk.Button(link_frame, text="Modifica Collegamento", command=self.edit_link).grid(column=1, row=1, sticky=tk.W)
ttk.Button(link_frame, text="Cancella Collegamento", command=self.delete_link).grid(column=2, row=1, sticky=tk.W)
self.links_listbox = tk.Listbox(link_frame, height=4)
self.links_listbox.grid(column=0, row=2, columnspan=3, sticky=(tk.W, tk.E))
# Frame per azioni personalizzate
actions_frame = ttk.Labelframe(self.scrollable_frame, text="Azioni Personalizzate", padding="10")
actions_frame.grid(row=10, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(actions_frame, text="Definisci azioni personalizzate per il modulo.", foreground="grey").grid(column=0, row=0, columnspan=3, sticky=tk.W)
ttk.Button(actions_frame, text="Aggiungi Azione", command=self.add_action).grid(column=0, row=1, sticky=tk.W)
ttk.Button(actions_frame, text="Modifica Azione", command=self.edit_action).grid(column=1, row=1, sticky=tk.W)
ttk.Button(actions_frame, text="Cancella Azione", command=self.delete_action).grid(column=2, row=1, sticky=tk.W)
self.actions_listbox = tk.Listbox(actions_frame, height=4)
self.actions_listbox.grid(column=0, row=2, columnspan=3, sticky=(tk.W, tk.E))
# Frame per regole di sicurezza
security_frame = ttk.Labelframe(self.scrollable_frame, text="Regole di Sicurezza", padding="10")
security_frame.grid(row=11, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Label(security_frame, text="Gestisci le regole di sicurezza per il modulo.", foreground="grey").grid(column=0, row=0, columnspan=3, sticky=tk.W)
ttk.Button(security_frame, text="Aggiungi Regola", command=self.add_security_rule).grid(column=0, row=1, sticky=tk.W)
ttk.Button(security_frame, text="Modifica Regola", command=self.edit_security_rule).grid(column=1, row=1, sticky=tk.W)
ttk.Button(security_frame, text="Cancella Regola", command=self.delete_security_rule).grid(column=2, row=1, sticky=tk.W)
self.security_listbox = tk.Listbox(security_frame, height=4)
self.security_listbox.grid(column=0, row=2, columnspan=3, sticky=(tk.W, tk.E))
# Frame per pulsanti di azione
action_frame = ttk.Frame(self.scrollable_frame, padding="10")
action_frame.grid(row=12, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
ttk.Button(action_frame, text="Genera Modulo", command=self.generate_module).grid(column=0, row=0, sticky=tk.W)
ttk.Button(action_frame, text="Salva Configurazione", command=self.save_config).grid(column=1, row=0, sticky=tk.W)
ttk.Button(action_frame, text="Carica Configurazione", command=self.load_config).grid(column=2, row=0, sticky=tk.W)
def browse_path(self):
path = filedialog.askdirectory()
if path:
self.module_path.delete(0, tk.END)
self.module_path.insert(0, path)
def browse_excel(self):
path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xlsx *.xls")])
if path:
self.excel_path.delete(0, tk.END)
self.excel_path.insert(0, path)
self.load_excel_data()
def toggle_excel(self):
state = tk.NORMAL if self.use_excel.get() else tk.DISABLED
self.excel_path.config(state=state)
self.excel_browse_btn.config(state=state)
self.excel_col_table.config(state=state)
self.excel_col_field.config(state=state)
def load_excel_data(self):
if not self.excel_path.get() or not self.excel_col_table.get() or not self.excel_col_field.get():
messagebox.showerror("Errore", "Per favore completa il percorso del file Excel e le colonne.")
return
try:
df = pd.read_excel(self.excel_path.get())
table_col = self.excel_col_table.get()
field_col = self.excel_col_field.get()
model_names = df[table_col].unique().tolist()
self.models = []
self.models_listbox.delete(0, tk.END)
for model_name in model_names:
model_fields = df[df[table_col] == model_name][field_col].tolist()
model_fields_tuples = [(field, 'Char', field.split('_')[1].capitalize(), False, False, '') for field in model_fields] # Default values for tuples
self.models.append({"name": model_name, "fields": model_fields_tuples})
self.models_listbox.insert(tk.END, f"{model_name} ({len(model_fields)} campi)")
except Exception as e:
messagebox.showerror("Errore", f"Errore durante l'importazione da Excel: {str(e)}")
def load_field_data(self):
path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if path:
try:
with open(path, 'r') as file:
self.fields_data = json.load(file)
except json.JSONDecodeError as ex:
messagebox.showerror("Errore", f"Errore durante il caricamento del JSON: {str(ex)}")
def add_field_row(self, frame_fields, fields_entries, field_data=None):
order = len(fields_entries)
row = order + 1
field_name = ttk.Entry(frame_fields)
field_name.grid(row=row, column=0, sticky=tk.W)
if field_data:
field_name.insert(0, field_data[0])
field_type = ttk.Combobox(frame_fields, values=[
"Binary", "Boolean", "Char", "Date", "Datetime", "Float", "HTML",
"Integer", "JSON", "Many2many", "Many2one", "Many2one Reference",
"Monetary", "One2many", "Properties", "Properties Definition",
"Reference", "Selection", "Text"
])
field_type.grid(row=row, column=1, sticky=tk.W)
if field_data:
field_type.set(field_data[1])
field_label = ttk.Entry(frame_fields)
field_label.grid(row=row, column=2, sticky=tk.W)
if field_data:
field_label.insert(0, field_data[2])
mandatory_var = tk.BooleanVar()
field_mandatory = ttk.Checkbutton(frame_fields, variable=mandatory_var)
field_mandatory.grid(row=row, column=3, sticky=tk.W)
if field_data:
mandatory_var.set(field_data[3])
primary_key_var = tk.BooleanVar()
field_primary_key = ttk.Checkbutton(frame_fields, variable=primary_key_var)
field_primary_key.grid(row=row, column=4, sticky=tk.W)
if field_data:
primary_key_var.set(field_data[4])
field_related = ttk.Combobox(frame_fields, values=list(self.fields_data.keys()) + [''])
field_related.grid(row=row, column=5, sticky=tk.W)
if field_data:
field_related.set(field_data[5])
# Definizione e posizionamento di field_inverse se necessario
field_inverse = ttk.Combobox(frame_fields, values=[f for fields in self.fields_data.values() for f in fields] + [''])
if field_data and len(field_data) > 6:
field_inverse.set(field_data[6])
field_inverse.grid(row=row, column=6, sticky=tk.W)
def move_up():
index = fields_entries.index(field_entry)
if index > 0:
fields_entries.insert(index - 1, fields_entries.pop(index))
self.reorder_fields(frame_fields, fields_entries)
def move_down():
index = fields_entries.index(field_entry)
if index < len(fields_entries) - 1:
fields_entries.insert(index + 1, fields_entries.pop(index))
self.reorder_fields(frame_fields, fields_entries)
btn_up = ttk.Button(frame_fields, text="↑", command=move_up)
btn_up.grid(row=row, column=7, sticky=tk.W)
btn_down = ttk.Button(frame_fields, text="↓", command=move_down)
btn_down.grid(row=row, column=8, sticky=tk.W)
order_label = ttk.Label(frame_fields, text=f"{order + 1}")
order_label.grid(row=row, column=9, sticky=tk.W)
# Memorizzare le variabili anziché i widget Checkbutton diretti
field_entry = (field_name, field_type, field_label, mandatory_var, primary_key_var, field_related, field_inverse, btn_up, btn_down, order_label)
fields_entries.append(field_entry)
def reorder_fields(self, frame_fields, fields_entries):
for index, (field_name, field_type, field_label, field_mandatory, field_primary_key, field_related, field_inverse, btn_up, btn_down, order_label) in enumerate(fields_entries):
row = index + 1
field_name.grid(row=row, column=0, sticky=tk.W)
field_type.grid(row=row, column=1, sticky=tk.W)
field_label.grid(row=row, column=2, sticky=tk.W)
field_mandatory.grid(row=row, column=3, sticky=tk.W)
field_primary_key.grid(row=row, column=4, sticky=tk.W)
field_related.grid(row=row, column=5, sticky=tk.W)
if field_inverse:
field_inverse.grid(row=row, column=6, sticky=tk.W)
btn_up.grid(row=row, column=7, sticky=tk.W)
btn_down.grid(row=row, column=8, sticky=tk.W)
order_label.config(text=f"{index + 1}")
order_label.grid(row=row, column=9, sticky=tk.W)
def add_menu(self):
add_menu_dialog = tk.Toplevel(self.root)
add_menu_dialog.title("Aggiungi Menu")
ttk.Label(add_menu_dialog, text="Nome del Menu:").grid(row=0, column=0, sticky=tk.W)
menu_name_entry = ttk.Entry(add_menu_dialog, width=20)
menu_name_entry.grid(row=0, column=1, sticky=(tk.W, tk.E))
ttk.Label(add_menu_dialog, text="ID del Menu:").grid(row=1, column=0, sticky=tk.W)
menu_id_entry = ttk.Entry(add_menu_dialog, width=20)
menu_id_entry.grid(row=1, column=1, sticky=(tk.W, tk.E))
ttk.Label(add_menu_dialog, text="ID del Menu Padre (opzionale):").grid(row=2, column=0, sticky=tk.W)
parent_menu_id_entry = ttk.Entry(add_menu_dialog, width=20)
parent_menu_id_entry.grid(row=2, column=1, sticky=(tk.W, tk.E))
ttk.Label(add_menu_dialog, text="Azione (ID XML):").grid(row=3, column=0, sticky=tk.W)
action_id_entry = ttk.Entry(add_menu_dialog, width=20)
action_id_entry.grid(row=3, column=1, sticky=(tk.W, tk.E))
def save_menu():
menu_name = menu_name_entry.get()
menu_id = menu_id_entry.get()
parent_menu_id = parent_menu_id_entry.get()
action_id = action_id_entry.get()
if not menu_name or not menu_id or not action_id:
messagebox.showerror("Errore", "Per favore compila tutti i campi obbligatori.")
return
new_menu = {
"name": menu_name,
"id": menu_id,
"parent_id": parent_menu_id,
"action": action_id
}
# Aggiungi il nuovo menu alla lista dei menu
self.options.get('menus', []).append(new_menu)
self.menus_listbox.insert(tk.END, f"{menu_name}")
add_menu_dialog.destroy()
ttk.Button(add_menu_dialog, text="Salva Menu", command=save_menu).grid(row=4, column=0, columnspan=2, sticky=(tk.W, tk.E))
def edit_menu(self):
selection = self.menus_listbox.curselection()
if not selection:
return
index = selection[0]
menu = self.options.get('menus', [])[index]
edit_menu_dialog = tk.Toplevel(self.root)
edit_menu_dialog.title("Modifica Menu")
ttk.Label(edit_menu_dialog, text="Nome del Menu:").grid(row=0, column=0, sticky=tk.W)
menu_name_entry = ttk.Entry(edit_menu_dialog, width=20)
menu_name_entry.grid(row=0, column=1, sticky=(tk.W, tk.E))
menu_name_entry.insert(0, menu["name"])
ttk.Label(edit_menu_dialog, text="ID del Menu:").grid(row=1, column=0, sticky=tk.W)
menu_id_entry = ttk.Entry(edit_menu_dialog, width=20)
menu_id_entry.grid(row=1, column=1, sticky=(tk.W, tk.E))
menu_id_entry.insert(0, menu["id"])
ttk.Label(edit_menu_dialog, text="ID del Menu Padre (opzionale):").grid(row=2, column=0, sticky=tk.W)
parent_menu_id_entry = ttk.Entry(edit_menu_dialog, width=20)
parent_menu_id_entry.grid(row=2, column=1, sticky=(tk.W, tk.E))
parent_menu_id_entry.insert(0, menu["parent_id"])
ttk.Label(edit_menu_dialog, text="Azione (ID XML):").grid(row=3, column=0, sticky=tk.W)
action_id_entry = ttk.Entry(edit_menu_dialog, width=20)
action_id_entry.grid(row=3, column=1, sticky=(tk.W, tk.E))
action_id_entry.insert(0, menu["action"])
def save_menu():
menu_name = menu_name_entry.get()
menu_id = menu_id_entry.get()
parent_menu_id = parent_menu_id_entry.get()
action_id = action_id_entry.get()
if not menu_name or not menu_id or not action_id:
messagebox.showerror("Errore", "Per favore compila tutti i campi obbligatori.")
return
updated_menu = {
"name": menu_name,
"id": menu_id,
"parent_id": parent_menu_id,
"action": action_id
}
self.options.get('menus', [])[index] = updated_menu
self.menus_listbox.delete(index)
self.menus_listbox.insert(index, f"{menu_name}")
edit_menu_dialog.destroy()
ttk.Button(edit_menu_dialog, text="Salva Menu", command=save_menu).grid(row=4, column=0, columnspan=2, sticky=(tk.W, tk.E))
def delete_menu(self):
selection = self.menus_listbox.curselection()
if not selection:
return
index = selection[0]
self.menus_listbox.delete(index)
del self.options.get('menus', [])[index]
def add_model(self):
add_model_dialog = tk.Toplevel(self.root)
add_model_dialog.title("Aggiungi Modello")
ttk.Label(add_model_dialog, text="Nome del modello:").grid(row=0, column=0, sticky=tk.W)
model_name_entry = ttk.Entry(add_model_dialog, width=20)
model_name_entry.grid(row=0, column=1, sticky=(tk.W, tk.E))
ttk.Label(add_model_dialog, text="Inherit (opzionale):").grid(row=1, column=0, sticky=tk.W)
inherit_entry = ttk.Entry(add_model_dialog, width=20)
inherit_entry.grid(row=1, column=1, sticky=(tk.W, tk.E))
columns = ["Nome Campo", "Tipo Campo", "Etichetta", "Obbligatorio", "Chiave Primaria", "Modello Correlato", "Campo Relazionato"]
frame_fields = ttk.Frame(add_model_dialog)
frame_fields.grid(row=2, column=0, columnspan=2, sticky="nsew")
for i, col in enumerate(columns):
ttk.Label(frame_fields, text=col).grid(row=0, column=i, sticky=tk.W)
fields_entries = []
ttk.Button(add_model_dialog, text="Aggiungi Campo", command=lambda: self.add_field_row(frame_fields, fields_entries)).grid(row=3, column=0, columnspan=2, sticky=tk.W)
def save_model():
model_name = model_name_entry.get().strip()
if not model_name:
messagebox.showerror("Errore", "Il nome del modello non può essere vuoto.")
return
inherit_model = inherit_entry.get().strip() if inherit_entry.get().strip() else None
model_fields = []
for field_name, field_type, field_label, field_mandatory, field_primary_key, field_related, field_inverse, btn_up, btn_down, order_label in fields_entries:
name = field_name.get().strip()
ftype = field_type.get().strip()
label = field_label.get().strip()
if not name:
messagebox.showerror("Errore", "Il nome del campo non può essere vuoto.")
return
if not name.isidentifier():
messagebox.showerror("Errore", f"Nome del campo '{name}' in un formato errato.")
return
# Se stanno usando il virgola, o white space in nome del campo, e.g.: "nome campo"
if " " in name or "," in name:
messagebox.showerror("Errore", f"Nome del campo '{name}' in un formato errato.")
return
mandatory = field_mandatory.get()
primary_key = field_primary_key.get()
related = field_related.get().strip() if ftype in ['Many2one', 'One2many', 'Many2many'] else ''
inverse = field_inverse.get().strip() if ftype == 'One2many' else ''
model_fields.append((name, ftype, label, mandatory, primary_key, related, inverse))
new_model = {"name": model_name, "fields": model_fields}
if inherit_model:
new_model['inherit'] = inherit_model
# Aggiorna la lista dei modelli
if any(model['name'] == model_name for model in self.models):
index = next(i for i, model in enumerate(self.models) if model['name'] == model_name)
self.models[index] = new_model
else:
self.models.append(new_model)
self.models_listbox.delete(0, tk.END)
for model in self.models:
self.models_listbox.insert(tk.END, f"{model['name']} ({len(model['fields'])} campi)")
ttk.Button(add_model_dialog, text="Salva Modello", command=save_model).grid(row=4, column=0, columnspan=2, sticky=(tk.W, tk.E))
def edit_model(self):
selection = self.models_listbox.curselection()
if not selection:
return
index = selection[0]
model = self.models[index]
edit_model_dialog = tk.Toplevel(self.root)
edit_model_dialog.title("Modifica Modello")
ttk.Label(edit_model_dialog, text="Nome del modello:").grid(row=0, column=0, sticky=tk.W)
model_name_entry = ttk.Entry(edit_model_dialog, width=20)
model_name_entry.grid(row=0, column=1, sticky=(tk.W, tk.E))
model_name_entry.insert(0, model["name"])
ttk.Label(edit_model_dialog, text="Inherit (opzionale):").grid(row=1, column=0, sticky=tk.W)
inherit_entry = ttk.Entry(edit_model_dialog, width=20)
inherit_entry.grid(row=1, column=1, sticky=(tk.W, tk.E))
if 'inherit' in model:
inherit_entry.insert(0, model['inherit'])
columns = ["Nome Campo", "Tipo Campo", "Etichetta", "Obbligatorio", "Chiave Primaria", "Modello Correlato", "Campo Relazionato"]
frame_fields = ttk.Frame(edit_model_dialog)
frame_fields.grid(row=2, column=0, columnspan=2, sticky="nsew")
for i, col in enumerate(columns):
ttk.Label(frame_fields, text=col).grid(row=0, column=i, sticky=tk.W)
fields_entries = []
def add_field_row(field_data=None):
row = len(fields_entries) + 1
field_name = ttk.Entry(frame_fields)
field_name.grid(row=row, column=0, sticky=tk.W)
if field_data:
field_name.insert(0, field_data[0])
field_type = ttk.Combobox(frame_fields, values=[
"Binary", "Boolean", "Char", "Date", "Datetime", "Float", "HTML",
"Integer", "JSON", "Many2many", "Many2one", "Many2one Reference",
"Monetary", "One2many", "Properties", "Properties Definition",
"Reference", "Selection", "Text"
])
field_type.grid(row=row, column=1, sticky=tk.W)
if field_data:
field_type.set(field_data[1])
field_label = ttk.Entry(frame_fields)
field_label.grid(row=row, column=2, sticky=tk.W)
if field_data:
field_label.insert(0, field_data[2])
mandatory_var = tk.BooleanVar()
field_mandatory = ttk.Checkbutton(frame_fields, variable=mandatory_var)
field_mandatory.grid(row=row, column=3, sticky=tk.W)
if field_data:
mandatory_var.set(field_data[3])
primary_key_var = tk.BooleanVar()
field_primary_key = ttk.Checkbutton(frame_fields, variable=primary_key_var)
field_primary_key.grid(row=row, column=4, sticky=tk.W)
if field_data:
primary_key_var.set(field_data[4])
field_related = ttk.Combobox(frame_fields, values=list(self.fields_data.keys()) + [''])
field_related.grid(row=row, column=5, sticky=tk.W)
if field_data:
field_related.set(field_data[5])
field_inverse = ttk.Combobox(frame_fields, values=[field for model_fields in self.fields_data.values() for field in model_fields] + [''])
field_inverse.grid(row=row, column=6, sticky=tk.W)
if field_data and len(field_data) > 6:
field_inverse.set(field_data[6])
fields_entries.append((field_name, field_type, field_label, mandatory_var, primary_key_var, field_related, field_inverse))
for field in model["fields"]:
add_field_row(field)
def delete_field_row():
selection = list(self.models_listbox.curselection())
if selection:
i = selection[-1] # Prendi l'ultimo elemento selezionato
entries = fields_entries.pop(i)
for widget in entries:
if hasattr(widget, 'grid_forget'):
widget[0].grid_forget()
widget[1].grid_forget()
widget[2].grid_forget()
widget[3].grid_forget()
widget[4].grid_forget()
widget[5].grid_forget()
widget[6].grid_forget()
def save_model():
model_name = model_name_entry.get()
if not model_name:
messagebox.showerror("Errore", "Per favore inserisci un nome per il modello.")
return
inherit_model = inherit_entry.get()
model_fields = []
for field_name, field_type, field_label, field_mandatory, field_primary_key, field_related, field_inverse in fields_entries:
name = field_name.get()
ftype = field_type.get()
label = field_label.get()
mandatory = field_mandatory.get()
primary_key = field_primary_key.get()
related = field_related.get() if ftype in ['Many2one', 'One2many', 'Many2many'] else ''
inverse = field_inverse.get() if ftype == 'One2many' else ''
if not name or not ftype or not label:
messagebox.showerror("Errore", "Compila tutti i campi per ogni riga.")
return
if primary_key and not mandatory:
messagebox.showerror("Errore", "Un campo chiave primaria deve essere obbligatorio.")
return
model_fields.append((name, ftype, label, mandatory, primary_key, related, inverse))
new_model = {"name": model_name, "fields": model_fields}
if inherit_model:
new_model['inherit'] = inherit_model
self.models[index] = new_model
self.models_listbox.delete(index)
self.models_listbox.insert(index, f"{model_name} ({len(model_fields)} campi)")
edit_model_dialog.destroy()
ttk.Button(edit_model_dialog, text="Aggiungi Campo", command=lambda: add_field_row(None)).grid(row=3, column=0, sticky=tk.W)
ttk.Button(edit_model_dialog, text="Elimina Campo", command=delete_field_row).grid(row=3, column=1, sticky=tk.W)
ttk.Button(edit_model_dialog, text="Salva Modello", command=save_model).grid(row=4, column=0, columnspan=2, sticky=(tk.W, tk.E))
def delete_model(self):
selection = self.models_listbox.curselection()
if not selection:
return
index = selection[0]
self.models_listbox.delete(index)
del self.models[index]
def add_button(self):
button_name = self.prompt_input("Inserisci il nome del bottone:")
button_string = self.prompt_input("Inserisci la stringa del bottone:")
button_icon = self.prompt_input("Inserisci l'icona del bottone (es. fa-check):")
button_class = self.prompt_input("Inserisci la classe del bottone (es. btn-primary):")
self.buttons_listbox.insert(tk.END, f"{button_name} ({button_string})")
self.options['buttons'].append({
"name": button_name,
"string": button_string,
"icon": button_icon,
"class": button_class
})
def edit_button(self):
selection = self.buttons_listbox.curselection()
if not selection:
return
index = selection[0]
button = self.options['buttons'][index]
self.buttons_listbox.delete(index)
button_name = self.prompt_input("Inserisci il nome del bottone:", default_value=button["name"])
button_string = self.prompt_input("Inserisci la stringa del bottone:", default_value=button["string"])
button_icon = self.prompt_input("Inserisci l'icona del bottone (es. fa-check):", default_value=button["icon"])
button_class = self.prompt_input("Inserisci la classe del bottone (es. btn-primary):", default_value=button["class"])
self.buttons_listbox.insert(index, f"{button_name} ({button_string})")
self.options['buttons'][index] = {
"name": button_name,
"string": button_string,
"icon": button_icon,
"class": button_class
}
def delete_button(self):
selection = self.buttons_listbox.curselection()
if not selection:
return
index = selection[0]
self.buttons_listbox.delete(index)
del self.options['buttons'][index]
def add_link(self):
if not form_xml_id:
return
tab_name = self.prompt_input("Inserisci il nome del tab o sezione in cui vuoi aggiungere questo modulo:")
if not tab_name:
return
self.links_listbox.insert(tk.END, f"{form_xml_id} -> {tab_name}")
self.options.get('link_locations', []).append({"form": form_xml_id, "tab": tab_name})
def edit_link(self):
selection = self.links_listbox.curselection()
if not selection:
return
index = selection[0]
link = self.options.get('link_locations', [])[index]
self.links_listbox.delete(index)
form_xml_id = self.prompt_input("Inserisci l'ID XML del form per collegare questo modulo:", default_value=link["form"])
tab_name = self.prompt_input("Inserisci il nome del tab o sezione in cui vuoi aggiungere questo modulo:", default_value=link["tab"])
self.links_listbox.insert(index, f"{form_xml_id} -> {tab_name}")
self.options.get('link_locations', [])[index] = {"form": form_xml_id, "tab": tab_name}
def delete_link(self):
selection = self.links_listbox.curselection()
if not selection:
return
index = selection[0]
self.links_listbox.delete(index)
del self.options.get('link_locations', [])[index]
def add_action(self):
action_name = self.prompt_input("Inserisci il nome dell'azione:")
action_model = self.prompt_input("Inserisci il nome del modello per l'azione:")
action_code = self.prompt_input("Inserisci il codice Python per l'azione:")
self.actions_listbox.insert(tk.END, f"{action_name} ({action_model})")
self.actions.append({"name": action_name, "model": action_model, "code": action_code})
def edit_action(self):
selection = self.actions_listbox.curselection()
if not selection:
return
index = selection[0]
action = self.actions[index]
self.actions_listbox.delete(index)
action_name = self.prompt_input("Inserisci il nome dell'azione:", default_value=action["name"])
action_model = self.prompt_input("Inserisci il nome del modello per l'azione:", default_value=action["model"])
action_code = self.prompt_input("Inserisci il codice Python per l'azione:", default_value=action["code"])
self.actions_listbox.insert(index, f"{action_name} ({action_model})")
self.actions[index] = {"name": action_name, "model": action_model, "code": action_code}
def delete_action(self):
selection = self.actions_listbox.curselection()
if not selection:
return
index = selection[0]
self.actions_listbox.delete(index)
del self.actions[index]
def add_security_rule(self):
rule_id = self.prompt_input("Inserisci l'ID della regola di sicurezza:")
rule_name = self.prompt_input("Inserisci il nome della regola:")
rule_model = self.prompt_input("Inserisci il nome del modello per la regola:")
rule_domain = self.prompt_input("Inserisci il dominio per la regola:")
self.security_listbox.insert(tk.END, f"{rule_name} ({rule_model})")
self.security_rules.append({"id": rule_id, "name": rule_name, "model": rule_model, "domain": rule_domain})
def edit_security_rule(self):
selection = self.security_listbox.curselection()
if not selection:
return
index = selection[0]
rule = self.security_rules[index]
self.security_listbox.delete(index)
rule_id = self.prompt_input("Inserisci l'ID della regola di sicurezza:", default_value=rule["id"])
rule_name = self.prompt_input("Inserisci il nome della regola:", default_value=rule["name"])
rule_model = self.prompt_input("Inserisci il nome del modello per la regola:", default_value=rule["model"])
rule_domain = self.prompt_input("Inserisci il dominio per la regola:", default_value=rule["domain"])
self.security_listbox.insert(index, f"{rule_name} ({rule_model})")
self.security_rules[index] = {"id": rule_id, "name": rule_name, "model": rule_model, "domain": rule_domain}
def delete_security_rule(self):
selection = self.security_listbox.curselection()
if not selection:
return
index = selection[0]
self.security_listbox.delete(index)
del self.security_rules[index]
def prompt_input(self, message, default_value=None):
dialog = tk.Toplevel()
dialog.grab_set()
dialog.title("Input")
ttk.Label(dialog, text=message).grid(column=0, row=0, sticky=tk.W)
entry = ttk.Entry(dialog, width=20)
entry.grid(column=1, row=0, sticky=(tk.W, tk.E))
if default_value:
entry.insert(0, default_value)
def on_ok():
self.input_value = entry.get()
dialog.destroy()
def on_cancel():
self.input_value = None
dialog.destroy()
ttk.Button(dialog, text="OK", command=on_ok).grid(column=0, row=1, sticky=tk.E)
ttk.Button(dialog, text="Cancel", command=on_cancel).grid(column=1, row=1, sticky=tk.W)
dialog.wait_window()
return self.input_value
def validate_field(self, event):
widget = event.widget
if not widget.get():
widget.config(background="pink")
else:
widget.config(background="white")
def generate_module(self):
if not self.module_name.get() or not self.module_path.get() or not self.models:
messagebox.showerror("Errore", "Per favore compila tutti i campi richiesti.")
return
options = {
"views": [view for view in self.view_types if self.view_vars[view].get()],
"buttons": self.options.get('buttons', []),
"link_locations": self.options.get('link_locations', [])
}
manifest_data = {
'name': self.module_name.get(),
'version': self.manifest_version.get(),
'summary': self.manifest_summary.get(),
'category': self.manifest_category.get(),
'author': self.manifest_author.get(),
'website': self.manifest_website.get()
}
generator = ModuleGenerator(self.module_name.get(), self.module_path.get(), self.models, options, self.actions, self.security_rules, manifest_data)
try:
generator.generate_module()
messagebox.showinfo("Successo", "Modulo generato con successo.")
except Exception as e:
messagebox.showerror("Errore", str(e))
def save_config(self):
config_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json")])
if not config_path:
return
config = {
'module_name': self.module_name.get(),
'module_path': self.module_path.get(),
'models': self.models,
'options': {
'views': [view for view in self.view_types if self.view_vars[view].get()],
'buttons': self.options.get('buttons', []),
'link_locations': self.options.get('link_locations', [])
},
'actions': self.actions,
'security_rules': self.security_rules,
'manifest_data': {
'version': self.manifest_version.get(),
'summary': self.manifest_summary.get(),
'category': self.manifest_category.get(),
'author': self.manifest_author.get(),
'website': self.manifest_website.get()
}
}
with open(config_path, 'w') as config_file:
json.dump(config, config_file, indent=4)
messagebox.showinfo("Successo", "Configurazione salvata con successo.")
def load_config(self):
config_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if not config_path:
return
with open(config_path, 'r') as config_file:
config = json.load(config_file)
self.module_name.delete(0, tk.END)
self.module_name.insert(0, config.get('module_name', ''))
self.module_path.delete(0, tk.END)
self.module_path.insert(0, config.get('module_path', ''))
self.models_listbox.delete(0, tk.END)
self.models = config.get('models', [])
for model in self.models:
self.models_listbox.insert(tk.END, f"{model['name']} ({len(model['fields'])} campi)")
options = config.get('options', {})
for view in self.view_types:
self.view_vars[view].set(view in options.get('views', []))
self.buttons_listbox.delete(0, tk.END)
buttons = options.get('buttons', [])
for button in buttons:
self.buttons_listbox.insert(tk.END, f"{button['name']} ({button['string']})")
self.options['buttons'] = buttons
self.links_listbox.delete(0, tk.END)
links = options.get('link_locations', [])
for link in links:
self.links_listbox.insert(tk.END, f"{link['form']} -> {link['tab']}")
self.options['link_locations'] = links
self.actions_listbox.delete(0, tk.END)
self.actions = config.get('actions', [])
for action in self.actions:
self.actions_listbox.insert(tk.END, f"{action['name']} ({action['model']})")
self.security_listbox.delete(0, tk.END)
self.security_rules = config.get('security_rules', [])
for rule in self.security_rules:
self.security_listbox.insert(tk.END, f"{rule['name']} ({rule['model']})")
manifest_data = config.get('manifest_data', {})
self.manifest_version.delete(0, tk.END)
self.manifest_version.insert(0, manifest_data.get('version', ''))
self.manifest_summary.delete(0, tk.END)
self.manifest_summary.insert(0, manifest_data.get('summary', ''))
self.manifest_category.delete(0, tk.END)
self.manifest_category.insert(0, manifest_data.get('category', ''))
self.manifest_author.delete(0, tk.END)
self.manifest_author.insert(0, manifest_data.get('author', ''))
self.manifest_website.delete(0, tk.END)
self.manifest_website.insert(0, manifest_data.get('website', ''))
messagebox.showinfo("Successo", "Configurazione caricata con successo.")