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 17, 2017
1 parent 7da177c commit 4d6cc4b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
10 changes: 5 additions & 5 deletions customers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ def newcustomer():
if not current_user.can_create:
return redirect(url_for('customers.customers'))
form = AddForm()
error = None
if request.method == 'GET':
form.number_shares.data = 0
if form.validate_on_submit():
customer = Customer.query.filter_by(name=form.first_name.data.strip()+ ' ' + form.last_name.data.strip()).first()
customer = Customer.query.filter(Customer.name==form.first_name.data.strip()+ ' ' + form.last_name.data.strip()).filter(Customer.is_dormant == 0).first()
if customer is None:
customer = Customer(
first_name=form.first_name.data.strip(),
Expand All @@ -47,7 +46,7 @@ def newcustomer():
return redirect(url_for('customers.customers'))
else:
flash('Name ' + form.first_name.data.strip()+ ' ' + form.last_name.data.strip() + ' is already taken. Please choose another name.', 'danger')
return render_template('newcustomer.html', form=form, error=error)
return render_template('newcustomer.html', form=form)
except TemplateNotFound:
abort(404)

Expand All @@ -70,10 +69,11 @@ def editcustomer(id):
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 and customer is not None:
name_exist = Customer.query.filter(Customer.name==request.form['first_name'].strip() + ' ' + request.form['last_name'].strip()).filter(Customer.is_dormant == 0).first()
if form.validate_on_submit() and name_exist is None:
customer.first_name = request.form['first_name']
customer.last_name = request.form['last_name']
customer.name = customer.first_name + ' ' + customer.last_name
customer.number_shares = int(request.form['number_shares'])
customer.email = request.form['email']
customer.address = request.form['address']
Expand Down
2 changes: 1 addition & 1 deletion db_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# create the database and the db table
db.create_all()
#insert
user = User(1, 1, 1, "guest","[email protected]", "jasper")
user = User(1, 1, 1, "guest","[email protected]", "jasper", 0)
db.session.add(user)
first_names = [ 'test', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7']
last_names = [ 'ting', 'ting2', 'ting3', 'ting4', 'ting5', 'ting6', 'ting7']
Expand Down
10 changes: 6 additions & 4 deletions users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ class User(db.Model):
__tablename__ = "users"

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
name = db.Column(db.String(80), unique=False)
email = db.Column(db.String(150), unique=False)
password = db.Column(db.String(150))
can_create = db.Column(db.Integer)
can_update = db.Column(db.Integer)
can_delete = db.Column(db.Integer)
is_dormant = db.Column(db.Boolean)

def __init__(self, can_create=0, can_update=0, can_delete=0, name=None, email=None, password=None):
self.set_property(can_create, can_update, can_delete, name, email, password)
def __init__(self, can_create=0, can_update=0, can_delete=0, name=None, email=None, password=None, is_dormant=0):
self.set_property(can_create, can_update, can_delete, name, email, password, is_dormant)

def set_property(self, can_create=0, can_update=0, can_delete=0, name=None, email=None, password=None):
def set_property(self, can_create=0, can_update=0, can_delete=0, name=None, email=None, password=None, is_dormant=0):
self.name = name
self.email = email
self.password = bcrypt.generate_password_hash(password)
self.can_create = can_create
self.can_update = can_update
self.can_delete = can_delete
self.is_dormant = is_dormant

def is_authenticated(self):
return True
Expand Down
10 changes: 5 additions & 5 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def users():
from models import User
try:
query_users = User.query.filter(User.name != current_user.name).order_by(User.name)
query_users = User.query.filter(User.name != current_user.name).filter(User.is_dormant == 0).order_by(User.name)
return render_template('users.html', query_users=query_users)
except TemplateNotFound:
abort(404)
Expand All @@ -24,7 +24,7 @@ def edituser(id):
from models import User
if not current_user.can_update:
return redirect(url_for('users.users'))
user = User.query.filter_by(id=id).first()
user = User.query.filter(User.id==id).filter(User.is_dormant==0).first()
if user is None:
flash(u'Cannot find user.', 'danger')
return redirect(url_for('users.users'))
Expand Down Expand Up @@ -54,9 +54,9 @@ def deleteuser():
if not current_user.can_delete:
return redirect(url_for('users.users'))
id = request.form['id']
user = User.query.filter_by(id=id).first()
user = User.query.filter(User.id==id).filter(User.is_dormant==0).first()
if not user is None:
db.session.delete(user)
user.is_dormant = 1
db.session.commit()
flash(u'Record was successfully deleted.', 'success')
return redirect(url_for('users.users'))
Expand All @@ -74,7 +74,7 @@ def add():
form.can_update.data = 0
error = None
if form.validate_on_submit():
user = User.query.filter_by(name=form.username.data.strip()).first()
user = User.query.filter(User.name==form.username.data.strip()).filter(User.is_dormant==0).first()
if user is None:
user = User(
name=form.username.data.strip(),
Expand Down

0 comments on commit 4d6cc4b

Please sign in to comment.