# Path: rhizi/rz_mail.py
# def send_email__flask_ctx(recipients, subject, body, attachments=[]):
#     """
#     Flask context dependent email sending utility
#     """
# 
#     send_from = current_app.rz_config.mail_default_sender
#     mta_host = current_app.rz_config.mta_host
#     mta_port = current_app.rz_config.mta_port
# 
#     send_email(mta_host,
#                         mta_port,
#                         send_from=send_from,
#                         recipients=recipients,
#                         subject=subject,
#                         attachments=attachments,
#                         body=body)
# 
#     log.info('email sent: recipients: %s: subject: %s, attachment-count: %d' % (recipients, subject, len(attachments)))
# 
# Path: rhizi/rz_req_handling.py
# def make_response__json(status=200, data={}):
#     """
#     Construct a json response with proper content-type header
# 
#     @param data: must be serializable via json.dumps
#     """
#     data_str = json.dumps(data)
#     resp = make_response(data_str)
#     resp.headers['Content-Type'] = "application/json"
#     resp.status = str(status)
# 
#     return resp
# 
# HTTP_STATUS__500_INTERNAL_SERVER_ERROR = 500


import base64
import logging
from flask import current_app
from flask import json
from flask import request
from flask import session
from .rz_mail import send_email__flask_ctx
from .rz_req_handling import make_response__json, HTTP_STATUS__500_INTERNAL_SERVER_ERROR
                                img=img,
                                html=html,
                                user_agent=user_agent)

    try:
        u_feedback = sanitize_input(request)
    except:
        log.warn('failed to sanitize inputs. request: %s' % request)
        return make_response__json(status=400)  # bad Request

    # FIXME: should be async via celery (or another method)
    session_user = session.get('username')

    msg_body = ['Feedback from user:',
                     '',
                     'user: %s' % (session_user if session_user else "<not-logged-in>"),
                     'user-agent: %s' % (u_feedback.user_agent),
                     'watching URL: %s' % (u_feedback.url),
                     'user-note: %s' % (u_feedback.note),
                     ''
                     ]
    msg_body = '\n'.join(msg_body)

    try:
        send_email__flask_ctx(recipients=[current_app.rz_config.feedback_recipient],
                           subject="User Feedback",
                           body=msg_body,
                           attachments=[('feedback_screenshot.png', 'image/png', u_feedback.img),
                                        ('feedback_page.html', 'text/html', u_feedback.html.encode('utf-8')),
                                        ])

# Path: rhizi/rz_mail.py
# def send_email__flask_ctx(recipients, subject, body, attachments=[]):
#     """
#     Flask context dependent email sending utility
#     """
# 
#     send_from = current_app.rz_config.mail_default_sender
#     mta_host = current_app.rz_config.mta_host
#     mta_port = current_app.rz_config.mta_port
# 
#     send_email(mta_host,
#                         mta_port,
#                         send_from=send_from,
#                         recipients=recipients,
#                         subject=subject,
#                         attachments=attachments,
#                         body=body)
# 
#     log.info('email sent: recipients: %s: subject: %s, attachment-count: %d' % (recipients, subject, len(attachments)))
# 
# Path: rhizi/rz_req_handling.py
# def make_response__json(status=200, data={}):
#     """
#     Construct a json response with proper content-type header
# 
#     @param data: must be serializable via json.dumps
#     """
#     data_str = json.dumps(data)
#     resp = make_response(data_str)
#     resp.headers['Content-Type'] = "application/json"
#     resp.status = str(status)
# 
#     return resp
# 
# HTTP_STATUS__500_INTERNAL_SERVER_ERROR = 500


# Path: rhizi/rz_feedback.py
import base64
import logging
from flask import current_app
from flask import json
from flask import request
from flask import session
from .rz_mail import send_email__flask_ctx
from .rz_req_handling import make_response__json, HTTP_STATUS__500_INTERNAL_SERVER_ERROR

                                img=img,
                                html=html,
                                user_agent=user_agent)

    try:
        u_feedback = sanitize_input(request)
    except:
        log.warn('failed to sanitize inputs. request: %s' % request)
        return make_response__json(status=400)  # bad Request

    # FIXME: should be async via celery (or another method)
    session_user = session.get('username')

    msg_body = ['Feedback from user:',
                     '',
                     'user: %s' % (session_user if session_user else "<not-logged-in>"),
                     'user-agent: %s' % (u_feedback.user_agent),
                     'watching URL: %s' % (u_feedback.url),
                     'user-note: %s' % (u_feedback.note),
                     ''
                     ]
    msg_body = '\n'.join(msg_body)

    try:
        send_email__flask_ctx(recipients=[current_app.rz_config.feedback_recipient],
                           subject="User Feedback",
                           body=msg_body,
                           attachments=[('feedback_screenshot.png', 'image/png', u_feedback.img),
                                        ('feedback_page.html', 'text/html', u_feedback.html.encode('utf-8')),
                                        ])

        return make_response__json()  # return empty json response