예제 #1
0
def edit_item(category_slug, item_slug):
    """
    Edit the current item form.

    :param category_slug: The category slug
    :param item_slug: The item slug
    """

    # Get the item and check if it's exist or fail to 404 page if not
    item = Item.where('slug', item_slug).first_or_fail()

    # Make sure the current user is authorized or not
    if not item.user or item.user.id != current_user.id:
        return current_app.login_manager.unauthorized()

    # Get the category slug from url and compare it with the item's
    # category slug, if both are not the same, redirect to the correct slug
    if category_slug != item.category.slug:
        return redirect(
            url_for('edit_item',
                    category_slug=item.category.slug,
                    item_slug=item.slug))

    return render_template('items/edit.html',
                           categories=Category.all(),
                           item=item)
예제 #2
0
def update_item():
    """
    Update the requested item.
    """

    # Get the category and check if it's exist or fail to 404 page if not
    category = Category.find_or_fail(request.form['category_id'])

    # Get the item and check if it's exist or fail to 404 page if not
    item = Item.find_or_fail(request.form['item_id'])

    # Make sure the current user is authorized or not
    if not item.user or item.user.id != current_user.id:
        return current_app.login_manager.unauthorized()

    # The below is for validate the unique slug, if it
    # was exists before, then the ORM will not fall to
    # the ModelNotFound exception and the code will
    # continue flashing the error message to the user.
    # Otherwise, if the slug is not exists, then the
    # request is completely valid and the ORM will
    # going to fall to the ModelNotFound exception
    # then save the new Item
    try:
        # Get the current item slug before it changing using ORM mutator
        old_item_slug = item.slug

        item.title = request.form['title']
        item.description = request.form['description']

        # Associate the new item with the category
        item.category().associate(category)

        # Associate the new item to the current user
        item.user().associate(current_user)

        # Get the item and check if it's exist or fail to 404 page if not
        Item.where('slug', item.slug).first_or_fail()

        # if the current slug is the same as requested then throw
        # to the ModelNotFound Exception and save the current item
        if item.slug == old_item_slug:
            raise ModelNotFound(item.__class__)

        flash('The title seems similar to another'
              ' title, please choose another one.')

        return render_template('items/edit.html',
                               categories=Category.all(),
                               item=item)
    except ModelNotFound:
        # Save the item
        item.save()

    return redirect(
        url_for('view_item', category_slug=category.slug, item_slug=item.slug))
예제 #3
0
def get_category(category_id=None):
    """
    Return a JSON version of the category.
    :param category_id:
    :return: Response
    """
    try:
        category = Category.by_id(category_id)
    except Exception:
        return jsonify(Category.all())
    else:
        return jsonify(category.to_dict())
예제 #4
0
def add_item(category_slug=None):
    """
    Add a new Item Form.

    :param category_slug: The category slug
    """

    # Get the current category using the slug
    current_category = Category.where('slug', category_slug).first()

    return render_template('items/add.html',
                           categories=Category.all(),
                           current_category=current_category)
예제 #5
0
def index():
    """
    Get all categories and latest items
    to be loaded to the homepage
    """

    # Get all categories
    categories = Category.all()

    # Get last 10 items ordering by its id
    latest_items = Item.with_('category').take(10).order_by('id', 'desc').get()

    return render_template('index.html',
                           categories=categories,
                           latest_items=latest_items)
예제 #6
0
def store_item():
    """
    Store the new requested item.
    """

    # Get the category and check if it's exist or fail to 404 page if not
    category = Category.find_or_fail(request.form['category_id'])

    # Create new item
    item = Item()

    item.title = request.form['title']
    item.description = request.form['description']

    # Associate the new item with the category
    item.category().associate(category)

    # Associate the new item to the current user
    item.user().associate(current_user)

    # The below is for validate the unique slug, if it
    # was exists before, then the ORM will not fall to
    # the ModelNotFound exception and the code will
    # continue flashing the error message to the user.
    # Otherwise, if the slug is not exists, then the
    # request is completely valid and the ORM will
    # going to fall to the ModelNotFound exception
    # then save the new Item
    try:
        # Get the item and check if it's exist or fail to 404 page if not
        Item.where('slug', item.slug).first_or_fail()

        flash('The title seems similar to another'
              ' title, please choose another one.')

        return render_template('items/add.html',
                               categories=Category.all(),
                               current_category=category,
                               item=item)
    except ModelNotFound:
        # Save the item
        item.save()

    return redirect(
        url_for('view_item', category_slug=category.slug, item_slug=item.slug))
예제 #7
0
 def get(self):
     return Category.all()