def render_GET(self, request): try: token = request.args.get('token', None)[0] auth = request.args.get('auth', None)[0] canarydrop = Canarydrop(**get_canarydrop(canarytoken=token)) if not canarydrop['auth'] or canarydrop['auth'] != auth: raise NoCanarytokenPresent() if canarydrop.get('triggered_list', None): for timestamp in canarydrop['triggered_list'].keys(): formatted_timestamp = datetime.datetime.fromtimestamp( float(timestamp)).strftime('%Y %b %d %H:%M:%S') canarydrop['triggered_list'][ formatted_timestamp] = canarydrop[ 'triggered_list'].pop(timestamp) if canarydrop.get('memo'): canarydrop['memo'] = unicode(canarydrop['memo'], "utf8") except (TypeError, NoCanarytokenPresent): return NoResource().render(request) g_api_key = get_canary_google_api_key() template = env.get_template('history.html') return template.render(canarydrop=canarydrop, API_KEY=g_api_key).encode('utf8')
def render_POST(self, request): try: try: token = request.args.get('token', None)[0] auth = request.args.get('auth', None)[0] canarydrop = Canarydrop(**get_canarydrop(canarytoken=token)) if not canarydrop['auth'] or canarydrop['auth'] != auth: raise NoCanarytokenPresent() except (IndexError, TypeError, NoCanarytokenPresent): return NoResource().render(request) try: email_enable_status = request.args.get('email_enable', None)[0] == "on" except (TypeError, IndexError): email_enable_status = False try: webhook_enable_status = request.args.get('webhook_enable', None)[0] == "on" except (TypeError, IndexError): webhook_enable_status = False try: sms_enable_status = request.args.get('sms_enable', None)[0] == "on" except (TypeError, IndexError): sms_enable_status = False try: web_image_status = request.args.get('web_image_enable', None)[0] == "on" except (TypeError, IndexError): web_image_status = False try: token_fmt = request.args.get('fmt', None)[0] except (TypeError, IndexError): token_fmt = '' canarydrop['alert_email_enabled'] = email_enable_status canarydrop['alert_webhook_enabled'] = webhook_enable_status canarydrop['alert_sms_enabled'] = sms_enable_status canarydrop['web_image_enabled'] = web_image_status save_canarydrop(canarydrop=canarydrop) g_api_key = get_canary_google_api_key() template = env.get_template('manage.html') return template.render(canarydrop=canarydrop, saved=True, settings=settings, API_KEY=g_api_key).encode('utf8') except Exception as e: import traceback log.err('Exception in manage.html: {e}, {stack}'.format(e=e, stack=traceback.format_exc())) template = env.get_template('manage.html') return template.render(canarydrop=canarydrop, error=e, settings=settings).encode('utf8')
def render_GET(self, request): try: token = request.args.get('token', None)[0] auth = request.args.get('auth', None)[0] canarydrop = Canarydrop(**get_canarydrop(canarytoken=token)) if not canarydrop['auth'] or canarydrop['auth'] != auth: raise NoCanarytokenPresent() if canarydrop.get('triggered_list', None): for timestamp in canarydrop['triggered_list'].keys(): formatted_timestamp = datetime.datetime.fromtimestamp( float(timestamp)).strftime('%Y %b %d %H:%M:%S') canarydrop['triggered_list'][formatted_timestamp] = canarydrop['triggered_list'].pop(timestamp) except (TypeError, NoCanarytokenPresent): return NoResource().render(request) g_api_key = get_canary_google_api_key() template = env.get_template('manage.html') return template.render(canarydrop=canarydrop, API_KEY=g_api_key).encode('utf8')
def render_POST(self, request): try: try: token = request.args.get('token', None)[0] auth = request.args.get('auth', None)[0] canarydrop = Canarydrop(**get_canarydrop(canarytoken=token)) if not canarydrop['auth'] or canarydrop['auth'] != auth: raise NoCanarytokenPresent() except (IndexError, NoCanarytokenPresent): return NoResource().render(request) try: email_enable_status = request.args.get('email_enable', None)[0] == "on" except (TypeError, IndexError): email_enable_status = False try: webhook_enable_status = request.args.get('webhook_enable', None)[0] == "on" except (TypeError, IndexError): webhook_enable_status = False try: sms_enable_status = request.args.get('sms_enable', None)[0] == "on" except (TypeError, IndexError): sms_enable_status = False try: web_image_status = request.args.get('web_image_enable', None)[0] == "on" except (TypeError, IndexError): web_image_status = False try: token_fmt = request.args.get('fmt', None)[0] except (TypeError, IndexError): token_fmt = '' canarydrop['alert_email_enabled'] = email_enable_status canarydrop['alert_webhook_enabled'] = webhook_enable_status canarydrop['alert_sms_enabled'] = sms_enable_status canarydrop['web_image_enabled'] = web_image_status if token_fmt == 'web_image': if not settings.WEB_IMAGE_UPLOAD_PATH: raise Exception("Image upload not supported, set CANARY_WEB_IMAGE_UPLOAD_PATH in frontend.env.") fields = cgi.FieldStorage( fp = request.content, headers = request.getAllHeaders(), environ = {'REQUEST_METHOD':'POST', 'CONTENT_TYPE': request.getAllHeaders()['content-type'], } ) filename = fields['web_image'].filename filebody = fields['web_image'].value if len(filebody) > settings.MAX_UPLOAD_SIZE: raise Exception('File too large') if not filename.lower().endswith(('.png','.gif','.jpg')): raise Exception('Uploaded image must be a PNG, GIF or JPG') ext = filename.lower()[-4:] #create a random local filename r = hashlib.md5(os.urandom(32)).hexdigest() filepath = os.path.join(settings.WEB_IMAGE_UPLOAD_PATH, r[:2], r[2:])+ext if not os.path.exists(os.path.dirname(filepath)): try: os.makedirs(os.path.dirname(filepath)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise with open(filepath, "w") as f: f.write(filebody) canarydrop['web_image_enabled'] = True canarydrop['web_image_path'] = filepath save_canarydrop(canarydrop=canarydrop) g_api_key = get_canary_google_api_key() template = env.get_template('manage.html') return template.render(canarydrop=canarydrop, saved=True, settings=settings, API_KEY=g_api_key).encode('utf8') except Exception as e: import traceback log.err('Exception in manage.html: {e}, {stack}'.format(e=e, stack=traceback.format_exc())) template = env.get_template('manage.html') return template.render(canarydrop=canarydrop, error=e, settings=settings).encode('utf8')