Coverage for airflow/www/utils.py: 79%

# Coverage for airflow/www/utils.py : 79%

## 85 statements 67 run 18 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | from future import standard_library standard_library.install_aliases() from builtins import str from builtins import object from cgi import escape from io import BytesIO as IO import functools import gzip import dateutil.parser as dateparser import json import os from flask import after_this_request, request, Response from flask.ext.login import current_user from jinja2 import Template import wtforms from wtforms.compat import text_type from airflow import configuration, models, settings, utils AUTHENTICATE = configuration.getboolean('webserver', 'AUTHENTICATE') class LoginMixin(object): def is_accessible(self): return ( not AUTHENTICATE or ( not current_user.is_anonymous() and current_user.is_authenticated() ) ) class SuperUserMixin(object): def is_accessible(self): return ( not AUTHENTICATE or (not current_user.is_anonymous() and current_user.is_superuser()) ) class DataProfilingMixin(object): def is_accessible(self): return ( not AUTHENTICATE or (not current_user.is_anonymous() and current_user.data_profiling()) ) def limit_sql(sql, limit, conn_type): sql = sql.strip() sql = sql.rstrip(';') if sql.lower().startswith("select"): if conn_type in ['mssql']: sql = """\ SELECT TOP {limit} * FROM ( {sql} ) qry """.format(**locals()) elif conn_type in ['oracle']: sql = """\ SELECT * FROM ( {sql} ) qry WHERE ROWNUM <= {limit} """.format(**locals()) else: sql = """\ SELECT * FROM ( {sql} ) qry LIMIT {limit} """.format(**locals()) return sql def action_logging(f): ''' Decorator to log user actions ''' @functools.wraps(f) def wrapper(*args, **kwargs): session = settings.Session() if current_user and hasattr(current_user, 'username'): user = current_user.username else: user = 'anonymous' log = models.Log( event=f.__name__, task_instance=None, owner=user, extra=str(request.args.items()), task_id=request.args.get('task_id'), dag_id=request.args.get('dag_id')) if 'execution_date' in request.args: log.execution_date = dateparser.parse( request.args.get('execution_date')) session.add(log) session.commit() return f(*args, **kwargs) return wrapper def notify_owner(f): ''' Decorator to notify owner of actions taken on their DAGs by others ''' @functools.wraps(f) def wrapper(*args, **kwargs): """ if request.args.get('confirmed') == "true": dag_id = request.args.get('dag_id') task_id = request.args.get('task_id') dagbag = models.DagBag( os.path.expanduser(configuration.get('core', 'DAGS_FOLDER'))) dag = dagbag.get_dag(dag_id) task = dag.get_task(task_id) if current_user and hasattr(current_user, 'username'): user = current_user.username else: user = 'anonymous' if task.owner != user: subject = ( 'Actions taken on DAG {0} by {1}'.format( dag_id, user)) items = request.args.items() content = Template(''' action: <i>{{ f.__name__ }}</i><br> <br> <b>Parameters</b>:<br> <table> {% for k, v in items %} {% if k != 'origin' %} <tr> <td>{{ k }}</td> <td>{{ v }}</td> </tr> {% endif %} {% endfor %} </table> ''').render(**locals()) if task.email: utils.send_email(task.email, subject, content) """ return f(*args, **kwargs) return wrapper def json_response(obj): """ returns a json response from a json serializable python object """ return Response( response=json.dumps( obj, indent=4, cls=utils.AirflowJsonEncoder), status=200, mimetype="application/json") def gzipped(f): ''' Decorator to make a view compressed ''' @functools.wraps(f) def view_func(*args, **kwargs): @after_this_request def zipper(response): accept_encoding = request.headers.get('Accept-Encoding', '') if 'gzip' not in accept_encoding.lower(): return response response.direct_passthrough = False if (response.status_code < 200 or response.status_code >= 300 or 'Content-Encoding' in response.headers): return response gzip_buffer = IO() gzip_file = gzip.GzipFile(mode='wb', fileobj=gzip_buffer) gzip_file.write(response.data) gzip_file.close() response.data = gzip_buffer.getvalue() response.headers['Content-Encoding'] = 'gzip' response.headers['Vary'] = 'Accept-Encoding' response.headers['Content-Length'] = len(response.data) return response return f(*args, **kwargs) return view_func def make_cache_key(*args, **kwargs): ''' Used by cache to get a unique key per URL ''' path = request.path args = str(hash(frozenset(request.args.items()))) return (path + args).encode('ascii', 'ignore') class AceEditorWidget(wtforms.widgets.TextArea): """ Renders an ACE code editor. """ def __call__(self, field, **kwargs): kwargs.setdefault('id', field.id) html = ''' <div id="{el_id}" style="height:100px;">{contents}</div> <textarea id="{el_id}_ace" name="{form_name}" style="display:none;visibility:hidden;"> </textarea> '''.format( el_id=kwargs.get('id', field.id), contents=escape(text_type(field._value())), form_name=field.id, ) return wtforms.widgets.core.HTMLString(html)