Skip to content

Commit

Permalink
Add AdminFlagValue
Browse files Browse the repository at this point in the history
  • Loading branch information
seyeong committed Sep 9, 2019
1 parent f40b1c1 commit ab3a323
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
6 changes: 4 additions & 2 deletions tests/unit/accounts/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
TokenMissing,
TooManyFailedLogins,
)
from warehouse.admin.flags import AdminFlag
from warehouse.admin.flags import AdminFlag, AdminFlagValue

from ...common.db.accounts import EmailFactory, UserFactory

Expand Down Expand Up @@ -903,7 +903,9 @@ def test_register_redirect(self, db_request, monkeypatch):

def test_register_fails_with_admin_flag_set(self, db_request):
# This flag was already set via migration, just need to enable it
flag = db_request.db.query(AdminFlag).get("disallow-new-user-registration")
flag = db_request.db.query(AdminFlag).get(
AdminFlagValue.DISALLOW_NEW_USER_REGISTRATION
)
flag.enabled = True

db_request.method = "POST"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from wtforms.form import Form
from wtforms.validators import ValidationError

from warehouse.admin.flags import AdminFlag
from warehouse.admin.flags import AdminFlag, AdminFlagValue
from warehouse.admin.squats import Squat
from warehouse.classifiers.models import Classifier
from warehouse.forklift import legacy
Expand Down Expand Up @@ -1118,7 +1118,7 @@ def test_fails_with_stdlib_names(self, pyramid_config, db_request, name):
def test_fails_with_admin_flag_set(self, pyramid_config, db_request):
admin_flag = (
db_request.db.query(AdminFlag)
.filter(AdminFlag.id == "disallow-new-project-registration")
.filter(AdminFlag.id == AdminFlagValue.DISALLOW_NEW_PROJECT_REGISTRATION)
.first()
)
admin_flag.enabled = True
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy.exc import OperationalError

from warehouse import db
from warehouse.admin.flags import AdminFlagValue
from warehouse.db import (
DEFAULT_ISOLATION,
DatabaseNotAvailable,
Expand Down Expand Up @@ -273,7 +274,7 @@ def test_create_session_read_only_mode(
)

assert _create_session(request) is session_obj
assert get.calls == [pretend.call("read-only")]
assert get.calls == [pretend.call(AdminFlagValue.READ_ONLY)]
assert request.tm.doom.calls == doom_calls


Expand Down
3 changes: 2 additions & 1 deletion warehouse/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
TooManyFailedLogins,
)
from warehouse.accounts.models import Email, User
from warehouse.admin.flags import AdminFlagValue
from warehouse.cache.origin import origin_cache
from warehouse.email import send_email_verification_email, send_password_reset_email
from warehouse.packaging.models import Project, Release
Expand Down Expand Up @@ -377,7 +378,7 @@ def register(request, _form_class=RegistrationForm):
if request.method == "POST" and request.POST.get("confirm_form"):
return HTTPSeeOther(request.route_path("index"))

if request.flags.enabled("disallow-new-user-registration"):
if request.flags.enabled(AdminFlagValue.DISALLOW_NEW_USER_REGISTRATION):
request.session.flash(
(
"New user registration temporarily disabled. "
Expand Down
6 changes: 6 additions & 0 deletions warehouse/admin/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
from warehouse import db


class AdminFlagValue:
DISALLOW_NEW_PROJECT_REGISTRATION = "disallow-new-project-registration"
DISALLOW_NEW_USER_REGISTRATION = "disallow-new-user-registration"
READ_ONLY = "read-only"


class AdminFlag(db.ModelBase):

__tablename__ = "admin_flags"
Expand Down
4 changes: 2 additions & 2 deletions warehouse/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ def cleanup(request):
connection.close()

# Check if we're in read-only mode
from warehouse.admin.flags import AdminFlag
from warehouse.admin.flags import AdminFlag, AdminFlagValue

flag = session.query(AdminFlag).get("read-only")
flag = session.query(AdminFlag).get(AdminFlagValue.READ_ONLY)
if flag and flag.enabled and not request.user.is_superuser:
request.tm.doom()

Expand Down
5 changes: 3 additions & 2 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound

from warehouse import forms
from warehouse.admin.flags import AdminFlagValue
from warehouse.admin.squats import Squat
from warehouse.classifiers.models import Classifier
from warehouse.metrics import IMetricsService
Expand Down Expand Up @@ -728,7 +729,7 @@ def validate_no_deprecated_classifiers(form, field):
)
def file_upload(request):
# If we're in read-only mode, let upload clients know
if request.flags.enabled("read-only"):
if request.flags.enabled(AdminFlagValue.READ_ONLY):
raise _exc_with_message(
HTTPForbidden, "Read-only mode: Uploads are temporarily disabled"
)
Expand Down Expand Up @@ -850,7 +851,7 @@ def file_upload(request):
# Check for AdminFlag set by a PyPI Administrator disabling new project
# registration, reasons for this include Spammers, security
# vulnerabilities, or just wanting to be lazy and not worry ;)
if request.flags.enabled("disallow-new-project-registration"):
if request.flags.enabled(AdminFlagValue.DISALLOW_NEW_PROJECT_REGISTRATION):
raise _exc_with_message(
HTTPForbidden,
(
Expand Down

0 comments on commit ab3a323

Please sign in to comment.