add create email from CSV
This commit is contained in:
parent
62b96c7d0b
commit
26407664e7
50
app/app.py
50
app/app.py
@ -3,9 +3,10 @@ from flask_login import login_user, logout_user, login_required, current_user
|
||||
from forms import LoginForm, MailboxForm, IPUnbanForm
|
||||
from flask_wtf.csrf import CSRFProtect
|
||||
from flask_login import LoginManager, UserMixin
|
||||
from io import TextIOWrapper
|
||||
from werkzeug.utils import secure_filename
|
||||
from config import Config
|
||||
import subprocess
|
||||
import os
|
||||
import subprocess, os, csv
|
||||
|
||||
|
||||
# Config
|
||||
@ -88,6 +89,7 @@ def index():
|
||||
# Output
|
||||
output = ""
|
||||
|
||||
# Create mail
|
||||
if create_form.validate_on_submit() and create_form.submit.data:
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', MAILSERVER_CONTAINER, "setup", "email", "add", create_form.email.data, create_form.password.data],
|
||||
@ -95,6 +97,42 @@ def index():
|
||||
)
|
||||
output = result.stdout or result.stderr
|
||||
|
||||
# Create mail from CSV import
|
||||
if create_form.validate_on_submit() and create_form.import_submit.data and create_form.csv_file.data:
|
||||
try:
|
||||
csv_file = create_form.csv_file.data
|
||||
stream = TextIOWrapper(csv_file.stream, encoding='utf-8')
|
||||
reader = csv.DictReader(stream)
|
||||
|
||||
output_lines = []
|
||||
|
||||
for row in reader:
|
||||
email = row.get('email')
|
||||
password = row.get('password')
|
||||
if email and password:
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', MAILSERVER_CONTAINER, "setup", "email", "add", email, password],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
line_output = result.stdout or result.stderr
|
||||
output_lines.append(f"{email}: {line_output.strip()}")
|
||||
else:
|
||||
output_lines.append(f"Ligne invalide: {row}")
|
||||
|
||||
output = '\n'.join(output_lines)
|
||||
|
||||
except Exception as e:
|
||||
output = f"Erreur lors du traitement du CSV : {e}"
|
||||
|
||||
# Update email password
|
||||
elif change_form.validate_on_submit() and change_form.submit.data:
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', MAILSERVER_CONTAINER, "email", "update", change_form.email.data, change_form.password.data],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
output = result.stdout or result.stderr
|
||||
|
||||
# Delete mail
|
||||
elif delete_form.validate_on_submit() and delete_form.submit.data:
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', MAILSERVER_CONTAINER, "setup", "email", "del", delete_form.email.data],
|
||||
@ -102,6 +140,7 @@ def index():
|
||||
)
|
||||
output = result.stdout or result.stderr
|
||||
|
||||
# Fail2ban dovecot
|
||||
elif ip_form.validate_on_submit():
|
||||
jail = ip_form.jail.data or 'dovecot'
|
||||
result = subprocess.run(
|
||||
@ -111,13 +150,6 @@ def index():
|
||||
)
|
||||
output = result.stdout or result.stderr
|
||||
|
||||
elif change_form.validate_on_submit() and change_form.submit.data:
|
||||
result = subprocess.run(
|
||||
['docker', 'exec', MAILSERVER_CONTAINER, "email", "update", change_form.email.data, change_form.password.data],
|
||||
capture_output=True, text=True
|
||||
)
|
||||
output = result.stdout or result.stderr
|
||||
|
||||
return render_template(
|
||||
'index.html',
|
||||
create_form=create_form,
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
from flask_wtf import FlaskForm
|
||||
from wtforms import StringField, PasswordField, SubmitField
|
||||
from wtforms.validators import DataRequired, IPAddress
|
||||
from flask_wtf.file import FileField, FileAllowed
|
||||
from wtforms import SubmitField
|
||||
|
||||
class LoginForm(FlaskForm):
|
||||
username = StringField('Username', validators=[DataRequired()])
|
||||
@ -10,6 +12,8 @@ class LoginForm(FlaskForm):
|
||||
class MailboxForm(FlaskForm):
|
||||
email = StringField('Email', validators=[DataRequired()])
|
||||
password = PasswordField('Password')
|
||||
csv_file = FileField('Import CSV', validators=[FileAllowed(['csv'], 'CSV only!')])
|
||||
import_submit = SubmitField('Importer depuis CSV')
|
||||
submit = SubmitField('Valider')
|
||||
|
||||
class IPUnbanForm(FlaskForm):
|
||||
|
||||
@ -20,6 +20,13 @@
|
||||
{{ create_form.submit }}
|
||||
</form>
|
||||
|
||||
<h3>Créer boîte mail à partir d'un fichier CSV</h3>
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
{{ create_form.hidden_tag() }}
|
||||
{{ create_form.csv_file.label }} {{ create_form.csv_file() }}
|
||||
{{ create_form.import_submit() }}
|
||||
</form>
|
||||
|
||||
<h3>Modifier mot de passe boîte mail</h3>
|
||||
<form method="post">
|
||||
{{ change_form.hidden_tag() }}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user