def __init__(self): self.db = Database() self.table = 'suppliers' self.columns = [ 'name', 'email_address', 'phone_number', 'website_url', 'street_address', 'city', 'region', 'postal_zip', 'country' ]
def __init__(self): self.db = Database() self.table = 'products' self.columns = [ 'name', 'category', 'description', 'sku', 'status', 'supplier', 'country_of_origin', 'quantity', 'quantity_sold', 'cost_per_unit', 'price_per_unit', 'notes' ]
class Controller(): def __init__(self): self.db = Database() self.table = None self.columns = None def record_to_model(self, record): pass def form_to_model(self, form): pass def form_to_values(self, form): pass def select_all_records(self): records = self.db.select_all_records(self.table) models = [] for record in records: model = self.record_to_model(record) models.append(model) return models def select_records_with_condition(self, column, value): pass def select_record_by_id(self, record_id): pass def create_record(self, form): pass def update_record(self, record_id, form): pass def delete_record(self, record_id): pass def validate_form(self, form): pass def form_to_model(self, form): pass def models_to_dictionaries(self, models): pass def dictionaries_to_dataframe(self, dictionaries): pass def dataframe_to_excel(self, dataframe): pass def dataframe_to_csv(self, dataframe): pass def dataframe_to_json(self, dataframe): pass
class AuthController(): def __init__(self): self.db = Database() self.table = 'users' self.columns = ['username', 'email_address', 'full_name', 'street_address', 'city', 'region', 'postal_zip', 'country', 'password'] def login_user(self, username, password): records = self.db.select_records_by_field(self.table, 'username', username) user_record = None if len(records) >= 1: user_record = records[0] valid, errors = self.validate_user(user_record, password) if errors is not None: return errors else: self.set_user_session(user_record[0], user_record[1]) return errors def validate_user(self, user_record, password): ''' Validates the user by making sure the password provided corresponds to the user with the username provided ''' if check_password_hash(user_record[9], password) is True: return True, None else: return False, ['The password provided is incorrect! Please try again!'] def register_new_user(self, reg_form): ''' Processes the user registration form ''' user_form = { 'username': reg_form.get('username'), 'email_address': reg_form.get('email_address'), 'full_name': reg_form.get('full_name'), 'street_address': reg_form.get('street_address'), 'city': reg_form.get('city'), 'region': reg_form.get('region'), 'postal_zip': reg_form.get('postal_zip'), 'country': reg_form.get('country'), 'password': reg_form.get('password'), 'confirm_password': reg_form.get('confirm_password') } errors = [] # Validate username un_valid, un_errors = self.validate_username(user_form.get('username')) if un_valid is False: for err in un_errors: errors.append(err) # Validate email address ea_valid, ea_errors = self.validate_email_address(user_form.get('email_address')) if ea_valid is False: for err in ea_errors: errors.append(err) # Validate password pw_valid, pw_errors = self.validate_password(user_form.get('password'), user_form.get('confirm_password')) if pw_valid is False: for err in pw_errors: errors.append(err) if len(errors) >= 1: return False, errors else: values = [ reg_form.get('username'), reg_form.get('email_address'), reg_form.get('full_name'), reg_form.get('street_address'), reg_form.get('city'), reg_form.get('region'), reg_form.get('postal_zip'), reg_form.get('country'), generate_password_hash(reg_form.get('password')) ] self.db.insert_new_record(self.table, self.columns, values) return True, None def validate_username(self, username): valid = False errors = [] records = self.db.select_records_by_field(self.table, 'username', username) if len(records) >= 1: error = 'The username provided is already taken!' errors.append(error) if len(username) < 4: error = 'The username field must be at least 4 characters long!' errors.append(error) if len(username) > 24: error = 'The username field cannot be more than 24 characters long!' errors.append(error) if len(errors) <= 0: valid = True return valid, errors def validate_email_address(self, email_address): valid = False errors = [] records = self.db.select_records_by_field(self.table, 'email_address', email_address) if not re.match(r"[^@]+@[^@]+\.[^@]+", email_address): error = 'The email address provided is invalid!' errors.append(error) if len(records) > 0: error = 'The email address provided is already in use!' errors.append(error) if len(email_address) < 3: error = 'The email address field must be at least 3 characters long!' errors.append(error) if len(email_address) > 320: error = 'The email address field cannot be longer than 320 characters!' errors.append(error) if len(errors) <= 0: valid = True return valid, errors def validate_password(self, password, confirm_password): valid = False errors = [] if password != confirm_password: error = 'The password fields provided must match!' errors.append(error) if len(password) < 6: error = 'The password fields must be at least characters long!' errors.append(error) if len(password) > 255: error = 'The password fields cannot be more than 255 characters long!' errors.append(error) if len(errors) <= 0: valid = True return valid, errors def set_user_session(self, id, username): session['uid'] = id session['username'] = username def clear_user_session(self): session.pop('uid') session.pop('username')
def __init__(self): self.db = Database() self.table = 'users' self.columns = ['username', 'email_address', 'full_name', 'street_address', 'city', 'region', 'postal_zip', 'country', 'password']
class ProductController(): def __init__(self): self.db = Database() self.table = 'products' self.columns = [ 'name', 'category', 'description', 'sku', 'status', 'supplier', 'country_of_origin', 'quantity', 'quantity_sold', 'cost_per_unit', 'price_per_unit', 'notes' ] def create_new_product(self, product_form): product = ProductModel(product_form.get('name'), product_form.get('category'), product_form.get('description'), product_form.get('sku'), product_form.get('status'), product_form.get('supplier'), product_form.get('country_of_origin'), product_form.get('quantity'), product_form.get('quantity_sold'), product_form.get('cost_per_unit'), product_form.get('price_per_unit'), product_form.get('notes')) product_errors = self.validate_product(product) if product_errors is not None: return product_errors else: values = product.to_values() self.db.insert_new_record(self.table, self.columns, values) return None def validate_product(self, product): nam_error = self.validate_name(product.name) cat_error = self.validate_category(product.category) sku_error = self.validate_sku(product.sku) errors = [] if nam_error is not None: errors.append(nam_error) if cat_error is not None: errors.append(cat_error) if sku_error is not None: errors.append(sku_error) if len(errors) > 0: return errors else: return None def validate_name(self, name): error = None if len(name) < 3: error = 'The product\'s name must be at least 3 characters long!' elif len(name) > 255: error = 'The product\'s name cannot be more than 255 characters long!' return error def validate_category(self, category): error = None if len(category) < 3: error = 'The product\'s category field must be at least 3 characters long!' elif len(category) > 45: error = 'The product\'s category field cannot be more than 45 characters long!' return error def validate_sku(self, sku): error = None if len(sku) < 6: error = 'The product\'s SKU field must be at least 6 characters long!' elif len(sku) > 12: error = 'The product\'s SKU field cannot be more than 12 characters long!' return error def record_to_product_model(self, record): product_model = ProductModel(record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9], record[10], record[11], record[12]) product_model.id = record[0] product_model.created_at = record[13] product_model.updated_at = record[14] return product_model def records_to_product_model(self, records): product_models = [] for rec in records: product_model = ProductModel(rec[1], rec[2], rec[3], rec[4], rec[5], rec[6], rec[7], rec[8], rec[9], rec[10], rec[11], rec[12]) product_model.id = rec[0] product_model.created_at = rec[13] product_model.updated_at = rec[14] product_models.append(product_model) return product_models def select_all_products(self): product_records = self.db.select_all_records(self.table) product_models = self.records_to_product_model(product_records) return product_models def select_all_products_with_status(self): pass def select_product_by_id(self, product_id): product_record = self.db.select_record_by_id(self.table, product_id) product_model = self.record_to_product_model(product_record) return product_model def edit_product(self, product_id, product_form): ''' Edits a product and updates the record in the database ''' # Create a product model object based on the newly submitted form product = ProductModel(product_form.get('name'), product_form.get('category'), product_form.get('description'), product_form.get('sku'), product_form.get('status'), product_form.get('supplier'), product_form.get('country_of_origin'), product_form.get('quantity'), product_form.get('quantity_sold'), product_form.get('cost_per_unit'), product_form.get('price_per_unit'), product_form.get('notes')) # Validate the product form product_errors = self.validate_product( product ) # Returns an array of errors or None if there aren't any validation issues # Return an error of errors if the form is invalid if product_errors is not None: return product_errors # If there are errors, this function returns and ends here # Create the array of values for each product field values = product.to_values() # Use the database object to execute the update self.db.update_existing_record(self.table, product_id, self.columns, values) # Return None since there are no form validation errors return None def delete_product(self, product_id): ''' Deletes a product in the database ''' self.db.delete_existing_record(self.table, product_id) def product_models_to_dict(self, product_models): ''' Converts an array of product models into an array of product dicts ''' products_dict = [] for p in product_models: product_dict = p.to_dict() products_dict.append(product_dict) return products_dict def product_dicts_to_dataframe(self, product_dicts): ''' Converts an array of product dicts into a pandas dataframe ''' products_df = pd.DataFrame(product_dicts) return products_df def products_dataframe_to_excel(self, products_df): ''' Exports the dataframe data into an XLSX file ''' file_name = 'products-inventory.xlsx' file_path = os.path.join(XLSX_DIR, 'products/' + file_name) products_df.to_excel(file_path) return file_path def search(self, search_term): products = self.select_all_products() matches = [] for p in products: p_dict = p.to_dict() if search_term.lower() in p_dict.get('name').lower(): matches.append(p_dict) return matches
def __init__(self): self.db = Database() self.table = None self.columns = None
class SupplierController(Controller): def __init__(self): self.db = Database() self.table = 'suppliers' self.columns = [ 'name', 'email_address', 'phone_number', 'website_url', 'street_address', 'city', 'region', 'postal_zip', 'country' ] def record_to_model(self, record): supplier_model = SupplierModel(record[1], record[2], record[3], record[4], record[5], record[6], record[7], record[8], record[9]) supplier_model.id = record[0] supplier_model.created_at = record[10] supplier_model.updated_at = record[11] return supplier_model def form_to_model(self, form): model = SupplierModel(form.get('name'), form.get('email_address'), form.get('phone_number'), form.get('website_url'), form.get('street_address'), form.get('city'), form.get('region'), form.get('postal_zip'), form.get('country')) return model def form_to_values(self, form): values = [ form.get('name'), form.get('email_address'), form.get('phone_number'), form.get('website_url'), form.get('street_address'), form.get('city'), form.get('region'), form.get('postal_zip'), form.get('country') ] return values def create_record(self, form): form_errors = self.validate_form(form) if form_errors is not None: return form_errors else: values = self.form_to_values(form) self.db.insert_new_record(self.table, self.columns, values) return None def update_record(self, record_id, form): form_errors = self.validate_form(form) if form_errors is not None: return form_errors values = self.form_to_values(form) self.db.update_existing_record(self.table, record_id, self.columns, values) return None def delete_record(self, record_id): self.db.delete_existing_record(self.table, record_id) def validate_form(self, form): errors = [] name = form.get('name') email_address = form.get('email_address') if len(name) < 3: errors.append( 'The name field cannot be less than 3 characters long!') elif len(name) > 100: errors.append( 'The name field cannot be greater than 100 characters long!') if len(email_address ) == 0 or email_address == '' or email_address is None: pass elif not re.match(r"[^@]+@[^@]+\.[^@]+", email_address): errors.append('The email address provided is invalid!') if len(errors) > 0: return errors else: return None def select_all_records(self): records = self.db.select_all_records(self.table) models = [] for record in records: model = self.record_to_model(record) models.append(model) return models def select_record_by_id(self, record_id): record = self.db.select_record_by_id(self.table, record_id) model = self.record_to_model(record) return model