Skip to content

Commit

Permalink
frontend: use common enum constants for sharing descriptions
Browse files Browse the repository at this point in the history
... between fields and forms
  • Loading branch information
nikromen committed Nov 27, 2023
1 parent 3b21979 commit f0e3c47
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 47 deletions.
50 changes: 49 additions & 1 deletion frontend/coprs_frontend/coprs/constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
"""
File which contains only constants. Nothing else.
"""

from collections import namedtuple
from enum import Enum
from typing import Any

BANNER_LOCATION = "/var/lib/copr/data/banner-include.html"

DEFAULT_COPR_REPO_PRIORITY = 99


CommonAttribute = namedtuple(
"CommonAttribute", ["description", "default"], defaults=("", None)
)


# just shortcut
c = CommonAttribute # pylint: disable=invalid-name


# Common descriptions for forms, fields, etc.
class CommonDescriptions(Enum):
"""
Enumerator for common descriptions and their default value between forms,
fields, etc.
"""
ADDITIONAL_PACKAGES = c(
"Additional packages to be always present in minimal buildroot"
)
MOCK_CHROOT = c("Mock chroot", "fedora-latest-x86_64")
ADDITIONAL_REPOS = c("Additional repos to be used for builds in this chroot")
ENABLE_NET = c("Enable internet access during builds")
PYPI_PACKAGE_NAME = c("Package name in the Python Package Index")
PYPI_PACKAGE_VERSION = c("PyPI package version")
SPEC_GENERATOR = c(
"Tool for generating specfile from a PyPI package. "
"The options are full-featured pyp2rpm with cross "
"distribution support, and pyp2spec that is being actively "
"developed and considered to be the future."
)
AUTO_REBUILD = c("Auto-rebuild the package? (i.e. every commit or new tag)")

@property
def description(self) -> str:
"""
Get description of Enum member
"""
return self.value.description

@property
def default(self) -> Any:
"""
Fet default value of Enum member
"""
return self.value.default
13 changes: 7 additions & 6 deletions frontend/coprs_frontend/coprs/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from coprs import exceptions
from coprs import helpers
from coprs import models
from coprs.constants import CommonDescriptions
from coprs.logic.coprs_logic import CoprsLogic, MockChrootsLogic
from coprs.logic.users_logic import UsersLogic
from coprs.logic.dist_git_logic import DistGitLogic
Expand Down Expand Up @@ -625,11 +626,11 @@ class CoprForm(BaseForm):

# Deprecated, use `enable_net` instead
build_enable_net = wtforms.BooleanField(
"Enable internet access during builds",
CommonDescriptions.ENABLE_NET.description,
default=False, false_values=FALSE_VALUES)

enable_net = wtforms.BooleanField(
"Enable internet access during builds",
CommonDescriptions.ENABLE_NET.description,
default=False, false_values=FALSE_VALUES)

module_hotfixes = wtforms.BooleanField(
Expand Down Expand Up @@ -1057,9 +1058,9 @@ class PackageFormCustom(BasePackageForm):
filters=[StringListFilter()])

chroot = wtforms.SelectField(
'Mock chroot',
CommonDescriptions.MOCK_CHROOT.description,
choices=[],
default='fedora-latest-x86_64',
default=CommonDescriptions.MOCK_CHROOT.default,
)

resultdir = wtforms.StringField(
Expand Down Expand Up @@ -1624,8 +1625,8 @@ class F(BaseForm):


class ModifyChrootForm(ChrootForm):
buildroot_pkgs = wtforms.StringField('Additional packages to be always present in minimal buildroot')
repos = wtforms.TextAreaField('Additional repos to be used for builds in chroot',
buildroot_pkgs = wtforms.StringField(CommonDescriptions.ADDITIONAL_PACKAGES.description)
repos = wtforms.TextAreaField(CommonDescriptions.ADDITIONAL_REPOS.description,
validators=[UrlRepoListValidator(),
wtforms.validators.Optional()],
filters=[StringListFilter()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ <h3 class="panel-title">{{ counter('instructions') }}. Select chroots and other
{% endmacro %}


{% macro copr_build_form_pypi(form, view, copr) %}
{% macro copr_build_form_pypi(form, view, copr, common_descriptions) %}
{{ copr_build_form_begin(form, view, copr) }}

{{ source_description(
Expand All @@ -161,15 +161,12 @@ <h3 class="panel-title">{{ counter('instructions') }}. Select chroots and other
)
}}

{{ render_field(form.pypi_package_name, placeholder="Package name in the Python Package Index.") }}
{{ render_field(form.pypi_package_name, placeholder="{{ common_descriptions.PYPI_PACKAGE_NAME.description }}.") }}
{{ render_field(form.pypi_package_version, placeholder="Optional - Version of the package PyPI") }}

{{ render_field(
form.spec_generator,
info="Tool for generating specfile from a PyPI package. The options "
"are full-featured <strong>pyp2rpm</strong> with cross "
"distribution support, and <strong>pyp2spec</strong> that is "
"being actively developed and considered to be the future."
info="{{ common_descriptions.SPEC_GENERATOR.description }}"
)
}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ <h3 class="panel-title">{{ counter('instructions') }}. Provide the source</h3>
</form>
{% endmacro %}

{% macro render_webhook_rebuild(form) %}
{% macro render_webhook_rebuild(form, common_descriptions) %}
<div class="form-group">
<label class="col-sm-2 control-label" for="textInput-markup">
Auto-rebuild
</label>
<div class="col-sm-10">
<input type="checkbox" name="webhook_rebuild" {% if form.webhook_rebuild.data == True %}checked="checked"{% endif %}/>
Auto-rebuild the package? (i.e. every commit or new tag)
{{ common_descriptions.SPEC_GENERATOR.description }}
| See <a href="{{ copr_url('coprs_ns.copr_integrations', copr) }}">Integrations</a>
</div>
</div>
Expand Down Expand Up @@ -88,7 +88,7 @@ <h3 class="panel-title">{{ counter('instructions') }}. Generic package setup</h3

{% macro copr_package_form_pypi(form, view, copr, package) %}
{{ copr_package_form_begin(form, view, copr, package) }}
{{ render_field(form.pypi_package_name, placeholder="Package name in the Python Package Index.") }}
{{ render_field(form.pypi_package_name, placeholder="{{ common_descriptions.PYPI_PACKAGE_NAME.description }}.") }}

{{ render_field(
form.spec_generator,
Expand Down Expand Up @@ -132,11 +132,11 @@ <h3 class="panel-title">
{% endmacro %}


{% macro copr_package_form_custom(form, view, copr, package) %}
{% macro copr_package_form_custom(form, view, copr, package, common_descriptions) %}
{{ copr_package_form_begin(form, view, copr, package) }}
{{ copr_method_form_fileds_custom(form) }}
{{ render_generic_pkg_form(form) }}
{{ render_webhook_rebuild(form) }}
{{ render_webhook_rebuild(form, common_descriptions) }}
{{ copr_package_form_end(form, package, 'custom') }}
{% endmacro %}

Expand Down Expand Up @@ -164,7 +164,7 @@ <h3 class="panel-title">
{% endmacro %}


{% macro copr_package_form_scm(form, view, copr, package) %}
{% macro copr_package_form_scm(form, view, copr, package, common_descriptions) %}
{{ copr_package_form_begin(form, view, copr, package) }}

{{ render_field(form.scm_type) }}
Expand All @@ -175,7 +175,7 @@ <h3 class="panel-title">

{{ render_srpm_build_method_box(form) }}
{{ render_generic_pkg_form(form) }}
{{ render_webhook_rebuild(form) }}
{{ render_webhook_rebuild(form, common_descriptions) }}
{{ copr_package_form_end(form, package, 'mock_scm') }}
{% endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
{{ copr_package_form_rubygems(form_rubygems, view, copr, package) }}

{% elif source_type_text == "custom" %}
{{ copr_package_form_custom(form_custom, view, copr, package) }}
{{ copr_package_form_custom(form_custom, view, copr, package, common_descriptions) }}

{% else %}
<h3>Wrong source type</h3>
Expand Down
41 changes: 18 additions & 23 deletions frontend/coprs_frontend/coprs/views/apiv3_ns/schema/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@

from flask_restx.fields import String, List, Integer, Boolean, Url, Raw

from coprs.constants import CommonDescriptions

# TODO: split these fields to some hierarchy e.g. using dataclasses or to some clusters

# TODO: Use some shared constants for description - a lot of it is basically copied
# description from forms
# If you find that some descriptions/examples can be shared between forms and
# fields, please specify it in CommonDescriptions

id_field = Integer(
description="Numeric ID",
example=123,
)

mock_chroot = String(
description="Mock chroot",
example="fedora-rawhide-x86_64",
description=CommonDescriptions.MOCK_CHROOT.description,
example=CommonDescriptions.MOCK_CHROOT.default,
)

ownername = String(
description="User or group name",
description="User name or group name (starts with @)",
example="@copr",
)

full_name = String(
description="Full name of the project",
description="Full name of the project in format ownername/projectname",
example="@copr/pull-requests",
)

Expand All @@ -39,12 +41,12 @@
)

project_dirname = String(
description="",
description="Path to directory in project separated by colon",
example="copr-dev:pr:123",
)

packagename = String(
description="Name of the package",
description="Name of the package in project",
example="copr-cli",
)

Expand All @@ -56,18 +58,18 @@

additional_repos = List(
String,
description="Additional repos to be used for builds in this chroot",
description=CommonDescriptions.ADDITIONAL_REPOS.description,
)

additional_packages = List(
String,
description="Additional packages to be always present in minimal buildroot",
description=CommonDescriptions.ADDITIONAL_PACKAGES.description,
)

additional_modules = List(
String,
description=(
"List of modules that will be enabled " "or disabled in the given chroot"
"List of modules that will be enabled or disabled in the given chroot"
),
example=["module1:stream", "!module2:stream"],
)
Expand Down Expand Up @@ -101,7 +103,7 @@
)

enable_net = Boolean(
description="Enable internet access during builds",
description=CommonDescriptions.ENABLE_NET.description,
)

source_type = String(
Expand All @@ -123,24 +125,17 @@
)

pypi_package_name = String(
description="Package name in the Python Package Index.",
description=CommonDescriptions.PYPI_PACKAGE_NAME.description,
example="copr",
)

pypi_package_version = String(
description="PyPI package version",
description=CommonDescriptions.PYPI_PACKAGE_VERSION.description,
example="1.128pre",
)

# TODO We are copy-pasting descriptions from web UI to this file. This field
# is an ideal candidate for figuring out how to share the descriptions
spec_generator = String(
description=(
"Tool for generating specfile from a PyPI package. "
"The options are full-featured pyp2rpm with cross "
"distribution support, and pyp2spec that is being actively "
"developed and considered to be the future."
),
description=CommonDescriptions.SPEC_GENERATOR.description,
example="pyp2spec",
)

Expand All @@ -162,7 +157,7 @@
)

auto_rebuild = Boolean(
description="Auto-rebuild the package? (i.e. every commit or new tag)",
description=CommonDescriptions.AUTO_REBUILD.description,
)

clone_url = String(
Expand Down
4 changes: 3 additions & 1 deletion frontend/coprs_frontend/coprs/views/coprs_ns/coprs_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from coprs import helpers
from coprs import models

from coprs.constants import CommonDescriptions
from coprs.logic import builds_logic
from coprs.logic.builds_logic import BuildsLogic
from coprs.logic.complex_logic import ComplexLogic
Expand Down Expand Up @@ -271,7 +272,8 @@ def render_add_build_pypi(copr, form, view, package=None):
if not form:
form = forms.BuildFormPyPIFactory(copr.active_chroots)()
return flask.render_template("coprs/detail/add_build/pypi.html",
copr=copr, form=form, view=view, package=package)
copr=copr, form=form, view=view, package=package,
common_descriptions=CommonDescriptions)


@coprs_ns.route("/<username>/<coprname>/new_build_pypi/", methods=["POST"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from coprs import db
from coprs import forms
from coprs import helpers
from coprs.constants import CommonDescriptions
from coprs.views.coprs_ns import coprs_ns
from coprs.views.coprs_ns.coprs_builds import (
render_add_build_scm,
Expand Down Expand Up @@ -185,7 +186,8 @@ def copr_add_package(copr, source_type_text="scm", **kwargs):
form_scm=form["scm"], form_pypi=form["pypi"],
form_rubygems=form["rubygems"],
form_distgit=form['distgit'],
form_custom=form['custom'])
form_custom=form['custom'],
common_descriptions=CommonDescriptions)


@coprs_ns.route("/<username>/<coprname>/package/new/<source_type_text>", methods=["POST"])
Expand Down Expand Up @@ -234,7 +236,8 @@ def copr_edit_package(copr, package_name, source_type_text=None, **kwargs):
form_scm=form["scm"], form_pypi=form["pypi"],
form_rubygems=form["rubygems"],
form_distgit=form["distgit"],
form_custom=form['custom'])
form_custom=form['custom'],
common_descriptions=CommonDescriptions)


@coprs_ns.route("/<username>/<coprname>/package/<package_name>/edit/<source_type_text>", methods=["POST"])
Expand Down

0 comments on commit f0e3c47

Please sign in to comment.