Lead Management - Zwischenstand
Dieser Commit ist enthalten in:
@@ -9,22 +9,144 @@ from db import get_db_connection
|
||||
from uuid import UUID
|
||||
import traceback
|
||||
|
||||
# Initialize service
|
||||
lead_repository = LeadRepository(get_db_connection)
|
||||
lead_service = LeadService(lead_repository)
|
||||
# Service will be initialized per request
|
||||
lead_repository = None
|
||||
lead_service = None
|
||||
|
||||
def get_lead_service():
|
||||
"""Get or create lead service instance"""
|
||||
global lead_repository, lead_service
|
||||
if lead_service is None:
|
||||
lead_repository = LeadRepository(get_db_connection) # Pass the function, not call it
|
||||
lead_service = LeadService(lead_repository)
|
||||
return lead_service
|
||||
|
||||
# HTML Routes
|
||||
@leads_bp.route('/management')
|
||||
@login_required
|
||||
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'
|
||||
|
||||
# Initialize defaults
|
||||
total_institutions = 0
|
||||
total_contacts = 0
|
||||
my_entries = 0
|
||||
other_entries = 0
|
||||
activities = []
|
||||
|
||||
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()
|
||||
|
||||
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)
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"Error in lead_management: {str(e)}")
|
||||
print(traceback.format_exc())
|
||||
flash(f'Fehler beim Laden des Dashboards: {str(e)}', 'error')
|
||||
current_user = flask_session.get('username', 'System')
|
||||
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=[])
|
||||
|
||||
@leads_bp.route('/')
|
||||
@login_required
|
||||
def institutions():
|
||||
"""List all institutions"""
|
||||
try:
|
||||
institutions = lead_service.list_institutions()
|
||||
institutions = get_lead_service().list_institutions()
|
||||
return render_template('leads/institutions.html', institutions=institutions)
|
||||
except Exception as e:
|
||||
flash(f'Fehler beim Laden der Institutionen: {str(e)}', 'error')
|
||||
return render_template('leads/institutions.html', institutions=[])
|
||||
|
||||
@leads_bp.route('/institution/add', methods=['POST'])
|
||||
@login_required
|
||||
def add_institution():
|
||||
"""Add new institution from form"""
|
||||
try:
|
||||
name = request.form.get('name')
|
||||
if not name:
|
||||
flash('Name ist erforderlich', 'error')
|
||||
return redirect(url_for('leads.lead_management'))
|
||||
|
||||
# Add institution
|
||||
get_lead_service().create_institution(name, flask_session.get('username', 'System'))
|
||||
flash(f'Institution "{name}" wurde erfolgreich hinzugefügt', 'success')
|
||||
except Exception as e:
|
||||
flash(f'Fehler beim Hinzufügen der Institution: {str(e)}', 'error')
|
||||
|
||||
return redirect(url_for('leads.lead_management'))
|
||||
|
||||
@leads_bp.route('/institution/<uuid:institution_id>')
|
||||
@login_required
|
||||
def institution_detail(institution_id):
|
||||
@@ -35,7 +157,7 @@ def institution_detail(institution_id):
|
||||
flash('Institution nicht gefunden', 'error')
|
||||
return redirect(url_for('leads.institutions'))
|
||||
|
||||
contacts = lead_service.list_contacts_by_institution(institution_id)
|
||||
contacts = get_lead_service().list_contacts_by_institution(institution_id)
|
||||
return render_template('leads/institution_detail.html',
|
||||
institution=institution,
|
||||
contacts=contacts)
|
||||
@@ -48,7 +170,7 @@ def institution_detail(institution_id):
|
||||
def contact_detail(contact_id):
|
||||
"""Show contact details with notes"""
|
||||
try:
|
||||
contact = lead_service.get_contact_details(contact_id)
|
||||
contact = get_lead_service().get_contact_details(contact_id)
|
||||
return render_template('leads/contact_detail.html', contact=contact)
|
||||
except Exception as e:
|
||||
flash(f'Fehler beim Laden des Kontakts: {str(e)}', 'error')
|
||||
@@ -59,7 +181,7 @@ def contact_detail(contact_id):
|
||||
def all_contacts():
|
||||
"""Show all contacts across all institutions"""
|
||||
try:
|
||||
contacts = lead_service.list_all_contacts()
|
||||
contacts = get_lead_service().list_all_contacts()
|
||||
return render_template('leads/all_contacts.html', contacts=contacts)
|
||||
except Exception as e:
|
||||
flash(f'Fehler beim Laden der Kontakte: {str(e)}', 'error')
|
||||
@@ -72,7 +194,7 @@ def create_institution():
|
||||
"""Create new institution"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
institution = lead_service.create_institution(
|
||||
institution = get_lead_service().create_institution(
|
||||
data['name'],
|
||||
flask_session.get('username')
|
||||
)
|
||||
@@ -88,7 +210,7 @@ def update_institution(institution_id):
|
||||
"""Update institution"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
institution = lead_service.update_institution(
|
||||
institution = get_lead_service().update_institution(
|
||||
institution_id,
|
||||
data['name'],
|
||||
flask_session.get('username')
|
||||
@@ -105,7 +227,7 @@ def create_contact():
|
||||
"""Create new contact"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
contact = lead_service.create_contact(data, flask_session.get('username'))
|
||||
contact = get_lead_service().create_contact(data, flask_session.get('username'))
|
||||
return jsonify({'success': True, 'contact': contact})
|
||||
except ValueError as e:
|
||||
return jsonify({'success': False, 'error': str(e)}), 400
|
||||
@@ -118,7 +240,7 @@ def update_contact(contact_id):
|
||||
"""Update contact"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
contact = lead_service.update_contact(
|
||||
contact = get_lead_service().update_contact(
|
||||
contact_id,
|
||||
data,
|
||||
flask_session.get('username')
|
||||
@@ -135,7 +257,7 @@ def add_phone(contact_id):
|
||||
"""Add phone to contact"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
detail = lead_service.add_phone(
|
||||
detail = get_lead_service().add_phone(
|
||||
contact_id,
|
||||
data['phone_number'],
|
||||
data.get('phone_type'),
|
||||
@@ -153,7 +275,7 @@ def add_email(contact_id):
|
||||
"""Add email to contact"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
detail = lead_service.add_email(
|
||||
detail = get_lead_service().add_email(
|
||||
contact_id,
|
||||
data['email'],
|
||||
data.get('email_type'),
|
||||
@@ -171,7 +293,7 @@ def update_detail(detail_id):
|
||||
"""Update contact detail (phone/email)"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
detail = lead_service.update_contact_detail(
|
||||
detail = get_lead_service().update_contact_detail(
|
||||
detail_id,
|
||||
data['detail_value'],
|
||||
data.get('detail_label'),
|
||||
@@ -188,7 +310,7 @@ def update_detail(detail_id):
|
||||
def delete_detail(detail_id):
|
||||
"""Delete contact detail"""
|
||||
try:
|
||||
success = lead_service.delete_contact_detail(
|
||||
success = get_lead_service().delete_contact_detail(
|
||||
detail_id,
|
||||
flask_session.get('username')
|
||||
)
|
||||
@@ -202,7 +324,7 @@ def add_note(contact_id):
|
||||
"""Add note to contact"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
note = lead_service.add_note(
|
||||
note = get_lead_service().add_note(
|
||||
contact_id,
|
||||
data['note_text'],
|
||||
flask_session.get('username')
|
||||
@@ -219,7 +341,7 @@ def update_note(note_id):
|
||||
"""Update note"""
|
||||
try:
|
||||
data = request.get_json()
|
||||
note = lead_service.update_note(
|
||||
note = get_lead_service().update_note(
|
||||
note_id,
|
||||
data['note_text'],
|
||||
flask_session.get('username')
|
||||
@@ -235,7 +357,7 @@ def update_note(note_id):
|
||||
def delete_note(note_id):
|
||||
"""Delete note"""
|
||||
try:
|
||||
success = lead_service.delete_note(
|
||||
success = get_lead_service().delete_note(
|
||||
note_id,
|
||||
flask_session.get('username')
|
||||
)
|
||||
|
||||
In neuem Issue referenzieren
Einen Benutzer sperren