Skip to content

Commit

Permalink
[proofing] Add more project metadata columns
Browse files Browse the repository at this point in the history
As requested by Suhas.

- Add a `Genre` table for project genres.
- Add the following columns to `Project`: `page_title` (`title` renamed
  to `display_title`), `worldcat_link`, `notes`, `genre_id`
- Add admin forms for `Genre`
- Update the project metadata form to include these new fields and add
  fieldset headers

Test plan: unit tests
  • Loading branch information
akprasad authored Apr 9, 2023
1 parent 73c5a17 commit 360c503
Show file tree
Hide file tree
Showing 37 changed files with 345 additions and 200 deletions.
7 changes: 6 additions & 1 deletion ambuda/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ class TextView(BaseView):


class ProjectView(BaseView):
column_list = ["slug", "title", "creator"]
column_list = ["slug", "display_title", "creator"]
form_excluded_columns = ["creator", "board", "pages", "created_at", "updated_at"]


class DictionaryView(BaseView):
column_list = form_columns = ["slug", "title"]


class GenreView(ModeratorBaseView):
pass


class SponsorshipView(ModeratorBaseView):
column_labels = dict(
sa_title="Sanskrit title",
Expand Down Expand Up @@ -100,6 +104,7 @@ def create_admin_manager(app):
admin.add_view(TextBlockView(db.TextBlock, session))
admin.add_view(TextView(db.Text, session))
admin.add_view(UserView(db.User, session))
admin.add_view(GenreView(db.Genre, session))
admin.add_view(SponsorshipView(db.ProjectSponsorship, session))
admin.add_view(ContributorInfoView(db.ContributorInfo, session))

Expand Down
2 changes: 1 addition & 1 deletion ambuda/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class SiteRole(str, Enum):
"""Defines user roles on Ambuda."""
"""Defines user roles."""

#: Basic proofer. Can mark pages as yellow and upload simple projects.
P1 = "p1"
Expand Down
33 changes: 30 additions & 3 deletions ambuda/models/proofing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ def text():
return Column(Text_, nullable=False, default="")


class Genre(Base):
"""A text genre.
We use genre to help proofers sort through different projects and select
one to work on.
"""

__tablename__ = "genres"

#: Primary key.
id = pk()
#: The name of this genre.
name = Column(String, unique=True, nullable=False)

def __str__(self):
return self.name


class Project(Base):

"""A proofreading project.
Expand All @@ -32,9 +50,11 @@ class Project(Base):
id = pk()
#: Human-readable ID, which we display in the URL.
slug = Column(String, unique=True, nullable=False)
#: Human-readable title, which we show on the page.
title = Column(String, nullable=False)

#: Human-readable title, which we show on the page.
display_title = Column(String, nullable=False)
#: The full book title as it appears in print.
print_title = string()
#: The document's author.
author = string()
#: The document's editor.
Expand All @@ -43,9 +63,13 @@ class Project(Base):
publisher = string()
#: The document's publication year.
publication_year = string()
#: A link to the book's WorldCat entry, if available.
worldcat_link = string()

#: Markdown for this project (to entice contributors, etc.)
description = text()
#: Notes about the project, for internal and scholarly use.
notes = text()
#: Defines page numbers (e.g. "x", "vii", ...)
page_numbers = text()

Expand All @@ -59,9 +83,12 @@ class Project(Base):
#: Creator of this project.
#: FIXME: make non-nullable once we manually migrate the production setup.
creator_id = Column(Integer, ForeignKey("users.id"), index=True)
#: The genre of this project.
genre_id = Column(Integer, ForeignKey("genres.id"), index=True)

creator = relationship("User")
board = relationship("Board", cascade="delete")
genre = relationship("Genre")

#: An ordered list of pages belonging to this project.
pages = relationship(
Expand Down Expand Up @@ -126,7 +153,7 @@ class PageStatus(Base):

#: Primary key.
id = pk()
#: Short human-readable label for this status.
#: A short human-readable label for this status.
name = Column(String, nullable=False, unique=True)


Expand Down
5 changes: 5 additions & 0 deletions ambuda/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,8 @@ def project_sponsorships() -> list[db.ProjectSponsorship]:
def contributor_info() -> list[db.ContributorInfo]:
session = get_session()
return session.query(db.ContributorInfo).order_by(db.ContributorInfo.name).all()


def genres() -> list[db.Genre]:
session = get_session()
return session.query(db.Genre).all()
24 changes: 13 additions & 11 deletions ambuda/tasks/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ def _split_pdf_into_pages(
return doc.page_count


def _add_project_to_database(title: str, slug: str, num_pages: int, creator_id: int):
def _add_project_to_database(
display_title: str, slug: str, num_pages: int, creator_id: int
):
"""Create a project on the database.
:param title: the project title
:param display_title: the project title
:param num_pages: the number of pages in the project
"""

Expand All @@ -49,7 +51,7 @@ def _add_project_to_database(title: str, slug: str, num_pages: int, creator_id:
session.add(board)
session.flush()

project = db.Project(slug=slug, title=title, creator_id=creator_id)
project = db.Project(slug=slug, display_title=display_title, creator_id=creator_id)
project.board_id = board.id
session.add(project)
session.flush()
Expand All @@ -72,7 +74,7 @@ def _add_project_to_database(title: str, slug: str, num_pages: int, creator_id:

def create_project_inner(
*,
title: str,
display_title: str,
pdf_path: str,
output_dir: str,
app_environment: str,
Expand All @@ -84,25 +86,25 @@ def create_project_inner(
We separate this function from `create_project` so that we can run this
function in a non-Celery context (for example, in `cli.py`).
:param title: the project title.
:param display_title: the project's title.
:param pdf_path: local path to the source PDF.
:param output_dir: local path where page images will be stored.
:param app_environment: the app environment, e.g. `"development"`.
:param creator_id: the user that created this project.
:param task_status: tracks progress on the task.
"""
logging.info(f'Received upload task "{title}" for path {pdf_path}.')
logging.info(f'Received upload task "{display_title}" for path {pdf_path}.')

# Tasks must be idempotent. Exit if the project already exists.
app = create_config_only_app(app_environment)
with app.app_context():
session = q.get_session()
slug = slugify(title)
slug = slugify(display_title)
project = session.query(db.Project).filter_by(slug=slug).first()

if project:
raise ValueError(
f'Project "{title}" already exists. Please choose a different title.'
f'Project "{display_title}" already exists. Please choose a different title.'
)

pdf_path = Path(pdf_path)
Expand All @@ -111,7 +113,7 @@ def create_project_inner(
num_pages = _split_pdf_into_pages(Path(pdf_path), Path(pages_dir), task_status)
with app.app_context():
_add_project_to_database(
title=title,
display_title=display_title,
slug=slug,
num_pages=num_pages,
creator_id=creator_id,
Expand All @@ -124,7 +126,7 @@ def create_project_inner(
def create_project(
self,
*,
title: str,
display_title: str,
pdf_path: str,
output_dir: str,
app_environment: str,
Expand All @@ -136,7 +138,7 @@ def create_project(
"""
task_status = CeleryTaskStatus(self)
create_project_inner(
title=title,
display_title=display_title,
pdf_path=pdf_path,
output_dir=output_dir,
app_environment=app_environment,
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/blog/edit-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% import 'macros/forms.html' as mf %}


{% block title %}{{ mc.title('Edit post: {}'.format(post.title)) }}{% endblock %}
{% block title %}{{ mc.title('Edit post: {}'.format(post.display_title)) }}{% endblock %}


{% block content %}
Expand Down
10 changes: 5 additions & 5 deletions ambuda/templates/macros/proofing.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h1 class="font-bold text-4xl">{{ title|safe }}</h1>

{% macro project_header_nested(label, project) %}
{% set url = url_for("proofing.project.summary", slug=project.slug) %}
{{ nested_header(label, project.title, url) }}
{{ nested_header(label, project.display_title, url) }}
{% endmacro %}


Expand Down Expand Up @@ -113,7 +113,7 @@ <h1 class="mt-2 font-bold">{{ _('Contribute') }}</h1>
<h1 class="text-3xl">
<a class="text-slate-400 font-bold"
href="{{ url_for("proofing.project.summary", slug=project.slug) }}">
{{ project.title }}</a>
{{ project.display_title }}</a>
/{{ cur.slug }}
</h1>

Expand Down Expand Up @@ -141,7 +141,7 @@ <h1 class="text-3xl">
project_slug=r.project.slug, page_slug=r.page.slug) %}
{% set author_url = url_for('proofing.user.summary',
username=r.author.username) %}
{% set page_title = r.project.title + "/" + r.page.slug %}
{% set page_title = r.project.display_title + "/" + r.page.slug %}
{% set author = r.author.username %}

<span class="{{ revision_colors(r.status.name) }} inline-block w-2 h-2 mr-0.5 rounded-sm"></span>
Expand All @@ -158,7 +158,7 @@ <h1 class="text-3xl">
{% macro project_li(p) %}
<li class="my-2 flex justify-between">
{% set project_url = url_for('proofing.project.summary', slug=p.slug) %}
{% set title = p.title %}
{% set title = p.display_title %}
{% set creator = p.creator.username %}
{% set creator_url = url_for('proofing.user.summary', username=creator) %}
<div>
Expand Down Expand Up @@ -202,7 +202,7 @@ <h1 class="text-3xl">
project_slug=r.project.slug, page_slug=r.page.slug) %}
{% set author_url = url_for('proofing.user.summary',
username=r.author.username) %}
{% set page_title = r.project.title + "/" + r.page.slug %}
{% set page_title = r.project.display_title + "/" + r.page.slug %}
{% set author = r.author.username %}

<span class="{{ revision_colors(r.status.name) }} inline-block w-2 h-2 mr-0.5 rounded-full"></span>
Expand Down
4 changes: 2 additions & 2 deletions ambuda/templates/proofing/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ <h1 class="md:!mt-0">{{ _('Ongoing projects') }}</h1>
<ul x-ref="list">
{% for p in projects %}
<li data-key="{{ p.id }}"
data-title="{{ p.title }}"
data-title="{{ p.display_title }}"
data-created="{{ p.created_at }}"
data-progress="{{ progress_per_project[p.id] }}"
x-show="displayed.has('{{ p.id }}')">
<div class="mt-4 a-hover-underline flex justify-between items-baseline">
<a href="{{ url_for("proofing.project.summary", slug=p.slug) }}">{{ p.title }}</a>
<a href="{{ url_for("proofing.project.summary", slug=p.slug) }}">{{ p.display_title }}</a>
{% set count = pages_per_project[p.id] %}
<span class="text-xs">
{{ ngettext('%(num)d page', '%(num)d pages', count) }}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/pages/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{% endmacro %}


{% block title %}Edit: {{ project.title }}/{{ cur.slug }} | Ambuda{% endblock %}
{% block title %}Edit: {{ project.display_title }}/{{ cur.slug }} | Ambuda{% endblock %}


{% block main %}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/pages/editor-components.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<h1 class="text pr-4">
<a class="text-black font-bold text-xl"
href="{{ url_for("proofing.project.summary", slug=project.slug) }}">
{{ project.title }}</a> / {{ image_number }}</a>
{{ project.display_title }}</a> / {{ image_number }}</a>
<span class="block text-sm text-slate-500">
Image {{ image_number }} of {{ num_images }}
{% if image_title != page_number %}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/pages/history.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'proofing/base.html' %}
{% import "macros/proofing.html" as m %}

{% block title %}History: {{ project.title }}/{{ cur.slug }} | Ambuda{% endblock %}
{% block title %}History: {{ project.display_title }}/{{ cur.slug }} | Ambuda{% endblock %}

{% block content %}
{{ m.page_header(project, cur=cur, prev=prev, next=next) }}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/pages/revision.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% extends 'proofing/base.html' %}
{% import "macros/proofing.html" as m %}

{% block title %}Revision: {{ project.title }}/{{ cur.slug }} | Ambuda{% endblock %}
{% block title %}Revision: {{ project.display_title }}/{{ cur.slug }} | Ambuda{% endblock %}

{% block content %}
<div class="mb-4">
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/projects/activity.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% import "macros/proofing.html" as m %}


{% block title %}Activity: {{ project.title }} | Ambuda{% endblock %}
{% block title %}Activity: {{ project.display_title }} | Ambuda{% endblock %}


{% block sidebar %}{{ m.main_nav('projects', current_user=current_user) }}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/projects/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% import "macros/proofing.html" as m %}


{% block title %}Admin: {{ project.title }} | Ambuda{% endblock %}
{% block title %}Admin: {{ project.display_title }} | Ambuda{% endblock %}


{% block sidebar %}{{ m.main_nav('projects', current_user=current_user) }}{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/projects/batch-ocr-post.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% import "macros/proofing.html" as m %}


{% block title %}OCR {{ project.title }} | Ambuda{% endblock %}
{% block title %}OCR: {{ project.display_title }} | Ambuda{% endblock %}


{% block content %}
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/projects/batch-ocr.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{% import "macros/proofing.html" as m %}


{% block title %}OCR {{ project.title }} | Ambuda{% endblock %}
{% block title %}OCR {{ project.display_title }} | Ambuda{% endblock %}


{% block sidebar %}{{ m.main_nav('projects', current_user=current_user) }}{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions ambuda/templates/proofing/projects/confirm_changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% from "macros/forms.html" import field %}
{% import "macros/proofing.html" as m %}

{% block title %} Search and Replace | {{ project.title }}{% endblock %}
{% block title %} Search and Replace | {{ project.display_title }}{% endblock %}

{% block content %}

Expand All @@ -28,7 +28,7 @@ <h1>Confirm Changes</h1>
{% set matches = result.matches %}
{% for match in matches %}
<div class="match" style="background-color: rgb(243, 239, 239);">
<p>Page <a href="{{ page_url }}">{{ project.title }}/{{ page.slug }}:</a> Line {{ match.line_num }}</p>
<p>Page <a href="{{ page_url }}">{{ project.display_title }}/{{ page.slug }}:</a> Line {{ match.line_num }}</p>
<label for="match{{ page.slug }}-{{ match.line_num }}">{{ match.query }}</label>
<input type="hidden" name="match{{ page.slug }}-{{ match.line_num }}-query" value="{{ match.query }}">
<br>
Expand Down
2 changes: 1 addition & 1 deletion ambuda/templates/proofing/projects/download.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% import "macros/proofing.html" as m %}


{% block title %}Download: {{ project.title }} | Ambuda{% endblock %}
{% block title %}Download: {{ project.display_title }} | Ambuda{% endblock %}


{% block sidebar %}{{ m.main_nav('projects', current_user=current_user) }}{% endblock %}
Expand Down
Loading

0 comments on commit 360c503

Please sign in to comment.