Skip to content

Commit

Permalink
CERNAccess: Ensure unique reservation codes (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
AjobK authored Oct 25, 2024
1 parent efce4b6 commit 00a257f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Make reservation code unique and nullable
Revision ID: 92377810f14e
Revises: ee88b64f9494
Create Date: 2024-10-15 17:02:59.886902
"""

from alembic import op


# revision identifiers, used by Alembic.
revision = '92377810f14e'
down_revision = 'ee88b64f9494'
branch_labels = None
depends_on = None


def upgrade():
op.alter_column('access_requests', 'reservation_code', nullable=True, schema='plugin_cern_access')
op.execute("UPDATE plugin_cern_access.access_requests SET reservation_code = NULL WHERE reservation_code = ''")
op.create_unique_constraint(None, 'access_requests', ['reservation_code'], schema='plugin_cern_access')


def downgrade():
op.drop_constraint('uq_access_requests_reservation_code', 'access_requests', schema='plugin_cern_access')
op.execute("UPDATE plugin_cern_access.access_requests SET reservation_code = '' WHERE reservation_code IS NULL")
op.alter_column('access_requests', 'reservation_code', nullable=False, schema='plugin_cern_access')
3 changes: 2 additions & 1 deletion cern_access/indico_cern_access/models/access_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class CERNAccessRequest(db.Model):
)
reservation_code = db.Column(
db.String,
nullable=False
nullable=True,
unique=True
)
adams_nonce = db.Column(
db.String,
Expand Down
1 change: 0 additions & 1 deletion cern_access/indico_cern_access/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ def _registration_created(self, registration, management, **kwargs):
license_plate=access_request_data['license_plate'],
accompanying_persons=sanitize_accompanying_persons(accompanying_persons, registration),
request_state=CERNAccessRequestState.not_requested,
reservation_code=''
)

def _event_time_changed(self, sender, obj, **kwargs):
Expand Down
6 changes: 5 additions & 1 deletion cern_access/indico_cern_access/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@ def withdraw_event_access_request(req):
def get_random_reservation_code():
"""Generate random reservation code for data required by ADaMS API."""
charset = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789'
return 'I' + ''.join(random.sample(charset, 6))

while True:
reservation_code = 'I' + ''.join(random.sample(charset, 6))
if not CERNAccessRequest.query.filter_by(reservation_code=reservation_code).has_rows():
return reservation_code


def create_access_request(registration, state, reservation_code, nonce):
Expand Down
1 change: 0 additions & 1 deletion cern_access/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,4 @@ def dummy_access_request(dummy_regform, accompanying_persons_field):
birth_place='bar',
license_plate=None,
request_state=CERNAccessRequestState.not_requested,
reservation_code='',
accompanying_persons=accompaning_persons)

0 comments on commit 00a257f

Please sign in to comment.