116 lines
4.6 KiB
Python
116 lines
4.6 KiB
Python
"""Gmail › Contacten: contactlijsten in de mailboxen en het adresboek van de organisatie (zichtbaar/verborgen)."""
|
||
|
||
from flask import Blueprint, render_template, request, session, jsonify
|
||
|
||
from blueprints.auth import login_required
|
||
from rechten import recht_vereist, log
|
||
from services import contacten_service as cs
|
||
|
||
contacten_bp = Blueprint('contacten', __name__)
|
||
TABS = ('lijsten', 'adresboek')
|
||
|
||
|
||
def _door():
|
||
return session.get('naam') or session.get('user')
|
||
|
||
|
||
@contacten_bp.route('/', methods=['GET'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def overzicht():
|
||
tab = request.args.get('tab') if request.args.get('tab') in TABS else TABS[0]
|
||
ctx = {'tab': tab, 'fout': None, 'status': cs.LijstSync().status}
|
||
try:
|
||
if tab == 'adresboek':
|
||
ctx['ous'] = cs.ou_lijst()
|
||
ctx['ou'] = request.args.get('ou') or ''
|
||
ctx['met_sub'] = 'sub' not in request.args or '1' in request.args.getlist('sub')
|
||
ctx['gebruikers'] = cs.gebruikers_in([ctx['ou']], ctx['met_sub']) if ctx['ou'] else []
|
||
else:
|
||
ctx['lijsten'] = cs.lijsten()
|
||
lid = request.args.get('lijst', type=int)
|
||
ctx['gekozen'] = cs.lijst(lid) if lid else None
|
||
ctx['nieuw'] = bool(request.args.get('nieuw'))
|
||
if ctx['gekozen'] or ctx['nieuw']:
|
||
ctx['ous'] = cs.ou_lijst()
|
||
except cs.ContactFout as e:
|
||
ctx['fout'] = str(e)
|
||
except Exception as e:
|
||
ctx['fout'] = cs.vriendelijk(e)
|
||
return render_template('contacten.html', **ctx)
|
||
|
||
|
||
# ---------------------------------------------------------------------
|
||
# 1. Adresboek
|
||
# ---------------------------------------------------------------------
|
||
@contacten_bp.route('/adresboek', methods=['POST'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def adresboek():
|
||
d = request.get_json(silent=True) or {}
|
||
emails = [e for e in d.get('emails') or [] if isinstance(e, str)][:2000]
|
||
if not emails:
|
||
return jsonify(ok=False, fout="Niemand gekozen."), 400
|
||
try:
|
||
fouten = cs.zet_zichtbaar(emails, bool(d.get('zichtbaar')))
|
||
except Exception as e:
|
||
return jsonify(ok=False, fout=cs.vriendelijk(e)), 400
|
||
log(f"Contacten: {len(emails)} gebruiker(s) {'zichtbaar' if d.get('zichtbaar') else 'verborgen'} in het adresboek")
|
||
return jsonify(ok=not fouten, fouten=fouten, aantal=len(emails) - len(fouten))
|
||
|
||
|
||
# ---------------------------------------------------------------------
|
||
# 2. Contactlijsten in de mailboxen
|
||
# ---------------------------------------------------------------------
|
||
@contacten_bp.route('/lijst/bewaren', methods=['POST'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def lijst_bewaren():
|
||
d = request.get_json(silent=True) or {}
|
||
lid = int(d['id']) if str(d.get('id') or '').isdigit() else None
|
||
oud = cs.lijst(lid) if lid else None
|
||
try:
|
||
lid = cs.lijst_bewaren(lid, d.get('naam'), [o for o in d.get('ous') or [] if isinstance(o, str)],
|
||
bool(d.get('met_sub')), [i for i in d.get('items') or [] if isinstance(i, dict)], _door())
|
||
except cs.ContactFout as e:
|
||
return jsonify(ok=False, fout=str(e)), 400
|
||
log(f"Contacten: contactlijst {d.get('naam')} bewaard")
|
||
gestart = False
|
||
# Stond de lijst al in de mailboxen? Dan meteen bijwerken (ook bij wie er door een OU-wijziging uit valt)
|
||
if oud and oud.get('Laatste_sync'):
|
||
gestart = cs.LijstSync().start(lid, oude_naam=oud['Naam'], oude_ous=oud['ous'])
|
||
return jsonify(ok=True, id=lid, gestart=gestart)
|
||
|
||
|
||
@contacten_bp.route('/lijst/<int:lijst_id>/sync', methods=['POST'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def lijst_sync(lijst_id):
|
||
if not cs.LijstSync().start(lijst_id):
|
||
return jsonify(ok=False, fout="Er loopt al een opdracht."), 409
|
||
log(f"Contacten: contactlijst {lijst_id} naar de mailboxen")
|
||
return jsonify(ok=True)
|
||
|
||
|
||
@contacten_bp.route('/lijst/<int:lijst_id>/verwijderen', methods=['POST'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def lijst_verwijderen(lijst_id):
|
||
l = cs.lijst(lijst_id)
|
||
if not l:
|
||
return jsonify(ok=False, fout="Lijst niet gevonden."), 404
|
||
log(f"Contacten: contactlijst {l['Naam']} verwijderd")
|
||
if l.get('Laatste_sync'):
|
||
if not cs.LijstSync().start(lijst_id, verwijderen=True):
|
||
return jsonify(ok=False, fout="Er loopt al een opdracht."), 409
|
||
return jsonify(ok=True, gestart=True)
|
||
cs.lijst_verwijderen_db(lijst_id)
|
||
return jsonify(ok=True, gestart=False)
|
||
|
||
|
||
@contacten_bp.route('/status.json', methods=['GET'])
|
||
@login_required
|
||
@recht_vereist('gmail_contacten')
|
||
def status():
|
||
return jsonify(cs.LijstSync().status)
|