Skip to content

Commit

Permalink
commit a lot changes from db relationship and CRUP for loans
Browse files Browse the repository at this point in the history
  • Loading branch information
marzim committed Feb 21, 2017
1 parent 4d6cc4b commit 899a130
Show file tree
Hide file tree
Showing 22 changed files with 323 additions and 137 deletions.
2 changes: 1 addition & 1 deletion accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def login():
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
user = User.query.filter_by(name=request.form['username']).first()
user = User.query.filter_by(name=request.form['username'].strip()).first()
if user is not None and bcrypt.check_password_hash(
user.password, request.form['password']
):
Expand Down
7 changes: 4 additions & 3 deletions coinage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from flask_bcrypt import Bcrypt
from flask_login import LoginManager


app = Flask(__name__)
bcrypt = Bcrypt(app)
login_manager = LoginManager()
login_manager.init_app(app)
app.config.from_object('config.ProductionConfig')
app.config.from_object('config.DevelopmentConfig')
app.config['SQLALCHEMY_POOL_RECYCLE'] = 299
app.config['SQLALCHEMY_POOL_TIMEOUT'] = 20
db = SQLAlchemy(app)
Expand All @@ -17,8 +18,8 @@
from contributions.views import contributions_blueprint
from summary.views import summary_blueprint
from loans.views import loans_blueprint
from interestearned.views import interestearned_blueprint
from customers.views import customers_blueprint
from interestearned.views import interestearned_blueprint
from notes.views import notes_blueprint
from guidelines.views import guidelines_blueprint
from accounts.views import accounts_blueprint
Expand All @@ -29,8 +30,8 @@
app.register_blueprint(home_blueprint)
app.register_blueprint(summary_blueprint)
app.register_blueprint(loans_blueprint)
app.register_blueprint(interestearned_blueprint)
app.register_blueprint(customers_blueprint)
app.register_blueprint(interestearned_blueprint)
app.register_blueprint(notes_blueprint)
app.register_blueprint(accounts_blueprint)
app.register_blueprint(users_blueprint)
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# default configuration

class BaseConfig(object):
DEBUG = False
#DEBUG = False
SECRET_KEY = 'p\xfa\xa6\x0fM\x7f\xb4o\xae\xb1\x1dv\x82\xacky\xd6\xe4\x93\x0b\x00\x86\xc6&'
SQLALCHEMY_DATABASE_URI = os.environ.get('FLASK_DATABASE_URL')

Expand Down
8 changes: 5 additions & 3 deletions customers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ 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)
loans = db.relationship('Loan', backref='customer', lazy='dynamic')

def __init__(self, first_name=None, last_name=None, number_shares=0, email=None, address=None, mobile_phone=None, is_dormant=0):
def __init__(self, first_name=None, last_name=None, number_shares=0, email=None, address=None, mobile_phone=None):
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

def __repr__(self):
return self.name

6 changes: 3 additions & 3 deletions customers/templates/customers.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@
{% if current_user.can_update or current_user.can_delete: %}
<td align="center">
{% if current_user.can_update: %}
<a href="/customers/edit/{{ customer.id }}/" data-toggle="tooltip" title="Edit Customer: {{ customer.first_name }} {{ customer.last_name }}">
<a href="/customers/edit/{{ customer.id }}/" data-toggle="tooltip" title="Edit Customer: {{ customer.name }}">
<span class="glyphicon glyphicon-pencil"></span>
</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.name }}" title="Delete Customer: {{ customer.name }}">
<span class="glyphicon glyphicon-trash"></span>
</a>
{% endif %}
</td>
{% endif %}
<td>{{ customer.first_name }} {{ customer.last_name }}</td>
<td>{{ customer }}</td>
<td>{{ customer.number_shares }}</td>
<td>{{ customer.email }}</td>
<td>{{ customer.mobile_phone }}</td>
Expand Down
15 changes: 7 additions & 8 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.filter(Customer.is_dormant == 0).order_by(Customer.first_name)
query_customers = Customer.query.order_by(Customer.name.asc())
return render_template('customers.html', query_customers=query_customers)
except TemplateNotFound:
abort(404)
Expand All @@ -29,16 +29,15 @@ def newcustomer():
if request.method == 'GET':
form.number_shares.data = 0
if form.validate_on_submit():
customer = Customer.query.filter(Customer.name==form.first_name.data.strip()+ ' ' + form.last_name.data.strip()).filter(Customer.is_dormant == 0).first()
customer = Customer.query.filter(Customer.name==form.first_name.data.strip()+ ' ' + form.last_name.data.strip()).first()
if customer is None:
customer = Customer(
first_name=form.first_name.data.strip(),
last_name=form.last_name.data.strip(),
number_shares=form.number_shares.data,
email=form.email.data.strip(),
address=form.address.data.strip(),
mobile_phone=form.mobile_phone.data.strip(),
is_dormant=0
mobile_phone=form.mobile_phone.data.strip()
)
db.session.add(customer)
db.session.commit()
Expand All @@ -59,7 +58,7 @@ def editcustomer(id):
if not current_user.can_update:
return redirect(url_for('customers.customers'))
form = EditCustomerForm(request.form)
customer = Customer.query.filter(Customer.id == id).filter(Customer.is_dormant == 0).first()
customer = Customer.query.filter_by(id=id).first()
if customer is None:
flash(u'Cannot find customer.', 'danger')
return redirect(url_for('customers.customers'))
Expand All @@ -69,7 +68,7 @@ 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(Customer.name==request.form['first_name'].strip() + ' ' + request.form['last_name'].strip()).filter(Customer.is_dormant == 0).first()
name_exist = Customer.query.filter(Customer.name==request.form['first_name'].strip() + ' ' + request.form['last_name'].strip()).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']
Expand Down Expand Up @@ -102,12 +101,12 @@ def deletecustomer():
if not current_user.can_delete:
return redirect(url_for('customers.customers'))
id = request.form['id']
customer = Customer.query.filter(Customer.id == id).filter(Customer.is_dormant == 0).first()
customer = Customer.query.filter_by(id=id).first()
if customer is None:
flash(u'Cannot find customer.', 'danger')
return redirect(url_for('customers.customers'))
else:
customer.is_dormant = 1
db.session.delete(customer)
db.session.commit()
flash(u'Record was successfully deleted.', 'success')
return redirect(url_for('customers.customers'))
10 changes: 5 additions & 5 deletions db_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
# create the database and the db table
db.create_all()
#insert
user = User(1, 1, 1, "guest","[email protected]", "jasper", 0)
user = User(1, 1, 1, "guest","[email protected]", "jasper")
db.session.add(user)
first_names = [ 'test', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7']
last_names = [ 'ting', 'ting2', 'ting3', 'ting4', 'ting5', 'ting6', 'ting7']

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", 0)
db.session.add(customer)
loan = Loan(i+1, 1000, 3, 100, 1030, 100, 930, date.today(), date.today(), date.today(), 0)
c = Customer(first_names[i], last_names[i], i + 1, first_names[i].lower() + "@gmail.com",
first_names[i].lower() + " address", "1234567")
db.session.add(c)
loan = Loan(customer=c, amount=1000, interest=3, payment=100, total_payable=1030, total_payment=100, outstanding_balance=930, fully_paid_on=date.today(), date_release=date.today(), date_due=date.today())
db.session.add(loan)


Expand Down
5 changes: 3 additions & 2 deletions home/templates/modalform.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ <h2 id="confirmDeleteLabel" align="center"><span class="label label-danger">Dele
</div>
<form id="deleteForm" method="POST">
<div class="modal-body" align="center">
<h4><label for="name">Are you sure want to delete this record?</label></h4>
<h4><label for="name">Are you sure want to delete this record?</label></h4>
<input type="hidden" name="id" id="id" value="" />
<input type="hidden" name="url" id="url" value="" />
<br>

<h4><label name="name" id="name" text=""/></h4>
<label name="instructions" id="instructions" text="" style="color:red"/>
</div>
<div align="center">
<button data-dismiss="modal" aria-hidden="true" class="btn btn-primary btn-lg">No</button>
Expand Down
35 changes: 14 additions & 21 deletions loans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,17 @@ class AddForm(Form):
outstanding_balance = FloatField('outstanding_balance')

class EditForm(Form):
first_name = StringField(
'first_name',
validators=[DataRequired(), Length(min=3, max=25)]
)
last_name = StringField(
'last_name',
validators=[DataRequired(), Length(min=3, max=25)]
)
number_shares = IntegerField('number_shares')
email = StringField(
'email',
validators=[DataRequired(), Email(message=None), Length(min=6, max=40)]
)
address = StringField(
'address',
validators=[DataRequired(), Length(min=3, max=25)]
)
mobile_phone = StringField(
'mobile_phone',
validators=[DataRequired(), Length(min=3, max=25)]
)
customer_name = SelectField(u'Customers', coerce=int)

date_release = StringField(
'date_release')
date_due = StringField(
'date_release')
fully_paid_on = StringField(
'fully_paid_on')
amount = FloatField('amount')
interest = SelectField(u'Interest', coerce=int)
total_payable = FloatField('total_payable')
payment = FloatField('payment')
total_payment = FloatField('total_payment')
outstanding_balance = FloatField('outstanding_balance')
27 changes: 12 additions & 15 deletions loans/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Loan(db.Model):
__tablename__ = "loans"

id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey("customers.id"))
amount = db.Column(db.Float, unique=False)
interest = db.Column(db.Float, unique=False)
payment = db.Column(db.Float, unique=False)
Expand All @@ -14,21 +13,19 @@ class Loan(db.Model):
fully_paid_on = db.Column(db.String(80))
date_release = db.Column(db.String(80))
date_due = db.Column(db.String(80))
is_dormant = db.Column(db.Boolean)
customer = db.relationship('Customer', backref='customers')
customer_id = db.Column(db.Integer, db.ForeignKey("customers.id"))

def __init__(self, customer_id=0, amount=0, interest=0, payment=0, total_payable=0, total_payment=0, outstanding_balance=0, fully_paid_on=None, date_release=None, date_due=None, is_dormant=0):
self.customer_id = customer_id
self.amount = amount
self.interest = interest
self.payment = payment
self.total_payable = total_payable
self.total_payment = total_payment
self.outstanding_balance = outstanding_balance
self.fully_paid_on = fully_paid_on
self.date_release = date_release
self.date_due = date_due
self.is_dormant = is_dormant
# def __init__(self, customer_id=0, amount=0, interest=0, payment=0, total_payable=0, total_payment=0, outstanding_balance=0, fully_paid_on=None, date_release=None, date_due=None):
# self.customer_id = customer_id
# self.amount = amount
# self.interest = interest
# self.payment = payment
# self.total_payable = total_payable
# self.total_payment = total_payment
# self.outstanding_balance = outstanding_balance
# self.fully_paid_on = fully_paid_on
# self.date_release = date_release
# self.date_due = date_due

class Interest(db.Model):
__tablename__ = "interest"
Expand Down
4 changes: 2 additions & 2 deletions loans/templates/addloan.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="col-6 col-sm-6 col-lg-4">
<label>Customer Name</label>
<p>
{{ form.customer_name(type="text", autofocus="True", class="form-control") }}
{{ render_field(form.customer_name, type="text", autofocus="True", class="form-control") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
Expand Down Expand Up @@ -58,7 +58,7 @@
<div class="col-6 col-sm-6 col-lg-4">
<label>Interest per Month</label>
<p>
{{ form.interest(type="text", class="form-control", style="width:75%;") }}
{{ render_field(form.interest, type="text", class="form-control", style="width:75%;") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
Expand Down
83 changes: 76 additions & 7 deletions loans/templates/editloan.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
<div>
<ul class="nav nav-tabs actions-nav">
<li>
<a href="/users" title="List of Loans">List</a>
<a href="/loans/" title="List of Loans">List</a>
</li>
{% if current_user.can_create: %}
<li>
<a href="/users/create" title="Create New Loan">Create</a>
<a href="/loans/new/" title="Create New Loan">Create</a>
</li>
{% endif %}
{% if current_user.can_update: %}
Expand All @@ -27,13 +27,82 @@
</li>
{% endif %}
</ul>
<form class="form-signin form-horizontal col-md-6" role="form" method="post" action="">
<form class="well" role="form" method="post" action="">
{{ form.csrf_token }}
<p>
{{ form.name(type="text", autofocus="True", class="form-control") }}
</p>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<label>Customer Name</label>
<p>
{{ render_field(form.customer_name, type="text", autofocus="True", class="form-control") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
<label>Date Release</label>
<div class="input-group date" id="dp3" style="width:75%;" data-date-format="mm-dd-yyyy">
<input class="form-control input-sm" type="text" readonly="" name="date_rel" id="date_rel" value="" >
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<label>Amount</label>
<p>
{{ render_field(form.amount, type="text", class="form-control") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
<label>Date Due</label>
<div class="input-group date" id="divdate_due" style="width:75%;" data-date-format="mm-dd-yyyy">
<input class="form-control input-sm" type="text" readonly="" name="date_due" id="date_due" >
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
</div>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<label>Interest per Month</label>
<p>
{{ render_field(form.interest, type="text", class="form-control", style="width:75%;") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
<label>Total Payable</label>
{{ render_field(form.total_payable, type="text", class="form-control", style="width:75%;") }}
</div>
</div>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<label>Payment</label>
<p>
{{ form.payment(type="text", class="form-control", style="width:75%;") }}
</p>
</div>
<div class="col-6 col-sm-6 col-lg-4">
<label>Total Payment</label>
{{ render_field(form.total_payment, type="text", class="form-control", style="width:75%;") }}

<button class="btn btn-lg btn-success btn-block" type="submit">Save</button>
</div>
</div>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<label>Fully Paid On</label>
<div class="input-group date" id="divdate_fullypaid" style="width:75%;" data-date-format="mm-dd-yyyy">
<input class="form-control input-sm" type="text" readonly="" name="date_fullypaid" id="date_fullypaid" value="" >
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
</div>
</div>
<div class="col-6 col-sm-6 col-lg-4">
<label>Outstanding Balance</label>
{{ render_field(form.outstanding_balance, type="text", class="form-control", style="width:75%;") }}
</div>
</div>
<div class="row">
<div class="col-6 col-sm-6 col-lg-4">
<br />
<button class="btn btn-lg btn-success btn-block" type="submit">Save</button>
</div>
</div>
</form>
{% endblock %}
</div>
Loading

0 comments on commit 899a130

Please sign in to comment.