Skip to content

Commit

Permalink
component: support additional attributes in component duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
ParthS007 committed May 29, 2024
1 parent ac044a3 commit 7b0e24d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Not yet released.

* :ref:`subscriptions` now include strings which need updating.
* Improved compatibility with password managers.
* :ref:`trans` Clone more settings automatically during duplication of existing component.

**Bug fixes**

Expand Down
1 change: 1 addition & 0 deletions weblate/templates/component.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<li><a href="{% url 'manage-access' project=object.project.slug %}">{% trans "Users" %}</a></li>
{% endif %}
{% if user_can_edit_component %}
<li><a href="{% url 'create-component' %}?component={{ object.id }}#existing">{% trans "Duplicate Component" %}</li>
<li><a href="{% url 'guide' path=object.get_url_path %}">{% trans "Community localization checklist" %}</a></li>
<li><a href="{% url 'addons' path=object.get_url_path %}">{% trans "Add-ons" %}</a></li>
{% endif %}
Expand Down
17 changes: 17 additions & 0 deletions weblate/trans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1686,10 +1686,27 @@ class Meta:
"language_regex",
"source_language",
"is_glossary",
"agreement",
"merge_style",
"commit_message",
"add_message",
"delete_message",
"merge_message",
"addon_message",
"pull_message",
]
widgets = {
"source_language": SortedSelect,
"language_code_style": SortedSelect,
"license": forms.HiddenInput(),
"agreement": forms.HiddenInput(),
"merge_style": forms.HiddenInput(),
"commit_message": forms.HiddenInput(),
"add_message": forms.HiddenInput(),
"delete_message": forms.HiddenInput(),
"merge_message": forms.HiddenInput(),
"addon_message": forms.HiddenInput(),
"pull_message": forms.HiddenInput(),
}


Expand Down
10 changes: 9 additions & 1 deletion weblate/trans/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from weblate.auth.models import User
from weblate.configuration.models import Setting
from weblate.formats.models import FILE_FORMATS
from weblate.trans.models import Component, Project
from weblate.trans.models import Category, Component, Project
from weblate.utils.files import remove_tree
from weblate.vcs.models import VCS_REGISTRY

Expand Down Expand Up @@ -167,6 +167,12 @@ def create_project(self, **kwargs):
self.addCleanup(remove_tree, project.full_path, True)
return project

def create_category(self, project, **kwargs):
"""Create test category."""
return Category.objects.create(
name="Test", slug="test", project=project, **kwargs
)

def format_local_path(self, path):
"""Format path for local access to the repository."""
if sys.platform != "win32":
Expand All @@ -188,6 +194,8 @@ def _create_component(
raise SkipTest(f"File format {file_format} is not supported!")
if "project" not in kwargs:
kwargs["project"] = self.create_project()
if "category" not in kwargs:
kwargs["category"] = self.create_category(project=kwargs["project"])

repo = push = self.format_local_path(getattr(self, f"{vcs}_repo_path"))
if vcs not in VCS_REGISTRY:
Expand Down
27 changes: 26 additions & 1 deletion weblate/trans/views/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class CreateComponentSelection(CreateComponent):

components: ComponentQuerySet
origin = None
duplicate_existing_component = None

@cached_property
def branch_data(self):
Expand Down Expand Up @@ -463,6 +464,16 @@ def fetch_params(self, request) -> None:
self.components = self.components.filter(project__pk=self.selected_project)
self.origin = request.POST.get("origin")

try:
self.duplicate_existing_component = int(request.GET.get("component"))
except (ValueError, TypeError):
self.duplicate_existing_component = None
self.initial = {}
if self.duplicate_existing_component:
self.initial["component"] = Component.objects.get(
pk=self.duplicate_existing_component
)

def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs)
kwargs["components"] = self.components
Expand Down Expand Up @@ -493,6 +504,10 @@ def get_form(self, form_class=None, empty=False):
).order_project()
form.branch_data = self.branch_data
elif isinstance(form, ComponentSelectForm):
if self.duplicate_existing_component:
self.components |= Component.objects.filter(
pk=self.duplicate_existing_component
)
form.fields["component"].queryset = self.components
return form

Expand All @@ -518,12 +533,22 @@ def form_valid(self, form):
component = form.cleaned_data["component"]
if self.origin == "existing":
return self.redirect_create(
repo=component.get_repo_link_url(),
repo=component.repo or component.get_repo_link_url(),
project=component.project.pk,
category=component.category.pk if component.category else "",
name=form.cleaned_data["name"],
slug=form.cleaned_data["slug"],
vcs=component.vcs,
source_language=component.source_language.pk,
license=component.license,
agreement=component.agreement,
merge_style=component.merge_style,
commit_message=component.commit_message,
add_message=component.add_message,
delete_message=component.delete_message,
merge_message=component.merge_message,
addon_message=component.addon_message,
pull_message=component.pull_message,
)
if self.origin == "branch":
form.instance.save()
Expand Down

0 comments on commit 7b0e24d

Please sign in to comment.