Exemplo n.º 1
0
def check_viz():
    if 'viz_id' in request.args:
        viz_id = request.args['viz_id']
        return viz_qa(viz_id)
    else:
        background = requests.get('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US')
        bkgd_json = background.json()
        img_url = 'http://www.bing.com' + bkgd_json['images'][0]['url']
        html_string = '''
            <!doctype html>
            <head>
                <title>Q/A</title>
                <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" media="screen">
                <style>
                    body {
                    background: url(''' + img_url + ''') no-repeat center center fixed;
                    -webkit-background-size: cover;
                    -moz-background-size: cover;
                    -o-background-size: cover;
                    background-size: cover;
                    }
                    .container, .container-box {
                    text-align: center;
                    margin: auto;
                    background: white;
                    padding: 1px;
                    padding-bottom: 20px;
                    margin-top: 25px;
                    display: block;
                    }
                    .container-box {
                    width: 230px;
                    }
                    form {
                        display: inline-block;
                        left-margin: auto;
                        right-margin: auto;
                    }
                    table {
                        margin: auto;
                        margin-top: 30px;
                        color: #333;
                        font-family: monospace;
                        width: 640px;
                        border-collapse:
                        collapse; border-spacing: 0;
                    }

                    td, th {
                        border: 1px solid #CCC;
                        height: 30px;
                    }

                    th {
                        background: #F3F3F3;
                        font-weight: bold;
                        text-align: left;
                        padding: 5px;
                    }

                    td {
                        background: #FAFAFA;
                        padding: 5px;
                    }
                </style>
            </head>
            <div class="container-box">
                <h1>Viz URL</h1>
                <form role="form" method='POST' action='/qa'>
                    <div class="form-group">
                      <input type="text" name="url" class="form-control" id="url-box" placeholder="Enter URL..." style="max-width: 300px;" autofocus required>
                    </div>
                    <button type="submit" class="btn btn-default">Submit</button>
                </form>
            </div>
                '''
        if request.method == "POST":
            try:
                viz_url = request.form['url']
                json_pat = re.compile(r'(.*.com\/w\/|.*\/preview\/)(\w*)')
                matches = json_pat.findall(viz_url)
                viz_id = matches[0][1]
            except:
                return "There was an error!"
            try:
                viz = Viz(viz_id)
                errors = viz.json_response()
                html_string += '''
                            <table>
                            <tr>
                                <th>Error Type</th>
                                <th>Error Message</th>
                            </tr>
                            '''
                # Parse error json and create 1 sentence per error, storing each sentence in error_list
                for item in errors:
                    for key, value in item.iteritems():
                        error_type = key
                        if error_type == 'Timeline errors':
                            for slide, messages in value.iteritems():
                                for message in messages:
                                    html_string = html_string + "<tr><td>" + slide + "</td><td>" + message.encode("utf-8") + "</td></tr>"
                        else:
                            for error_val in value:
                                try:
                                    html_string = html_string + '<tr><td>' + error_type + '</td><td>' + error_val.encode('utf-8') + '</td></tr>'

                                except:
                                    for sub_error in error_val:
                                        html_string = html_string + '<tr><td>' + error_type + '</td><td>' + sub_error.encode('utf-8') + '</td></tr>'
                html_string = html_string + '</table><br><div class="ftb-widget" data-widget-id="' + viz_id + '"></div><script async src="https://s.graphiq.com/rx/widgets.js"></script>'
            except:
                return 'There was an error with the API! Let Corie know.'
                html_string = html_string + viz_qa(viz_id)
        return html_string
Exemplo n.º 2
0
def initialize_qapi():
    background = requests.get('http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US')
    bkgd_json = background.json()
    img_url = 'http://www.bing.com' + bkgd_json['images'][0]['url']
    html_string = '''
            <!doctype html>
            <head>
                <title>Q/A</title>
                <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" media="screen">
                <style>
                body {
                background: url(''' + img_url + ''') no-repeat center center fixed;
                -webkit-background-size: cover;
                -moz-background-size: cover;
                -o-background-size: cover;
                background-size: cover;
                }
                .container, .container-box {
                text-align: center;
                margin: auto;
                background: white;
                padding: 1px;
                padding-bottom: 20px;
                margin-top: 25px;
                display: block;
                }
                .container {
                width: 450px;
                }
                .container-box {
                width: 230px;
                }
                form {
                    display: inline-block;
                    left-margin: auto;
                    right-margin: auto;
                }
                </style>
                </head>
                <div class="container-box">
                    <h1>Viz URL</h1>
                    <form role="form" method='POST' action='/qapi'>
                        <div class="form-group">
                          <input type="text" name="url" class="form-control" id="url-box" placeholder="Enter URL..." style="max-width: 300px;" autofocus required>
                        </div>
                        <button type="submit" class="btn btn-default">Submit</button>
                    </form>
                </div>
                '''
    if request.method == "POST":
        try:
            viz_url = request.form['url']
            json_pat = re.compile(r'(.*.com\/w\/|.*\/preview\/)(\w*)')
            matches = json_pat.findall(viz_url)
            viz_id = matches[0][1]
        except:
            return 'Error parsing viz ID from URL. Make sure to submit a viz preview link!'
        try:
            viz = Viz(viz_id)
            errors = viz.json_response()
            error_list = []
            # Parse error json and create 1 sentence per error, storing each sentence in error_list
            for item in errors:
                for key, value in item.iteritems():
                    error_type = key
                    if error_type == 'Timeline errors':
                        for slide, messages in value.iteritems():
                            for message in messages:
                                error = slide + ': ' + message.encode('utf-8')
                                error_list.append(error)
                    else:
                        for error_val in value:
                            try:
                                error = error_type + ': ' + error_val.encode('utf-8')
                                error_list.append(error)
                            except:
                                for sub_error in error_val:
                                    error = error_type + ': ' + sub_error.encode('utf-8')
                                    error_list.append(error)
            # Create new QA task
            task_data = '"{\\"data\\": {  \\"notes\\" : \\"' + viz_url + '\\" ,  \\"projects\\" : \\"244458016124389\\" , \\"name\\" : \\"' + viz.title + '\\" } }"'
            task_shell_command = '/usr/bin/curl -s --request POST -H "Authorization: Bearer 0/a3244bb3177f3e0c7242c459c4324863" -H "Content-Type: application/json" https://app.asana.com/api/1.0/tasks -d ' + task_data
            new_task = subprocess.check_output(task_shell_command, shell=True)
            new_task_id = json.loads(new_task)['data']['id']
            task_url = 'https://app.asana.com/0/244458016124389/' + str(new_task_id)
            subtask_url = 'https://app.asana.com/api/1.0/tasks/' + str(new_task_id) + '/subtasks'
            html_string = html_string + '<div class="container">' + task_url + '<br><a href="' + task_url + '" target="_blank">Go!</a></div>'

            # Post each error to Asana
            for error_text in sorted(error_list, reverse=True):
                subtask_data = '"{\\"data\\": {  \\"name\\" : \\"' + error_text + '\\"  } }"'
                subtask_shell_command = '/usr/bin/curl -s --request POST -H "Authorization: Bearer 0/a3244bb3177f3e0c7242c459c4324863" -H "Content-Type: application/json" ' + subtask_url + ' -d ' + subtask_data
                subprocess.check_output(subtask_shell_command, shell=True)

            # QA 1 and 2 subtasks
            qa2_shell = '/usr/bin/curl -s --request POST -H "Authorization: Bearer 0/a3244bb3177f3e0c7242c459c4324863" -H "Content-Type: application/json" ' + subtask_url + ' -d "{\\"data\\": {  \\"name\\" : \\"QA 2\\"  } }"'
            qa1_shell = '/usr/bin/curl -s --request POST -H "Authorization: Bearer 0/a3244bb3177f3e0c7242c459c4324863" -H "Content-Type: application/json" ' + subtask_url + ' -d "{\\"data\\": {  \\"name\\" : \\"QA 1\\"  } }"'
            subprocess.check_output(qa2_shell, shell=True)
            subprocess.check_output(qa1_shell, shell=True)
        except:
            return 'There was an error with the API- let Corie know!'
    return html_string