Exemple #1
0
def create_item():
    """ Allows logged in users to create an item

    Can only be accessed by logged in users """
    categories = crud.get_all_categories()

    # Only allow item creation on POST request
    if request.method == 'POST':
        name = request.form['name']
        description = request.form['description']
        category_id = request.form['category-id']

        file = request.files['file']

        # Only allow upload of files which have an allowed extension
        if file and allowed_file(file.filename):
            extension = get_file_extension(file.filename)
            logging.debug("The file's extension is: %s" % extension)

            # Generate a UUID filename to avoid duplicate filenames
            filename = "%s.%s" % (uuid_gen(), extension)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        else:
            filename = None

        crud.create_item(name, description, category_id, filename)

        return redirect(url_for('index'), code=302)

    return render_template(
        'item_create_form.html',
        categories=categories)
Exemple #2
0
def create_item():
    """ Allows logged in users to create an item

    Can only be accessed by logged in users """
    categories = crud.get_all_categories()

    # Only allow item creation on POST request
    if request.method == 'POST':
        name = request.form['name']
        description = request.form['description']
        category_id = request.form['category-id']

        file = request.files['file']

        # Only allow upload of files which have an allowed extension
        if file and allowed_file(file.filename):
            extension = get_file_extension(file.filename)
            logging.debug("The file's extension is: %s" % extension)

            # Generate a UUID filename to avoid duplicate filenames
            filename = "%s.%s" % (uuid_gen(), extension)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        else:
            filename = None

        crud.create_item(name, description, category_id, filename)

        return redirect(url_for('index'), code=302)

    return render_template('item_create_form.html', categories=categories)
Exemple #3
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)
Exemple #4
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)
Exemple #5
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
Exemple #6
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
Exemple #7
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')
Exemple #8
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')