Exemplo n.º 1
0
def message(message_id):
    if request.args.get('lang') == 'french':
        message_form = FrenchMessageForm()
    else:
        message_form = MessageForm()
    message = Messages.query.filter_by(IDmessage=message_id).first_or_404()
    if message_form.validate_on_submit():
        print message_form.notifyemail.data
        last_reply = Replies.query.with_entities(
            Replies.name,
            Replies.email,
            Replies.notifyemail
        ).filter_by(
            IDmessage=message_id
        ).order_by(Replies.rdate.desc()).first()
        if not last_reply:
            last_reply = message
        new_reply = Replies(
            message_id,
            message_form.subject.data,
            message_form.name.data,
            message_form.email.data,
            message_form.notifyemail.data,
            message_form.message.data,
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        )
        db.session.add(new_reply)
        db.session.commit()
        db.session.query(Messages).filter_by(
            IDmessage=message_id
        ).update({
            'last_rdate': time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
        })
        msg = Message()
        if last_reply.notifyemail == 'True':
            msg.recipients = [last_reply.email]
        msg.sender = "*****@*****.**"
        msg.bcc = ['*****@*****.**']
        msg.subject = 'Swing Paints Forum Reply'
        msg.html = 'Hello ' + \
            last_reply.name + \
            ',<br />' + \
            new_reply.name +\
            ' has posted a reply to your message.<br />Click <a href="' + \
            url_for(
                'message',
                message_id=message_id,
                _external=True
            ) + \
            '">here</a> to view the message board.<br />Yours sincerely,<br />\
            Swing Paints'
        mail.send(msg)
        flash('success', 'Success! Your reply has been posted to the forum.')
        return redirect(url_for(
            'message',
            message_id=message_id,
            _anchor=str(
                time.strftime(
                    '%b %d, %Y<br />%H:%M:%S',
                    time.strptime(str(new_reply.rdate), '%Y-%m-%d %H:%M:%S')
                )
            )
        ))
    message.date = time.strftime(
        '%b %d, %Y<br />%H:%M:%S',
        time.strptime(str(message.mdate), '%Y-%m-%d %H:%M:%S')
    )
    replies = Replies.query.filter_by(
        IDmessage=message_id
    ).order_by(Replies.rdate.asc()).all()
    for reply in replies:
        reply.date = time.strftime(
            '%b %d, %Y<br />%H:%M:%S',
            time.strptime(str(reply.rdate), '%Y-%m-%d %H:%M:%S')
        )
    flash_errors(message_form)
    return sidebar_lang_render(
        'message',
        request,
        message=message,
        replies=replies,
        message_form=message_form
    )
Exemplo n.º 2
0
def forum(page=1):
    if request.args.get('lang') == 'french':
        message_form = FrenchMessageForm()
    else:
        message_form = MessageForm()
    if message_form.validate_on_submit():
        new_message = Messages(
            message_form.subject.data,
            message_form.name.data,
            message_form.email.data,
            message_form.notifyemail.data,
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
            message_form.message.data,
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        )
        db.session.add(new_message)
        db.session.commit()
        msg = Message()
        msg.sender = "*****@*****.**"
        msg.recipients = ['*****@*****.**']
        msg.subject = 'Swing Paints Forum Message'
        msg.html = 'Hello Mark,<br />' + \
            message_form.name.data + \
            ' has posted a new message.<br />Click <a href="' + \
            url_for(
                'message',
                message_id=new_message.IDmessage,
                _external=True
            ) + \
            '">here</a> to view the message board.<br />Yours sincerely,\
            <br />Swing Paints'
        mail.send(msg)
        flash('success', 'Success! Your message has been posted to the forum.')
        if request.args.get('lang') == 'french':
            return redirect(url_for(
                'message',
                message_id=new_message.IDmessage,
                lang='french'
            ))
        else:
            return redirect(url_for(
                'message',
                message_id=new_message.IDmessage
            ))
    messages = Messages.query.order_by(
        Messages.last_rdate.desc()
    ).paginate(int(page), 50)
    for message in messages.items:
        message.replies = Replies.query.filter_by(
            IDmessage=message.IDmessage
        ).count()
        message.date = time.strftime(
            '%H:%M %m/%d/%Y',
            time.strptime(str(message.last_rdate), '%Y-%m-%d %H:%M:%S')
        )
    flash_errors(message_form)
    return sidebar_lang_render(
        'forum',
        request,
        messages=messages,
        message_form=message_form
    )