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

Add functionality for counting file uploads #3522

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion pontoon/base/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import re

Expand Down Expand Up @@ -805,8 +806,15 @@ def upload(request):

upload = request.FILES["uploadfile"]
try:
import_uploaded_file(project, locale, res_path, upload, request.user)
badge_update = import_uploaded_file(
project, locale, res_path, upload, request.user
)
messages.success(request, "Translations updated from uploaded file.")
if badge_update[0]:
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
message = json.dumps(
{"badgeName": badge_update[0], "badgeLevel": badge_update[1]}
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
)
messages.info(request, message)
except Exception as error:
messages.error(request, str(error))
else:
Expand Down
29 changes: 26 additions & 3 deletions pontoon/sync/core/translations_from_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)
from pontoon.checks import DB_FORMATS
from pontoon.checks.utils import bulk_run_checks
from pontoon.messaging.notifications import send_badge_notification
from pontoon.sync.core.checkout import Checkout, Checkouts
from pontoon.sync.core.paths import UploadPaths
from pontoon.sync.formats import parse
Expand Down Expand Up @@ -75,12 +76,13 @@ def sync_translations_from_repo(

def write_db_updates(
project: Project, updates: Updates, user: User | None, now: datetime
) -> None:
updated_translations, new_translations = update_db_translations(
) -> tuple[str, int]:
badge_update, updated_translations, new_translations = update_db_translations(
project, updates, user, now
)
add_failed_checks(new_translations)
add_translation_memory_entries(project, new_translations + updated_translations)
return badge_update


def delete_removed_bilingual_resources(
Expand Down Expand Up @@ -433,10 +435,31 @@ def update_db_translations(
f"[{project.slug}] Created {str_n_translations(created)} from repo changes"
)

badge_update = ("", 0)
if actions:
translation_before_level = log_user.badges_translation_level
review_before_level = log_user.badges_review_level

ActionLog.objects.bulk_create(actions)

return created, list(suggestions.values())
if (
log_user.badges_translation_level > translation_before_level
and log_user.username != "pontoon-sync"
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
):
send_badge_notification(
log_user, "Translation Champion", log_user.badges_translation_level
)
badge_update = ("Translation Champion", log_user.badges_translation_level)
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
if (
log_user.badges_review_level > review_before_level
and log_user.username != "pontoon-sync"
):
send_badge_notification(
log_user, "Review Master", log_user.badges_review_level
)
badge_update = ("Review Master", log_user.badges_review_level)

return badge_update, created, list(suggestions.values())


def str_n_translations(n: int | Sized) -> str:
Expand Down
3 changes: 2 additions & 1 deletion pontoon/sync/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def import_uploaded_file(
)
if updates:
now = timezone.now()
write_db_updates(project, updates, user, now)
badge_update = write_db_updates(project, updates, user, now)
update_stats(project)
ChangedEntityLocale.objects.bulk_create(
(
Expand All @@ -105,5 +105,6 @@ def import_uploaded_file(
),
ignore_conflicts=True,
)
return badge_update
else:
raise Exception("Upload failed.")
20 changes: 20 additions & 0 deletions translate/src/context/BadgeTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ export function BadgeTooltipProvider({
}) {
const [message, setMessage] = useState<BadgeTooltipMessage | null>(null);

useEffect(() => {
const rootElt = document.getElementById('root');
if (rootElt?.dataset.notifications) {
const notifications = JSON.parse(rootElt.dataset.notifications);
if (notifications.length > 0) {
const parsed = notifications.map(
(notification: { content: string; type: string }) => {
if (notification.type === 'info') {
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
// Badge update information
return JSON.parse(notification.content);
} else {
return { content: notification.content, type: notification.type };
}
harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
},
);
setMessage(parsed[1]);
}
}
}, []);

harmitgoswami marked this conversation as resolved.
Show resolved Hide resolved
return (
<BadgeTooltipMessage.Provider value={message}>
<ShowBadgeTooltip.Provider value={(tooltip) => setMessage(tooltip)}>
Expand Down
Loading