|
| 1 | +import uuid |
| 2 | + |
| 3 | +from django.contrib.auth.models import User |
| 4 | +from django.db.models.query import QuerySet |
| 5 | +from django.http import Http404 |
| 6 | +from django.shortcuts import redirect |
| 7 | +from django.urls import reverse |
| 8 | + |
| 9 | +from pontoon.actionlog.models import ActionLog |
| 10 | +from pontoon.api.models import PersonalAccessToken |
| 11 | +from pontoon.base.models.comment import Comment |
| 12 | +from pontoon.base.models.locale import Locale, LocaleCodeHistory |
| 13 | +from pontoon.base.models.permission_changelog import PermissionChangelog |
| 14 | +from pontoon.base.models.project import Project, ProjectSlugHistory |
| 15 | +from pontoon.base.models.project_locale import ProjectLocale |
| 16 | +from pontoon.base.models.translation import Translation |
| 17 | +from pontoon.terminology.models import Term |
| 18 | + |
| 19 | + |
| 20 | +def readonly_exists(projects, locale): |
| 21 | + if not isinstance(projects, (QuerySet, tuple, list)): |
| 22 | + projects = [projects] |
| 23 | + |
| 24 | + return ProjectLocale.objects.filter( |
| 25 | + project__in=projects, |
| 26 | + locale=locale, |
| 27 | + readonly=True, |
| 28 | + ).exists() |
| 29 | + |
| 30 | + |
| 31 | +def get_project_or_redirect( |
| 32 | + slug, redirect_view_name, slug_arg_name, request_user, **kwargs |
| 33 | +): |
| 34 | + """ |
| 35 | + Attempts to get a project with the given slug. If the project doesn't exist, it checks if the slug is in the |
| 36 | + ProjectSlugHistory and if so, it redirects to the current project slug URL. If the old slug is not found in the |
| 37 | + history, it raises an Http404 error. |
| 38 | + """ |
| 39 | + |
| 40 | + try: |
| 41 | + project = Project.objects.visible_for(request_user).available().get(slug=slug) |
| 42 | + return project |
| 43 | + except Project.DoesNotExist: |
| 44 | + slug_history = ( |
| 45 | + ProjectSlugHistory.objects.filter(old_slug=slug) |
| 46 | + .order_by("-created_at") |
| 47 | + .first() |
| 48 | + ) |
| 49 | + if slug_history is not None: |
| 50 | + redirect_kwargs = {slug_arg_name: slug_history.project.slug} |
| 51 | + redirect_kwargs.update(kwargs) |
| 52 | + redirect_url = reverse(redirect_view_name, kwargs=redirect_kwargs) |
| 53 | + return redirect(redirect_url) |
| 54 | + else: |
| 55 | + raise Http404 |
| 56 | + |
| 57 | + |
| 58 | +def get_locale_or_redirect(code, redirect_view_name=None, url_arg_name=None, **kwargs): |
| 59 | + """ |
| 60 | + Attempts to retrieve a locale using the given code. If the locale does not exist, it checks the LocaleCodeHistory |
| 61 | + for a record of the old code. If an entry is found, it either redirects to the view specified by redirect_view_name |
| 62 | + using the new locale code or returns the Locale object if no redirect_view_name is provided. |
| 63 | + The url_arg_name parameter specifies the argument name for the locale code used in the URL pattern of the redirect view. |
| 64 | + If the old code is not found in the history, it raises an Http404 error. |
| 65 | + """ |
| 66 | + |
| 67 | + try: |
| 68 | + return Locale.objects.get(code=code) |
| 69 | + except Locale.DoesNotExist: |
| 70 | + code_history = ( |
| 71 | + LocaleCodeHistory.objects.filter(old_code=code) |
| 72 | + .order_by("-created_at") |
| 73 | + .first() |
| 74 | + ) |
| 75 | + if code_history: |
| 76 | + if not redirect_view_name or not url_arg_name: |
| 77 | + return code_history.locale |
| 78 | + |
| 79 | + redirect_kwargs = {url_arg_name: code_history.locale.code} |
| 80 | + redirect_kwargs.update(kwargs) |
| 81 | + redirect_url = reverse(redirect_view_name, kwargs=redirect_kwargs) |
| 82 | + return redirect(redirect_url) |
| 83 | + |
| 84 | + raise Http404 |
| 85 | + |
| 86 | + |
| 87 | +def anonymize_user(user): |
| 88 | + random_hash = uuid.uuid4().hex |
| 89 | + new_user = User.objects.create_user( |
| 90 | + username="deleted-user-" + random_hash, |
| 91 | + email="deleted-user-" + random_hash + "@example.com", |
| 92 | + first_name="Deleted User", |
| 93 | + is_active=False, |
| 94 | + ) |
| 95 | + |
| 96 | + ActionLog.objects.filter(performed_by=user).update(performed_by=new_user) |
| 97 | + PermissionChangelog.objects.filter(performed_by=user).update(performed_by=new_user) |
| 98 | + PermissionChangelog.objects.filter(performed_on=user).update(performed_on=new_user) |
| 99 | + Project.objects.filter(contact=user).update(contact=new_user) |
| 100 | + Translation.objects.filter(user=user).update(user=new_user) |
| 101 | + Translation.objects.filter(approved_user=user).update(approved_user=new_user) |
| 102 | + Translation.objects.filter(unapproved_user=user).update(unapproved_user=new_user) |
| 103 | + Translation.objects.filter(rejected_user=user).update(rejected_user=new_user) |
| 104 | + Translation.objects.filter(unrejected_user=user).update(unrejected_user=new_user) |
| 105 | + Term.objects.filter(created_by=user).update(created_by=new_user) |
| 106 | + Comment.objects.filter(author=user).update(author=new_user) |
| 107 | + PersonalAccessToken.objects.filter(user=user).update(revoked=True) |
0 commit comments