Compare commits
3 Commits
cb0795a6ac
...
193ffeff9d
Author | SHA1 | Date |
---|---|---|
Tim Gröger | 193ffeff9d | |
Tim Gröger | 7eb30b662f | |
Tim Gröger | 11204662be |
|
@ -165,7 +165,7 @@ def get_assocd_user(token, current_session, **kwargs):
|
||||||
def reset_password():
|
def reset_password():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if "userid" in data:
|
if "userid" in data:
|
||||||
user = userController.find_user(data["userid"])
|
user = userController.get_user(data["userid"])
|
||||||
if user:
|
if user:
|
||||||
userController.request_reset(user)
|
userController.request_reset(user)
|
||||||
elif "password" in data and "token" in data:
|
elif "password" in data and "token" in data:
|
||||||
|
|
|
@ -3,13 +3,14 @@
|
||||||
# English: Debit -> from account
|
# English: Debit -> from account
|
||||||
# Credit -> to account
|
# Credit -> to account
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
from sqlalchemy import func, case, and_
|
from sqlalchemy import func, case, and_, or_
|
||||||
from sqlalchemy.ext.hybrid import hybrid_property
|
from sqlalchemy.ext.hybrid import hybrid_property
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from werkzeug.exceptions import BadRequest, NotFound, Conflict
|
from werkzeug.exceptions import BadRequest, NotFound, Conflict
|
||||||
|
|
||||||
from flaschengeist.database import db
|
from flaschengeist.database import db
|
||||||
from flaschengeist.models.user import User, _UserAttribute
|
from flaschengeist.models.user import User, _UserAttribute
|
||||||
|
from flaschengeist.app import logger
|
||||||
|
|
||||||
from .models import Transaction
|
from .models import Transaction
|
||||||
from . import permissions, BalancePlugin
|
from . import permissions, BalancePlugin
|
||||||
|
@ -44,7 +45,13 @@ def get_balance(user, start: datetime = None, end: datetime = None):
|
||||||
return credit, debit, credit - debit
|
return credit, debit, credit - debit
|
||||||
|
|
||||||
|
|
||||||
def get_balances(start: datetime = None, end: datetime = None, limit=None, offset=None, descending=None, sortBy=None):
|
def get_balances(
|
||||||
|
start: datetime = None, end: datetime = None, limit=None, offset=None, descending=None, sortBy=None, _filter=None
|
||||||
|
):
|
||||||
|
logger.debug(
|
||||||
|
f"get_balances(start={start}, end={end}, limit={limit}, offset={offset}, descending={descending}, sortBy={sortBy}, _filter={_filter})"
|
||||||
|
)
|
||||||
|
|
||||||
class _User(User):
|
class _User(User):
|
||||||
_debit = db.relationship(Transaction, back_populates="sender_", foreign_keys=[Transaction._sender_id])
|
_debit = db.relationship(Transaction, back_populates="sender_", foreign_keys=[Transaction._sender_id])
|
||||||
_credit = db.relationship(Transaction, back_populates="receiver_", foreign_keys=[Transaction._receiver_id])
|
_credit = db.relationship(Transaction, back_populates="receiver_", foreign_keys=[Transaction._receiver_id])
|
||||||
|
@ -116,6 +123,11 @@ def get_balances(start: datetime = None, end: datetime = None, limit=None, offse
|
||||||
q2 = query.join(_User._debit).filter(Transaction.time <= end)
|
q2 = query.join(_User._debit).filter(Transaction.time <= end)
|
||||||
query = q1.union(q2)
|
query = q1.union(q2)
|
||||||
|
|
||||||
|
if _filter:
|
||||||
|
query = query.filter(
|
||||||
|
or_(_User.firstname.ilike(f"%{_filter.lower()}%"), _User.lastname.ilike(f"%{_filter.lower()}%"))
|
||||||
|
)
|
||||||
|
|
||||||
if sortBy == "balance":
|
if sortBy == "balance":
|
||||||
if descending:
|
if descending:
|
||||||
query = query.order_by((_User.credit - _User.debit).desc(), _User.lastname.asc(), _User.firstname.asc())
|
query = query.order_by((_User.credit - _User.debit).desc(), _User.lastname.asc(), _User.firstname.asc())
|
||||||
|
|
|
@ -7,6 +7,7 @@ from flaschengeist.models.session import Session
|
||||||
from flaschengeist.utils.datetime import from_iso_format
|
from flaschengeist.utils.datetime import from_iso_format
|
||||||
from flaschengeist.utils.decorators import login_required
|
from flaschengeist.utils.decorators import login_required
|
||||||
from flaschengeist.controller import userController
|
from flaschengeist.controller import userController
|
||||||
|
from flaschengeist.app import logger
|
||||||
from . import BalancePlugin, balance_controller, permissions
|
from . import BalancePlugin, balance_controller, permissions
|
||||||
|
|
||||||
|
|
||||||
|
@ -317,7 +318,11 @@ def get_balances(current_session: Session):
|
||||||
offset = request.args.get("offset", type=int)
|
offset = request.args.get("offset", type=int)
|
||||||
descending = request.args.get("descending", False, type=bool)
|
descending = request.args.get("descending", False, type=bool)
|
||||||
sortBy = request.args.get("sortBy", type=str)
|
sortBy = request.args.get("sortBy", type=str)
|
||||||
balances, count = balance_controller.get_balances(limit=limit, offset=offset, descending=descending, sortBy=sortBy)
|
_filter = request.args.get("filter", None, type=str)
|
||||||
|
logger.debug(f"request.args: {request.args}")
|
||||||
|
balances, count = balance_controller.get_balances(
|
||||||
|
limit=limit, offset=offset, descending=descending, sortBy=sortBy, _filter=_filter
|
||||||
|
)
|
||||||
return jsonify(
|
return jsonify(
|
||||||
{
|
{
|
||||||
"balances": [{"userid": u, "credit": v[0], "debit": v[1]} for u, v in balances.items()],
|
"balances": [{"userid": u, "credit": v[0], "debit": v[1]} for u, v in balances.items()],
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import smtplib
|
import smtplib
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from werkzeug.exceptions import InternalServerError
|
||||||
|
|
||||||
from flaschengeist import logger
|
from flaschengeist import logger
|
||||||
from flaschengeist.models import User
|
from flaschengeist.models import User
|
||||||
|
@ -8,23 +9,29 @@ from flaschengeist.plugins import Plugin
|
||||||
from flaschengeist.utils.hook import HookAfter
|
from flaschengeist.utils.hook import HookAfter
|
||||||
from flaschengeist.controller import userController
|
from flaschengeist.controller import userController
|
||||||
from flaschengeist.controller.messageController import Message
|
from flaschengeist.controller.messageController import Message
|
||||||
|
from flaschengeist.config import config
|
||||||
|
|
||||||
|
|
||||||
class MailMessagePlugin(Plugin):
|
class MailMessagePlugin(Plugin):
|
||||||
def __init__(self, entry_point, config):
|
def load(self):
|
||||||
super().__init__(entry_point, config)
|
self.config = config.get("mail", None)
|
||||||
self.server = config["SERVER"]
|
if self.config is None:
|
||||||
self.port = config["PORT"]
|
logger.error("mail was not configured in flaschengeist.toml")
|
||||||
self.user = config["USER"]
|
raise InternalServerError
|
||||||
self.password = config["PASSWORD"]
|
self.server = self.config["SERVER"]
|
||||||
self.crypt = config["CRYPT"]
|
self.port = self.config["PORT"]
|
||||||
self.mail = config["MAIL"]
|
self.user = self.config["USER"]
|
||||||
|
self.password = self.config["PASSWORD"]
|
||||||
|
self.crypt = self.config["CRYPT"]
|
||||||
|
self.mail = self.config["MAIL"]
|
||||||
|
|
||||||
@HookAfter("send_message")
|
@HookAfter("send_message")
|
||||||
def dummy_send(msg):
|
def dummy_send(msg, *args, **kwargs):
|
||||||
|
logger.info(f"(dummy_send) Sending message to {msg.receiver}")
|
||||||
self.send_mail(msg)
|
self.send_mail(msg)
|
||||||
|
|
||||||
def send_mail(self, msg: Message):
|
def send_mail(self, msg: Message):
|
||||||
|
logger.debug(f"Sending mail to {msg.receiver}")
|
||||||
if isinstance(msg.receiver, User):
|
if isinstance(msg.receiver, User):
|
||||||
if not msg.receiver.mail:
|
if not msg.receiver.mail:
|
||||||
logger.warning("Could not send Mail, mail missing: {}".format(msg.receiver))
|
logger.warning("Could not send Mail, mail missing: {}".format(msg.receiver))
|
||||||
|
@ -34,8 +41,13 @@ class MailMessagePlugin(Plugin):
|
||||||
recipients = userController.get_user_by_role(msg.receiver)
|
recipients = userController.get_user_by_role(msg.receiver)
|
||||||
|
|
||||||
mail = MIMEMultipart()
|
mail = MIMEMultipart()
|
||||||
mail["From"] = self.mail
|
try:
|
||||||
mail["To"] = ", ".join(recipients)
|
mail["From"] = self.mail
|
||||||
|
mail["To"] = ", ".join(recipients)
|
||||||
|
except Exception as e:
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
print(traceback.format_exc())
|
||||||
mail["Subject"] = msg.subject
|
mail["Subject"] = msg.subject
|
||||||
mail.attach(MIMEText(msg.message))
|
mail.attach(MIMEText(msg.message))
|
||||||
if not hasattr(self, "smtp"):
|
if not hasattr(self, "smtp"):
|
||||||
|
|
Loading…
Reference in New Issue