示例#1
0
def test_mgr_user_without_translations(translation_a, user_a, user_b):
    """
    Checks if user contributors without translations aren't returned.
    """
    assert translation_a.user == user_a
    top_contributors = users_with_translations_counts()
    assert user_a in top_contributors
    assert user_b not in top_contributors
示例#2
0
def test_mgr_user_without_translations(translation_a, user_a, user_b):
    """
    Checks if user contributors without translations aren't returned.
    """
    assert translation_a.user == user_a
    top_contributors = users_with_translations_counts()
    assert user_a in top_contributors
    assert user_b not in top_contributors
示例#3
0
def test_mgr_user_translation_counts(
    resource_a,
    locale_a,
):
    """Checks if translation counts are calculated properly.

    Tests creates 3 contributors with different numbers translations
    and checks if their counts match.

    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=36, resource=resource_a)
    batch_kwargs = sum([
        [dict(user=contributors[0], approved=True)] * 7,
        [dict(user=contributors[0], approved=False, fuzzy=False)] * 3,
        [dict(user=contributors[0], fuzzy=True)] * 2,
        [dict(user=contributors[1], approved=True)] * 5,
        [dict(user=contributors[1], approved=False, fuzzy=False)] * 9,
        [dict(user=contributors[1], fuzzy=True)] * 2,
        [dict(user=contributors[2], approved=True)] * 1,
        [dict(user=contributors[2], approved=False, fuzzy=False)] * 2,
        [dict(user=contributors[2], fuzzy=True)] * 5,
    ], [])

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args['user'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = users_with_translations_counts()

    assert len(top_contribs) == 3
    assert top_contribs[0] == contributors[1]
    assert top_contribs[1] == contributors[0]
    assert top_contribs[2] == contributors[2]

    assert top_contribs[0].translations_count == 16
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_unapproved_count == 9
    assert top_contribs[0].translations_needs_work_count == 2

    assert top_contribs[1].translations_count == 12
    assert top_contribs[1].translations_approved_count == 7
    assert top_contribs[1].translations_unapproved_count == 3
    assert top_contribs[1].translations_needs_work_count == 2

    assert top_contribs[2].translations_count == 8
    assert top_contribs[2].translations_approved_count == 1
    assert top_contribs[2].translations_unapproved_count == 2
    assert top_contribs[2].translations_needs_work_count == 5
示例#4
0
def ajax_permissions(request, locale):
    locale = get_object_or_404(Locale, code=locale)
    project_locales = locale.project_locale.visible()

    if request.method == 'POST':
        locale_form = forms.LocalePermsForm(request.POST,
                                            instance=locale,
                                            prefix='general',
                                            user=request.user)
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            request.POST,
            prefix='project-locale',
            queryset=project_locales,
            form_kwargs={'user': request.user})

        if locale_form.is_valid() and project_locale_form.is_valid():
            locale_form.save()
            project_locale_form.save()

        else:
            errors = locale_form.errors
            errors.update(project_locale_form.errors_dict)
            error_msg = bleach.clean(json.dumps(errors))
            return HttpResponseBadRequest(error_msg)

    else:
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            prefix='project-locale',
            queryset=project_locales,
            form_kwargs={'user': request.user})

    managers = locale.managers_group.user_set.order_by('email')
    translators = locale.translators_group.user_set.exclude(
        pk__in=managers).order_by('email')
    all_users = (User.objects.exclude(pk__in=managers | translators).exclude(
        email='').order_by('email'))

    contributors_emails = set(
        contributor.email for contributor in users_with_translations_counts(
            None, Q(locale=locale), None))

    locale_projects = locale.projects_permissions

    return render(
        request, 'teams/includes/permissions.html', {
            'locale': locale,
            'all_users': all_users,
            'contributors_emails': contributors_emails,
            'translators': translators,
            'managers': managers,
            'locale_projects': locale_projects,
            'project_locale_form': project_locale_form,
            'all_projects_in_translation': all([x[5] for x in locale_projects])
        })
示例#5
0
def test_mgr_user_contributors_limit(
    resource_a, locale_a,
):
    """
    Checks if proper count of user is returned.
    """
    contributors = UserFactory.create_batch(size=102)
    entities = EntityFactory.create_batch(size=102, resource=resource_a,)
    for i, contrib in enumerate(contributors):
        TranslationFactory.create(
            locale=locale_a, approved=True, user=contrib, entity=entities[i],
        )
    top_contributors = users_with_translations_counts()
    assert len(top_contributors) == 100
示例#6
0
文件: views.py 项目: tomer/pontoon
    def get_context_data(self, **kwargs):
        """Top contributors view."""
        context = super(ContributorsMixin, self).get_context_data(**kwargs)
        try:
            period = int(self.request.GET['period'])
            if period <= 0:
                raise ValueError
            start_date = (timezone.now() + relativedelta(months=-period))
        except (KeyError, ValueError):
            period = None
            start_date = None

        context['contributors'] = (users_with_translations_counts(
            start_date, self.contributors_filter(**kwargs)))
        context['period'] = period
        return context
示例#7
0
文件: views.py 项目: mathjazz/pontoon
    def get_context_data(self, **kwargs):
        """Top contributors view."""
        context = super(ContributorsMixin, self).get_context_data(**kwargs)
        try:
            period = int(self.request.GET['period'])
            if period <= 0:
                raise ValueError
            start_date = (timezone.now() + relativedelta(months=-period))
        except (KeyError, ValueError):
            period = None
            start_date = None

        context['contributors'] = (
            users_with_translations_counts(start_date, self.contributors_filter(**kwargs))
        )
        context['period'] = period
        return context
示例#8
0
    def get_context_data(self, **kwargs):
        """Top contributors view."""
        context = super().get_context_data(**kwargs)
        try:
            period = int(self.request.GET["period"])
            if period <= 0:
                raise ValueError
            start_date = timezone.now() + relativedelta(months=-period)
        except (KeyError, ValueError):
            period = None
            start_date = None

        context["contributors"] = users_with_translations_counts(
            start_date, self.contributors_filter(**kwargs) & Q(user__isnull=False)
        )
        context["period"] = period
        return context
示例#9
0
def test_mgr_user_contributors_order(
    resource_b, locale_a,
):
    """
    Checks if users are ordered by count of contributions.
    """
    contributors = UserFactory.create_batch(size=5)
    entities = EntityFactory.create_batch(size=22, resource=resource_b)

    # create a list of contributors/entity for translations
    for i, count in enumerate([2, 4, 9, 1, 6]):
        for j in range(count):
            TranslationFactory.create(
                locale=locale_a, user=contributors[i], entity=entities[i],
            )

    # Ordered by approved count
    assert list(users_with_translations_counts()) == [
        contributors[i] for i in [2, 4, 1, 0, 3]
    ]
示例#10
0
def test_mgr_user_contributors_limit(
    resource_a,
    locale_a,
):
    """
    Checks if proper count of user is returned.
    """
    contributors = UserFactory.create_batch(size=102)
    entities = EntityFactory.create_batch(
        size=102,
        resource=resource_a,
    )
    for i, contrib in enumerate(contributors):
        TranslationFactory.create(
            locale=locale_a,
            approved=True,
            user=contrib,
            entity=entities[i],
        )
    top_contributors = users_with_translations_counts()
    assert len(top_contributors) == 100
示例#11
0
def test_mgr_user_contributors_order(
    resource_b,
    locale_a,
):
    """
    Checks if users are ordered by count of contributions.
    """
    contributors = UserFactory.create_batch(size=5)
    entities = EntityFactory.create_batch(size=22, resource=resource_b)

    # create a list of contributors/entity for translations
    for i, count in enumerate([2, 4, 9, 1, 6]):
        for j in range(count):
            TranslationFactory.create(
                locale=locale_a,
                user=contributors[i],
                entity=entities[i],
            )

    # Ordered by approved count
    assert (
        list(users_with_translations_counts())
        == [contributors[i] for i in [2, 4, 1, 0, 3]]
    )
示例#12
0
def test_mgr_user_query_args_filtering(
    locale_a, resource_a, locale_b,
):
    """
    Tests if query args are honored properly and contributors are filtered.
    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=53, resource=resource_a)

    batch_kwargs = sum(
        [
            [dict(user=contributors[0], locale=locale_a, approved=True)] * 12,
            [dict(user=contributors[0], locale=locale_a, approved=False, fuzzy=False)]
            * 1,
            [dict(user=contributors[0], locale=locale_a, fuzzy=True)] * 2,
            [dict(user=contributors[1], locale=locale_b, approved=True)] * 11,
            [dict(user=contributors[1], locale=locale_b, approved=False, fuzzy=False)]
            * 1,
            [dict(user=contributors[1], locale=locale_b, fuzzy=True)] * 2,
            [dict(user=contributors[2], locale=locale_a, approved=True)] * 10,
            [dict(user=contributors[2], locale=locale_a, approved=False, fuzzy=False)]
            * 12,
            [dict(user=contributors[2], locale=locale_a, fuzzy=True)] * 2,
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=args["locale"],
            user=args["user"],
            approved=args.get("approved", False),
            fuzzy=args.get("fuzzy", False),
        )

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 1, 1), Q(locale=locale_a),
    )
    assert len(top_contribs) == 2
    assert top_contribs[0] == contributors[2]
    assert top_contribs[0].translations_count == 24
    assert top_contribs[0].translations_approved_count == 10
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 12
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1] == contributors[0]
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 12
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 1
    assert top_contribs[1].translations_needs_work_count == 2

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 1, 1), Q(locale=locale_b),
    )
    assert len(top_contribs) == 1
    assert top_contribs[0] == contributors[1]
    assert top_contribs[0].translations_count == 14
    assert top_contribs[0].translations_approved_count == 11
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2
示例#13
0
def test_mgr_user_query_args_filtering(
    locale_a,
    resource_a,
    locale_b,
):
    """
    Tests if query args are honored properly and contributors are filtered.
    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=53, resource=resource_a)

    batch_kwargs = sum(
        [
            [dict(
                user=contributors[0],
                locale=locale_a,
                approved=True
            )] * 12,
            [dict(
                user=contributors[0],
                locale=locale_a,
                approved=False,
                fuzzy=False
            )] * 1,
            [dict(
                user=contributors[0],
                locale=locale_a,
                fuzzy=True
            )] * 2,
            [dict(
                user=contributors[1],
                locale=locale_b,
                approved=True
            )] * 11,
            [dict(
                user=contributors[1],
                locale=locale_b,
                approved=False,
                fuzzy=False
            )] * 1,
            [dict(
                user=contributors[1],
                locale=locale_b,
                fuzzy=True
            )] * 2,
            [dict(
                user=contributors[2],
                locale=locale_a,
                approved=True
            )] * 10,
            [dict(
                user=contributors[2],
                locale=locale_a,
                approved=False,
                fuzzy=False
            )] * 12,
            [dict(
                user=contributors[2],
                locale=locale_a,
                fuzzy=True
            )] * 2
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=args['locale'],
            user=args['user'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 1, 1),
        Q(locale=locale_a),
    )
    assert len(top_contribs) == 2
    assert top_contribs[0] == contributors[2]
    assert top_contribs[0].translations_count == 24
    assert top_contribs[0].translations_approved_count == 10
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 12
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1] == contributors[0]
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 12
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 1
    assert top_contribs[1].translations_needs_work_count == 2

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 1, 1),
        Q(locale=locale_b),
    )
    assert len(top_contribs) == 1
    assert top_contribs[0] == contributors[1]
    assert top_contribs[0].translations_count == 14
    assert top_contribs[0].translations_approved_count == 11
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2
示例#14
0
def test_mgr_user_period_filters(
    locale_a, resource_a,
):
    """Total counts should be filtered by given date.

    Test creates 2 contributors with different activity periods and checks
    if they are filtered properly.
    """
    contributors = UserFactory.create_batch(size=2)
    entities = EntityFactory.create_batch(size=35, resource=resource_a)
    batch_kwargs = sum(
        [
            [
                dict(
                    user=contributors[0],
                    date=aware_datetime(2015, 3, 2),
                    approved=True,
                )
            ]
            * 12,
            [
                dict(
                    user=contributors[0],
                    date=aware_datetime(2015, 7, 2),
                    approved=True,
                )
            ]
            * 5,
            [
                dict(
                    user=contributors[0],
                    date=aware_datetime(2015, 3, 2),
                    approved=False,
                    fuzzy=False,
                )
            ]
            * 1,
            [dict(user=contributors[0], date=aware_datetime(2015, 3, 2), fuzzy=True,)]
            * 2,
            [
                dict(
                    user=contributors[1],
                    date=aware_datetime(2015, 6, 1),
                    approved=True,
                )
            ]
            * 2,
            [
                dict(
                    user=contributors[1],
                    date=aware_datetime(2015, 6, 1),
                    approved=False,
                    fuzzy=False,
                )
            ]
            * 11,
            [dict(user=contributors[1], date=aware_datetime(2015, 6, 1), fuzzy=True,)]
            * 2,
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args["user"],
            date=args["date"],
            approved=args.get("approved", False),
            fuzzy=args.get("fuzzy", False),
        )

    top_contribs = users_with_translations_counts(aware_datetime(2015, 6, 10))
    assert len(top_contribs) == 1
    assert top_contribs[0].translations_count == 5
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 0
    assert top_contribs[0].translations_needs_work_count == 0

    top_contribs = users_with_translations_counts(aware_datetime(2015, 5, 10))
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 15
    assert top_contribs[0].translations_approved_count == 2
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 11
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 5
    assert top_contribs[1].translations_approved_count == 5
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 0
    assert top_contribs[1].translations_needs_work_count == 0

    top_contribs = users_with_translations_counts(aware_datetime(2015, 1, 10))
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 20
    assert top_contribs[0].translations_approved_count == 17
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 2
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 11
    assert top_contribs[1].translations_needs_work_count == 2
示例#15
0
        settings.SITE_URL,
        reverse('pontoon.contributors.contributor.username', args=[username]))


output = []
output.append('Role,Locale,Profile URL,Date Joined,Last Login,Last Activity')

locales = Locale.objects.available()
if len(LOCALES) > 0:
    locales = locales.filter(code__in=LOCALES)

for locale in locales:
    if ROLE == 'manager':
        users = locale.managers_group.user_set.all()
    elif ROLE == 'translator':
        users = locale.translators_group.user_set.all()
    else:
        users = users_with_translations_counts(None, Q(locale=locale), None)
        ROLE = 'contributor'
    for user in users:
        output.append('{},{},{},{},{},{}'.format(
            ROLE,
            locale.code,
            get_profile(user.username),
            user.date_joined.date(),
            user.last_login.date(),
            get_latests_activity(user),
        ))

print('\n'.join(output))
示例#16
0
        reverse(
            'pontoon.contributors.contributor.username',
            args=[username]
        )
    )

def get_ratio(approved, rejected):
    try:
        return format(approved / rejected, '.2f')
    except ZeroDivisionError:
        return '-1'

output = []
output.append('Locale,Date Joined,Profile URL,Role,Translations,Approved,Rejected,Unapproved,Ratio')
for locale in locales:
    contributors = users_with_translations_counts(start_date, Q(locale=locale), None)
    for contributor in contributors:
        role = contributor.user_role.split(' ')[0]
        if role not in ROLES:
            continue
        output.append('{},{},{},{},{},{},{},{},{}'.format(
            locale.code,
            contributor.date_joined.date(),
            get_profile(contributor.username),
            role,
            contributor.translations_count,
            contributor.translations_approved_count,
            contributor.translations_rejected_count,
            contributor.translations_unapproved_count,
            get_ratio(contributor.translations_approved_count, contributor.translations_rejected_count)
        ))
示例#17
0
def ajax_permissions(request, locale):
    locale = get_object_or_404(Locale, code=locale)
    project_locales = locale.project_locale.visible().visible_for(request.user)

    if request.method == "POST":
        locale_form = forms.LocalePermsForm(request.POST,
                                            instance=locale,
                                            prefix="general",
                                            user=request.user)
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            request.POST,
            prefix="project-locale",
            queryset=project_locales,
            form_kwargs={"user": request.user},
        )

        if locale_form.is_valid() and project_locale_form.is_valid():
            locale_form.save()
            project_locale_form.save()

        else:
            errors = locale_form.errors
            errors.update(project_locale_form.errors_dict)
            error_msg = bleach.clean(json.dumps(errors))
            return HttpResponseBadRequest(error_msg)

    else:
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            prefix="project-locale",
            queryset=project_locales,
            form_kwargs={"user": request.user},
        )

    managers = locale.managers_group.user_set.order_by("email")
    translators = locale.translators_group.user_set.exclude(
        pk__in=managers).order_by("email")
    all_users = (User.objects.exclude(pk__in=managers | translators).exclude(
        email="").order_by("email"))

    contributors_emails = {
        contributor.email
        for contributor in users_with_translations_counts(
            None,
            Q(locale=locale) & Q(user__isnull=False), None)
    }

    locale_projects = locale.projects_permissions(request.user)

    return render(
        request,
        "teams/includes/permissions.html",
        {
            "locale": locale,
            "all_users": all_users,
            "contributors_emails": contributors_emails,
            "translators": translators,
            "managers": managers,
            "locale_projects": locale_projects,
            "project_locale_form": project_locale_form,
            "all_projects_in_translation": all([x[5]
                                                for x in locale_projects]),
        },
    )
示例#18
0
def test_mgr_user_translation_counts(
    resource_a,
    locale_a,
):
    """Checks if translation counts are calculated properly.

    Tests creates 3 contributors with different numbers translations
    and checks if their counts match.

    """
    contributors = UserFactory.create_batch(size=3)
    entities = EntityFactory.create_batch(size=36, resource=resource_a)
    batch_kwargs = sum(
        [
            [dict(user=contributors[0], approved=True)] * 7,
            [dict(user=contributors[0], approved=False, fuzzy=False, rejected=True)] * 3,
            [dict(user=contributors[0], fuzzy=True)] * 2,
            [dict(user=contributors[1], approved=True)] * 5,
            [dict(user=contributors[1], approved=False, fuzzy=False, rejected=True)] * 9,
            [dict(user=contributors[1], fuzzy=True)] * 2,
            [dict(user=contributors[2], approved=True)] * 1,
            [dict(user=contributors[2], approved=False, fuzzy=False)] * 2,
            [dict(user=contributors[2], fuzzy=True)] * 5,
        ],
        []
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args['user'],
            approved=args.get('approved', False),
            rejected=args.get('rejected', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = users_with_translations_counts()

    assert len(top_contribs) == 3
    assert top_contribs[0] == contributors[1]
    assert top_contribs[1] == contributors[0]
    assert top_contribs[2] == contributors[2]

    assert top_contribs[0].translations_count == 16
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_rejected_count == 9
    assert top_contribs[0].translations_unapproved_count == 0
    assert top_contribs[0].translations_needs_work_count == 2

    assert top_contribs[1].translations_count == 12
    assert top_contribs[1].translations_approved_count == 7
    assert top_contribs[1].translations_rejected_count == 3
    assert top_contribs[1].translations_unapproved_count == 0
    assert top_contribs[1].translations_needs_work_count == 2

    assert top_contribs[2].translations_count == 8
    assert top_contribs[2].translations_approved_count == 1
    assert top_contribs[2].translations_rejected_count == 0
    assert top_contribs[2].translations_unapproved_count == 2
    assert top_contribs[2].translations_needs_work_count == 5
示例#19
0
    approved = translations.latest("approved_date").approved_date
    activities = []
    if translated:
        activities.append(translated)
    if approved:
        activities.append(approved)
    activities.sort()
    return activities[-1].date() if len(activities) > 0 else None


output = []
output.append(
    "Locale,Date Joined,Latest Activity,Profile URL,Role,Translations,Approved,Rejected,Pending,Ratio"
)
for locale in locales:
    contributors = users_with_translations_counts(start_date, Q(locale=locale), None)
    for contributor in contributors:
        role = contributor.locale_role(locale)
        if role not in ROLES:
            continue
        # Ignore "imported" strings
        if contributor.username == "Imported":
            continue
        output.append(
            "{},{},{},{},{},{},{},{},{},{}".format(
                locale.code,
                contributor.date_joined.date(),
                get_latest_activity(contributor),
                get_profile(contributor.username),
                role,
                contributor.translations_count,
示例#20
0
文件: views.py 项目: mathjazz/pontoon
def ajax_permissions(request, locale):
    locale = get_object_or_404(Locale, code=locale)
    project_locales = locale.project_locale.visible()

    if request.method == 'POST':
        locale_form = forms.LocalePermsForm(
            request.POST,
            instance=locale,
            prefix='general',
            user=request.user
        )
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            request.POST,
            prefix='project-locale',
            queryset=project_locales,
            form_kwargs={
                'user': request.user
            }
        )

        if locale_form.is_valid() and project_locale_form.is_valid():
            locale_form.save()
            project_locale_form.save()

        else:
            errors = locale_form.errors
            errors.update(project_locale_form.errors_dict)
            error_msg = bleach.clean(json.dumps(errors))
            return HttpResponseBadRequest(error_msg)

    else:
        project_locale_form = forms.ProjectLocalePermsFormsSet(
            prefix='project-locale',
            queryset=project_locales,
            form_kwargs={
                'user': request.user
            }
        )

    managers = locale.managers_group.user_set.order_by('email')
    translators = locale.translators_group.user_set.exclude(pk__in=managers).order_by('email')
    all_users = (
        User.objects
            .exclude(pk__in=managers | translators)
            .exclude(email='')
            .order_by('email')
    )

    contributors_emails = set(
        contributor.email
        for contributor in users_with_translations_counts(None, Q(locale=locale), None)
    )

    locale_projects = locale.projects_permissions

    return render(request, 'teams/includes/permissions.html', {
        'locale': locale,
        'all_users': all_users,
        'contributors_emails': contributors_emails,
        'translators': translators,
        'managers': managers,
        'locale_projects': locale_projects,
        'project_locale_form': project_locale_form,
        'all_projects_in_translation': all([x[5] for x in locale_projects])
    })
示例#21
0
def test_mgr_user_period_filters(
    locale_a,
    resource_a,
):
    """Total counts should be filtered by given date.

    Test creates 2 contributors with different activity periods and checks
    if they are filtered properly.
    """
    contributors = UserFactory.create_batch(size=2)
    entities = EntityFactory.create_batch(size=35, resource=resource_a)
    batch_kwargs = sum(
        [
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                approved=True,
            )] * 12,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 7, 2),
                approved=True,
            )] * 5,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                approved=False,
                fuzzy=False,
            )] * 1,
            [dict(
                user=contributors[0],
                date=aware_datetime(2015, 3, 2),
                fuzzy=True,
            )] * 2,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                approved=True,
            )] * 2,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                approved=False,
                fuzzy=False,
            )] * 11,
            [dict(
                user=contributors[1],
                date=aware_datetime(2015, 6, 1),
                fuzzy=True,
            )] * 2
        ],
        [],
    )

    for i, kwa in enumerate(batch_kwargs):
        kwa.update(dict(entity=entities[i]))

    for args in batch_kwargs:
        TranslationFactory.create(
            locale=locale_a,
            user=args['user'],
            date=args['date'],
            approved=args.get('approved', False),
            fuzzy=args.get('fuzzy', False),
        )

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 6, 10)
    )
    assert len(top_contribs) == 1
    assert top_contribs[0].translations_count == 5
    assert top_contribs[0].translations_approved_count == 5
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 0
    assert top_contribs[0].translations_needs_work_count == 0

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 5, 10)
    )
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 15
    assert top_contribs[0].translations_approved_count == 2
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 11
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 5
    assert top_contribs[1].translations_approved_count == 5
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 0
    assert top_contribs[1].translations_needs_work_count == 0

    top_contribs = users_with_translations_counts(
        aware_datetime(2015, 1, 10)
    )
    assert len(top_contribs) == 2
    assert top_contribs[0].translations_count == 20
    assert top_contribs[0].translations_approved_count == 17
    assert top_contribs[0].translations_rejected_count == 0
    assert top_contribs[0].translations_unapproved_count == 1
    assert top_contribs[0].translations_needs_work_count == 2
    assert top_contribs[1].translations_count == 15
    assert top_contribs[1].translations_approved_count == 2
    assert top_contribs[1].translations_rejected_count == 0
    assert top_contribs[1].translations_unapproved_count == 11
    assert top_contribs[1].translations_needs_work_count == 2