def get_validation_token(self, uid, email): user_info = self._db.get('%s%s' % (self.USER_DB, uid)) if user_info is None: logger.warn('No user information found for uid %s' % uid) return None user = cjson.decode(user_info) aemail = email.encode('ascii') if (aemail in user.get('emails', {}) and user['emails'][aemail].get('state', None) == 'pending'): return user['emails'][aemail].get('conf_code', None) logger.info('No validation token found for uid %s ' % uid) return None
def remove_email(self, uid, email, state = 'pending'): user = self._user_db[uid] if (email in user['emails'] and user['emails'][email].get('state', None) == state): rtoken = user['emails'][email].get('conf_code', None) try: del user['emails'][email] self.set_user_info(uid, user) if rtoken: self._validate_db.remove({'_id': rtoken}) except (KeyError, OperationFailure): logger.warn('Could not remove email %s from user %s' % (email, uid)); return False return True
def _create_response(self, request, result, function): if not isinstance(result, basestring): # result is already a Response return result response = getattr(request, 'response', None) if response is None: response = Response(result) elif isinstance(result, str): response.body = result else: # if it's not str it's unicode, which really shouldn't happen module = getattr(function, '__module__', 'unknown') name = getattr(function, '__name__', 'unknown') logger.warn('Unicode response returned from: %s - %s' % (module, name)) response.unicode_body = result return response
def _load_model(self): """ Load a trained model if one already exists for a given sku. Otherwise, return None. return model: a previously trained model or None #TODO: could models belonging to products from different merchants #TODO: have conflicting names due to identical SKUs? How likely is this? """ self.path += "/" + self.sku model = None try: model = Doc2Vec.load(self.path) logger.info("Model successfully loaded") except IOError: logger.warn("Model not found") return model
def remove_email(self, uid, email, state = 'pending'): user_info = self._db.get('%s%s' % (self.USER_DB, uid)) if user_info is None: logger.warn('No user information found for uid %s' % uid) return False user = cjson.decode(user_info) if (email in user.get('emails', {}) and user['emails'][email].get('state', None) == state): rtoken = user['emails'][email].get('conf_code', None) try: del user['emails'][email] self.set_user_info(uid, user) if rtoken: self._db.delete('validate_%s' % rtoken) except KeyError, ex: logger.warn("Could not remove email " + "address [%s] from uid [%s] [%s]" % (email, uid, str(ex))) return False
def __call__(self, request): if request.method in ('HEAD',): raise HTTPBadRequest('"%s" not supported' % request.method) request.server_time = round_time() # gets request-specific config request.config = self._host_specific(request.host, self.config) # pre-hook before_headers = self._before_call(request) # XXX # removing the trailing slash - ambiguity on client side url = request.path_info.rstrip('/') if url != '': request.environ['PATH_INFO'] = request.path_info = url if (self.heartbeat_page is not None and url == '/%s' % self.heartbeat_page): return self._heartbeat(request) if self.debug_page is not None and url == '/%s' % self.debug_page: return self._debug(request) match = self.mapper.routematch(environ=request.environ) if match is None: return HTTPNotFound() match, __ = match # authentication control if self.auth is not None: self.auth.check(request, match) function = self._get_function(match['controller'], match['action']) if function is None: raise HTTPNotFound('Unkown URL %r' % request.path_info) # extracting all the info from the headers and the url request.sync_info = match # the GET mapping is filled on GET and DELETE requests if request.method in ('GET', 'DELETE'): params = dict(request.GET) else: params = {} try: result = function(request, **params) except BackendError: err = traceback.format_exc() logger.error(err) raise HTTPServiceUnavailable(retry_after=self.retry_after) if isinstance(result, basestring): response = getattr(request, 'response', None) if response is None: response = Response(result) elif isinstance(result, str): response.body = result else: # if it's not str it's unicode, which really shouldn't happen module = getattr(function, '__module__', 'unknown') name = getattr(function, '__name__', 'unknown') logger.warn('Unicode response returned from: %s - %s' % (module, name)) response.unicode_body = result else: # result is already a Response response = result # setting up the X-Weave-Timestamp response.headers['X-Weave-Timestamp'] = str(request.server_time) response.headers.update(before_headers) return response