Exemplo n.º 1
0
def json_endpoint():
    """ The endpoint to access the catalogue in JSON format

    Be advised that this currently exposes the complete catalogue
    which is something you may want to avoid

    Big thanks to michael_940140431 on the Udacity forums
    for getting me on the right track """
    output = []
    entries = crud.get_all_categories()

    for entry in entries:
        items = crud.get_items_by_category(entry.id)

        entry = entry.serialise
        entry['Item'] = [i.serialise for i in items]

        output.append(entry)

    return jsonify(Category=output)
Exemplo n.º 2
0
def json_endpoint():
    """ The endpoint to access the catalogue in JSON format

    Be advised that this currently exposes the complete catalogue
    which is something you may want to avoid

    Big thanks to michael_940140431 on the Udacity forums
    for getting me on the right track """
    output = []
    entries = crud.get_all_categories()

    for entry in entries:
        items = crud.get_items_by_category(entry.id)

        entry = entry.serialise
        entry['Item'] = [i.serialise for i in items]

        output.append(entry)

    return jsonify(Category=output)
Exemplo n.º 3
0
def generate_context(item_id=None, category_id=None):
    """ Returns a catalogue context with categories and item(s)
    item_id:        if set retrieves item details
    category_id:    if set retrieves items of this category
                    (note: category_id is ignored, if item_id is provided)

    If neither category_id nor item_id is provided,
    all items in the catalogue will be returned

    Always returns all categories """
    context = dict()
    context['categories'] = crud.get_all_categories()

    if item_id is not None:
        context['items'] = crud.get_item_by_id(item_id)
    elif category_id is not None:
        context['items'] = crud.get_items_by_category(category_id)
    else:
        context['items'] = crud.get_all_items()

    return context
Exemplo n.º 4
0
def generate_context(item_id=None, category_id=None):
    """ Returns a catalogue context with categories and item(s)
    item_id:        if set retrieves item details
    category_id:    if set retrieves items of this category
                    (note: category_id is ignored, if item_id is provided)

    If neither category_id nor item_id is provided,
    all items in the catalogue will be returned

    Always returns all categories """
    context = dict()
    context['categories'] = crud.get_all_categories()

    if item_id is not None:
        context['items'] = crud.get_item_by_id(item_id)
    elif category_id is not None:
        context['items'] = crud.get_items_by_category(category_id)
    else:
        context['items'] = crud.get_all_items()

    return context
Exemplo n.º 5
0
def xml_endpoint():
    """ The endpoint to access the catalogue in XML format

    Be advised that this currently exposes the complete catalogue
    which is something you may want to avoid """
    def generate_children(parent, children):
        """ This function assigns a list of children to a parent element
        parent:     the name of the parent tag (provide as string)
        children:   a dictionary of children
                    (the key becomes the tag name, the value the text)

        This function does not deal with attributes,
        but only tags and text values """
        parent = Element(parent)
        children = children.serialise

        for key, value in children.items():
            child = Element(key)
            child.text = str(value)
            parent.append(child)

        return parent

    categories = crud.get_all_categories()

    catalogue_elem = Element('catalogue')
    for category in categories:
        cats_elem = generate_children('category', category)

        items_elem = Element('items')
        items = crud.get_items_by_category(category.id)
        for item in items:
            item_elem = generate_children('item', item)
            items_elem.append(item_elem)

        cats_elem.append(items_elem)
        catalogue_elem.append(cats_elem)

    return Response(tostring(catalogue_elem), mimetype='application/xml')
Exemplo n.º 6
0
def xml_endpoint():
    """ The endpoint to access the catalogue in XML format

    Be advised that this currently exposes the complete catalogue
    which is something you may want to avoid """
    def generate_children(parent, children):
        """ This function assigns a list of children to a parent element
        parent:     the name of the parent tag (provide as string)
        children:   a dictionary of children
                    (the key becomes the tag name, the value the text)

        This function does not deal with attributes,
        but only tags and text values """
        parent = Element(parent)
        children = children.serialise

        for key, value in children.items():
            child = Element(key)
            child.text = str(value)
            parent.append(child)

        return parent

    categories = crud.get_all_categories()

    catalogue_elem = Element('catalogue')
    for category in categories:
        cats_elem = generate_children('category', category)

        items_elem = Element('items')
        items = crud.get_items_by_category(category.id)
        for item in items:
            item_elem = generate_children('item', item)
            items_elem.append(item_elem)

        cats_elem.append(items_elem)
        catalogue_elem.append(cats_elem)

    return Response(tostring(catalogue_elem), mimetype='application/xml')