Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frontend: use common enum constants for sharing descriptions #3030

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 20 additions & 0 deletions frontend/coprs_frontend/coprs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,3 +933,23 @@ def generate_repo_id_and_name_ext(dependent, url, dep_idx):
generate_repo_name(url),
)
return repo_id, name


def multiple_get(dictionary: dict, *keys) -> list:
"""
Get multiple values from dictionary.
Args:
dictionary: Any dictionary
*keys: list of keys to obtain from dictionary
Returns:
*keys values in the same order as keys were given.
"""
empty = "__empty_content"
result = []
for key in keys:
content = dictionary.get(key, empty)
if content == empty:
raise KeyError(f"Key missing: {key}")

result.append(content)
return result
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 }}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are losing formatting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also note there are some form field metdata arguments we could perhaps use:

label="Create repositories manually",
description="""Repository meta data is normally refreshed
after each build. If you want to do this manually, turn
this option on.""",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are losing formatting.

that's true... perhaps this one is worth not unifying

)
}}

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 }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only part of the message is moved here.

| 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
Loading