예제 #1
0
파일: views.py 프로젝트: athaun/OurJSEditor
def index(request):
    top_programs = get_programs("top", limit=4)
    programs = {
        "popular": [p.to_dict(include_code=False) for p in top_programs],
    }

    if request.user.is_authenticated:
        recently_created = get_programs("new",
                                        Q(user=request.user),
                                        published_only=False,
                                        limit=3)
        subscriptions = get_programs(
            "new",
            Q(user__profile__in=request.user.profile.subscriptions.all()),
            limit=4)

        programs["recent"] = [
            p.to_dict(include_code=False) for p in recently_created
        ]
        programs["subscriptions"] = [
            p.to_dict(include_code=False) for p in subscriptions
        ]

    return render(request, 'ourjseditor/index.html',
                  {"programs": json.dumps(programs)})
예제 #2
0
def index(request, username):
    try:
        user = User.objects.select_related('profile').get(username=username)

        list_options = {
            'perPage': 50,
            'sort': 'new' if user == request.user else
            'top'  # If you're looking at your own profile, show new programs
        }

        programs = get_programs(list_options['sort'],
                                Q(user=user),
                                published_only=False,
                                limit=list_options['perPage'] + 1)
        program_dicts = [p.to_dict(include_code=False) for p in programs]

        list_options["initialPrograms"] = program_dicts
        user_data = {
            'user': user,
            'currentUser': request.user,
            'editing': False,
            'listOptions': json.dumps(list_options)
        }

        if request.user.is_authenticated:
            user_data[
                "subscribed"] = request.user.profile.subscriptions.filter(
                    profile_id=user.profile.profile_id).exists()

        return render(request, 'user_profile/user-profile.html', user_data)
    except User.DoesNotExist:
        return render(request,
                      'user_profile/does-not-exist.html',
                      {'username': username},
                      status=404)
예제 #3
0
def program_list(request, user_id, sort):
    if request.method == "POST":
        # It seems intutive that POSTing here would make a new program. However, that is not the case
        return api.error("Make a new program by posting to /api/program/new")

    requested_user = get_user(user_id, and_profile=False)

    offset = get_as_int(request.GET, "offset", 0)
    limit = get_as_int(request.GET, "limit", 20)

    if (limit > 20 or limit <= 0):
        limit = 20

    try:
        programs = get_programs(sort,
                                Q(user=requested_user),
                                offset=offset,
                                limit=limit,
                                published_only=False)
    except ValueError as err:
        return api.error(str(err))

    program_dicts = [p.to_dict(include_code=False) for p in programs]

    return api.succeed({"sort": sort, "programs": program_dicts})
예제 #4
0
def program_list(request, sort):
    if (not sort):
        sort = "new"  # Default sort. sort is actually passed in as None, so we can't use an argument default

    try:
        programs = get_programs(sort)
    except ValueError:
        return redirect("/programs")

    program_dicts = []
    for program in programs:
        program = program.to_dict(include_code=False)
        program_dicts.append(program)

    #TODO: complete? Right now we don't track that
    return render(request, "program/list.html", {
        "programs": json.dumps(program_dicts),
        "sort": sort
    })
예제 #5
0
def program_list(request, sort):
    if not sort:
        sort = "new" # Default sort. sort is actually passed in as None, so we can't use an argument default

    per_page = 20 # Per DRY, there should be one place that specifies how many items are on a page. It is here!

    try:
        programs = get_programs(sort, limit=per_page + 1) # Load and pass one extra program
    except ValueError:
        return redirect("/programs")

    program_dicts = [p.to_dict(include_code=False) for p in programs]

    return render(request, "program/list.html", {
        "listOptions": json.dumps({
            "initialPrograms": program_dicts,
            "perPage": per_page,
            "sort": sort
        })
    })