-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
456 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from collections import defaultdict | ||
|
||
|
||
def find_common_prefixes(strings): | ||
prefix_dict = defaultdict(int) | ||
|
||
for string in strings: | ||
parts = string.split("_") | ||
for i in range(1, len(parts) + 1): | ||
prefix = "_".join(parts[:i]) | ||
prefix_dict[prefix] += 1 | ||
|
||
common_prefixes = [prefix for prefix, count in prefix_dict.items() if count > 1] | ||
return common_prefixes | ||
|
||
|
||
# Example usage | ||
strings = [ | ||
"basic", | ||
"card_dist_offline", | ||
"clear_password", | ||
"choice_name_test", | ||
"RTL Alignment Test", | ||
"staff_registration_center_11030", | ||
"staff_registration_center_11031", | ||
"staff_registration_center_11032", | ||
"staff_registration_center_11033", | ||
"staff_registration_center_11034", | ||
"staff_registration_center_11035", | ||
"staff_registration_center_11036", | ||
"staff_registration_center_11038", | ||
"staff_registration_center_11039", | ||
"staff_registration_center_16005", | ||
"staff_registration_center_32038", | ||
"voter_registration", | ||
"biomini_test", | ||
"device_testing", | ||
"device_testing_center_11030", | ||
"device_testing_center_11031", | ||
"device_testing_center_11032", | ||
"device_testing_center_11033", | ||
"device_testing_center_11034", | ||
"device_testing_center_11035", | ||
"device_testing_center_11036", | ||
"device_testing_center_11037", | ||
"device_testing_center_11038", | ||
"device_testing_center_11039", | ||
"device_testing_center_11046", | ||
"device_testing_center_32038", | ||
"device_testing_center_gabby-test", | ||
"device_testing_test", | ||
"device_testing_11030", | ||
"device_testing_11031", | ||
"device_testing_11032", | ||
"device_testing_11033", | ||
"device_testing_11034", | ||
"device_testing_11035", | ||
"device_testing_11036", | ||
"device_testing_11037", | ||
"device_testing_11038", | ||
"device_testing_11039", | ||
"device_testing_11046", | ||
"device_testing_32038", | ||
"device_testing_gabby-test", | ||
"polling_module", | ||
"polling_module_11030", | ||
"polling_module_11031", | ||
"polling_module_11032", | ||
"polling_module_11034", | ||
"polling_module_11035", | ||
"polling_module_11036", | ||
"polling_module_11037", | ||
"polling_module_11038", | ||
"polling_module_11039", | ||
"polling_module_test", | ||
"undist_card_recon", | ||
"voter_card_dist", | ||
"card_distribution_center_11030", | ||
"card_distribution_center_11031", | ||
"card_distribution_center_11032", | ||
"card_distribution_center_11034", | ||
"card_distribution_center_11035", | ||
"card_distribution_center_11036", | ||
"card_distribution_center_11037", | ||
"card_distribution_center_11038", | ||
"card_distribution_center_11039", | ||
"card_distribution_center_11046", | ||
"card_distribution_center_32038", | ||
] | ||
|
||
print(find_common_prefixes(strings)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from django import forms | ||
from django.http import QueryDict | ||
from django.urls import reverse_lazy | ||
|
||
from apps.patterns.forms import PlatformFormMixin | ||
from apps.patterns.widgets import Select | ||
|
||
from .etl.odk.client import ODKPublishClient | ||
from .http import HttpRequest | ||
|
||
|
||
class ProjectSyncForm(PlatformFormMixin, forms.Form): | ||
"""Form for syncing projects from an ODK Central server. | ||
In addition to processing the form normally, this form also handles | ||
render logic for the project field during an HTMX request. | ||
""" | ||
|
||
server = forms.ChoiceField( | ||
# The server field is populated with the available ODK Central servers | ||
# (from an environment variable) when the form is rendered. | ||
choices=[("", "Select an ODK Central server...")] | ||
+ [ | ||
(config.base_url, config.base_url) for config in ODKPublishClient.get_configs().values() | ||
], | ||
# When a server is selected, the project field below is populated with | ||
# the available projects for that server using HMTX. | ||
widget=Select( | ||
attrs={ | ||
"hx-trigger": "change", | ||
"hx-get": reverse_lazy("odk_publish:server-sync-projects"), | ||
"hx-target": "#id_project_container", | ||
"hx-swap": "innerHTML", | ||
"hx-indicator": ".loading", | ||
} | ||
), | ||
) | ||
project = forms.ChoiceField(widget=Select(attrs={"disabled": "disabled"})) | ||
|
||
def __init__(self, request: HttpRequest, data: QueryDict, *args, **kwargs): | ||
htmx_data = data.copy() if request.htmx else {} | ||
# Don't bind the form on an htmx request, otherwise we'll see "This | ||
# field is required" errors | ||
data = data if not request.htmx else None | ||
super().__init__(data, *args, **kwargs) | ||
# Set `project` field choices when a server is provided either via a | ||
# POST or HTMX request | ||
if server := htmx_data.get("server") or self.data.get("server"): | ||
self.set_project_choices(base_url=server) | ||
self.fields["project"].widget.attrs.pop("disabled", None) | ||
|
||
def set_project_choices(self, base_url: str): | ||
with ODKPublishClient(base_url=base_url) as client: | ||
self.fields["project"].choices = [ | ||
(project.id, project.name) for project in client.projects.list() | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from django.db.models import QuerySet | ||
from django.http import HttpRequest as HttpRequestBase | ||
from django_htmx.middleware import HtmxDetails | ||
|
||
from .models import Project | ||
from .nav import Breadcrumbs | ||
|
||
|
||
class HttpRequest(HttpRequestBase): | ||
"""Custom HttpRequest class for type-checking purposes.""" | ||
|
||
htmx: HtmxDetails | ||
odk_project = Project | None | ||
odk_project_tabs = Breadcrumbs | None | ||
odk_projects = QuerySet[Project] | None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.