def upload_recaptcha_validator_shim(form, field): ''' Selectively does a recaptcha validation ''' if app.config['USE_RECAPTCHA']: # Recaptcha anonymous and new users if not flask.g.user or flask.g.user.age < app.config['ACCOUNT_RECAPTCHA_AGE']: return RecaptchaValidator()(form, field) else: # Always pass validating the recaptcha field if disabled return True
class CommentForm(FlaskForm): name = TextField(lazy_gettext("Your Name"), validators=[RequiredIf(not_logged_in), Length(max=80)]) text = TextAreaField(lazy_gettext("Comment")) captcha = RecaptchaField(validators=[OptionalIf(logged_in), RecaptchaValidator()])
def recaptcha_validator_shim(form, field): if app.config['USE_RECAPTCHA']: return RecaptchaValidator()(form, field) else: # Always pass validating the recaptcha field if disabled return True
class UploadForm(FlaskForm): torrent_file = FileField('Torrent file', [FileRequired()]) display_name = StringField('Torrent display name (optional)', [ Optional(), Length( min=3, max=255, message= 'Torrent display name must be at least %(min)d characters long and ' '%(max)d at most.') ]) if config['USE_RECAPTCHA']: # Captcha only for not logged in users _recaptcha_validator = RecaptchaValidator() def _validate_recaptcha(form, field): if not flask.g.user: return UploadForm._recaptcha_validator(form, field) recaptcha = RecaptchaField(validators=[_validate_recaptcha]) # category = SelectField('Category') category = DisabledSelectField('Category') def validate_category(form, field): cat_match = re.match(r'^(\d+)_(\d+)$', field.data) if not cat_match: raise ValidationError('Please select a category') main_cat_id = int(cat_match.group(1)) sub_cat_id = int(cat_match.group(2)) cat = models.SubCategory.by_category_ids(main_cat_id, sub_cat_id) if not cat: raise ValidationError('Please select a proper category') field.parsed_data = cat is_hidden = BooleanField('Hidden') is_remake = BooleanField('Remake') is_anonymous = BooleanField('Anonymous') is_complete = BooleanField('Complete') is_trusted = BooleanField('Trusted') information = StringField('Information', [ Length(max=255, message='Information must be at most %(max)d characters long.') ]) description = TextAreaField('Description', [ Length(max=10 * 1024, message='Description must be at most %(max)d characters long.') ]) def validate_torrent_file(form, field): # Decode and ensure data is bencoded data try: torrent_dict = bencode.decode(field.data) # field.data.close() except (bencode.MalformedBencodeException, UnicodeError): raise ValidationError('Malformed torrent file') # Uncomment for debug print of the torrent # _debug_print_torrent_metadata(torrent_dict) try: _validate_torrent_metadata(torrent_dict) except AssertionError as e: raise ValidationError('Malformed torrent metadata ({})'.format( e.args[0])) site_tracker = app.config.get('MAIN_ANNOUNCE_URL') ensure_tracker = app.config.get('ENFORCE_MAIN_ANNOUNCE_URL') try: tracker_found = _validate_trackers(torrent_dict, site_tracker) except AssertionError as e: raise ValidationError('Malformed torrent trackers ({})'.format( e.args[0])) # Ensure private torrents are using our tracker if torrent_dict['info'].get('private') == 1: if torrent_dict['announce'].decode('utf-8') != site_tracker: raise ValidationError( 'Private torrent: please set {} as the main tracker'. format(site_tracker)) elif ensure_tracker and not tracker_found: raise ValidationError( 'Please include {} in the trackers of the torrent'.format( site_tracker)) # Note! bencode will sort dict keys, as per the spec # This may result in a different hash if the uploaded torrent does not match the # spec, but it's their own fault for using broken software! Right? bencoded_info_dict = bencode.encode(torrent_dict['info']) info_hash = utils.sha1_hash(bencoded_info_dict) # Check if the info_hash exists already in the database existing_torrent = models.Torrent.by_info_hash(info_hash) existing_torrent_id = existing_torrent.id if existing_torrent else None if existing_torrent and not existing_torrent.deleted: raise ValidationError('This torrent already exists (#{})'.format( existing_torrent.id)) if existing_torrent and existing_torrent.banned: raise ValidationError('This torrent is banned'.format( existing_torrent.id)) # Torrent is legit, pass original filename and dict along field.parsed_data = TorrentFileData( filename=os.path.basename(field.data.filename), torrent_dict=torrent_dict, info_hash=info_hash, bencoded_info_dict=bencoded_info_dict, db_id=existing_torrent_id)