def run(self, options): logs.begin( saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, nodeName=stampedAPI.node_name ) logs.async_request('alerts') lock = os.path.join(base, 'alerts.lock') if os.path.exists(lock): logs.warning('Locked - aborting') return try: open(lock, 'w').close() self.buildAlerts(limit=options.limit, noop=options.noop) self.buildInvitations(limit=options.limit, noop=options.noop) if len(self._emailQueue) > 0: self.sendEmails(noop=options.noop) if len(self._pushQueue) > 0: self.sendPush(noop=options.noop) self.cleanupPush(noop=options.noop) except Exception as e: logs.warning('Exception: %s' % e) logs.warning(utils.getFormattedException()) logs.error(500) finally: os.remove(lock) try: logs.save() except Exception: print '\n\n\nWARNING: UNABLE TO SAVE LOGS\n\n\n'
def passwordReset(request, **kwargs): token = kwargs.pop('token', None) errorMsg = 'An error occurred. Please try again.' try: # Verify token is valid authUserId = stampedAuth.verifyPasswordResetToken(token) # Check if a form exists with data if request.method == 'POST': # User submitted the form data = request.POST # Verify passwords match and len > 0 if not data['password']: logs.warning("Invalid password: %s" % data['password']) raise StampedInvalidPasswordError("Invalid password") if data['password'] != data['confirm']: logs.warning("Password match failed") raise StampedInvalidPasswordError("Password match failed") # Store password stampedAuth.updatePassword(authUserId, data['password']) # Return success response = HttpResponseRedirect('/settings/password/success') else: # Display 'change password' form account = stampedAPI.getAccount(authUserId) params = { 'token': token, 'form': True, } response = render_to_response('password-reset.html', params) return response except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return render_to_response('password-reset.html', {'error': errorMsg})
def _wrapper(request, *args, **kwargs): import servers.web2.error.views as web_error try: logs.begin(saveLog=stampedAPIProxy.api._logsDB.saveLog, saveStat=stampedAPIProxy.api._statsDB.addStat, requestData=request, nodeName=stampedAPIProxy.api.node_name) logs.info("%s %s" % (request.method, request.path)) subkwargs = kwargs if schema is not None: parse_kwargs = parse_request_kwargs or {} django_kwargs = {} if parse_django_kwargs: django_kwargs = kwargs or {} subkwargs = {} result = parse_request(request, schema(), django_kwargs, overflow=ignore_extra_params, **parse_kwargs) subkwargs['schema'] = result response = fn(request, *args, **subkwargs) logs.info("End request: Success") if no_cache: expires = (dt.datetime.utcnow() - dt.timedelta(minutes=10)).ctime() cache_control = 'no-cache' elif utils.is_ec2(): expires = (dt.datetime.utcnow() + dt.timedelta(minutes=60)).ctime() cache_control = 'max-age=600' else: # disable caching for local development / debugging expires = (dt.datetime.utcnow() - dt.timedelta(minutes=10)).ctime() cache_control = 'max-age=0' response['Expires'] = expires response['Cache-Control'] = cache_control return response except urllib2.HTTPError, e: logs.warning("%s Error: %s" % (e.code, e)) logs.warning(utils.getFormattedException()) if e.code == 404: return web_error.error_404(request) elif e.code >= 500: return web_error.error_500(request) raise # invoke django's default 500 handler
def passwordForgot(request): errorMsg = 'An error occurred. Please try again.' try: # Check if a form exists with data if request.method == 'POST': # User submitted the form data = request.POST # Validate email address email = str(data['forgotemail']).lower().strip() if not utils.validate_email(email): msg = "Invalid format for email address" logs.warning(msg) raise StampedInvalidEmailError("Invalid email address") # Verify account exists try: user = stampedAPI.checkAccount(email) except: ### TODO: Display appropriate error message errorMsg = 'No account information was found for that email address.' raise StampedHTTPError(404, msg="Email address not found", kind='invalid_input') # Send email stampedAuth.forgotPassword(email) # Return success response = HttpResponseRedirect('/settings/password/sent') else: # Display 'submit email' form response = render_to_response('password-forgot.html', None) return response except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return render_to_response('password-forgot.html', {'error': errorMsg}) return True
def alertSettings(request, **kwargs): try: # Check token tokenId = request.GET.get('token', None) authUserId = stampedAuth.verifyEmailAlertToken(tokenId) # Display 'change settings' form account = stampedAPI.getAccount(authUserId) alerts = HTTPAccountAlerts().importSchema(account) user = HTTPUser().importSchema(account) image_url = user['image_url'].replace('.jpg', '-31x31.jpg') stamp_url = 'http://static.stamped.com/logos/%s-%s-credit-18x18.png' % \ (user.color_primary, user.color_secondary) settings = { 'email_alert_credit': alerts.email_alert_credit, 'email_alert_like': alerts.email_alert_like, 'email_alert_fav': alerts.email_alert_fav, 'email_alert_mention': alerts.email_alert_mention, 'email_alert_comment': alerts.email_alert_comment, 'email_alert_reply': alerts.email_alert_reply, 'email_alert_follow': alerts.email_alert_follow, } params = { 'screen_name': user.screen_name, 'name': user.name, 'image_url': image_url, 'stamp_url': stamp_url, 'action_token': tokenId, 'json_settings': json.dumps(settings, sort_keys=True) } return render_to_response('notifications.html', params) except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() ### TODO: CHANGE URL return render_to_response('password-reset.html', {'error': 'FAIL'})
def alertSettingsUpdate(request, **kwargs): try: # Check token tokenId = request.POST.get('token', None) authUserId = stampedAuth.verifyEmailAlertToken(tokenId) def _checkBool(v): if v in ['True', 'true', '1', 1, True]: return True return False # Get settings alerts = { 'email_alert_credit': _checkBool(request.POST.get('email_alert_credit', False)), 'email_alert_like': _checkBool(request.POST.get('email_alert_like', False)), 'email_alert_fav': _checkBool(request.POST.get('email_alert_fav', False)), 'email_alert_mention': _checkBool(request.POST.get('email_alert_mention', False)), 'email_alert_comment': _checkBool(request.POST.get('email_alert_comment', False)), 'email_alert_reply': _checkBool(request.POST.get('email_alert_reply', False)), 'email_alert_follow': _checkBool(request.POST.get('email_alert_follow', False)), } stampedAPI.updateAlerts(authUserId, alerts) params = {} params.setdefault('content_type', 'text/javascript; charset=UTF-8') params.setdefault('mimetype', 'application/json') output_json = json.dumps(alerts, sort_keys=True) output = HttpResponse(output_json, **params) return output except Exception as e: logs.begin( addLog=stampedAPI._logsDB.addLog, saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, ) logs.request(request) logs.warning("500 Error: %s" % e) logs.error(500) logs.save() return HttpResponse("internal server error", status=500)
def wrapper(worker, job): try: k = job.task logs.begin(saveLog=api._logsDB.saveLog, saveStat=api._statsDB.addStat, nodeName=api.node_name) logs.async_request(k) v = functions[k] data = pickle.loads(job.data) logs.info("%s: %s: %s" % (k, v, data)) v(k, data) except Exception as e: logs.error(str(e)) finally: try: logs.save() except Exception: print "Unable to save logs" import traceback traceback.print_exc() logs.warning(traceback.format_exc()) return ""
def wrapper(task_id, key, data, **kwargs): try: logs.begin(saveLog=api._logsDB.saveLog, saveStat=api._statsDB.addStat, nodeName=api.node_name) logs.async_request(key) logs.info("Request %s: %s: %s: %s" % (task_id, key, data, kwargs)) handler(task_id, key, data, **kwargs) logs.info("Finished with request %s" % (task_id,)) except Exception as e: logs.error("Failed request %s" % (task_id,)) logs.report() _warningEmail('%s - %s failed (%s)' % (api.node_name, key, datetime.utcnow().isoformat())) finally: logs.info('Saving request log for request %s' % (task_id,)) try: logs.save() except Exception: print 'Unable to save logs' import traceback traceback.print_exc() logs.warning(traceback.format_exc())
def invoke(request, *args, **kwargs): """ wrapper to invoke a stamped api function in an asynchronous context which adds logging and exception handling. """ taskId = kwargs.pop('taskId', None) try: stampedAPI = getStampedAPI() func = "%sAsync" % utils.getFuncName(1) if not request.is_eager: logs.begin( saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, nodeName=stampedAPI.node_name, ) logs.async_request(func, *args, **kwargs) logs.info("%s %s %s (is_eager=%s, hostname=%s, task_id=%s)" % (func, args, kwargs, request.is_eager, request.hostname, request.id)) getattr(stampedAPI, func)(*args, **kwargs) except Exception as e: logs.error(str(e)) raise finally: try: if taskId is not None: stampedAPI._asyncTasksDB.removeTask(taskId) if not request.is_eager: logs.save() except: pass
def wrapper(request, *args, **kwargs): try: origin = None try: origin = request.META['HTTP_ORIGIN'] valid_origin = origin assert valid_origin in VALID_ORIGINS except Exception: valid_origin = None def _add_cors_headers(response): response['Access-Control-Allow-Origin'] = valid_origin response['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS' response['Access-Control-Max-Age'] = 1000 response['Access-Control-Allow-Headers'] = '*' return response # allow API to gracefully handle cross-domain requests from trusted origins if request.method == 'OPTIONS' and valid_origin is not None: return _add_cors_headers(HttpResponse()) logs.begin(saveLog=stampedAPI._logsDB.saveLog, saveStat=stampedAPI._statsDB.addStat, requestData=request, nodeName=stampedAPI.node_name) logs.info("%s %s" % (request.method, request.path)) if valid_origin is None: if origin is not None: logs.warning("Invalid origin: %s" % origin) else: logs.debug("Origin not included") params = {} params['authUserId'], params['authClientId'] = checkOAuthWithRequest(request, requires_auth) params['client_id'] = checkClient(request, required=requires_client) if parse_request: parse_kwargs = parse_request_kwargs or {} if upload is not None: if http_schema is None: raise Exception("ERROR: handleHTTPRequest requires http_schema if upload is provided") params['http_schema'] = parseFileUpload(http_schema(), request, upload, **parse_kwargs) elif http_schema is not None: params['http_schema'] = parseRequest(http_schema(), request, **parse_kwargs) else: params['http_schema'] = parseRequest(None, request, **parse_kwargs) if conversion is not None: params['schema'] = conversion(params['http_schema']) elif http_schema is not None: params['data'] = params['http_schema'].dataExport() if 'PATH_INFO' in request.META: params['uri'] = request.META['PATH_INFO'].lower() kwargs.update(params) ret = fn(request, *args, **kwargs) logs.info("End request: Success") if valid_origin and isinstance(ret, HttpResponse): _add_cors_headers(ret) return ret except Exception as e: return handleStampedExceptions(e, exceptions) finally: try: logs.save() except Exception: print 'Unable to save logs' import traceback traceback.print_exc() logs.warning(traceback.format_exc())