# 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
        self.html = html
        self.user_agent = user_agent

def decode_base64_uri(base64_encoded_data_uri):
    start = base64_encoded_data_uri.find(',') + 1
    encoded = base64_encoded_data_uri[start:]
    return base64.decodestring(encoded)

def rest__send_user_feedback__email():
    """
    REST API endpoint: send user feedback by email along with screen capture attachments
    """

    def sanitize_input(req):
        req_dict = req.get_json()
        url = req_dict['url']
        note = req_dict['note']
        img = decode_base64_uri(req_dict['img'])
        html = req_dict['html']
        user_agent = req_dict['browser']['userAgent']
        return RZ_User_Feedback(url=url,
                                note=note,
                                img=img,
                                html=html,
                                user_agent=user_agent)

    try:
        u_feedback = sanitize_input(request)
    except:
        log.warn('failed to sanitize inputs. request: %s' % request)

# 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

        self.html = html
        self.user_agent = user_agent

def decode_base64_uri(base64_encoded_data_uri):
    start = base64_encoded_data_uri.find(',') + 1
    encoded = base64_encoded_data_uri[start:]
    return base64.decodestring(encoded)

def rest__send_user_feedback__email():
    """
    REST API endpoint: send user feedback by email along with screen capture attachments
    """

    def sanitize_input(req):
        req_dict = req.get_json()
        url = req_dict['url']
        note = req_dict['note']
        img = decode_base64_uri(req_dict['img'])
        html = req_dict['html']
        user_agent = req_dict['browser']['userAgent']
        return RZ_User_Feedback(url=url,
                                note=note,
                                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