<commit_before># -*- coding: utf-8 -*-
"""Utility functions that are used by both views and celery tasks."""

from __future__ import (
    absolute_import, division, print_function, unicode_literals)

import hashlib
import logging

from rest_framework.pagination import PageNumberPagination

from readthedocs.builds.constants import NON_REPOSITORY_VERSIONS
from readthedocs.builds.models import Version
from readthedocs.search.indexes import PageIndex, ProjectIndex, SectionIndex

log = logging.getLogger(__name__)


def sync_versions(project, versions, type):  # pylint: disable=redefined-builtin
    """Update the database with the current versions from the repository."""
    old_version_values = project.versions.filter(type=type).values_list(
        'verbose_name', 'identifier'
    )
    old_versions = dict(old_version_values)

    added = set()
    # Add new versions
    for version in versions:
        version_id = version['identifier']
        version_name = version['verbose_name']
        if version_name in old_versions:
            if version_id == old_versions[version_name]:
                # Version is correct
                continue
            else:
                # Update slug with new identifier
                Version.objects.filter(
                    project=project, verbose_name=version_name).update(
                        identifier=version_id,
                        type=type,
                        machine=False,
                    )  # noqa

                log.info(
                    '(Sync Versions) Updated Version: [%s=%s] ',
                    version_name,
                    version_id,
                )
        else:
            # New Version
            created_version = Version.objects.create(
                project=project,
                type=type,
                identifier=version_id,
                verbose_name=version_name,
            )
            added.add(created_version.slug)<commit_msg>Recover RTD's stable after delete the one from the user<commit_after># -*- coding: utf-8 -*-
"""Utility functions that are used by both views and celery tasks."""

from __future__ import (
    absolute_import, division, print_function, unicode_literals)

import hashlib
import logging

from rest_framework.pagination import PageNumberPagination

from readthedocs.builds.constants import (LATEST, LATEST_VERBOSE_NAME,
                                          NON_REPOSITORY_VERSIONS, STABLE,
                                          STABLE_VERBOSE_NAME)
from readthedocs.builds.models import Version
from readthedocs.search.indexes import PageIndex, ProjectIndex, SectionIndex

log = logging.getLogger(__name__)


def sync_versions(project, versions, type):  # pylint: disable=redefined-builtin
    """Update the database with the current versions from the repository."""
    old_version_values = project.versions.filter(type=type).values_list(
        'verbose_name', 'identifier'
    )
    old_versions = dict(old_version_values)

    # Add new versions
    added = set()
    has_user_stable = False
    for version in versions:
        version_id = version['identifier']
        version_name = version['verbose_name']
        if version_name == STABLE_VERBOSE_NAME:
            has_user_stable = True
        if version_name in old_versions:
            if version_id == old_versions[version_name]:
                # Version is correct
                continue
            else:
                # Update slug with new identifier
                Version.objects.filter(
                    project=project, verbose_name=version_name).update(
                        identifier=version_id,
                        type=type,
                        machine=False,
                    )  # noqa

                log.info(
                    '(Sync Versions) Updated Version: [%s=%s] ',
                    version_name,
                    version_id,
                )
        else:
            # New Version
            created_version = Version.objects.create(
                project=project,
                type=type,
                identifier=version_id,
                verbose_name=version_name,
            )
            added.add(created_version.slug)
    if not has_user_stable:
        stable_version = (
            Version.objects
            .filter(project=project, slug=STABLE, type=type)
            .first()
        )
        if stable_version:
            # Put back the RTD's stable version
            stable_version.machine = True
            stable_version.save()
{'fraction_non_alphanumeric': 0.07194560210572494, 'fraction_numerical': 0.0004386926957666155, 'mean_word_length': 1.8934010152284264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '95521', 'n_tokens_mistral': 1173, 'n_tokens_neox': 1116, 'n_words': 283}