Example #1
0
 def get(self, request, user, identity, next_path):
     # At this point @with_user guarantees that we have a valid fxa
     # identity. We are proceeding with either registering the user or
     # logging them on.
     if user is None or user.deleted:
         action = 'register'
         if user is None:
             user = register_user(identity)
         else:
             reregister_user(user)
         if not is_safe_url(next_path, request):
             next_path = None
         # If we just reverse() directly, we'd use a prefixer instance
         # initialized from the current view, which would not contain the
         # app information since it's a generic callback, the same for
         # everyone. To ensure the user stays on the app/locale they were
         # on, we extract that information from the next_path if present
         # and set locale/app on the prefixer instance that reverse() will
         # use automatically.
         if next_path:
             if prefixer := get_url_prefix():
                 splitted = prefixer.split_path(next_path)
                 prefixer.locale = splitted[0]
                 prefixer.app = splitted[1]
         edit_page = reverse('users.edit')
         if next_path:
             next_path = f'{edit_page}?to={quote_plus(next_path)}'
         else:
             next_path = edit_page
Example #2
0
    def items(self):
        def additems(type):
            items = []
            for category in CATEGORIES[current_app.id][type].values():
                items.append((category, 1))
                pages_needed = math.ceil(addon_counts.get(category.id, 1) / page_size)
                for page in range(2, pages_needed + 1):
                    items.append((category, page))
            return items

        page_size = settings.REST_FRAMEWORK['PAGE_SIZE']
        current_app = amo.APPS[get_url_prefix().get_app()]
        counts_qs = (
            AddonCategory.objects.filter(
                addon___current_version__isnull=False,
                addon___current_version__apps__application=current_app.id,
                addon__disabled_by_user=False,
                addon__status__in=amo.REVIEWED_STATUSES,
            )
            .values('category_id')
            .annotate(count=Count('addon_id'))
        )
        addon_counts = {cat['category_id']: cat['count'] for cat in counts_qs}

        items = additems(amo.ADDON_EXTENSION)
        if current_app == amo.FIREFOX:
            items.extend(additems(amo.ADDON_STATICTHEME))
        return items
Example #3
0
def activate_locale(locale=None, app=None):
    """Active an app or a locale."""
    prefixer = old_prefix = get_url_prefix()
    old_app = old_prefix.app
    old_locale = translation.get_language()
    if locale:
        rf = RequestFactory()
        prefixer = Prefixer(rf.get('/%s/' % (locale,)))
        translation.activate(locale)
    if app:
        prefixer.app = app
    set_url_prefix(prefixer)
    yield
    old_prefix.app = old_app
    set_url_prefix(old_prefix)
    translation.activate(old_locale)
Example #4
0
def locale_url(url):
    """Take a URL and give it the locale prefix."""
    prefixer = get_url_prefix()
    script = prefixer.request.META['SCRIPT_NAME']
    parts = [script, prefixer.locale, url.lstrip('/')]
    return '/'.join(parts)
Example #5
0
 def _current_app(self):
     return amo.APPS[get_url_prefix().app]
Example #6
0
    def _cached_items(self):
        current_app = amo.APPS[get_url_prefix().app]
        addon_q = Q(
            addons___current_version__isnull=False,
            addons___current_version__apps__application=current_app.id,
            addons__disabled_by_user=False,
            addons__status__in=amo.REVIEWED_STATUSES,
            addonuser__listed=True,
            addonuser__role__in=(amo.AUTHOR_ROLE_DEV, amo.AUTHOR_ROLE_OWNER),
        )
        # android is currently limited to a small number of recommended addons, so get
        # the list of those and filter further
        if current_app == amo.ANDROID:
            promoted_addon_ids = PromotedAddon.objects.filter(
                Q(application_id=amo.ANDROID.id) | Q(application_id__isnull=True),
                group_id=RECOMMENDED.id,
                addon___current_version__promoted_approvals__application_id=(
                    amo.ANDROID.id
                ),
                addon___current_version__promoted_approvals__group_id=RECOMMENDED.id,
            ).values_list('addon_id', flat=True)
            addon_q = addon_q & Q(addons__id__in=promoted_addon_ids)

        users = (
            UserProfile.objects.filter(is_public=True, deleted=False)
            .annotate(
                theme_count=Count(
                    'addons', filter=Q(addon_q, addons__type=amo.ADDON_STATICTHEME)
                )
            )
            .annotate(
                extension_count=Count(
                    'addons', filter=Q(addon_q, addons__type=amo.ADDON_EXTENSION)
                )
            )
            .annotate(addons_updated=Max('addons__last_updated', filter=addon_q))
            .order_by('-addons_updated', '-modified')
            .values_list(
                'addons_updated', 'id', 'extension_count', 'theme_count', named=True
            )
        )
        items = []
        for user in users:
            if not user.extension_count and not user.theme_count:
                # some users have an empty page for various reasons, no need to include
                continue
            extension_pages_needed = math.ceil(
                (user.extension_count or 1) / EXTENSIONS_BY_AUTHORS_PAGE_SIZE
            )
            theme_pages_needed = math.ceil(
                (user.theme_count or 1) / THEMES_BY_AUTHORS_PAGE_SIZE
            )
            items.extend(
                self.item_tuple(user.addons_updated, user.id, ext_page, 1)
                for ext_page in range(1, extension_pages_needed + 1)
            )
            # start themes at 2 because we don't want (1, 1) twice
            items.extend(
                self.item_tuple(user.addons_updated, user.id, 1, theme_page)
                for theme_page in range(2, theme_pages_needed + 1)
            )
        return items