From 7befcfb5fcf3000b7a49fa6c468e9970abd1f1e7 Mon Sep 17 00:00:00 2001 From: Oleksandr Cherniavskyi Date: Sun, 23 Jul 2023 14:45:01 +0300 Subject: [PATCH] integrate with admin-panel, write models and migrations --- ckanext/tour/cli.py | 20 -- ckanext/tour/logic/action.py | 14 +- ckanext/tour/migration/tour/README | 1 + ckanext/tour/migration/tour/alembic.ini | 74 +++++++ ckanext/tour/migration/tour/env.py | 81 ++++++++ ckanext/tour/migration/tour/script.py.mako | 24 +++ ...e58_create_tour_tourstep_tourstepimage_.py | 104 ++++++++++ ckanext/tour/model.py | 188 ++++++++++++++++++ ckanext/tour/plugin.py | 92 ++++----- ckanext/tour/templates/tour/tour_list.html | 14 ++ ckanext/tour/views.py | 24 ++- requirements.txt | 2 + 12 files changed, 549 insertions(+), 89 deletions(-) delete mode 100644 ckanext/tour/cli.py create mode 100644 ckanext/tour/migration/tour/README create mode 100644 ckanext/tour/migration/tour/alembic.ini create mode 100644 ckanext/tour/migration/tour/env.py create mode 100644 ckanext/tour/migration/tour/script.py.mako create mode 100644 ckanext/tour/migration/tour/versions/c9e5d4235e58_create_tour_tourstep_tourstepimage_.py create mode 100644 ckanext/tour/model.py create mode 100644 ckanext/tour/templates/tour/tour_list.html diff --git a/ckanext/tour/cli.py b/ckanext/tour/cli.py deleted file mode 100644 index 2b59e80..0000000 --- a/ckanext/tour/cli.py +++ /dev/null @@ -1,20 +0,0 @@ -import click - - -@click.group(short_help="tour CLI.") -def tour(): - """tour CLI. - """ - pass - - -@tour.command() -@click.argument("name", default="tour") -def command(name): - """Docs. - """ - click.echo("Hello, {name}!".format(name=name)) - - -def get_commands(): - return [tour] diff --git a/ckanext/tour/logic/action.py b/ckanext/tour/logic/action.py index 8fdc8da..11d7d2a 100644 --- a/ckanext/tour/logic/action.py +++ b/ckanext/tour/logic/action.py @@ -3,7 +3,7 @@ @tk.side_effect_free -def tour_get_sum(context, data_dict): +def tour_list(context, data_dict): tk.check_access( "tour_get_sum", context, data_dict) data, errors = tk.navl_validate( @@ -12,14 +12,4 @@ def tour_get_sum(context, data_dict): if errors: raise tk.ValidationError(errors) - return { - "left": data["left"], - "right": data["right"], - "sum": data["left"] + data["right"] - } - - -def get_actions(): - return { - 'tour_get_sum': tour_get_sum, - } + return [] diff --git a/ckanext/tour/migration/tour/README b/ckanext/tour/migration/tour/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/ckanext/tour/migration/tour/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/ckanext/tour/migration/tour/alembic.ini b/ckanext/tour/migration/tour/alembic.ini new file mode 100644 index 0000000..13faffa --- /dev/null +++ b/ckanext/tour/migration/tour/alembic.ini @@ -0,0 +1,74 @@ +# A generic, single database configuration. + +[alembic] +# path to migration scripts +script_location = %(here)s + +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# timezone to use when rendering the date +# within the migration file as well as the filename. +# string value is passed to dateutil.tz.gettz() +# leave blank for localtime +# timezone = + +# max length of characters to apply to the +# "slug" field +#truncate_slug_length = 40 + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + +# set to 'true' to allow .pyc and .pyo files without +# a source .py file to be detected as revisions in the +# versions/ directory +# sourceless = false + +# version location specification; this defaults +# to /home/berry/projects/master/ckanext-tour/ckanext/tour/migration/tour/versions. When using multiple version +# directories, initial revisions must be specified with --version-path +# version_locations = %(here)s/bar %(here)s/bat /home/berry/projects/master/ckanext-tour/ckanext/tour/migration/tour/versions + +# the output encoding used when revision files +# are written from script.py.mako +# output_encoding = utf-8 + +sqlalchemy.url = driver://user:pass@localhost/dbname + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/ckanext/tour/migration/tour/env.py b/ckanext/tour/migration/tour/env.py new file mode 100644 index 0000000..0093682 --- /dev/null +++ b/ckanext/tour/migration/tour/env.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- + +from __future__ import with_statement +from alembic import context +from sqlalchemy import engine_from_config, pool +from logging.config import fileConfig + +import os + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + +name = os.path.basename(os.path.dirname(__file__)) + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + + url = config.get_main_option(u"sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True, + version_table=u'{}_alembic_version'.format(name) + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix=u'sqlalchemy.', + poolclass=pool.NullPool) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + version_table=u'{}_alembic_version'.format(name) + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/ckanext/tour/migration/tour/script.py.mako b/ckanext/tour/migration/tour/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/ckanext/tour/migration/tour/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/ckanext/tour/migration/tour/versions/c9e5d4235e58_create_tour_tourstep_tourstepimage_.py b/ckanext/tour/migration/tour/versions/c9e5d4235e58_create_tour_tourstep_tourstepimage_.py new file mode 100644 index 0000000..0e3f4ec --- /dev/null +++ b/ckanext/tour/migration/tour/versions/c9e5d4235e58_create_tour_tourstep_tourstepimage_.py @@ -0,0 +1,104 @@ +"""Create Tour, TourStep, TourStepImage tables + +Revision ID: c9e5d4235e58 +Revises: +Create Date: 2023-07-23 13:20:58.129326 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "c9e5d4235e58" +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + "tour", + sa.Column("id", sa.Text, primary_key=True, unique=True), + sa.Column("state", sa.Text, nullable=False, server_default="active"), + sa.Column("anchor", sa.Text, nullable=False), + sa.Column("page", sa.Text, nullable=True), + sa.Column( + "created_at", + sa.DateTime, + nullable=False, + server_default=sa.func.current_timestamp(), + ), + sa.Column( + "modified_at", + sa.DateTime, + nullable=False, + server_default=sa.func.current_timestamp(), + ), + sa.Column( + "author_id", + sa.Text, + sa.ForeignKey("user.id", ondelete="CASCADE"), + primary_key=True, + ), + ) + + op.create_table( + "tour_step", + sa.Column("id", sa.Text, primary_key=True, unique=True), + sa.Column("title", sa.Text), + sa.Column("element", sa.Text), + sa.Column("intro", sa.Text), + sa.Column("position", sa.Text), + sa.Column( + "tour_id", + sa.Text, + sa.ForeignKey("tour.id", ondelete="CASCADE"), + primary_key=True, + ), + ) + + op.create_table( + "tour_step_image", + sa.Column("id", sa.Text, primary_key=True, unique=True), + sa.Column("file_id", sa.Text, unique=True), + sa.Column( + "uploaded_at", + sa.DateTime, + nullable=False, + server_default=sa.func.current_timestamp(), + ), + sa.Column( + "tour_step_id", + sa.Text, + sa.ForeignKey("tour_step.id", ondelete="CASCADE"), + primary_key=True, + ), + ) + + op.create_foreign_key( + "tour_step_fk", + "tour_step", + "tour", + ["tour_id"], + ["id"], + ondelete="CASCADE", + ) + + op.create_foreign_key( + "tour_step_image_fk", + "tour_step_image", + "tour_step", + ["tour_step_id"], + ["id"], + ondelete="CASCADE", + ) + + +def downgrade(): + op.drop_constraint("tour_step_fk", "tour_step", type_="foreignkey") + op.drop_constraint("tour_step_image_fk", "tour_step_image", type_="foreignkey") + + op.drop_table("tour_step_image") + op.drop_table("tour_step") + op.drop_table("tour") diff --git a/ckanext/tour/model.py b/ckanext/tour/model.py new file mode 100644 index 0000000..b832e2f --- /dev/null +++ b/ckanext/tour/model.py @@ -0,0 +1,188 @@ +from __future__ import annotations + +import logging +from datetime import datetime +from typing import Any + +from sqlalchemy import Column, DateTime, ForeignKey, Text +from sqlalchemy.orm import Query, relationship +from typing_extensions import Self + +from ckan import model +from ckan.model.types import make_uuid +from ckan.plugins import toolkit as tk + + +log = logging.getLogger(__name__) + + +class Tour(tk.BaseModel): + __tablename__ = "tour" + + class State: + active = "active" + inactive = "inactive" + + id = Column(Text, primary_key=True, default=make_uuid) + + state = Column(Text, nullable=False, default=State.active) + author_id = Column(ForeignKey(model.User.id, ondelete="CASCADE"), primary_key=True) + created_at = Column(DateTime, nullable=False, default=datetime.utcnow) + modified_at = Column(DateTime, nullable=False, default=datetime.utcnow) + + anchor = Column(Text, nullable=False) + page = Column(Text, nullable=True) + + user = relationship(model.User) + + def __repr__(self): + return f"Tour(id={self.id!r} author_id={self.author_id!r}" + + @classmethod + def create(cls, data_dict) -> Self: + tour = cls(**data_dict) + + model.Session.add(tour) + model.Session.commit() + + return tour + + def dictize(self, context): + return { + "id": self.id, + "author_id": self.author_id, + "state": self.state, + "created_at": self.created_at.isoformat(), + "modified_at": self.modified_at.isoformat(), + "anchor": self.anchor, + "page": self.page, + "steps": [step.dictize(context) for step in self.steps], + } + + @property + def steps(self) -> list[TourStep]: + return [request_study for request_study in TourStep.get_by_tour(self.id)] + + @classmethod + def get(cls, tour_id: str) -> Self | None: + query: Query = model.Session.query(cls).filter(cls.id == tour_id) + + return query.one_or_none() + + @classmethod + def all(cls) -> list[Tour]: + query: Query = model.Session.query(cls).order_by(cls.created_at.desc()) + + return query.all() # type: ignore + + +class TourStep(tk.BaseModel): + __tablename__ = "tour_step" + + class Position: + bottom = "bottom" + top = "top" + right = "right" + left = "left" + + id = Column(Text, primary_key=True, default=make_uuid) + + title = Column(Text, nullable=True) + element = Column(Text) + intro = Column(Text, nullable=True) + position = Column(Text, default=Position.bottom) + tour_id = Column(Text, ForeignKey("tour.id", ondelete="CASCADE")) + + @classmethod + def create(cls, data_dict) -> Self: + tour_step = cls(**data_dict) + + model.Session.add(tour_step) + model.Session.commit() + + return tour_step + + @classmethod + def get_by_tour(cls, touri_d: str) -> list[Self]: + query: Query = model.Session.query(cls).filter(cls.tour_id == _) + + return query.all() + + @property + def image(self) -> TourStepImage: + return TourStepImage.get_by_step(self.id) + + def dictize(self, context): + return { + "id": self.id, + "title": self.title, + "element": self.element, + "intro": self.intro, + "position": self.position, + "tour_id": self.tour_id, + "image": [request_file.dictize(context) for request_file in self.files], + } + + +class TourStepImage(tk.BaseModel): + __tablename__ = "tour_step_image" + + id = Column(Text, primary_key=True, default=make_uuid) + + file_id = Column(Text, unique=True) + uploaded_at = Column(DateTime, nullable=False, default=datetime.utcnow) + tour_step_id = Column(Text, ForeignKey("tour_step.id", ondelete="CASCADE")) + + @classmethod + def create(cls, data_dict) -> Self: + # data_dict.pop("name", None) + # data_dict.pop("upload", None) + + tour_step_image = cls(**data_dict) + + model.Session.add(tour_step_image) + model.Session.commit() + + return tour_step_image + + @classmethod + def get(cls, file_id: str) -> Self | None: + query: Query = model.Session.query(cls).filter(cls.file_id == file_id) + + return query.one_or_none() + + @classmethod + def get_by_step(cls, tour_step_id: str) -> Self | None: + query: Query = model.Session.query(cls).filter(cls.tour_step_id == tour_step_id) + + return query.all() # type: ignore + + def dictize(self, context): + uploaded_file = self.get_file_data(self.file_id) + + return { + "id": self.id, + "file_id": self.file_id, + "tour_step_id": self.tour_step_id, + "uploaded_at": self.uploaded_at.isoformat(), + "url": uploaded_file["url"], + } + + def delete(self) -> None: + model.Session().autoflush = False + model.Session.delete(self) + + def get_file_data(self, file_id: str) -> dict[str, Any]: + """Return a real uploaded file data""" + try: + result = tk.get_action("files_file_show")( + {"ignore_auth": True}, {"id": file_id} + ) + except tk.ObjectNotFound: + raise TourStepFileError("File not found.") + + return result + + +class TourStepFileError(Exception): + pass diff --git a/ckanext/tour/plugin.py b/ckanext/tour/plugin.py index ae32b32..bb57739 100644 --- a/ckanext/tour/plugin.py +++ b/ckanext/tour/plugin.py @@ -1,61 +1,51 @@ -import ckan.plugins as plugins -import ckan.plugins.toolkit as toolkit +from __future__ import annotations +import ckan.plugins as plugins +import ckan.plugins.toolkit as tk -# import ckanext.tour.cli as cli -# import ckanext.tour.helpers as helpers -# import ckanext.tour.views as views -# from ckanext.tour.logic import ( -# action, auth, validators -# ) +from ckanext.admin_panel.interfaces import IAdminPanel +from ckanext.admin_panel.types import SectionConfig, ConfigurationItem +@tk.blanket.helpers +@tk.blanket.actions +@tk.blanket.auth_functions +@tk.blanket.blueprints class TourPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) - - # plugins.implements(plugins.IAuthFunctions) - # plugins.implements(plugins.IActions) - # plugins.implements(plugins.IBlueprint) - # plugins.implements(plugins.IClick) - # plugins.implements(plugins.ITemplateHelpers) - # plugins.implements(plugins.IValidators) - + plugins.implements(IAdminPanel) # IConfigurer def update_config(self, config_): - toolkit.add_template_directory(config_, "templates") - toolkit.add_public_directory(config_, "public") - toolkit.add_resource("assets", "tour") - - - # IAuthFunctions - - # def get_auth_functions(self): - # return auth.get_auth_functions() - - # IActions - - # def get_actions(self): - # return action.get_actions() - - # IBlueprint - - # def get_blueprint(self): - # return views.get_blueprints() - - # IClick - - # def get_commands(self): - # return cli.get_commands() - - # ITemplateHelpers - - # def get_helpers(self): - # return helpers.get_helpers() - - # IValidators - - # def get_validators(self): - # return validators.get_validators() - + tk.add_template_directory(config_, "templates") + tk.add_public_directory(config_, "public") + tk.add_resource("assets", "tour") + + # IAdminPanel + + def register_config_sections( + self, config_list: list[SectionConfig] + ) -> list[SectionConfig]: + config_list.append( + SectionConfig( + name="Tour", + configs=[ + ConfigurationItem( + name="Global settings", + blueprint="tour.list", + ), + ConfigurationItem( + name="Manage tours", + blueprint="tour.list", + info="Manage existing tours", + ), + ConfigurationItem( + name="Add tour", + blueprint="user.index", + info="Add new tour", + ), + ], + ) + ) + return config_list diff --git a/ckanext/tour/templates/tour/tour_list.html b/ckanext/tour/templates/tour/tour_list.html new file mode 100644 index 0000000..0066ee4 --- /dev/null +++ b/ckanext/tour/templates/tour/tour_list.html @@ -0,0 +1,14 @@ +{% extends 'admin_panel/base.html' %} + +{% import 'macros/autoform.html' as autoform %} +{% import 'macros/form.html' as form %} + +{% block breadcrumb_content %} +
  • {% link_for _("Tour list"), named_route='tour.list' %}
  • +{% endblock breadcrumb_content %} + +{% block ap_content %} +

    {{ _("Tour list") }}

    + +

    hello world

    +{% endblock ap_content %} diff --git a/ckanext/tour/views.py b/ckanext/tour/views.py index 71acf57..91506ee 100644 --- a/ckanext/tour/views.py +++ b/ckanext/tour/views.py @@ -1,16 +1,28 @@ +from __future__ import annotations + from flask import Blueprint +from flask.views import MethodView + +import ckan.plugins.toolkit as tk + + +tour = Blueprint("tour", __name__) -tour = Blueprint( - "tour", __name__) +class TourListView(MethodView): + def get(self) -> str: + return tk.render("tour/tour_list.html") -def page(): - return "Hello, tour!" +class TourAddView(MethodView): + def get(self) -> str: + return tk.render("tour/tour_list.html") + def post(self) -> str: + pass -tour.add_url_rule( - "/tour/page", view_func=page) +tour.add_url_rule("/admin_panel/config/tour/", view_func=TourListView.as_view("list")) +tour.add_url_rule("/admin_panel/config/tour/new", view_func=TourAddView.as_view("add")) def get_blueprints(): diff --git a/requirements.txt b/requirements.txt index e69de29..e41a7a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1,2 @@ +ckanext-files==0.0.3 +ckanext-admin-panel==0.0.1