NexusDashboard/app/mail.py

86 lines
3.4 KiB
Python
Raw Normal View History

2022-03-13 02:09:35 +00:00
from flask import render_template, Blueprint, redirect, url_for, flash, request
2022-01-16 18:22:00 +00:00
from flask_user import login_required, current_user
2022-03-13 02:09:35 +00:00
from app.models import Mail, CharacterInfo
2022-01-16 18:22:00 +00:00
from app.forms import SendMailForm
2022-02-12 05:05:00 +00:00
from app import gm_level, log_audit
2022-05-30 01:12:32 +00:00
from app.luclient import translate_from_locale
from app.cdclient import Objects
2022-01-16 18:22:00 +00:00
import time
mail_blueprint = Blueprint('mail', __name__)
@mail_blueprint.route('/view/<id>', methods=['GET'])
@login_required
def view(id):
mail = Mail.query.filter(Mail.id == id).first()
return render_template('mail/view.html.j2', mail=mail)
@mail_blueprint.route('/send', methods=['GET', 'POST'])
@login_required
@gm_level(3)
def send():
form = SendMailForm()
if request.method == "POST":
if form.attachment.data != "0" and form.attachment_count.data == 0:
form.attachment_count.data = 1
if form.recipient.data == "0":
2022-02-12 05:05:00 +00:00
log_audit(f"Sending {form.subject.data}: {form.body.data} to All Characters with {form.attachment_count.data} of item {form.attachment.data}")
2022-01-16 18:22:00 +00:00
for character in CharacterInfo.query.all():
Mail(
2022-03-13 02:09:35 +00:00
sender_id=0,
sender_name=f"[GM] {current_user.username}",
receiver_id=character.id,
receiver_name=character.name,
time_sent=time.time(),
subject=form.subject.data,
body=form.body.data,
attachment_id=0,
attachment_lot=form.attachment.data,
attachment_count=form.attachment_count.data
2022-01-16 18:22:00 +00:00
).save()
2022-03-13 02:09:35 +00:00
log_audit(f"Sent {form.subject.data}: \
{form.body.data} to ({character.id}){character.name} \
with {form.attachment_count.data} of item {form.attachment.data}")
2022-01-16 18:22:00 +00:00
else:
Mail(
2022-03-13 02:09:35 +00:00
sender_id=0,
sender_name=f"[GM] {current_user.username}",
receiver_id=form.recipient.data,
receiver_name=CharacterInfo.query.filter(CharacterInfo.id == form.recipient.data).first().name,
time_sent=time.time(),
subject=form.subject.data,
body=form.body.data,
attachment_id=0,
attachment_lot=form.attachment.data,
attachment_count=form.attachment_count.data
2022-01-16 18:22:00 +00:00
).save()
2022-03-13 02:09:35 +00:00
log_audit(f"Sent {form.subject.data}: \
{form.body.data} to ({form.recipient.data}){CharacterInfo.query.filter(CharacterInfo.id == form.recipient.data).first().name} \
with {form.attachment_count.data} of item {form.attachment.data}")
2022-01-16 18:22:00 +00:00
flash("Sent Mail", "success")
return redirect(url_for('mail.send'))
recipients = CharacterInfo.query.all()
for character in recipients:
form.recipient.choices.append((character.id, character.name))
2022-05-30 01:12:32 +00:00
items = Objects.query.filter(Objects.type == "Loot").all()
2022-01-16 18:22:00 +00:00
for item in items:
name = translate_from_locale(f'Objects_{item[0]}_name')
2022-05-30 01:12:32 +00:00
if name == f'Objects_{item.id}_name':
name = (item.displayName if (item.displayName != "None" and item.displayName != "" and item.displayName is not None) else item.name)
2022-01-16 18:22:00 +00:00
form.attachment.choices.append(
(
item[0],
f'({item[0]}) {name}'
)
)
return render_template('mail/send.html.j2', form=form)