From 590c5902db40d26b10afa1ba383de8dafb8148d5 Mon Sep 17 00:00:00 2001 From: desafinadude Date: Fri, 6 Dec 2024 13:39:00 +0200 Subject: [PATCH 1/3] Adds independent attachments to bills --- migrations/versions/35d477d67b7_.py | 33 +++++++++++++++++++++++++++++ pmg/admin/__init__.py | 15 ++++++++++++- pmg/api/schemas.py | 10 +++++++++ pmg/models/resources.py | 10 +++++++++ pmg/templates/bills/detail.html | 18 ++++++++++++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/35d477d67b7_.py diff --git a/migrations/versions/35d477d67b7_.py b/migrations/versions/35d477d67b7_.py new file mode 100644 index 00000000..42de5393 --- /dev/null +++ b/migrations/versions/35d477d67b7_.py @@ -0,0 +1,33 @@ +"""empty message + +Revision ID: 35d477d67b7 +Revises: 33e499db410 +Create Date: 2024-12-06 10:16:54.464973 + +""" + +# revision identifiers, used by Alembic. +revision = '35d477d67b7' +down_revision = '33e499db410' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('bill_file', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('bill_id', sa.Integer(), nullable=False), + sa.Column('file_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['bill_id'], ['bill.id'], name=op.f('fk_bill_file_bill_id_bill')), + sa.ForeignKeyConstraint(['file_id'], ['file.id'], name=op.f('fk_bill_file_file_id_file')), + sa.PrimaryKeyConstraint('id', name=op.f('pk_bill_file')) + ) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_table('bill_file') + ### end Alembic commands ### diff --git a/pmg/admin/__init__.py b/pmg/admin/__init__.py index 65843c43..50cb1500 100644 --- a/pmg/admin/__init__.py +++ b/pmg/admin/__init__.py @@ -1260,6 +1260,18 @@ def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE): return query.offset(offset).limit(limit).all() +class InlineBillFileForm(InlineFormAdmin): + form_columns = ( + "id", + "file", + ) + form_ajax_refs = { + "file": { + "fields": ("title", "file_path"), + "page_size": 10, + }, + } + class BillsView(MyModelView): column_list = ( @@ -1289,13 +1301,14 @@ class BillsView(MyModelView): "date_of_assent", "effective_date", "act_name", - "versions", + "versions" ) column_default_sort = ("year", True) column_searchable_list = ("title",) inline_models = [ InlineBillEventsForm(Event), InlineBillVersionForm(BillVersion), + InlineBillFileForm(BillFile), ] form_args = { "events": {"widget": widgets.InlineBillEventsWidget()}, diff --git a/pmg/api/schemas.py b/pmg/api/schemas.py index 1330b584..732d2c53 100644 --- a/pmg/api/schemas.py +++ b/pmg/api/schemas.py @@ -18,6 +18,7 @@ Bill, BillType, BillVersion, + BillFile, BillStatus, Event, QuestionReply, @@ -396,6 +397,7 @@ class Meta: "effective_date", "act_name", "versions", + "bill_files", "events", "created_at", "updated_at", @@ -405,6 +407,7 @@ class Meta: status = fields.Nested("BillStatusSchema") place_of_introduction = fields.Nested("HouseSchema") versions = fields.Nested("BillVersionSchema", many=True) + bill_files = fields.Nested("BillFileSchema", many=True) events = PolyField(serialization_schema_selector=choose_event_schema, many=True) _links = ma.Hyperlinks({"self": AbsoluteUrlFor("api2.bills", id=""),}) @@ -428,3 +431,10 @@ class Meta: fields = ("id", "title", "file", "date", "enacted") file = fields.Nested("FileSchema") + +class BillFileSchema(ma.ModelSchema): + class Meta: + model = BillFile + fields = ("id", "file_id", "file") + + file = fields.Nested("FileSchema") diff --git a/pmg/models/resources.py b/pmg/models/resources.py index 928640a4..c6144b6e 100644 --- a/pmg/models/resources.py +++ b/pmg/models/resources.py @@ -163,6 +163,7 @@ class Bill(ApiResource, db.Model): versions = db.relationship( "BillVersion", backref="bill", cascade="all, delete, delete-orphan" ) + bill_files = db.relationship("BillFile", back_populates="bill", cascade="all, delete-orphan") @property def code(self): @@ -224,6 +225,15 @@ class BillVersion(db.Model): db.Boolean, default=False, server_default=sql.expression.false(), nullable=False ) +class BillFile(db.Model): + __tablename__ = 'bill_file' + id = db.Column(db.Integer, primary_key=True) + bill_id = db.Column(db.Integer, db.ForeignKey('bill.id'), nullable=False) + file_id = db.Column(db.Integer, db.ForeignKey('file.id'), nullable=False) + + bill = db.relationship("Bill", back_populates="bill_files") + file = db.relationship("File") + class File(db.Model): diff --git a/pmg/templates/bills/detail.html b/pmg/templates/bills/detail.html index 1c3c973f..af14a6eb 100644 --- a/pmg/templates/bills/detail.html +++ b/pmg/templates/bills/detail.html @@ -36,6 +36,7 @@

{% endif %} + {% if bill.status %} {% if bill.status.name in ["enacted", "president", "ncop", "na", "returned-to-na", "introduced"] -%}
@@ -169,6 +170,23 @@

{{ version.title }}

{% endif %} +{% print(bill) %} + +{% if bill.bill_files %} + +{% endif %} + {% endblock %} {% block javascript %} From a28378bf4b3d8d93410da54f9264aa16b856625b Mon Sep 17 00:00:00 2001 From: desafinadude Date: Fri, 6 Dec 2024 13:39:27 +0200 Subject: [PATCH 2/3] removed debug --- pmg/templates/bills/detail.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/pmg/templates/bills/detail.html b/pmg/templates/bills/detail.html index af14a6eb..7d244ab6 100644 --- a/pmg/templates/bills/detail.html +++ b/pmg/templates/bills/detail.html @@ -170,8 +170,6 @@

{{ version.title }}

{% endif %} -{% print(bill) %} - {% if bill.bill_files %}
  • Bill files:

  • From f36d9a96aedb65a447231dc4999748c7c3acba71 Mon Sep 17 00:00:00 2001 From: desafinadude Date: Wed, 15 Jan 2025 16:14:23 +0200 Subject: [PATCH 3/3] Fixed placeholder --- pmg/admin/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pmg/admin/__init__.py b/pmg/admin/__init__.py index 50cb1500..2473fbec 100644 --- a/pmg/admin/__init__.py +++ b/pmg/admin/__init__.py @@ -1261,6 +1261,7 @@ def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE): return query.offset(offset).limit(limit).all() class InlineBillFileForm(InlineFormAdmin): + form_columns = ( "id", "file", @@ -1269,6 +1270,7 @@ class InlineBillFileForm(InlineFormAdmin): "file": { "fields": ("title", "file_path"), "page_size": 10, + "placeholder": 'Select a File', }, } @@ -1311,6 +1313,7 @@ class BillsView(MyModelView): InlineBillFileForm(BillFile), ] form_args = { + "events": {"widget": widgets.InlineBillEventsWidget()}, }