diff --git a/customers/models.py b/customers/models.py index c2890f9..bc3b714 100644 --- a/customers/models.py +++ b/customers/models.py @@ -11,8 +11,9 @@ class Customer(db.Model): email = db.Column(db.String(150), unique=False) address = db.Column(db.String(80)) mobile_phone = db.Column(db.String(80)) + is_dormant = db.Column(db.Boolean) - def __init__(self, first_name=None, last_name=None, number_shares=0, email=None, address=None, mobile_phone=None): + def __init__(self, first_name=None, last_name=None, number_shares=0, email=None, address=None, mobile_phone=None, is_dormant=0): self.first_name = first_name self.last_name = last_name self.name = first_name + ' ' + last_name @@ -20,4 +21,5 @@ def __init__(self, first_name=None, last_name=None, number_shares=0, email=None, self.email = email self.address = address self.mobile_phone = mobile_phone + self.is_dormant = is_dormant diff --git a/customers/templates/customers.html b/customers/templates/customers.html index 3191252..31ed23a 100644 --- a/customers/templates/customers.html +++ b/customers/templates/customers.html @@ -44,7 +44,7 @@ {% endif %} {% if current_user.can_delete : %} - + {% endif %} diff --git a/customers/views.py b/customers/views.py index cc54abe..60e5f16 100644 --- a/customers/views.py +++ b/customers/views.py @@ -12,7 +12,7 @@ def customers(): from models import Customer try: - query_customers = Customer.query.order_by(Customer.first_name) + query_customers = Customer.query.filter(Customer.is_dormant == 0).order_by(Customer.first_name) return render_template('customers.html', query_customers=query_customers) except TemplateNotFound: abort(404) @@ -38,7 +38,8 @@ def newcustomer(): number_shares=form.number_shares.data, email=form.email.data.strip(), address=form.address.data.strip(), - mobile_phone=form.mobile_phone.data.strip() + mobile_phone=form.mobile_phone.data.strip(), + is_dormant=0 ) db.session.add(customer) db.session.commit() @@ -59,7 +60,10 @@ def editcustomer(id): if not current_user.can_update: return redirect(url_for('customers.customers')) form = EditCustomerForm(request.form) - customer = Customer.query.filter_by(id=id).first() + customer = Customer.query.filter(Customer.id == id).filter(Customer.is_dormant == 0).first() + if customer is None: + flash(u'Cannot find customer.', 'danger') + return redirect(url_for('customers.customers')) if request.method == 'POST': current_name = customer.name @@ -67,7 +71,7 @@ def editcustomer(id): name_exist = None if current_name != new_name: name_exist = Customer.query.filter_by(name=request.form['first_name'].strip() + ' ' + request.form['last_name'].strip()).first() - if form.validate_on_submit() and name_exist is None: + if form.validate_on_submit() and name_exist is None and customer is not None: customer.first_name = request.form['first_name'] customer.last_name = request.form['last_name'] customer.number_shares = int(request.form['number_shares']) @@ -75,7 +79,7 @@ def editcustomer(id): customer.address = request.form['address'] customer.mobile_phone = request.form['mobile_phone'] db.session.commit() - flash(u'Record successfully saved.', 'success') + #flash(u'Record successfully saved.', 'success') return redirect(url_for('customers.customers')) elif not name_exist is None: flash('Name ' + new_name + ' is already taken. Please choose another name.','danger') @@ -90,7 +94,7 @@ def editcustomer(id): except TemplateNotFound: abort(404) -@customers_blueprint.route("/customers/delete", methods=['POST']) # pragma: no cover) +@customers_blueprint.route("/customers/delete/", methods=['POST']) # pragma: no cover) @login_required def deletecustomer(): from coinage import db @@ -98,9 +102,12 @@ def deletecustomer(): if not current_user.can_delete: return redirect(url_for('customers.customers')) id = request.form['id'] - customer = Customer.query.filter_by(id=id).first() - if not customer is None: - db.session.delete(customer) + customer = Customer.query.filter(Customer.id == id).filter(Customer.is_dormant == 0).first() + if customer is None: + flash(u'Cannot find customer.', 'danger') + return redirect(url_for('customers.customers')) + else: + customer.is_dormant = 1 db.session.commit() flash(u'Record was successfully deleted.', 'success') return redirect(url_for('customers.customers')) diff --git a/db_create.py b/db_create.py index f8e27aa..9022524 100644 --- a/db_create.py +++ b/db_create.py @@ -22,7 +22,7 @@ for i in range(len(first_names)): customer = Customer(first_names[i], last_names[i], i + 1, first_names[i].lower() + "@gmail.com", - first_names[i].lower() + " address", "1234567") + first_names[i].lower() + " address", "1234567", 0) db.session.add(customer) loan = Loan(i+1, 1000, 3, 100, 1030, 100, 930, date.today(), date.today(), date.today(), 0) db.session.add(loan) diff --git a/home/templates/modalform.html b/home/templates/modalform.html index cd5302b..e4d26b6 100644 --- a/home/templates/modalform.html +++ b/home/templates/modalform.html @@ -1,4 +1,4 @@ -