From 80e4ddf1a8c497af4bc21987a45ee3a32174bd33 Mon Sep 17 00:00:00 2001 From: Gardient Date: Wed, 22 Sep 2021 20:12:06 +0300 Subject: [PATCH] update to registrations --- api/registration/models.py | 2 +- api/registration/serializers.py | 4 ++-- api/registration/views.py | 8 ++++---- migrations/versions/5dfa048b8215_added_registrations.py | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/api/registration/models.py b/api/registration/models.py index 495432f..abf8122 100644 --- a/api/registration/models.py +++ b/api/registration/models.py @@ -13,7 +13,7 @@ registration_target_assoc = db.Table( class Registration(SurrogatePK, Model): __tablename__ = "registration" name = Column(String(255), unique=True, nullable=False) - routing_key = Column(String(255), nullable=False) + token = Column(String(255), nullable=False) targets = relationship( "Target", secondary=registration_target_assoc, backref=db.backref("registrations")) diff --git a/api/registration/serializers.py b/api/registration/serializers.py index d04da8d..7f9a515 100644 --- a/api/registration/serializers.py +++ b/api/registration/serializers.py @@ -6,8 +6,8 @@ from api.target.serializers import TargetSchema class Registration(Schema): id = fields.Int() name = fields.Str() - Token = fields.Str(dump_only=True) - exchange = fields.Nested(TargetSchema, required=True) + token = fields.Str(dump_only=True) + targets = fields.Nested(TargetSchema, required=True, many=True) registration_schema = Registration() diff --git a/api/registration/views.py b/api/registration/views.py index a5007cb..792d94b 100644 --- a/api/registration/views.py +++ b/api/registration/views.py @@ -33,12 +33,12 @@ def get_list(exchange=None, target=None): @marshal_with(registration_schema) def create(name, routing_key, targets): target_ids = [t.id for t in targets] - xchanges = Target.query.filter(Target.target_exchange_id.in_(target_ids)) - if len(xchanges) != len(target_ids): - xchange_ids = [t.id for t in xchanges] + db_targets = Target.query.filter(Target.id.in_(target_ids)) + if len(db_targets) != len(targets): + xchange_ids = [t.id for t in db_targets] not_found = ','.join([f'{t.name}({t.id})' for t in targets if t.id not in xchange_ids]) raise BadRequestException(f"the target {not_found} could not be found") - registration = Registration(name=name, routing_key=routing_key, registration_exchange_id=xchanges.id) + registration = Registration(name=name, token=routing_key, targets=db_targets) registration.save() return registration diff --git a/migrations/versions/5dfa048b8215_added_registrations.py b/migrations/versions/5dfa048b8215_added_registrations.py index fbf1043..8dbaa6c 100644 --- a/migrations/versions/5dfa048b8215_added_registrations.py +++ b/migrations/versions/5dfa048b8215_added_registrations.py @@ -21,7 +21,7 @@ def upgrade(): op.create_table('registration', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(length=255), nullable=False), - sa.Column('routing_key', sa.String(length=255), nullable=False), + sa.Column('token', sa.String(length=255), nullable=False), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('name') )