diff --git a/v2_adminpanel/leads/routes.py b/v2_adminpanel/leads/routes.py index 7a8062f..136f545 100644 --- a/v2_adminpanel/leads/routes.py +++ b/v2_adminpanel/leads/routes.py @@ -27,82 +27,21 @@ def get_lead_service(): def lead_management(): """Lead Management Dashboard""" try: - # Get current user - current_user = flask_session.get('username', 'System') - other_user = 'w@rh@mm3r' if current_user == 'rac00n' else 'rac00n' + # Get institutions with contact counts + institutions = get_lead_service().list_institutions() - # Initialize defaults - total_institutions = 0 - total_contacts = 0 - my_entries = 0 - other_entries = 0 - activities = [] + # Get all contacts with institution names + all_contacts = get_lead_service().list_all_contacts() - with get_db_connection() as conn: - cur = conn.cursor() - - # Get statistics - cur.execute("SELECT COUNT(*) FROM lead_institutions") - result = cur.fetchone() - if result: - total_institutions = result[0] - - cur.execute("SELECT COUNT(*) FROM lead_contacts") - result = cur.fetchone() - if result: - total_contacts = result[0] - - # Count entries by current user - cur.execute(""" - SELECT COUNT(*) FROM audit_log - WHERE username = %s - AND entity_type IN ('lead_institution', 'lead_contact') - AND action = 'CREATE' - """, (current_user,)) - result = cur.fetchone() - if result: - my_entries = result[0] - - # Count entries by other user - cur.execute(""" - SELECT COUNT(*) FROM audit_log - WHERE username = %s - AND entity_type IN ('lead_institution', 'lead_contact') - AND action = 'CREATE' - """, (other_user,)) - result = cur.fetchone() - if result: - other_entries = result[0] - - # Get recent activities - cur.execute(""" - SELECT timestamp, username, action, entity_type, additional_info - FROM audit_log - WHERE entity_type IN ('lead_institution', 'lead_contact') - ORDER BY timestamp DESC - LIMIT 10 - """) - rows = cur.fetchall() - if rows: - for row in rows: - activities.append({ - 'timestamp': row[0], - 'username': row[1], - 'action': row[2], - 'entity_type': row[3].replace('lead_', ''), - 'additional_info': row[4] - }) - - cur.close() + # Calculate totals + total_institutions = len(institutions) + total_contacts = len(all_contacts) return render_template('leads/lead_management.html', total_institutions=total_institutions, total_contacts=total_contacts, - my_entries=my_entries, - other_entries=other_entries, - other_user=other_user, - current_user=current_user, - recent_activities=activities) + institutions=institutions, + all_contacts=all_contacts) except Exception as e: import traceback print(f"Error in lead_management: {str(e)}") @@ -112,11 +51,8 @@ def lead_management(): return render_template('leads/lead_management.html', total_institutions=0, total_contacts=0, - my_entries=0, - other_entries=0, - other_user='', - current_user=current_user, - recent_activities=[]) + institutions=[], + all_contacts=[]) @leads_bp.route('/') @login_required @@ -147,12 +83,50 @@ def add_institution(): return redirect(url_for('leads.lead_management')) +@leads_bp.route('/contact/add', methods=['POST']) +@login_required +def add_contact(): + """Add new contact from form""" + try: + data = { + 'institution_id': request.form.get('institution_id'), + 'first_name': request.form.get('first_name'), + 'last_name': request.form.get('last_name'), + 'position': request.form.get('position') + } + + # Validate required fields + if not data['institution_id'] or not data['first_name'] or not data['last_name']: + flash('Institution, Vorname und Nachname sind erforderlich', 'error') + return redirect(url_for('leads.lead_management')) + + # Create contact + contact = get_lead_service().create_contact(data, flask_session.get('username', 'System')) + + # Add email if provided + email = request.form.get('email') + if email: + get_lead_service().add_email(contact['id'], email, 'Primär', flask_session.get('username', 'System')) + + # Add phone if provided + phone = request.form.get('phone') + if phone: + get_lead_service().add_phone(contact['id'], phone, 'Primär', flask_session.get('username', 'System')) + + flash(f'Kontakt "{data["first_name"]} {data["last_name"]}" wurde erfolgreich hinzugefügt', 'success') + except Exception as e: + flash(f'Fehler beim Hinzufügen des Kontakts: {str(e)}', 'error') + + return redirect(url_for('leads.lead_management')) + @leads_bp.route('/institution/') @login_required def institution_detail(institution_id): """Show institution with all contacts""" try: - institution = lead_repository.get_institution_by_id(institution_id) + # Get institution through repository + service = get_lead_service() + institution = service.repo.get_institution_by_id(institution_id) if not institution: flash('Institution nicht gefunden', 'error') return redirect(url_for('leads.institutions')) diff --git a/v2_adminpanel/leads/templates/leads/lead_management.html b/v2_adminpanel/leads/templates/leads/lead_management.html index f5c1086..6056181 100644 --- a/v2_adminpanel/leads/templates/leads/lead_management.html +++ b/v2_adminpanel/leads/templates/leads/lead_management.html @@ -2,6 +2,28 @@ {% block title %}Lead Management{% endblock %} +{% block extra_css %} + +{% endblock %} + {% block content %}
@@ -33,11 +55,11 @@
Schnellaktionen
- - Institutionen anzeigen - - + Exportieren @@ -46,71 +68,161 @@
- -
-
-
Letzte Aktivitäten
-
-
-
- - - - - - - - - - - {% for activity in recent_activities %} - - - - - - - {% endfor %} - -
ZeitpunktBenutzerAktionDetails
{{ activity.timestamp.strftime('%d.%m.%Y %H:%M') }} - - {{ activity.username }} - - - {% if activity.action == 'CREATE' %} - ➕ Erstellt - {% elif activity.action == 'UPDATE' %} - ✏️ Bearbeitet - {% elif activity.action == 'DELETE' %} - 🗑️ Gelöscht - {% endif %} - - {{ activity.entity_type|title }} - {% if activity.additional_info %} - - {{ activity.additional_info }} - {% endif %} -
- {% if not recent_activities %} -

Noch keine Aktivitäten vorhanden.

- {% endif %} -
-
-
- - +
-
-
Suche
-
-
-
-
- - + + + + + +
+ +
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+ + + + + + + + + + + {% for institution in institutions %} + + + + + + + {% endfor %} + +
NameAnzahl KontakteErstellt amAktionen
+ + {{ institution.name }} + + {{ institution.contact_count }}{{ institution.created_at.strftime('%d.%m.%Y') }} + + Details + +
+ {% if not institutions %} +

Keine Institutionen vorhanden.

+ {% endif %} +
- + + +
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ + +
+ + + + + + + + + + + + + {% for contact in all_contacts %} + + + + + + + + + {% endfor %} + +
NamePositionInstitutionE-MailTelefonAktionen
+ + {{ contact.first_name }} {{ contact.last_name }} + + {{ contact.position or '-' }}{{ contact.institution_name }} + {% if contact.emails %} + {{ contact.emails[0] }} + {% else %} + - + {% endif %} + + {% if contact.phones %} + {{ contact.phones[0] }} + {% else %} + - + {% endif %} + + + Details + +
+ {% if not all_contacts %} +

Keine Kontakte vorhanden.

+ {% endif %} +
+
+
@@ -138,4 +250,118 @@
+ + + + +{% endblock %} + +{% block extra_js %} + {% endblock %} \ No newline at end of file