Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stapf functions #55

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def create_app(profile="default"):
app.register_blueprint(registration_blueprint)
init_reg(app)

from app.stapf import stapf_blueprint, init_app as init_stapf
app.register_blueprint(stapf_blueprint)
init_stapf(app)

return app

def check_sanity(fix=True):
Expand Down
2 changes: 2 additions & 0 deletions app/registration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from flask import Blueprint, abort, current_app, json

class Registration(db.Model):
__bind_key__ = 'anmeldung'
id = db.Column(db.Integer(), primary_key = True)
username = db.Column(db.Text(), unique = True)
blob = db.Column(db.Text())
Expand Down Expand Up @@ -49,6 +50,7 @@ def is_zapf_attendee(self):
return self.confirmed and self.priority < self.uni.slots

class Uni(db.Model):
__bind_key__ = 'anmeldung'
id = db.Column(db.Integer(), primary_key = True)
token = db.Column(db.String(256), unique = True)
name = db.Column(db.Text(), unique = True)
Expand Down
8 changes: 8 additions & 0 deletions app/stapf/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from flask import Blueprint

stapf_blueprint = Blueprint('stapf', __name__, template_folder='templates/')

from . import models, views

def init_app(app):
return app
37 changes: 37 additions & 0 deletions app/stapf/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import email.utils
from app.db import db
from .models import Recipient
from flask import current_app
from flask_mail import Message

def import_recipients_from_list(mail_list):
result = []
for line in mail_list.splitlines():
parsed_mail = email.utils.parseaddr(line)
recipient = Recipient()
recipient.name = parsed_mail[0]
recipient.mail = parsed_mail[1]
recipient.comment = 'Automatisch importiert (bitte überprüfen)'
db.session.add(recipient)
result.append(recipient)
db.session.commit()
return result

def send_batch_mails(batch):
with current_app.mail.connect() as conn:
for recipients_list in batch.decision.recipients_lists:
for recipient in recipients_list.recipients:
if not recipient.mail:
continue
msg = Message(batch.subject, recipients=[recipient.mail],
body=batch.message, sender=current_app.config['MAIL_STAPF'])
with current_app.open_resource(batch.decision.file_path) as fp:
msg.attach(batch.decision.filename, "application/pdf", fp.read())
conn.send(msg)

msg = Message(batch.subject, recipients=[current_app.config['MAIL_STAPF']],
body=batch.message, sender=current_app.config['MAIL_STAPF'])
with current_app.open_resource(batch.decision.file_path) as fp:
msg.attach(batch.decision.filename, "application/pdf", fp.read())
conn.send(msg)

65 changes: 65 additions & 0 deletions app/stapf/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from app.db import db
from app.user.models import User
from flask import Blueprint, abort, current_app, json

RecipientToList = db.Table('recipient_to_list',
db.Column('list_id', db.Integer, db.ForeignKey('recipients_lists.id'), primary_key=True),
db.Column('recipient_id', db.Integer, db.ForeignKey('recipients.id'), primary_key=True),
info={'bind_key': 'stapf'}
)

DecisionRecipientsLists = db.Table('decision_recipients_lists',
db.Column('decision_id', db.Integer, db.ForeignKey('decisions.id'), primary_key=True),
db.Column('recipients_list_id', db.Integer, db.ForeignKey('recipients_lists.id'), primary_key=True),
info={'bind_key': 'stapf'}
)

class Recipient(db.Model):
__bind_key__ = 'stapf'
__tablename__ = 'recipients'
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.Text())
organisation_name = db.Column(db.Text())
addressline1 = db.Column(db.Text())
addressline2 = db.Column(db.Text())
street = db.Column(db.Text())
postal_code = db.Column(db.Integer())
locality = db.Column(db.Text())
country = db.Column(db.Text())
mail = db.Column(db.Text())
comment = db.Column(db.Text())

@property
def has_address(self):
return self.street and self.locality

class RecipientsList(db.Model):
__bind_key__ = 'stapf'
__tablename__ = 'recipients_lists'
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.Text(), unique = True)
recipients = db.relationship("Recipient", secondary=RecipientToList, backref=db.backref('lists', lazy=True))
comment = db.Column(db.Text())

class Decision(db.Model):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly does a Decision represent? Maybe we can find better naming for this class...

__bind_key__ = 'stapf'
__tablename__ = 'decisions'
id = db.Column(db.Integer(), primary_key = True)
title = db.Column(db.Text())
decided = db.Column(db.Date())
recipients_lists = db.relationship("RecipientsList", secondary=DecisionRecipientsLists, backref=db.backref('decisions', lazy=True))
filename = db.Column(db.Text())
file_path = db.Column(db.Text())
comment = db.Column(db.Text())

class Batch(db.Model):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a more descriptive name here might be better, e.g. MailBatch

__bind_key__ = 'stapf'
__tablename__ = 'batches'
id = db.Column(db.Integer(), primary_key = True)
subject = db.Column(db.Text())
message = db.Column(db.Text())
decision_id = db.Column(db.Integer(), db.ForeignKey('decisions.id'))
decision = db.relationship("Decision", backref=db.backref('batches', lazy=True))
sent = db.Column(db.Boolean(), default=False)
sent_at = db.Column(db.DateTime())
comment = db.Column(db.Text())
72 changes: 72 additions & 0 deletions app/stapf/templates/batch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block styles %}
{{super()}}
<link rel="stylesheet"
href="{{url_for('static', filename='plugin-bootstrap-select-1.12.4/css/bootstrap-select.min.css')}}">
{% endblock %}

{% block scripts %}
{{super()}}
<script src="{{url_for('static', filename='plugin-bootstrap-select-1.12.4/js/bootstrap-select.min.js')}}"></script>
{% endblock %}

{% macro show_form_errors(form, container = True) %}
{% if form.errors -%} {# don't output anything if there are no errors #}
{% if container -%}
<div class="row">
<div class="col-sm-12">
{% endif -%}
<div class="alert alert-danger" role="alert">
<p><strong>Errors when validating form data:</strong></p>
{{ wtf.form_errors(form) }}
</div>
{% if container %}
</div>
</div>
{% endif -%}
{% endif -%}
{% endmacro -%}

{% block title %}Mail-Auftrag bearbeiten{% endblock %}

{% block content %}
<div class="container">
<form class="form-horizontal" method="POST">
{{ form.hidden_tag() }}

<div class="form-horizontal">
{{ show_form_errors(form) }}

<div class="form-group row">
{{ form.subject.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{% if readonly %}{{ form.subject(class='form-control', readonly='readonly') }}{% else %}{{ form.subject(class='form-control') }}{% endif %}</div>
</div>
<div class="form-group row{% if form.decision.errors %} has-error{% endif %}">
{{ form.decision.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">
{% if readonly %}
{{form.decision(class='form-control', readonly='readonly')}}
{% else %}
{{form.decision(class='form-control selectpicker', **{'data-live-search':'true', 'data-selected-text-format':'count > 3'})}}
{% endif %}
</div>
</div>
<div class="form-group row">
{{ form.message.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{% if readonly %}{{ form.message(class='form-control', readonly='readonly') }}{% else %}{{ form.message(class='form-control') }}{% endif %}</div>
</div>
<div class="form-group row">
{{ form.comment.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{% if readonly %}{{ form.comment(class='form-control', readonly='readonly') }}{% else %}{{ form.comment(class='form-control') }}{% endif %}</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-3 col-sm-9">
{{ form.submit(class='btn btn-lg btn-default') }}
</div>
</div>
</div>
</form>
</div>
{% endblock %}
56 changes: 56 additions & 0 deletions app/stapf/templates/batches.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{% extends "base.html" %}

{% block title %}Mail-Aufträge{% endblock %}

{% block content %}
<div class="container">
<h1>Mail-Aufträge</h1>

<div class="btn-group" role="group">
<a class="btn btn-default" href="{{url_for('stapf.batch_new')}}">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Neuer Auftrag
</a>
</div>

<br />

<div class="table-responsive">
<table class='table'>
<tr>
<th>Betreff</th>
<th>Versendet</th>
<th>Versendet am</th>
<th>Kommentar</th>
<th>Aktionen</th>
</tr>
{% for batch in batches %}
<tr>
<td>{{ batch.subject }}</td>
<td>{% if batch.sent %}<span class="glyphicon glyphicon-ok" aria-hidden="true"></span>{% endif %}</td>
<td>{% if batch.sent %}{{ batch.sent_at }}{% endif %}</td>
<td>{{ batch.comment }}</td>
<td>
<div class="btn-group" role="group">
{% if not batch.sent %}
<a class="btn-default btn" href="{{url_for('stapf.batch_send', batch_id=batch.id)}}">
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span> Versenden
</a>
<a class="btn-default btn" href="{{url_for('stapf.batch', batch_id=batch.id)}}">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span> Bearbeiten
</a>
<a class="btn-danger btn" href="{{url_for('stapf.batch_delete', batch_id=batch.id)}}">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Löschen
</a>
{% else %}
<a class="btn-default btn" href="{{url_for('stapf.batch', batch_id=batch.id)}}">
<span class="glyphicon glyphicon-search" aria-hidden="true"></span> Details
</a>
{% endif %}
</div>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
{% endblock %}
80 changes: 80 additions & 0 deletions app/stapf/templates/decision.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}

{% block styles %}
{{super()}}
<link rel="stylesheet"
href="{{url_for('static', filename='plugin-bootstrap-select-1.12.4/css/bootstrap-select.min.css')}}">
{% endblock %}

{% block scripts %}
{{super()}}
<script src="{{url_for('static', filename='plugin-bootstrap-select-1.12.4/js/bootstrap-select.min.js')}}"></script>
{% endblock %}

{% macro show_form_errors(form, container = True) %}
{% if form.errors -%} {# don't output anything if there are no errors #}
{% if container -%}
<div class="row">
<div class="col-sm-12">
{% endif -%}
<div class="alert alert-danger" role="alert">
<p><strong>Errors when validating form data:</strong></p>
{{ wtf.form_errors(form) }}
</div>
{% if container %}
</div>
</div>
{% endif -%}
{% endif -%}
{% endmacro -%}

{% block title %}Beschluss bearbeiten{% endblock %}

{% block content %}
<div class="container">
<form class="form-horizontal" method="POST" enctype="multipart/form-data">
{{ form.hidden_tag() }}

<div class="form-horizontal">
{{ show_form_errors(form) }}

<div class="form-group row">
{{ form.title.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{{ form.title(class='form-control') }}</div>
</div>
<div class="form-group row{% if form.decided.errors %} has-error{% endif %}">
{{ form.decided.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{{ form.decided(class='form-control') }}</div>
</div>
<div class="form-group row{% if form.recipients_lists.errors %} has-error{% endif %}">
{{ form.recipients_lists.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">
{{form.recipients_lists(class='form-control selectpicker', **{'data-live-search':'true', 'data-selected-text-format':'count > 3'})}}
</div>
</div>
<div class="form-group row{% if isNew and form.upload.errors %} has-error{% endif %}">
{{ form.upload.label(class='col-sm-3 control-label') }}
{% if isNew %}
<div class="col-sm-9">{{ form.upload() }}</div>
{% else %}
<div class="col-sm-9">
<a class='btn btn-link' href="{{url_for('stapf.decision_file', decision_id=decision_id)}}">
PDF des Beschluss downloaden
</a>
</div>
{% endif %}
</div>
<div class="form-group row">
{{ form.comment.label(class='col-sm-3 control-label') }}
<div class="col-sm-9">{{ form.comment(class='form-control') }}</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-3 col-sm-9">
{{ form.submit(class='btn btn-lg btn-default') }}
</div>
</div>
</div>
</form>
</div>
{% endblock %}
Loading