示例#1
0
    def create_translation(self, audio_format, language, phrase, row):
        if Translation.objects.filter(phrase=phrase, language=language).exists():
            return

        t = Translation(content=row['TRANSLATED_TEXT'], language=language, phrase=phrase)
        audio_url = row.get('AUDIO_URL')

        if audio_url:
            tmp_path = self.download_audio(audio_format, audio_url)
            md5 = hashlib.md5(row['TRANSLATED_TEXT'].encode()).hexdigest()

            with open(tmp_path, 'rb') as f:
                t.audio_clip.save(f'{md5}.{audio_format}', File(f))
        try:
            t.save()
        except IntegrityError:
            self.stderr.write(f'FAIL for {row["ENGLISH"]}')
        return t
示例#2
0
 def test_whitespace(self):
     t = Translation(localized_string='     khaaaaaan!    ', id=999)
     t.save()
     eq_('khaaaaaan!', t.localized_string)
示例#3
0
 def test_whitespace(self):
     t = Translation(localized_string='     khaaaaaan!    ', id=999)
     t.save()
     eq_('khaaaaaan!', t.localized_string)
示例#4
0
文件: views.py 项目: kaleissin/CALS
def create_language(request,
                    lang=None,
                    fvlist=None,
                    clone=False,
                    *args,
                    **kwargs):
    me = 'language'
    state = 'new'
    user = request.user

    # sort values into categories
    cats = make_feature_list_for_lang(lang=lang, fvlist=fvlist)

    editorform = EditorForm()

    cloned_from_lang = None
    if clone:
        author = request.user.profile.display_name
        name = 'Clone'
        if lang:
            name = 'Clone of %s' % lang
        elif fvlist:
            name = 'Clone of %i features' % len(fvlist)
        background = name
        langform = LanguageForm(initial={
            'name': name,
            'background': background,
            'author': author
        })
        cloned_from_lang = lang
    else:
        langform = LanguageForm()

    if request.method == 'POST':
        langform = LanguageForm(data=request.POST, initial=request.POST)
        if langform.is_valid():
            with transaction.atomic():
                lang = langform.save(commit=False)
                if language_exists(lang.name):
                    url = '/language/%s/' % lang.get_slug()
                    msg = '''A language named <a href="%s">%s</a> already
                    exists, you should edit that one or change the name of
                    this one''' % (url, escape(lang.name))
                    messages.error(request, msg)
                else:
                    # Good, not a dupe
                    lang.added_by = user
                    lang.last_modified_by = user
                    if not lang.manager:
                        lang.manager = user
                    # Must save early since is foreign-key in many other tables
                    lang.save(user=user, solo=False)
                    # Save tags if any
                    lang, tags_changed = set_tags_for_lang(
                        langform.cleaned_data['tags'], lang)
                    # Set editors
                    editorform = EditorForm(data=request.POST, instance=lang)
                    if editorform.is_valid():
                        editorform.save()
                    # greeting
                    if lang.greeting:
                        # use signal instead?
                        greetingexercise = TranslationExercise.objects.get(
                            id=1)
                        trans = Translation(translation=lang.greeting,
                                            language=lang,
                                            translator=user,
                                            exercise=greetingexercise)
                        trans.save()

                    # values
                    lang = set_featurevalues_for_lang(
                        lang, request.POST.getlist('value'))

                    # Final save
                    lang.save(user=user)
                    if cloned_from_lang:
                        streamaction.send(request.user,
                                          verb='cloned the language',
                                          action_object=cloned_from_lang,
                                          target=lang)
                    else:
                        streamaction.send(request.user,
                                          verb='added the language',
                                          action_object=lang)
                    messages.info(
                        request,
                        'You successfully added the language %s to CALS' %
                        lang.name)
                    return HttpResponseRedirect('/language/%s/' % lang.slug)
        else:
            if not clone:
                error = "Couldn't store language-description: " + str(
                    langform.errors)
                messages.error(request, error)
            else:
                help = "Remember to fill out the name and author of the language"
                messages.warn(request, help)
    data = {
        'form': langform,
        'categories': cats,
        'me': me,
        'editorform': editorform,
        'state': state,
        'clone': clone,
    }
    return render(request, 'language_form.html', data)
def sync_project(project: Project):
    """
    Syncing the Translation objects with the git repo for a project.
    This function creates Translation records for every key in a project's
    localisation files for languages that are configured.
    """

    sync_time = now()

    GITHUB_ACCESS_TOKEN = config("GITHUB_ACCESS_TOKEN")
    # Access git repo for project
    g = Github(GITHUB_ACCESS_TOKEN)
    repo = g.get_repo(project.repository_name)
    contents = repo.get_contents(project.locale_files_path)

    # Get previous translations
    previous_translations = filter_latest_translations(
        Translation.objects.filter(project=project)
    )

    # Collect all translations in project, to check which translations are removed
    all_translations_in_project: Dict[str, Dict[str, Translation]] = {}

    # Get all files recursively from the repo
    files = []
    while contents:
        file_content = contents.pop(0)
        if file_content.type == "dir":
            contents.extend(repo.get_contents(file_content.path))
        else:
            files.append(file_content)

    for file in files:
        # The file path relative to the locale files directory for the project
        relative_filepath = file.path.replace(project.locale_files_path, ".")
        # Add file path to all translations dict
        all_translations_in_project[relative_filepath] = {}
        # Get language
        language_code = relative_filepath.split("/")[-1].split(".")[0]
        # Get commit date for file
        commit = repo.get_commits(path=file.path)[0].commit
        commit_date = commit.committer.date
        # If language in the project languages
        if language_code in [lang.language_code for lang in project.languages.all()]:
            try:
                file_object = yaml.load(file.decoded_content, Loader=BaseLoader)
                flat_file_object = json_normalize(
                    file_object, sep=".", errors="ignore"
                ).to_dict(orient="records")[0]
                # Loop through each translation key/value pair and create translation object
                for key, value in flat_file_object.items():
                    if isinstance(value, str):
                        language = Language.objects.get(language_code=language_code)
                        try:
                            translation = Translation(
                                text=value,
                                author="",
                                from_repository=True,
                                file_path=relative_filepath,
                                object_path=key,
                                project=project,
                                language=language,
                                created_at=commit_date,
                            )

                            # Add translation to all translations in project
                            all_translations_in_project[relative_filepath][
                                key
                            ] = translation

                            # Attempt to create translation, will be ignored if translation already in project
                            translation.save()
                        # If translation with file_path, object_path & created_at exists, ignore save
                        except IntegrityError:
                            pass
            except scanner.ScannerError:
                pass

    mark_deleted_translations(
        previous_translations, all_translations_in_project, sync_time
    )

    # When done, set the time the sync occurred
    project.last_sync_time = sync_time
    project.save()
示例#6
0
def create_language(request, lang=None, fvlist=None, clone=False, *args, **kwargs):
    me = 'language'
    state = 'new'
    user = request.user

    # sort values into categories
    cats = make_feature_list_for_lang(lang=lang, fvlist=fvlist)

    editorform = EditorForm()

    cloned_from_lang = None
    if clone:
        author = request.user.profile.display_name
        name = 'Clone'
        if lang:
            name = 'Clone of %s' % lang
        elif fvlist:
            name = 'Clone of %i features' % len(fvlist)
        background = name
        langform = LanguageForm(initial={
                'name': name,
                'background': background,
                'author': author})
        cloned_from_lang = lang
    else:
        langform = LanguageForm()

    if request.method == 'POST':
        langform = LanguageForm(data=request.POST, initial=request.POST)
        if langform.is_valid():
            with transaction.atomic():
                lang = langform.save(commit=False)
                if language_exists(lang.name):
                    url = '/language/%s/' % lang.get_slug()
                    msg = '''A language named <a href="%s">%s</a> already
                    exists, you should edit that one or change the name of
                    this one''' % (url, escape(lang.name))
                    messages.error(request, msg)
                else:
                    # Good, not a dupe
                    lang.added_by = user
                    lang.last_modified_by = user
                    if not lang.manager:
                        lang.manager = user
                    # Must save early since is foreign-key in many other tables
                    lang.save(user=user, solo=False)
                    # Save tags if any
                    lang, tags_changed = set_tags_for_lang(langform.cleaned_data['tags'], lang)
                    # Set editors
                    editorform = EditorForm(data=request.POST, instance=lang)
                    if editorform.is_valid():
                        editorform.save()
                    # greeting
                    if lang.greeting:
                        # use signal instead?
                        greetingexercise = TranslationExercise.objects.get(id=1)
                        trans = Translation(translation=lang.greeting, language=lang,
                                translator=user, exercise=greetingexercise)
                        trans.save()

                    # values
                    lang = set_featurevalues_for_lang(lang, request.POST.getlist('value'))

                    # Final save
                    lang.save(user=user)
                    if cloned_from_lang:
                        streamaction.send(request.user, verb='cloned the language', action_object=cloned_from_lang, target=lang)
                    else:
                        streamaction.send(request.user, verb='added the language', action_object=lang)
                    messages.info(request, 'You successfully added the language %s to CALS' % lang.name)
                    return HttpResponseRedirect('/language/%s/' % lang.slug)
        else:
            if not clone:
                error = "Couldn't store language-description: " + str(langform.errors)
                messages.error(request, error)
            else:
                help = "Remember to fill out the name and author of the language"
                messages.warn(request, help)
    data = {'form': langform,
            'categories': cats,
            'me': me,
            'editorform': editorform,
            'state': state,
            'clone': clone,
    }
    return render(request, 'language_form.html', data)