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 all 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
3 changes: 3 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 All @@ -63,6 +65,7 @@ def __repr__(self):
return "<Uni: {}>".format(self.name)

class Mascot(db.Model):
__bind_key__ = 'anmeldung'
id = db.Column(db.Integer(), primary_key = True)
name = db.Column(db.Text(), unique = False)
uni_id = db.Column(db.Integer(), db.ForeignKey('uni.id'))
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 %}
113 changes: 113 additions & 0 deletions app/stapf/templates/batches.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{% extends "base.html" %}

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

{% block scripts %}
{{ super() }}
<script type="text/javascript">
// Adapted from http://jsfiddle.net/ukW2C/3/
$("#search").keyup(function () {
//split the current value of search
var data = this.value.split(" ");

//create a jquery object of the rows
var jo = $("#batches").find("tr");
if (this.value == "") {
jo.show();
$("#search-group").removeClass("has-error");
$("#search-group").removeClass("has-success");
return;
}
//hide all the rows
jo.hide();

//Recusively filter the jquery object to get results.
filtered = jo.filter(function (i, v) {
var $t = $(this);
for (var d = 0; d < data.length; ++d) {
if ($t.is(":contains('" + data[d] + "')")) {
return true;
}
}
return false;
});
//show the rows that match.
filtered.show();

//update the search bar validation status
if (filtered.length) {
$("#search-group").addClass("has-success");
$("#search-group").removeClass("has-error");
} else {
$("#search-group").addClass("has-error");
$("#search-group").removeClass("has-success");
}
}).focus(function () {
this.value = "";
$(this).unbind('focus');
});
</script>
{% endblock %}

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

<div id="search-group" class="form-group has-feedback">
<label class="control-label sr-only" for="search">Suche für Adressaten</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></span>
<input id="search" type="text" class="form-control input-lg" placeholder="Suche">
</div>
</div>

<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>
<tbody id="batches">
{% 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 %}
</tbody>
</table>
</div>
</div>
{% endblock %}
Loading