def get_signin_validator(payload): rules = [ val.NoneVal('email', 'Please provide a username or email address'), val.NoneVal('password', 'Please provide a password') ] return val.Validator(payload, rules)
def signup_with_fbid(self, name, email, password, fb_id, auth_token): email = email.strip() name = name.strip() validator = validation.Validator(immediate_exceptions=False) validator.add_rule(email, 'Email', email=True) validator.add_rule(name, 'Name', min_length=4, max_length=64) validator.add_rule(password, 'Password', min_length=6, max_length=64) validator.validate() # Make sure that FBID and email aren't already taken if self.db_session.query( model.User).filter_by(fb_id=fb_id).count() > 0: validator.error( 'This Facebook user is already registered on Instant.fm. Try logging in instead.' ) validator.validate() if self.db_session.query( model.User).filter_by(email=email).count() > 0: validator.error( 'This email is already registered on Instant.fm. Try logging in instead.' ) validator.validate() # Cache parameters for use in callback self._name = name self._email = email self._password = password self._fb_id = fb_id self._auth_token = auth_token # Authenticate to Facebook self.facebook_request("/me", access_token=auth_token, callback=self.async_callback(self._on_fb_auth))
def post(self): """ Handles the "New Playlist" form post. This can't be JSON RPC because of the file uploading. """ validator = validation.Validator(immediate_exceptions=True) title = self.get_argument('title', default='', strip=True) validator.add_rule(title, 'Title', min_length=1) description = self.get_argument('description', default=None, strip=True) songs = [] if self._has_uploaded_files(): try: songs = self._parse_songs_from_uploaded_file() except UnsupportedFormatException: validator.error('Unsupported format.') playlist = model.Playlist(title) playlist.description = description playlist.songs = songs playlist.session = self.get_current_session() playlist.user = self.get_current_user() self.db_session.add(playlist) self.db_session.flush() self.set_header("Content-Type", "application/json") return playlist.client_visible_attrs
def run(session): batcher = preprocessing.Batcher(config.SUBSAMPLE_THRESHOLD, config.TEST_MODE) num_batches = batcher.num_batches(config.BATCH_SIZE) graph = graph_fns.build_graph( batcher.vocab_size(), config.NUM_EMBEDDING_UNITS, config.NUM_NEGATIVE_SAMPLES, ) session.run(tf.global_variables_initializer()) run_info = RunInfo( session=session, graph=graph, saver=tf.train.Saver(), batcher=batcher, validator=validator.Validator(graph.embedding_matrix, batcher), batch_size=config.BATCH_SIZE, window_size=config.WINDOW_SIZE, batches_per_epoch=num_batches, batches_per_logging=int(num_batches * config.LOGGING_FREQUENCY), batches_per_save=int(num_batches * config.SAVING_FREQUENCY), batches_per_validation=int(num_batches * config.VALIDATION_FREQUENCY), ) for epoch_idx in range(1, config.NUM_EPOCHS + 1): run_epoch(run_info, epoch_idx)
def get_signup_validator(payload): payload['email'] = payload['email'].lower() payload['username'] = payload['username'].lower() rules = [val.GenericVal('username', 'The username is already in use', User.not_exist_by_username), val.EmailVal('email', 'Please provide a valid email address'), val.GenericVal('email', 'The email address is already in use', User.not_exist_by_email), val.PasswordValidator('password', 'Please provide a valid password') ] return val.Validator(payload, rules)
def new_playlist(self, title, description): title = title.strip() description = description.strip() validator = validation.Validator(immediate_exceptions=False) validator.add_rule(title, 'Title', min_length=1, max_length=250) validator.add_rule(description, 'Description', max_length=10000) validator.validate() playlist = model.Playlist(title) playlist.description = description playlist.session = self.get_current_session() self.db_session.add(playlist) self.db_session.flush() self.result(playlist.client_visible_attrs)
def login(self, email, password, remember_me): email = email.strip() validator = validation.Validator(immediate_exceptions=True) user = self.db_session.query(model.User).filter_by(email=email).first() if not user: validator.error('No user with that email found.') if not self._verify_password(password, user.password): validator.error('Password is incorrect.') # If we haven't failed out yet, the login is valid. self._log_user_in(user, expire_on_browser_close=(not remember_me)) # Return the session which includes the logged in user self.result(self.get_current_session().client_visible_attrs)
def _on_fb_auth(self, user): validator = validation.Validator(immediate_exceptions=True) if str(user['id']) != str(self._fb_id): validator.error('Failed to authenticate to Facebook.') # Write the user to DB user = model.User() user.fb_id = self._fb_id user.name = self._name user.email = self._email user.password = self._hash_password(self._password) user.profile = self._generate_unique_profile_name(self._name) self.db_session.add(user) self._log_user_in(user) self.result(self.get_current_session().client_visible_attrs)
import bs4 import validation import constants import nltk unflattenable_tags = set(['image']) validator = validation.Validator() def get_parser(text): return bs4.BeautifulSoup(text, constants.DESIRED_BEAUTIFULSOUP_PARSER) def is_useful_paragraph(p): try: validator.validate_paragraph(p) return True except validation.ValidationError: pass def extract_from_para(p): # At this point because they've already been validated we know that # they have the correct structure. # # We use decode() to obtain a Unicode string with full markup. Because # membercontributions have their own markup. member = p.find_all('member')[0].decode() membercontribution = p.find_all('membercontribution')[0].decode() return (member, membercontribution)