Neue Verwaltung der Gruppenzuordnung 1:n

This commit is contained in:
concierge committed 2026-10-30 09:02:46 +01:00
1 parent fa02a7ade0
commit 02c017d53d
34 files changed
+1306 -251

No files matched your search

+37 -50
View File
@@ -10,7 +10,7 @@ from cmbnet.models.groups import GroupModel
from cmbnet.models.streets import StreetModel
from cmbnet.services.groups_service import GroupsService
from cmbnet.services.history_service import HistoryService
from cmbnet.utils.helpers import log_transaction
from cmbnet.utils.helpers import log_transaction, notify_reload
from cmbnet.utils.csv_utils import parse_streets_csv, parse_house_numbers_csv
from cmbnet.ws import broker
from cmbnet.ws.protocol import WSMessage
@@ -38,7 +38,8 @@ def create_account(**kwargs):
password_hash = hash_password(password)
account_id = AccountModel.create(g.tenant_id, name, password_hash, role)
log_transaction('account.create', 'account', account_id, {'name': name, 'role': role})
notify_reload(f'Administration: Account „{name}“ wurde erstellt.')
return jsonify({'success': True, 'id': account_id})
@@ -47,9 +48,12 @@ def create_account(**kwargs):
def update_account_role(account_id, **kwargs):
data = request.get_json()
role = data.get('role')
account = AccountModel.get_by_id(account_id, g.tenant_id)
AccountModel.update_role(account_id, g.tenant_id, role)
log_transaction('account.role_change', 'account', account_id, {'role': role})
GroupsService.invalidate(g.tenant_id)
name = account['name'] if account else f'#{account_id}'
notify_reload(f'Administration: Rolle von Account „{name}“ wurde geändert.')
return jsonify({'success': True})
@@ -61,17 +65,23 @@ def reset_password(account_id, **kwargs):
if not password:
return jsonify({'error': 'Passwort erforderlich'}), 400
password_hash = hash_password(password)
account = AccountModel.get_by_id(account_id, g.tenant_id)
AccountModel.set_password(account_id, g.tenant_id, password_hash)
log_transaction('account.password_reset', 'account', account_id, {})
name = account['name'] if account else f'#{account_id}'
notify_reload(f'Administration: Passwort für Account „{name}“ wurde zurückgesetzt.')
return jsonify({'success': True})
@admin_api_bp.route('/accounts/<int:account_id>', methods=['DELETE'])
@role_required('Administrator')
def delete_account(account_id, **kwargs):
account = AccountModel.get_by_id(account_id, g.tenant_id)
AccountModel.delete(account_id, g.tenant_id)
log_transaction('account.delete', 'account', account_id, {})
GroupsService.invalidate(g.tenant_id)
name = account['name'] if account else f'#{account_id}'
notify_reload(f'Administration: Account „{name}“ wurde gelöscht.')
return jsonify({'success': True})
@@ -81,37 +91,18 @@ def delete_account(account_id, **kwargs):
def create_group(**kwargs):
data = request.get_json() or {}
aliasname = str(data.get('aliasname') or '').strip()
account_id = data.get('account_id')
db = get_db()
if account_id == '' or account_id is None:
account_id = None
else:
try:
account_id = int(account_id)
except (TypeError, ValueError):
return jsonify({'error': 'Ungültiger Account'}), 400
account = AccountModel.get_by_id(account_id, g.tenant_id)
if not account or account['role'] != 'Gruppe':
return jsonify({'error': 'Account muss die Rolle "Gruppe" haben'}), 400
linked = db.execute(
'SELECT id FROM groups WHERE tenant_id = ? AND account_id = ?',
(g.tenant_id, account_id)
).fetchone()
if linked:
return jsonify({'error': 'Dieser Account ist bereits mit einer Gruppe verknüpft'}), 400
group_id = GroupModel.create(g.tenant_id, aliasname, account_id, 'offen')
group_id = GroupModel.create(g.tenant_id, aliasname, 'offen')
db.execute(
'INSERT INTO group_state_history (tenant_id, group_id, from_state, to_state, actor_account_id) VALUES (?, ?, ?, ?, ?)',
(g.tenant_id, group_id, None, 'offen', session['account_id'])
)
db.commit()
log_transaction('group.create', 'group', group_id, {'aliasname': aliasname, 'account_id': account_id})
log_transaction('group.create', 'group', group_id, {'aliasname': aliasname})
GroupsService.invalidate(g.tenant_id)
GroupsService.load_from_db(g.tenant_id)
notify_reload(f'Administration: Gruppe „{aliasname or group_id}“ wurde erstellt.')
return jsonify({'success': True, 'id': group_id})
@@ -128,30 +119,6 @@ def update_group(group_id, **kwargs):
if 'aliasname' in data:
updates['aliasname'] = data['aliasname']
if 'account_id' in data:
account_id = data['account_id']
if account_id in ('', None):
account_id = None
else:
try:
account_id = int(account_id)
except (TypeError, ValueError):
return jsonify({'error': 'Ungültiger Account'}), 400
account = AccountModel.get_by_id(account_id, g.tenant_id)
if not account or account['role'] != 'Gruppe':
return jsonify({'error': 'Account muss die Rolle "Gruppe" haben'}), 400
linked = db.execute(
'SELECT id FROM groups WHERE tenant_id = ? AND account_id = ? AND id != ?',
(g.tenant_id, account_id, group_id)
).fetchone()
if linked:
return jsonify({'error': 'Dieser Account ist bereits mit einer Gruppe verknüpft'}), 400
if account_id != old['account_id']:
updates['account_id'] = account_id
allowed_states = {'offen', 'unterwegs', 'online', 'pausierend', 'fertig'}
state_changed = False
if 'state' in data:
@@ -183,15 +150,20 @@ def update_group(group_id, **kwargs):
log_transaction('group.update', 'group', group_id, updates)
GroupsService.invalidate(g.tenant_id)
GroupsService.load_from_db(g.tenant_id)
display_name = updates.get('aliasname', old['aliasname']) or group_id
notify_reload(f'Administration: Gruppe „{display_name}“ wurde geändert.')
return jsonify({'success': True})
@admin_api_bp.route('/gruppen/<int:group_id>', methods=['DELETE'])
@role_required('Administrator')
def delete_group(group_id, **kwargs):
group = GroupModel.get_by_id(group_id, g.tenant_id)
GroupModel.delete(group_id, g.tenant_id)
log_transaction('group.delete', 'group', group_id, {})
GroupsService.invalidate(g.tenant_id)
display_name = (group['aliasname'] if group and group['aliasname'] else group_id)
notify_reload(f'Administration: Gruppe „{display_name}“ wurde gelöscht.')
return jsonify({'success': True})
@@ -212,6 +184,7 @@ def revert_group_hist(group_id, hist_id, **kwargs):
success, msg = HistoryService.revert_group(g.tenant_id, group_id, hist_id, session['account_id'])
if not success:
return jsonify({'error': msg}), 400
notify_reload('Administration: Gruppenhistorie wurde wiederhergestellt.')
return jsonify({'success': True, 'message': msg})
@@ -223,6 +196,7 @@ def set_group_position_from_history(group_id, hist_id, **kwargs):
)
if not success:
return jsonify({'error': msg}), 400
notify_reload('Administration: Gruppenposition wurde aus der Historie gesetzt.')
return jsonify({'success': True, 'message': msg})
@@ -234,6 +208,7 @@ def clear_group_position(group_id, **kwargs):
)
if not success:
return jsonify({'error': msg}), 400
notify_reload('Administration: Gruppenposition wurde gelöscht.')
return jsonify({'success': True, 'message': msg})
@@ -258,6 +233,7 @@ def create_street(**kwargs):
)
db.commit()
log_transaction('street.create', 'street', street_id, {'name': name})
notify_reload(f'Administration: Straße „{name}“ wurde erstellt.')
return jsonify({'success': True, 'id': street_id})
@@ -303,7 +279,9 @@ def update_street(street_id, **kwargs):
if changes:
StreetModel.update(street_id, g.tenant_id, **changes)
log_transaction('street.update', 'street', street_id, changes)
display_name = changes.get('name', old['name'])
notify_reload(f'Administration: Straße „{display_name}“ wurde geändert.')
db.commit()
return jsonify({'success': True})
@@ -311,8 +289,11 @@ def update_street(street_id, **kwargs):
@admin_api_bp.route('/strassen/<int:street_id>', methods=['DELETE'])
@role_required('Administrator')
def delete_street(street_id, **kwargs):
street = StreetModel.get_by_id(street_id, g.tenant_id)
StreetModel.delete(street_id, g.tenant_id)
log_transaction('street.delete', 'street', street_id, {})
name = street['name'] if street else f'#{street_id}'
notify_reload(f'Administration: Straße „{name}“ wurde gelöscht.')
return jsonify({'success': True})
@@ -333,6 +314,7 @@ def revert_street_hist(street_id, hist_id, **kwargs):
success, msg = HistoryService.revert_street(g.tenant_id, street_id, hist_id, session['account_id'])
if not success:
return jsonify({'error': msg}), 400
notify_reload('Administration: Straßenhistorie wurde wiederhergestellt.')
return jsonify({'success': True, 'message': msg})
@@ -367,6 +349,7 @@ def import_streets(**kwargs):
count += 1
db.commit()
notify_reload(f'Administration: {count} Straßen wurden importiert.')
return jsonify({'success': True, 'count': count})
@@ -392,7 +375,10 @@ def broker_status(**kwargs):
}
if account['role'] == 'Gruppe':
group = get_db().execute(
'SELECT id, aliasname FROM groups WHERE account_id = ? AND tenant_id = ?',
'''SELECT g.id, g.aliasname FROM groups g
JOIN group_accounts ga
ON ga.group_id = g.id AND ga.tenant_id = g.tenant_id
WHERE ga.account_id = ? AND g.tenant_id = ?''',
(account['id'], g.tenant_id)
).fetchone()
if group:
@@ -447,4 +433,5 @@ def import_house_numbers(**kwargs):
None,
{'count': len(house_numbers)}
)
notify_reload(f'Administration: {len(house_numbers)} Hausnummern wurden importiert.')
return jsonify({'success': True, 'count': len(house_numbers)})
+2 -6
View File
@@ -14,11 +14,7 @@ def gruppen(**kwargs):
"""Gruppenverwaltung"""
db = get_db()
gruppen = db.execute(
'SELECT g.*, a.name as account_name FROM groups g LEFT JOIN accounts a ON g.account_id = a.id WHERE g.tenant_id = ? ORDER BY g.id',
'SELECT * FROM groups WHERE tenant_id = ? ORDER BY id',
(g.tenant_id,)
).fetchall()
accounts = db.execute(
"SELECT * FROM accounts WHERE tenant_id = ? AND role = 'Gruppe'",
(g.tenant_id,)
).fetchall()
return render_template('admin/gruppen.html', gruppen=gruppen, accounts=accounts)
return render_template('admin/gruppen.html', gruppen=gruppen)