Skip to content

Commit

Permalink
Merge pull request #136 from dinukadesilva/gh-135
Browse files Browse the repository at this point in the history
gh-135: Remove mappings with ballot boxes from CE-201
  • Loading branch information
dinukadesilva authored Sep 25, 2019
2 parents 1fc3237 + 0785aff commit eaa734c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 72 deletions.
10 changes: 4 additions & 6 deletions api/TallySheetVersionApi/TallySheetVersionCE201Api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
34 changes: 34 additions & 0 deletions migrations/versions/6d2544b671c0_.py
Original file line number Diff line number Diff line change
@@ -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 ###
37 changes: 37 additions & 0 deletions migrations/versions/9b00152b192b_.py
Original file line number Diff line number Diff line change
@@ -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 ###
Original file line number Diff line number Diff line change
Expand Up @@ -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("")

Expand Down
61 changes: 28 additions & 33 deletions orm/entities/TallySheetVersionRow/TallySheetVersionRow_CE_201.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions schemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 6 additions & 30 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit eaa734c

Please sign in to comment.