app package

Module contents

app.db = <SQLAlchemy engine=sqlite:////Users/nathanyee/Documents/131_project/app/webapp.db>

A SQLAlchemy object used to connect to the database

app.webapp = <Flask 'app'>

The Flask object used to run the webapp.

Submodules

app.forms module

class app.forms.CartForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used to add a product to the user’s cart

product_id = <UnboundField(HiddenField, ('product',), {'validators': [<wtforms.validators.DataRequired object>]})>

A hidden field containing the id of the product

quantity = <UnboundField(SelectField, ('Quantity',), {'choices': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'default': 1, 'validators': [<wtforms.validators.DataRequired object>]})>

The number of the item that the user wants to add to the cart

submit = <UnboundField(SubmitField, ('Add to Cart',), {})>

Submit input (should be True)

class app.forms.CheckoutForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used to receive order info in order to process the order

address = <UnboundField(StringField, ('Address',), {'validators': [<wtforms.validators.DataRequired object>]})>

The address to ship the order to.

billing = <UnboundField(StringField, ('Billing info',), {'validators': [<wtforms.validators.DataRequired object>]})>

Credit card information

discount_code = <UnboundField(HiddenField, ('Discount',), {})>

A discount code to apply to the order (optional).

submit = <UnboundField(SubmitField, ('Place Order',), {})>

Submit input (should be True)

class app.forms.DeleteAccountForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used to confirm account deletes

confirm = <UnboundField(BooleanField, ('I want to delete my account.',), {'validators': [<wtforms.validators.DataRequired object>]})>

A boolean input that confirms that the submitter wants their account to be deleted.

submit = <UnboundField(SubmitField, ('Delete my account.',), {})>

Submit input (should be True)

class app.forms.FillOrderForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used by a merchant to fill orders

merchant_id = <UnboundField(HiddenField, ('Merchant ID',), {'validators': [<wtforms.validators.DataRequired object>]})>

The id of the merchant submitting the order filled request

order_id = <UnboundField(HiddenField, ('Order ID',), {'validators': [<wtforms.validators.DataRequired object>]})>

The id of the order that the merchant has filled

submit = <UnboundField(SubmitField, ('Complete',), {})>

Submit input (should be True)

class app.forms.LoginForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

password = <UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object>]})>

The password input of the form

submit = <UnboundField(SubmitField, ('Submit',), {})>

Submit input (should be True)

username = <UnboundField(StringField, ('Username',), {'validators': [<wtforms.validators.DataRequired object>]})>

The username input of the form

class app.forms.NewDiscountForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used by a merchant to create a new discount

amount = <UnboundField(FloatField, ('Discount Amount',), {'validators': [<wtforms.validators.DataRequired object>]})>

The numerical amount of the discount

code = <UnboundField(StringField, ('Discount Code',), {'validators': [<wtforms.validators.DataRequired object>]})>

The code for the new discount

expiration_date = <UnboundField(DateField, ('Expiration Date',), {'validators': [<wtforms.validators.DataRequired object>]})>

The date when the discount expires

products = <UnboundField(SelectMultipleField, ('Products',), {'validators': [<wtforms.validators.DataRequired object>]})>

A multi-select field containing the products that the discount applies to

submit = <UnboundField(SubmitField, ('Complete',), {})>

Submit input (should be True)

class app.forms.NewPercentageDiscountForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used by a merchant to create a new discount

amount = <UnboundField(FloatField, ('Discount Amount',), {'validators': [<wtforms.validators.NumberRange object>, <wtforms.validators.DataRequired object>]})>

The percentage amount of the discount

code = <UnboundField(StringField, ('Discount Code',), {'validators': [<wtforms.validators.DataRequired object>]})>

The code for the new discount

expiration_date = <UnboundField(DateField, ('Expiration Date',), {'validators': [<wtforms.validators.DataRequired object>]})>

The date when the discount expires

products = <UnboundField(SelectMultipleField, ('Products',), {'validators': [<wtforms.validators.DataRequired object>]})>

A multi-select field containing the products that the discount applies to

submit = <UnboundField(SubmitField, ('Complete',), {})>

Submit input (should be True)

class app.forms.NewProductForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used to receive input for a new product

category = <UnboundField(SelectField, ('Category',), {'default': 1})>

a select field to select the category that the product belongs to

description = <UnboundField(TextAreaField, ('Description',), {})>

A text description of the product

merchant_id = <UnboundField(HiddenField, ('Merchant ID',), {'validators': [<wtforms.validators.DataRequired object>]})>

The id of the merchant submitting the new product

name = <UnboundField(StringField, ('Name',), {'validators': [<wtforms.validators.DataRequired object>]})>

The name of the product to create

pictures = <UnboundField(MultipleFileField, ('Pictures',), {'validators': [<wtforms.validators.Optional object>, <flask_wtf.file.FileAllowed object>]})>

a file upload form that accepts multiple image uploads (png, jpg, jpeg, webp)

price = <UnboundField(FloatField, ('Price',), {'validators': [<wtforms.validators.DataRequired object>]})>

The price of the product

submit = <UnboundField(SubmitField, ('Submit',), {})>

Submit input (should be True)

class app.forms.PasswordForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used for password resets

new_password = <UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object>]})>

The new choice for password for the account

new_password_repeat = <UnboundField(PasswordField, ('Confirm password',), {'validators': [<wtforms.validators.DataRequired object>]})>

A repeated user input for the password

original_password = <UnboundField(PasswordField, ('Current password',), {'validators': [<wtforms.validators.DataRequired object>]})>

The original password of the account

submit = <UnboundField(SubmitField, ('Submit',), {})>

Submit input (should be True)

class app.forms.RegisterForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

A form object that receives input for registration of new accounts.

email = <UnboundField(StringField, ('Email',), {'validators': [<wtforms.validators.DataRequired object>, <wtforms.validators.Email object>]})>

The email input of the form

password = <UnboundField(PasswordField, ('Password',), {'validators': [<wtforms.validators.DataRequired object>]})>

The password input of the form

submit = <UnboundField(SubmitField, ('Submit',), {})>

Submit input (should be True)

username = <UnboundField(StringField, ('Username',), {'validators': [<wtforms.validators.DataRequired object>]})>

The username input of the form

class app.forms.ReviewForm(*args, **kwargs)[source]

Bases: flask_wtf.form.FlaskForm

Form object used to add a review to a product

body = <UnboundField(TextAreaField, ('Add a written review',), {})>

The text body of the review

rating = <UnboundField(SelectField, ('Give a rating from 1 to 5',), {'choices': [1, 2, 3, 4, 5], 'default': 5, 'validators': [<wtforms.validators.DataRequired object>]})>

A dropdown rating for the user to give a review from 1 to 5

submit = <UnboundField(SubmitField, ('Submit',), {})>

Submit input (should be True)

app.models module

class app.models.CartItem(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents a row in a cart

id

The primary key of the cart item

product_id

The foreign key that relates the row to a product

quantity

The quantity of the product ordered

timestamp

The time at which the item was created

user_id

The foreign key that relates the row to a customer user

class app.models.Category(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents a category of products

id

The primary key of the category

name

The name of the category

products

A queryable relationship representing the products that belong to the category

class app.models.Discount(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents a user. Also supports UserMixin for FlaskLogin. Construct a discount using the app.utils.create_discount method.

apply_discount(cart_item: int)[source]

Returns the discount amount for a given app.models.CartItem row id.

Parameters

cart_item – The id field of the app.models.CartItem

Returns

The discount amount for the given row.

Return type

float

code

The code of the discount that the user can use

details

A JSON attribute containing the details of the discount

expiration

The expiration date of the discount

id

The primary key of the discount

is_valid()[source]

Compares the current datetime to the stored expiration date to confirm if the discount is still valid

Returns

True if the current date is on or before the expiration date. False if it is after.

Return type

bool

class app.models.Image(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that contains information about an uploaded image

id

The primary key of the image

path

The path to the image

product
product_id

The foreign key of which the image is related to

class app.models.Order(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents an order

id

The primary key of the order

order_row

A queryable relationship that retrieves the OrderRows belonging to an Order

ship_address

The address that the order will be shipped to

user
user_id

A foreign key that relates an order to a customer user

class app.models.OrderRow(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents a row of an Order

filled

A boolean that represents if the order was filled or not. Defaults to False.

id

A foreign key that relates to the Order that the row belongs to

order
product

A relationship that links to the Product object purchased in the row

product_id

A foreign key that relates to the product purchased in the row

product_price

The price of each product at the time it was purchased

quantity

The number of the product that was purchased

row_id

The primary key of the order row

timestamp

The time at which the OrderRow was created. Defaults to datetime.utcnow.

class app.models.Product(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object that represents a product

category
category_id

The foreign key of the category the product belongs to

description

The description of the product

id

The primary key of the product

images

A instrumentedList that contains the Image objects related to the product

merchant_id

The foreign key of the merchant User the product belongs to

name

The name of the product

orders

A relationship that retrieves orders of the product

price

The price of the product

reviews

A queryable database relationship that retrieves the reviews of the product

user
class app.models.Review(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

A Model object representing a review

body

The text body of the review

id

The primary key of the review

product
product_id

A foreign key that relates the review to a product

rating

A numerical rating given in the review

timestamp

The time at which the review was written. Defaults to datetime.utcnow.

user_id

A foreign key that relates the review to a customer

class app.models.Role(**kwargs)[source]

Bases: sqlalchemy.orm.decl_api.Model

An Model object that contains the name of a role

id

The primary key of the role

name

The name of the role

class app.models.User(**kwargs)[source]

Bases: flask_login.mixins.UserMixin, sqlalchemy.orm.decl_api.Model

A Model object that represents a user. Also supports UserMixin for FlaskLogin

cart_items

A queryable relationship that retrieves the rows in the cart related to the customer user

check_password(password)[source]

Checks a password input against the password stored in the User

Parameters

password (str) – The password to check

Returns

True if the passwords match, False if not

Return type

bool

email

The email of the user

id

The primary key of the user

orders

A queryable relationship that retrieves the orders related to the customer user

password_hash

The hashed password of the user

products

A queryable relationship that retrieves the products related to a merchant user

roles

A queryable database relationship that retrieves the roles related to the user

set_password(password)[source]

Sets the password of the given User object

Parameters

password (str) – The new password to set

username

The username of the user

app.models.load_user(id)[source]

A user loader for flask login

Parameters

id – The ID of the user to retrieve

Returns

The User object that has the given ID

Return type

app.models.User

app.routes module

app.routes.account_info()[source]

Returns a page containing information about the username and email as well as a form to change the password. When accessed through POST, will validate and process the password change form.

app.routes.account_test()[source]

Used for testing, should only be reachable if logged in, else it would redirect the user to the login page

app.routes.add_categories(func)[source]

Decorator function that automatically adds categories to render_template

app.routes.admin_promo()[source]

Returns a admin input to create a new promotion. When accessed through a POST request, will save inputs, create a new Product object, and redirect the user to the product page once submitted.

app.routes.admin_promo_percentage()[source]

Returns a admin input to create a new promotion. When accessed through a POST request, will save inputs, create a new Product object, and redirect the user to the product page once submitted.

app.routes.cart()[source]

Cart website for logged-in users to be able to see and edit their cart

app.routes.cart_remove(row_id)[source]

Allows a logged-in user to remove a row from their cart

Parameters

row_id (int) – The ID of the row to remove

app.routes.category(category_id)[source]

Generates a product catalog containing all the products of a given category

Parameters

category_id (int) – The category to fetch items for

app.routes.checkout()[source]

Site to recieve input/handle checkout requests for a logged in user

app.routes.delete_account()[source]

Contains a form for the user to confirm that they want to delete their account. When submitted through POST, will receive and validate the request before deleting the account from the database.

app.routes.discounts()[source]
app.routes.home()[source]

Returns the home page of the website to the user. Allows non-logged in users and customer users to access.

app.routes.images(filename)[source]

Retrieves an uploaded file

Parameters

filename (str) – The name of the file

Returns

an image (.png, .jpg, .jpeg) that was uploaded

app.routes.login()[source]

Return a site that takes user input for login/password to login to an account. Will verify login information if accessed through a POST request.

app.routes.logout()[source]

Logs out the user using flask-login when accessed.

app.routes.merchant()[source]

Returns a home page for a user logged in as a merchant. Redirects if not logged in as a merchant.

app.routes.merchant_account_test()[source]

Used for testing, should only be reachable if logged in as a merchant, else it would redirect the user to the login page

app.routes.merchant_login()[source]

Return a site that takes user input for login/password to log in to a merchant account. Will verify login information if accessed through a POST request.

app.routes.merchant_new_product()[source]

Returns a merchant input to submit a new product. When accessed through a POST request, will save inputs, create a new Product object, and redirect the user to the product page once submitted.

app.routes.merchant_orders()[source]

Return a site with unfilled orders for a logged-in merchant. When accessed with a POST request with the correct form, will change the status of the order rows to filled.

app.routes.merchant_orders_filled()[source]

Return a site with filled orders for a logged-in merchant.

app.routes.merchant_profile(merchant_id)[source]

Generates a product catalog containing all the products of a given merchant

Parameters

merchant_id (int) – The merchant to fetch items for

app.routes.merchant_promo()[source]

Returns a merchant input to create a new promotion. When accessed through a POST request, will save inputs, create a new Product object, and redirect the user to the product page once submitted.

app.routes.merchant_promo_percentage()[source]

Returns a merchant input to create a new promotion. When accessed through a POST request, will save inputs, create a new Product object, and redirect the user to the product page once submitted.

app.routes.merchant_register()[source]

Return a site that takes user input to create a new merchant account. Will verify registration information if accessed through a POST request.

app.routes.orders()[source]

Returns a page to a logged in customer user containing orders with unfilled rows. Redirects if logged in as a merchant or not logged in.

app.routes.orders_filled()[source]

Returns a page to a logged in customer user containing orders with filled rows. Redirects if logged in as a merchant or not logged in.

app.routes.product(prod_id)[source]

Retrieve a product page for a given product id. Will abort with a 404 if the ID was not found.

Parameters

prod_id (int) – The id of the Product to retrieve

app.routes.product_page(prod_id)[source]

Retrieve a product page for a given product id. Will abort with a 404 if the ID was not found.

Parameters

prod_id (int) – The id of the Product to retrieve

app.routes.product_review(product_id)[source]

Web route for logged in users to review a specific product

Parameters

product_id (int) – The ID of the product to review

Returns

A webpage allowing the user to review if they haven’t already and have ordered the product, redirect back

to the product page with a warning otherwise

app.routes.register()[source]

Return a site that takes user input to create a new customer account. Will verify registration information if accessed through a POST request.

app.routes.render_template(*args, **kwargs)
app.routes.search()[source]

Returns a page with search results from a regex search using the param q as the query from a get request.

app.utils module

app.utils.admin_required(func)[source]

decorator to require the current_user to have the merchant role or else it redirects :param func: The function to decorate :return: A function instance that checks the current user is a merchant before returning the action

app.utils.create_discount(code: str, type: int, applicable_ids, percentage: bool, amount: float, end_date)[source]

Creates a new Discount object

Parameters
  • code (str) – The code of the Discount

  • type (int) – The type of the discount (0=products, 1=categorywide, 2=sitewide)

  • applicable_ids (list[int]) – The applicable IDs (product IDs or category IDs) that the discount will apply to

  • percentage (bool) – True if the discount is percentage based (0 to 1). False if it is a fixed amount.

  • amount (float) – The amount of the discount

  • end_date (datetime.datetime) – The end date of the discount.

Returns

a new app.models.Discount object

Return type

app.models.Discount

app.utils.get_admin()[source]

Retrieve a Role object representing an admin :return: a Role object with the name admin

app.utils.get_categories()[source]

Retrieve a list of category names from the database

Returns

A python list containing the string names of all the categories

app.utils.get_category_dict()[source]

Retrieve a dictionary of categories in the form name:id

Returns

A python dictionary with key=category name and value=category id

app.utils.get_merchant()[source]

Retrieve a Role object representing a merchant :return: a Role object with the name merchant

app.utils.is_merchant(user)[source]
app.utils.merchant_required(func)[source]

decorator to require the current_user to have the merchant role or else it redirects :param func: The function to decorate :return: A function instance that checks the current user is a merchant before returning the action

app.utils.prevent_merchant(func)[source]

decorator to prevent merchants from accessing sites

Parameters

func – func: The function to decorate

Returns

A function instance that checks the current user is not a merchant before returning the action