mirror of
https://github.com/simon987/nyaa.git
synced 2025-12-14 15:49:02 +00:00
[Config change] Require recaptcha of new uploaders (#376)
* Reorganize config.example.py, add ACCOUNT_RECAPTCHA_AGE * Require new accounts to pass recaptcha on upload Based on ACCOUNT_RECAPTCHA_AGE in config.
This commit is contained in:
committed by
Arylide
parent
214952e7b6
commit
1e5f61ddf7
@@ -239,6 +239,17 @@ class BanForm(FlaskForm):
|
||||
])
|
||||
|
||||
|
||||
def 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 UploadForm(FlaskForm):
|
||||
torrent_file = FileField('Torrent file', [
|
||||
FileRequired()
|
||||
@@ -251,17 +262,8 @@ class UploadForm(FlaskForm):
|
||||
'%(max)d at most.')
|
||||
])
|
||||
|
||||
if config['USE_RECAPTCHA']:
|
||||
# Captcha only for not logged in users
|
||||
_recaptcha_validator = RecaptchaValidator()
|
||||
recaptcha = RecaptchaField(validators=[recaptcha_validator_shim])
|
||||
|
||||
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):
|
||||
|
||||
@@ -601,6 +601,11 @@ class User(db.Model):
|
||||
def is_banned(self):
|
||||
return self.status == UserStatusType.BANNED
|
||||
|
||||
@property
|
||||
def age(self):
|
||||
'''Account age in seconds'''
|
||||
return (datetime.utcnow() - self.created_time).total_seconds()
|
||||
|
||||
|
||||
class AdminLogBase(DeclarativeHelperBase):
|
||||
__tablename_base__ = 'adminlog'
|
||||
|
||||
@@ -86,7 +86,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if config.USE_RECAPTCHA and not g.user %}
|
||||
{% if config.USE_RECAPTCHA and (not g.user or g.user.age < config['ACCOUNT_RECAPTCHA_AGE']) %}
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{% for error in upload_form.recaptcha.errors %}
|
||||
|
||||
Reference in New Issue
Block a user