diff --git a/api/TallySheetVersionApi/TallySheetVersionCE201Api.py b/api/TallySheetVersionApi/TallySheetVersionCE201Api.py index 913e7e4f..438ed272 100644 --- a/api/TallySheetVersionApi/TallySheetVersionCE201Api.py +++ b/api/TallySheetVersionApi/TallySheetVersionCE201Api.py @@ -51,13 +51,11 @@ def create(tallySheetId, body): "tenderedBallotCountFromBallotPaperAccount") ) - for issued_ballot_body in party_count_body.get("ballotBoxesIssued"): - issued_ballot_body = RequestBody(issued_ballot_body) - tallySheetVersionRow.add_issued_ballot_box(issued_ballot_body.get("stationaryItemId")) + for issued_ballot_box_id in party_count_body.get("ballotBoxesIssued"): + tallySheetVersionRow.add_issued_ballot_box(issued_ballot_box_id) - for received_ballot_body in party_count_body.get("ballotBoxesReceived"): - received_ballot_body = RequestBody(received_ballot_body) - tallySheetVersionRow.add_received_ballot_box(received_ballot_body.get("stationaryItemId")) + for received_ballot_box_id in party_count_body.get("ballotBoxesReceived"): + tallySheetVersionRow.add_received_ballot_box(received_ballot_box_id) db.session.commit() diff --git a/migrations/versions/6d2544b671c0_.py b/migrations/versions/6d2544b671c0_.py new file mode 100644 index 00000000..981986bb --- /dev/null +++ b/migrations/versions/6d2544b671c0_.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: 6d2544b671c0 +Revises: 9b00152b192b +Create Date: 2019-09-25 14:39:47.498815 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6d2544b671c0' +down_revision = '9b00152b192b' +branch_labels = None +depends_on = None + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('tallySheetVersionRow_CE_201_ballotBox', + sa.Column('tallySheetVersionRowId', sa.Integer(), nullable=False), + sa.Column('ballotBoxId', sa.String(length=100), nullable=False), + sa.Column('invoiceStage', sa.Enum('Issued', 'Received', name='invoicestageenum'), nullable=False), + sa.ForeignKeyConstraint(['tallySheetVersionRowId'], ['tallySheetVersionRow_CE_201.tallySheetVersionRowId'], ), + sa.PrimaryKeyConstraint('tallySheetVersionRowId', 'ballotBoxId', 'invoiceStage') + ) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_table('tallySheetVersionRow_CE_201_ballotBox') + ### end Alembic commands ### diff --git a/migrations/versions/9b00152b192b_.py b/migrations/versions/9b00152b192b_.py new file mode 100644 index 00000000..878e22c7 --- /dev/null +++ b/migrations/versions/9b00152b192b_.py @@ -0,0 +1,37 @@ +"""empty message + +Revision ID: 9b00152b192b +Revises: 62f13addba77 +Create Date: 2019-09-25 14:39:23.742151 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import mysql + +# revision identifiers, used by Alembic. +revision = '9b00152b192b' +down_revision = '62f13addba77' +branch_labels = None +depends_on = None + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_table('tallySheetVersionRow_CE_201_ballotBox') + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('tallySheetVersionRow_CE_201_ballotBox', + sa.Column('tallySheetVersionRowId', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), + sa.Column('ballotBoxStationaryItemId', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False), + sa.Column('invoiceStage', mysql.ENUM('Issued', 'Received'), nullable=False), + sa.ForeignKeyConstraint(['ballotBoxStationaryItemId'], ['ballotBox.stationaryItemId'], name='tallySheetVersionRow_CE_201_ballotBox_ibfk_1'), + sa.ForeignKeyConstraint(['tallySheetVersionRowId'], ['tallySheetVersionRow_CE_201.tallySheetVersionRowId'], name='tallySheetVersionRow_CE_201_ballotBox_ibfk_2'), + sa.PrimaryKeyConstraint('tallySheetVersionRowId', 'ballotBoxStationaryItemId', 'invoiceStage'), + mysql_default_charset='latin1', + mysql_engine='InnoDB' + ) + ### end Alembic commands ### diff --git a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersionCE201.py b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersionCE201.py index 0001bfdc..db452595 100644 --- a/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersionCE201.py +++ b/orm/entities/SubmissionVersion/TallySheetVersion/TallySheetVersionCE201.py @@ -69,7 +69,7 @@ def html(self): # three ballot boxes for ballotBoxIndex in range(3): if ballotBoxIndex < len(row.ballotBoxesReceived): - data_row.append(row.ballotBoxesReceived[ballotBoxIndex].ballotBoxId) + data_row.append(row.ballotBoxesReceived[ballotBoxIndex]) else: data_row.append("") diff --git a/orm/entities/TallySheetVersionRow/TallySheetVersionRow_CE_201.py b/orm/entities/TallySheetVersionRow/TallySheetVersionRow_CE_201.py index a1f78766..8acdd0c2 100644 --- a/orm/entities/TallySheetVersionRow/TallySheetVersionRow_CE_201.py +++ b/orm/entities/TallySheetVersionRow/TallySheetVersionRow_CE_201.py @@ -36,42 +36,38 @@ class TallySheetVersionRow_CE_201_Model(db.Model): @hybrid_property def ballotBoxesIssued(self): - return db.session.query( - BallotBox.Model - ).join( - TallySheetVersionRow_CE_201_IssuedBallotBox_Model, - and_( - TallySheetVersionRow_CE_201_IssuedBallotBox_Model.tallySheetVersionRowId == self.tallySheetVersionRowId, - TallySheetVersionRow_CE_201_IssuedBallotBox_Model.ballotBoxStationaryItemId == BallotBox.Model.stationaryItemId - ) + ballot_boxes = db.session.query( + TallySheetVersionRow_CE_201_IssuedBallotBox_Model.ballotBoxId + ).filter( + TallySheetVersionRow_CE_201_IssuedBallotBox_Model.tallySheetVersionRowId == self.tallySheetVersionRowId ).all() + return [ballot_box.ballotBoxId for ballot_box in ballot_boxes] + @hybrid_property def ballotBoxesReceived(self): - return db.session.query( - BallotBox.Model - ).join( - TallySheetVersionRow_CE_201_ReceivedBallotBox_Model, - and_( - TallySheetVersionRow_CE_201_ReceivedBallotBox_Model.tallySheetVersionRowId == self.tallySheetVersionRowId, - TallySheetVersionRow_CE_201_ReceivedBallotBox_Model.ballotBoxStationaryItemId == BallotBox.Model.stationaryItemId - ) + ballot_boxes = db.session.query( + TallySheetVersionRow_CE_201_ReceivedBallotBox_Model.ballotBoxId + ).filter( + TallySheetVersionRow_CE_201_ReceivedBallotBox_Model.tallySheetVersionRowId == self.tallySheetVersionRowId ).all() + return [ballot_box.ballotBoxId for ballot_box in ballot_boxes] + __table_args__ = ( db.UniqueConstraint('tallySheetVersionId', 'areaId', name='PollingStationPerBallotPaperAccount'), ) - def add_received_ballot_box(self, stationaryItemId): + def add_received_ballot_box(self, ballotBoxId): TallySheetVersionRow_CE_201_ReceivedBallotBox_Model( tallySheetVersionRow=self, - ballotBoxStationaryItemId=stationaryItemId + ballotBoxId=ballotBoxId ) - def add_issued_ballot_box(self, stationaryItemId): + def add_issued_ballot_box(self, ballotBoxId): TallySheetVersionRow_CE_201_IssuedBallotBox_Model( tallySheetVersionRow=self, - ballotBoxStationaryItemId=stationaryItemId + ballotBoxId=ballotBoxId ) def __init__(self, tallySheetVersion, areaId, ballotsIssued, ballotsReceived, ballotsSpoilt, ballotsUnused, @@ -110,28 +106,27 @@ class TallySheetVersionRow_CE_201_BallotBox_Model(db.Model): __tablename__ = 'tallySheetVersionRow_CE_201_ballotBox' tallySheetVersionRowId = db.Column(db.Integer, db.ForeignKey( TallySheetVersionRow_CE_201_Model.__table__.c.tallySheetVersionRowId), primary_key=True) - ballotBoxStationaryItemId = db.Column(db.Integer, db.ForeignKey(BallotBox.Model.__table__.c.stationaryItemId), - primary_key=True) + ballotBoxId = db.Column(db.String(100), primary_key=True) invoiceStage = db.Column(db.Enum(InvoiceStageEnum), primary_key=True) __mapper_args__ = { 'polymorphic_on': invoiceStage } - def __init__(self, tallySheetVersionRow, ballotBoxStationaryItemId): - ballotBox = BallotBox.get_by_id(stationaryItemId=ballotBoxStationaryItemId) - - if ballotBox is None: - raise NotFoundException("Ballot Box not found. (stationaryItemId=%d)" % ballotBoxStationaryItemId) - - if ballotBox.electionId not in tallySheetVersionRow.tallySheetVersion.submission.election.mappedElectionIds: - raise NotFoundException( - "Ballot Box is not registered for the given election. (stationaryItemId=%d)" % ballotBoxStationaryItemId - ) + def __init__(self, tallySheetVersionRow, ballotBoxId): + # ballotBox = BallotBox.get_by_id(stationaryItemId=ballotBoxStationaryItemId) + # + # if ballotBox is None: + # raise NotFoundException("Ballot Box not found. (stationaryItemId=%d)" % ballotBoxStationaryItemId) + # + # if ballotBox.electionId not in tallySheetVersionRow.tallySheetVersion.submission.election.mappedElectionIds: + # raise NotFoundException( + # "Ballot Box is not registered for the given election. (stationaryItemId=%d)" % ballotBoxStationaryItemId + # ) super(TallySheetVersionRow_CE_201_BallotBox_Model, self).__init__( tallySheetVersionRowId=tallySheetVersionRow.tallySheetVersionRowId, - ballotBoxStationaryItemId=ballotBoxStationaryItemId + ballotBoxId=ballotBoxId ) db.session.add(self) db.session.flush() diff --git a/schemas/__init__.py b/schemas/__init__.py index abfa011f..c14fa65e 100644 --- a/schemas/__init__.py +++ b/schemas/__init__.py @@ -227,8 +227,8 @@ class Meta: # to use for deserialization sqla_session = db.session - ballotBoxesIssued = ma.Nested("BallotBox_Schema", only=["ballotBoxId", "stationaryItemId"], many=True) - ballotBoxesReceived = ma.Nested("BallotBox_Schema", only=["ballotBoxId", "stationaryItemId"], many=True) + # ballotBoxesIssued = ma.Nested("BallotBox_Schema", only=["ballotBoxId", "stationaryItemId"], many=True) + # ballotBoxesReceived = ma.Nested("BallotBox_Schema", only=["ballotBoxId", "stationaryItemId"], many=True) class AreaSchema(ma.ModelSchema): diff --git a/swagger.yml b/swagger.yml index c1b6ee0e..b5769ac2 100644 --- a/swagger.yml +++ b/swagger.yml @@ -2366,19 +2366,11 @@ paths: ballotBoxesIssued: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotBoxesReceived: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotsIssued: type: integer format: int64 @@ -2446,19 +2438,11 @@ paths: ballotBoxesIssued: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotBoxesReceived: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotsIssued: type: integer format: int64 @@ -2527,19 +2511,11 @@ paths: ballotBoxesIssued: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotBoxesReceived: type: array items: - type: object - properties: - stationaryItemId: - type: integer - format: int64 + type: string ballotsIssued: type: integer format: int64