Exemple #1
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "is_authenticated": True,
            "avatar_url": get_avatar_url(user),
            "email": user.email,
            "subscriber_number": user.subscriber_number,
        }
        if UserSubscription.objects.filter(user=user, canceled__isnull=True).exists():
            data["is_subscriber"] = True
        if user.is_staff:
            data["is_staff"] = True
        if user.is_superuser:
            data["is_superuser"] = True
        if user.is_beta_tester:
            data["is_beta_tester"] = True
    else:
        data = {}

    data["waffle"] = {
        "flags": {},
        "switches": {s.name: True for s in Switch.get_all() if s.is_active()},
    }
    # Specifically and more smartly loop over the waffle Flag objects
    # to avoid unnecessary `cache.get(...)` calls within the `flag.is_active(request)`.
    for flag in Flag.get_all():
        if not request.user.is_authenticated:
            # Majority of users are anonymous, so let's focus on that.
            # Let's see if there's a quick reason to bail the
            # expensive `flag.is_active(request)` call.
            if (
                flag.authenticated or flag.staff or flag.superusers
            ) and not flag.everyone:
                continue
            if not (flag.languages or flag.percent or flag.everyone):
                continue
            if flag.languages:
                languages = [ln.strip() for ln in flag.languages.split(",")]
                if (
                    not hasattr(request, "LANGUAGE_CODE")
                    or request.LANGUAGE_CODE not in languages
                ):
                    continue

        if flag.is_active(request):
            data["waffle"]["flags"][flag.name] = True

    return JsonResponse(data)
Exemple #2
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "is_authenticated": True,
            "avatar_url": get_avatar_url(user),
            "email": user.email,
            "subscriber_number": user.subscriber_number,
        }
        if UserSubscription.objects.filter(user=user,
                                           canceled__isnull=True).exists():
            data["is_subscriber"] = True
        if user.is_staff:
            data["is_staff"] = True
        if user.is_superuser:
            data["is_superuser"] = True
        if user.is_beta_tester:
            data["is_beta_tester"] = True

        # This is rather temporary field. Once we're off the Wiki and into Yari
        # this no longer makes sense to keep.
        data["wiki_contributions"] = user.created_revisions.count()
    else:
        data = {}

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data["waffle"] = {
        "flags":
        {f.name: True
         for f in Flag.get_all() if f.is_active(request)},
        "switches": {s.name: True
                     for s in Switch.get_all() if s.is_active()},
        "samples": {s.name: True
                    for s in Sample.get_all() if s.is_active()},
    }
    return JsonResponse(data)
Exemple #3
0
def get_user(request, username):
    """
    Returns a JSON response with a small subset of public information if a
    user with the given username exists, otherwise returns a status code of
    404. The case of the username is not important, since the collation of
    the username column of the user table in MySQL is case-insensitive.
    """
    fields = ('username', 'title', 'fullname', 'organization', 'location',
              'timezone', 'locale')
    try:
        user = User.objects.only(*fields).get(username=username)
    except User.DoesNotExist:
        raise Http404(f'No user exists with the username "{username}".')
    data = {field: getattr(user, field) for field in fields}
    data['avatar_url'] = get_avatar_url(user)
    return JsonResponse(data)
Exemple #4
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            "username": user.username,
            "timezone": user.timezone,
            "is_authenticated": True,
            "is_staff": user.is_staff,
            "is_superuser": user.is_superuser,
            "is_beta_tester": user.is_beta_tester,
            "avatar_url": get_avatar_url(user),
            "is_subscriber": UserSubscription.objects.filter(
                user=user, canceled__isnull=True
            ).exists(),
        }
    else:
        data = {
            "username": None,
            "timezone": settings.TIME_ZONE,
            "is_authenticated": False,
            "is_staff": False,
            "is_superuser": False,
            "is_beta_tester": False,
            "avatar_url": None,
            "is_subscriber": False,
        }

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data["waffle"] = {
        "flags": {f.name: f.is_active(request) for f in Flag.get_all()},
        "switches": {s.name: s.is_active() for s in Switch.get_all()},
        "samples": {s.name: s.is_active() for s in Sample.get_all()},
    }
    return JsonResponse(data)
Exemple #5
0
def whoami(request):
    """
    Return a JSON object representing the current user, either
    authenticated or anonymous.
    """
    user = request.user
    if user.is_authenticated:
        data = {
            'username': user.username,
            'timezone': user.timezone,
            'is_authenticated': True,
            'is_staff': user.is_staff,
            'is_superuser': user.is_superuser,
            'is_beta_tester': user.is_beta_tester,
            'avatar_url': get_avatar_url(user),
        }
    else:
        data = {
            'username': None,
            'timezone': settings.TIME_ZONE,
            'is_authenticated': False,
            'is_staff': False,
            'is_superuser': False,
            'is_beta_tester': False,
            'avatar_url': None,
        }

    # Add waffle data to the dict we're going to be returning.
    # This is what the waffle.wafflejs() template tag does, but we're
    # doing it via an API instead of hardcoding the settings into
    # the HTML page. See also from waffle.views._generate_waffle_js.
    #
    # Note that if we upgrade django-waffle, version 15 introduces a
    # pluggable flag model, and the approved way to get all flag
    # objects will then become:
    #    get_waffle_flag_model().get_all()
    #
    data['waffle'] = {
        'flags': {f.name: f.is_active(request)
                  for f in Flag.get_all()},
        'switches': {s.name: s.is_active()
                     for s in Switch.get_all()},
        'samples': {s.name: s.is_active()
                    for s in Sample.get_all()},
    }
    return JsonResponse(data)
Exemple #6
0
 def get_avatar_url_cached(user):
     if user.id not in user_to_avatar_map:
         user_to_avatar_map[user.id] = get_avatar_url(user)
     return user_to_avatar_map[user.id]