コード例 #1
0
ファイル: views.py プロジェクト: frost-byte/catalog
def newCategory():
    """Allow an Authorized User to add a new Category or Process the form for
    adding a new Category.

    Returns:
        For a GET operation returns a Web View containing a form for entering
        data for the new user.

        A successful POST request directs presents a View of the new Category.

    """
    # Only an Authenticated User can add New Categories
    if isActiveSession() is False:
        return redirect(url_for("listCategory"))

    # Process the New Category Form and add it to the Database
    if request.method == "POST":
        newCategory = Category(user_id=getSessionUserInfo()["id"], name=request.form["name"])

        session.add(newCategory)
        session.commit()

        flash("New Category created!")
        # Display the Information for the new Category
        return redirect(url_for("viewCategory", key=newCategory.id))

    else:
        return render_template(
            "generic.html",
            modelType="category",
            viewType=os.path.join("partials", "new.html"),
            traits=Category.defaultTraits(),
        )
コード例 #2
0
ファイル: views.py プロジェクト: frost-byte/catalog
def newItem():
    """This route is used behind the scenes to view an item.  It forwards
    the request on to viewCatItem.  Requires a user to be authenticated.

    Notes:
        This route could be changed to reflect which category it will be in.

    Returns:
        A GET request presents the user with a form for creating a new Item.

        A POST request processes the user's input from the form and adds the
        new item.

    """
    # A user session must exist to add an item.
    if isActiveSession() is False:
        return redirect(url_for("listItem"))

    if request.method == "POST":
        # Process the new Item from the submitted form.
        # Make sure that an item associated with this category doesn't already have
        # the name of the one submitted in the form.
        category = Category.query.filter_by(name=request.form["category"]).one()

        newItemName = request.form["name"]

        try:
            # We should find either zero or one item in a category with a given
            # name.
            items = Item.query.filter_by(cat_id=category.id, name=newItemName).one_or_none()
            print "newItem: items = {0}".format(items)

        except MultipleResultsFound as e:
            # We more than one item with the newItemName in it's category.
            print "Multiple " + e
            flash("{0} items named {1} in {2} already.".format(len(items), newItemName, category.name))
            return redirect(url_for("viewCategory", key=category.id))

        if items is None:
            # This is a new Item for this category and it's name is unique
            # in the category.

            # Handle uploaded image
            picture = request.files["picture"]
            pictureUrl = processImageUpload(picture)

            # Create the New Item and add it to the Database
            newItem = Item(
                name=request.form["name"],
                dateCreated=datetime.strptime(request.form["created"], "%Y-%m-%d"),
                cat_id=category.id,
                description=request.form["description"],
                user_id=getSessionUserInfo()["id"],
                picture=pictureUrl,
            )

            session.add(newItem)
            session.flush()
            session.commit()

            flash("New item created!")
            # Present the user with a view of the new item
            return redirect(url_for("viewItem", key=newItem.id))

        else:
            # Alert the user to an already exisiting item with the specified name.
            flash("An item with the name {0} already exists in {1}.".format(newItemName, category.name))
            # Send the user back to the newItem Form.
            return redirect(url_for("newItem"))
    else:
        # Present the User with the New Item Form
        return render_template(
            "generic.html", modelType="item", viewType=os.path.join("partials", "new.html"), traits=Item.defaultTraits()
        )