Exemplo n.º 1
0
def amail():
    form = AMailForm(request.form)
    if request.method == 'POST' and form.validate():
        subject = request.form.get('subject')
        body = request.form.get('body')
        receivers = request.form.get('receivers')
        if receivers == 'accepted':
            attendees = Attendee.query.filter(
                Attendee.accepted
            ).all()
        elif receivers == 'unaccepted':
            attendees = Attendee.query.filter(
                Attendee.accepted == False
            ).all()
        elif receivers == 'confirmed':
            attendees = Attendee.query.filter(
                Attendee.confirmation == 'yes'
            ).all()
        elif receivers == 'unconfirmed':
            attendees = Attendee.query.filter(
                Attendee.confirmation == 'noans'
            ).all()
        elif receivers == 'rejected':
            attendees = Attendee.query.filter(
                Attendee.confirmation == 'no'
            ).all()
        else:
            attendees = Attendee.query.all()
        count_mails = 0
        with mail.connect() as conn:
            for user in attendees:
                count_mails += 1
                msg = Message(recipients=[user.email],
                              body=body,
                              subject=subject)
                conn.send(msg)
        new_entry = MailHistory()
        new_entry.recivers = receivers
        new_entry.body = body
        new_entry.subject = subject
        new_entry.who_send = current_user.username
        db.session.add(new_entry)
        db.session.commit()
        flash("You succesfully send {} e-mail to all {} attendees".format(
            count_mails, receivers
        ))
    return render_template('amail.html', form=form)
Exemplo n.º 2
0
def send_confirmation(state):
    global EMAIL_ACCEPTED
    if not current_user.poweruser:
        return redirect('overview')
    if state == 'rest':
        attendees = Attendee.query.filter(and_(
            Attendee.accepted,
            Attendee.confirmation == 'noans'
        )
        ).all()
        EMAIL_ACCEPTED = EMAIL_ACCEPTED.replace(
            'środy 18 listopada', 'niedzili 22 listopada'
        )
    else:
        attendees = Attendee.query.filter(
            Attendee.accepted
        ).all()
    count_mails = 0
    with mail.connect() as conn:
        for user in attendees:
            thash = hashlib.sha224(user.email).hexdigest()
            user.ssh_tag = thash
            yes_url = "{}{}".format(
                server_url(),
                url_for('confirmation', answer='yes', ctag=thash)
            )
            no_url = "{}{}".format(
                server_url(),
                url_for('confirmation', answer='no', ctag=thash)
            )
            subject = "PyCode Carrots Poznań: Wybraliśmy właśnie Ciebie"
            count_mails += 1
            msg = Message(
                recipients=[user.email],
                html=EMAIL_ACCEPTED.format(
                    **{'yes': yes_url, 'no': no_url}
                ),
                subject=subject
            )
            conn.send(msg)
    db.session.commit()
    flash("You succesfully send {} e-mail to all accepted attendees".format(
        count_mails
    ))
    return redirect('overview')