Skip to content

Commit

Permalink
commit changes in deleting customers
Browse files Browse the repository at this point in the history
  • Loading branch information
marzim committed Feb 16, 2017
1 parent c4e83f9 commit 75f6459
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
4 changes: 3 additions & 1 deletion customers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ 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
self.number_shares = number_shares
self.email = email
self.address = address
self.mobile_phone = mobile_phone
self.is_dormant = is_dormant

2 changes: 1 addition & 1 deletion customers/templates/customers.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</a>
{% endif %}
{% if current_user.can_delete : %}
<a href="#ConfirmDeleteModal" role="button" data-toggle="modal" class='confirmDelete' data-url="/customers/delete" data-id="{{ customer.id }}" data-name="{{ customer.first_name }} {{ customer.last_name }}" title="Delete Customer: {{ customer.first_name }} {{ customer.last_name }}">
<a href="#ConfirmDeleteModal" role="button" data-toggle="modal" class='confirmDelete' data-url="/customers/delete/" data-id="{{ customer.id }}" data-name="{{ customer.first_name }} {{ customer.last_name }}" title="Delete Customer: {{ customer.first_name }} {{ customer.last_name }}">
<span class="glyphicon glyphicon-trash"></span>
</a>
{% endif %}
Expand Down
25 changes: 16 additions & 9 deletions customers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -59,23 +60,26 @@ 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
new_name = request.form['first_name'] + ' ' + request.form['last_name']
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'])
customer.email = request.form['email']
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')
Expand All @@ -90,17 +94,20 @@ 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
from models import Customer
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'))
2 changes: 1 addition & 1 deletion db_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion home/templates/modalform.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="withdrawSavingsModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="withdrawModalLabel" aria-hidden="true">
<div id="withdrawSavingsModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="withdrawModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
Expand All @@ -21,6 +21,7 @@ <h3 id="withdrawModalLabel">Withdraw Savings?</h3>
</div>
</div>
</div>

<div id="ConfirmDeleteModal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="confirmDeleteLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
Expand Down
1 change: 1 addition & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from coinage import app

if __name__ == '__main__':
#app.run(threaded=True)
app.run(debug=True)

4 changes: 4 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def edituser(id):
if not current_user.can_update:
return redirect(url_for('users.users'))
user = User.query.filter_by(id=id).first()
if user is None:
flash(u'Cannot find user.', 'danger')
return redirect(url_for('users.users'))

form = EditForm()
form.username.data = user.name
form.email.data = user.email
Expand Down

0 comments on commit 75f6459

Please sign in to comment.