コード例 #1
0
def index(request):
    """
    Query the database for a list of ALL categories currently stored
    Order the categories by "likes" in descending order
    Retrieve the top 5 only - or all if less than 5
    Place the list in dictionary:context_dict and pass to the template engine: index.html
    :param request:
    :return:
    """
    # GET category list, sort by "likes" in descending order -> order_by("-likes")
    # For each category object returned, add attribute "url", replace " " with "_"
    category_list = Category.objects.order_by("-likes")[:5]
    for a_category in category_list[:5]:
        if not hasattr(category, "url"):
            setattr(a_category, "url",
                    Category._encode_url_name(a_category.name))

    # GET page list, sort descending by "views", just the top 5
    page_list = Page.objects.order_by("-views")[:5]

    # Construct a dictionary to pass to the template engine as it's context
    # Note the key boldmessage is the same as {{ boldmessage }} in the template: rango/index.html
    context_dict = {
        "boldmessage": "You are welcome to register or login",
        "categories": category_list,
        "pages": page_list,
    }
    return _process_request(request, context_dict, r"rango/index.html")
コード例 #2
0
def add_category(request):
    """
    Either show a form or create a new instance of one.
    """

    # Is request HTTP POST or GET?
    if request.method == "POST":
        form = CategoryForm(request.POST)
        if form.is_valid():
            category = form.save(commit=False)
            setattr(category, "url", Category._encode_url_name(category.name))
            category.save()

            # now call the index() view
            # user redirected to home page
            return index(request)
        else:
            # Uh oh, supplied form contains error - print out to console
            print form.errors
    else:
        # request not a POST, display the form to enter details
        form = CategoryForm()

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return _process_request(request, {"form": form}, "rango/add_category.html")
コード例 #3
0
ファイル: views.py プロジェクト: catjake/rango
def index(request):
    """
    Query the database for a list of ALL categories currently stored
    Order the categories by "likes" in descending order
    Retrieve the top 5 only - or all if less than 5
    Place the list in dictionary:context_dict and pass to the template engine: index.html
    :param request:
    :return:
    """
    # GET category list, sort by "likes" in descending order -> order_by("-likes")
    # For each category object returned, add attribute "url", replace " " with "_"
    category_list = Category.objects.order_by("-likes")[:5]
    for a_category in category_list[:5]:
        if not hasattr(category,"url"):
            setattr(a_category,"url", Category._encode_url_name(a_category.name))

    # GET page list, sort descending by "views", just the top 5
    page_list = Page.objects.order_by("-views")[:5]

    # Construct a dictionary to pass to the template engine as it's context
    # Note the key boldmessage is the same as {{ boldmessage }} in the template: rango/index.html
    context_dict = {
        "boldmessage": "You are welcome to register or login",
        "categories": category_list,
        "pages": page_list,
    }
    return _process_request(request, context_dict, r"rango/index.html")
コード例 #4
0
ファイル: views.py プロジェクト: catjake/rango
def add_category(request):
    """
    Either show a form or create a new instance of one.
    """

    # Is request HTTP POST or GET?
    if request.method == "POST":
        form = CategoryForm(request.POST)
        if form.is_valid():
            category = form.save(commit=False)
            setattr(category,"url", Category._encode_url_name(category.name))
            category.save()

            # now call the index() view
            # user redirected to home page
            return index(request)
        else:
            # Uh oh, supplied form contains error - print out to console
            print form.errors
    else:
        # request not a POST, display the form to enter details
        form = CategoryForm()

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return _process_request(request, {"form": form}, "rango/add_category.html")
コード例 #5
0
def add_page(request, category_name_url):
    """
    Add page to category if it doesn't exist,
    :param request
    :param category_name_url
    """
    category_name = Category._encode_url_name(category_name_url,
                                              Category._url_decode_rules_dict)
    # category_name = "ooops"
    cat = None
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            # Cannot commit straight away, not all fields automatically populated
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it
            # Handle exception for Model.DoesNotExist, Go back and render the add category form
            try:
                cat = Category.objects.get(url=category_name_url)
                page.category = cat
            except Category.DoesNotExist:
                # Category does not exist, Go back and the render the add_category form
                return _process_request(request, {}, "rango/add_category.html")
            # set default value for number of views, and save new model instance
            page.views = 0
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {
        "category_name_url": category_name_url,
        "category_name": category_name,
        "form": form,
    }
    return _process_request(request, context_dict, "rango/add_page.html")
コード例 #6
0
ファイル: views.py プロジェクト: catjake/rango
def add_page(request, category_name_url):
    """
    Add page to category if it doesn't exist,
    :param request
    :param category_name_url
    """
    category_name = Category._encode_url_name(category_name_url, Category._url_decode_rules_dict)
    # category_name = "ooops"
    cat = None
    if request.method == "POST":
        form = PageForm(request.POST)

        if form.is_valid():
            # Cannot commit straight away, not all fields automatically populated
            page = form.save(commit=False)

            # Retrieve the associated Category object so we can add it
            # Handle exception for Model.DoesNotExist, Go back and render the add category form
            try:
                cat = Category.objects.get(url=category_name_url)
                page.category = cat
            except Category.DoesNotExist:
                # Category does not exist, Go back and the render the add_category form
                return _process_request(request, {}, "rango/add_category.html")
            # set default value for number of views, and save new model instance
            page.views = 0
            page.save()

            # Now that the page is saved, display the category instead.
            return category(request, category_name_url)
        else:
            print form.errors
    else:
        form = PageForm()

    context_dict = {
        "category_name_url": category_name_url,
        "category_name": category_name,
        "form": form,
    }
    return _process_request(request, context_dict, "rango/add_page.html")