Skip to content

Commit

Permalink
Added a column for thumbnail_images in the project model
Browse files Browse the repository at this point in the history
-Ran db migration
-Generate thumbnail method writes png to the db
  • Loading branch information
anishTP committed Sep 28, 2023
1 parent 554070c commit 43b41b3
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 13 deletions.
8 changes: 8 additions & 0 deletions funnel/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections.abc import Sequence

from pytz import utc
from sqlalchemy import LargeBinary
from sqlalchemy.orm import attribute_keyed_dict
from werkzeug.utils import cached_property

Expand Down Expand Up @@ -191,6 +192,13 @@ class Project(UuidMixin, BaseScopedNameMixin, Model):
read={'all'},
datasets={'primary', 'without_parent', 'related'},
)

thumbnail_image = with_roles(
sa.orm.mapped_column(LargeBinary, nullable=True),
read={'all'},
datasets={'primary', 'without_parent', 'related'},
)

allow_rsvp: Mapped[bool] = with_roles(
sa.orm.mapped_column(sa.Boolean, default=True, nullable=False),
read={'all'},
Expand Down
2 changes: 1 addition & 1 deletion funnel/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@
user_data_changed = app_signals.signal('user-data-changed')
org_data_changed = app_signals.signal('org-data-changed')
team_data_changed = app_signals.signal('team-data-changed')
project_data_changed = app_signals.signal('project_data_changed')
session_revoked = app_signals.signal('session-revoked')

# Commentset role change signals (sends user, document)
project_role_change = app_signals.signal('project_role_change')
proposal_role_change = app_signals.signal('proposal_role_change')

# Project data change signal
project_data_change = app_signals.signal('project_data_change')
File renamed without changes.
6 changes: 3 additions & 3 deletions funnel/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
db,
sa,
)
from ..signals import project_role_change, project_data_change
from ..signals import project_role_change, project_data_changed
from ..typing import ReturnRenderWith, ReturnView
from .helpers import html_in_json, render_redirect
from .jobs import import_tickets, tag_locations
Expand Down Expand Up @@ -283,7 +283,7 @@ def new_project(self) -> ReturnView:

flash(_("Your new project has been created"), 'info')

project_data_change.send(self.obj)
project_data_changed.send(self.obj, changes=['new'])

# tag locations
tag_locations.queue(project.id)
Expand Down Expand Up @@ -457,7 +457,7 @@ def edit(self) -> ReturnView:
db.session.commit()
flash(_("Your changes have been saved"), 'info')
tag_locations.queue(self.obj.id)
project_data_change.send(self.obj)
project_data_changed.send(self.obj, changes=['edit'])

# Find and delete draft if it exists
if self.get_draft() is not None:
Expand Down
18 changes: 9 additions & 9 deletions funnel/views/thumnbnails.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

from flask import render_template
from html2image import Html2Image
from ..models import (
Account,
Project,
)
from ..models import Project, db
from .jobs import rqjob
from ..signals import project_data_change
from ..signals import project_data_changed


thumbnail = Html2Image(size=(1067, 600))
thumbnail = Html2Image(size=(640, 360))


@project_data_change.connect
@project_data_changed.connect
def generate_thumbnail_image(project = Project) -> None:
render_thumbnail_image(project = project)

Expand All @@ -23,5 +20,8 @@ def render_thumbnail_image(project = Project) -> None:

image_html = render_template('thumbnail_preview.html.jinja2', project = project )

thumbnail_image = thumbnail.screenshot(html_str=image_html, save_as='thumbnail.png',
size=(1067, 600))
thumbnail_image = thumbnail.screenshot(html_str=image_html, save_as=f'{project.id}_thumbnail.png', size=(640, 360))

project.thumbnail_image = thumbnail_image
db.session.add(project)
db.session.commit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
"""Added thumbnail_image column to Project model
Revision ID: 8750fd8f82de
Revises: 4f9ca10b7b9d
Create Date: 2023-09-28 18:47:07.517533
"""

from typing import Optional, Tuple, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '8750fd8f82de'
down_revision: str = '4f9ca10b7b9d'
branch_labels: Optional[Union[str, Tuple[str, ...]]] = None
depends_on: Optional[Union[str, Tuple[str, ...]]] = None


def upgrade(engine_name: str = '') -> None:
"""Upgrade all databases."""
# Do not modify. Edit `upgrade_` instead
globals().get(f'upgrade_{engine_name}', lambda: None)()


def downgrade(engine_name: str = '') -> None:
"""Downgrade all databases."""
# Do not modify. Edit `downgrade_` instead
globals().get(f'downgrade_{engine_name}', lambda: None)()





def upgrade_() -> None:
"""Upgrade default database."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('project', schema=None) as batch_op:
batch_op.add_column(sa.Column('thumbnail_image', sa.LargeBinary(), nullable=True))

# ### end Alembic commands ###


def downgrade_() -> None:
"""Downgrade default database."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('project', schema=None) as batch_op:
batch_op.drop_column('thumbnail_image')

# ### end Alembic commands ###


def upgrade_geoname() -> None:
"""Upgrade geoname database."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('geo_country_info',
sa.Column('iso_alpha2', sa.CHAR(length=2), nullable=True),
sa.Column('iso_alpha3', sa.CHAR(length=3), nullable=True),
sa.Column('iso_numeric', sa.Integer(), nullable=True),
sa.Column('fips_code', sa.Unicode(length=3), nullable=True),
sa.Column('capital', sa.String(), nullable=True),
sa.Column('area_in_sqkm', sa.Numeric(), nullable=True),
sa.Column('population', sa.BigInteger(), nullable=True),
sa.Column('continent', sa.CHAR(length=2), nullable=True),
sa.Column('tld', sa.Unicode(length=3), nullable=True),
sa.Column('currency_code', sa.CHAR(length=3), nullable=True),
sa.Column('currency_name', sa.String(), nullable=True),
sa.Column('phone', sa.Unicode(length=16), nullable=True),
sa.Column('postal_code_format', sa.Unicode(), nullable=True),
sa.Column('postal_code_regex', sa.Unicode(), nullable=True),
sa.Column('languages', postgresql.ARRAY(sa.Unicode(), dimensions=1), nullable=True),
sa.Column('neighbours', postgresql.ARRAY(sa.CHAR(length=2), dimensions=1), nullable=True),
sa.Column('equivalent_fips_code', sa.Unicode(length=3), nullable=False),
sa.Column('name', sa.Unicode(length=250), nullable=False),
sa.Column('title', sa.Unicode(length=250), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('iso_alpha2'),
sa.UniqueConstraint('iso_alpha3'),
sa.UniqueConstraint('name')
)
with op.batch_alter_table('geo_country_info', schema=None) as batch_op:
batch_op.create_index('ix_geo_country_info_title', [sa.text("lower('title')")], unique=False, postgresql_ops={'title_lower': 'varchar_pattern_ops'})

op.create_table('geo_admin1_code',
sa.Column('title', sa.Unicode(), nullable=True),
sa.Column('ascii_title', sa.Unicode(), nullable=True),
sa.Column('country', sa.CHAR(length=2), nullable=True),
sa.Column('admin1_code', sa.Unicode(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['country'], ['geo_country_info.iso_alpha2'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('geo_admin2_code',
sa.Column('title', sa.Unicode(), nullable=True),
sa.Column('ascii_title', sa.Unicode(), nullable=True),
sa.Column('country', sa.CHAR(length=2), nullable=True),
sa.Column('admin1_code', sa.Unicode(), nullable=True),
sa.Column('admin2_code', sa.Unicode(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['country'], ['geo_country_info.iso_alpha2'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('geo_name',
sa.Column('ascii_title', sa.Unicode(), nullable=True),
sa.Column('latitude', sa.Numeric(), nullable=True),
sa.Column('longitude', sa.Numeric(), nullable=True),
sa.Column('fclass', sa.CHAR(length=1), nullable=True),
sa.Column('fcode', sa.Unicode(), nullable=True),
sa.Column('country', sa.CHAR(length=2), nullable=True),
sa.Column('cc2', sa.Unicode(), nullable=True),
sa.Column('admin1', sa.Unicode(), nullable=True),
sa.Column('admin1_id', sa.Integer(), nullable=True),
sa.Column('admin2', sa.Unicode(), nullable=True),
sa.Column('admin2_id', sa.Integer(), nullable=True),
sa.Column('admin4', sa.Unicode(), nullable=True),
sa.Column('admin3', sa.Unicode(), nullable=True),
sa.Column('population', sa.BigInteger(), nullable=True),
sa.Column('elevation', sa.Integer(), nullable=True),
sa.Column('dem', sa.Integer(), nullable=True),
sa.Column('timezone', sa.Unicode(), nullable=True),
sa.Column('moddate', sa.Date(), nullable=True),
sa.Column('name', sa.Unicode(length=250), nullable=False),
sa.Column('title', sa.Unicode(length=250), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['admin1_id'], ['geo_admin1_code.id'], ),
sa.ForeignKeyConstraint(['admin2_id'], ['geo_admin2_code.id'], ),
sa.ForeignKeyConstraint(['country'], ['geo_country_info.iso_alpha2'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name')
)
with op.batch_alter_table('geo_name', schema=None) as batch_op:
batch_op.create_index('ix_geo_name_ascii_title', [sa.text('lower(ascii_title)')], unique=False, postgresql_ops={'ascii_title_lower': 'varchar_pattern_ops'})
batch_op.create_index('ix_geo_name_title', [sa.text("lower('title')")], unique=False, postgresql_ops={'title_lower': 'varchar_pattern_ops'})

op.create_table('geo_alt_name',
sa.Column('geonameid', sa.Integer(), nullable=False),
sa.Column('lang', sa.Unicode(), nullable=True),
sa.Column('title', sa.Unicode(), nullable=False),
sa.Column('is_preferred_name', sa.Boolean(), nullable=False),
sa.Column('is_short_name', sa.Boolean(), nullable=False),
sa.Column('is_colloquial', sa.Boolean(), nullable=False),
sa.Column('is_historic', sa.Boolean(), nullable=False),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['geonameid'], ['geo_name.id'], ),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('geo_alt_name', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_geo_alt_name_lang'), ['lang'], unique=False)
batch_op.create_index('ix_geo_alt_name_title', [sa.text("lower('title')")], unique=False, postgresql_ops={'title_lower': 'varchar_pattern_ops'})

# ### end Alembic commands ###


def downgrade_geoname() -> None:
"""Downgrade geoname database."""
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('geo_alt_name', schema=None) as batch_op:
batch_op.drop_index('ix_geo_alt_name_title', postgresql_ops={'title_lower': 'varchar_pattern_ops'})
batch_op.drop_index(batch_op.f('ix_geo_alt_name_lang'))

op.drop_table('geo_alt_name')
with op.batch_alter_table('geo_name', schema=None) as batch_op:
batch_op.drop_index('ix_geo_name_title', postgresql_ops={'title_lower': 'varchar_pattern_ops'})
batch_op.drop_index('ix_geo_name_ascii_title', postgresql_ops={'ascii_title_lower': 'varchar_pattern_ops'})

op.drop_table('geo_name')
op.drop_table('geo_admin2_code')
op.drop_table('geo_admin1_code')
with op.batch_alter_table('geo_country_info', schema=None) as batch_op:
batch_op.drop_index('ix_geo_country_info_title', postgresql_ops={'title_lower': 'varchar_pattern_ops'})

op.drop_table('geo_country_info')
# ### end Alembic commands ###

0 comments on commit 43b41b3

Please sign in to comment.