diff --git a/README.md b/README.md index 5bd7891..08f1253 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,8 @@ $ pip install --allow-external pyst --allow-unverified pyst -r requirements.txt ## Database setup +You have to change the database path in the file config.py + Each SIP user must be inserted in the database of users with a balance. The python code snippet bellow is an example of a insertion of two users. ```python diff --git a/config.py b/config.py new file mode 100644 index 0000000..e98d122 --- /dev/null +++ b/config.py @@ -0,0 +1,2 @@ +from sqlalchemy import create_engine +engine = create_engine('sqlite://PATH TO THE DATABASE') diff --git a/forms.py b/forms.py new file mode 100644 index 0000000..8f288b3 --- /dev/null +++ b/forms.py @@ -0,0 +1,23 @@ +from flask_wtf import Form +from wtforms import StringField +from wtforms.validators import DataRequired +from wtforms.fields import PasswordField,IntegerField + + +#### Form functions. + +##Test forms. +class MyForm(Form): + name = StringField('name',validators=[DataRequired()]) + test= PasswordField('test',validators=[DataRequired()]) + +##Bill form. +class bill_form(Form): + userName=StringField('userName',validators=[DataRequired()]) + +##Form to insert new users. +class new_user_form(Form): + clid = IntegerField('clid',validators=[DataRequired()]) + name = StringField('name',validators=[DataRequired()]) + balance = IntegerField('balance',validators=[DataRequired()]) + diff --git a/main.py b/main.py new file mode 100644 index 0000000..8aafada --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +## Flask imports +from flask import Flask,render_template,url_for,request + +# flask-wtf imports +from wtforms.validators import DataRequired + +from forms import bill_form,MyForm,new_user_form + + +## sqlalchemy imports +import sqlalchemy +from sqlalchemy.orm import sessionmaker,aliased + + + +## Celcombiller imports +from models import CDR, User,session + +app = Flask(__name__) +app.secret_key = 'test' + +#### Flask functions +@app.route('/') +def main(): + return "test" + +@app.route('/new_user', methods=('GET','POST')) +def new_user(): + + form = new_user_form() + + if form.clid.data: + ## TODO: Handle when try to add an already exist user. + ## TODO: Add a confirmation page. + try: + session.add(User(clid=form.clid.data,\ + name=form.name.data,\ + balance=form.balance.data)) + session.commit() + + except BaseException: + pass + + return render_template('new_user.html',form=form) + + +##Billing page. +@app.route('/bill',methods=('GET','POST')) +def bill(): + + form = bill_form() + + ## Check if the form was filled. + if not form.userName.data: + return render_template('bill.html',form=form) + + else: + Id = [] + fromUser =[] + answer = [] + billSec = [] + toUser = [] + ##alias to user + User2 = aliased(User, name='User2') + + ##Using for was the best way I found to recieve the query result, other way would be multiple queries, one to eache wanted value. + if form.userName.data==" ": + + for call in session.query(CDR,User,User2).join(User,User.id_ == CDR.from_user_id).join(User2, CDR.to_user_id == User2.id_) : + Id.append(call.CDR.id_) + fromUser.append( call.User.clid) + toUser.append( call.User2.clid) + answer.append ( call.CDR.answer) + billSec.append ( call.CDR.billsec ) + + else: + + for call in session.query(CDR,User,User2).join(User,User.id_ == CDR.from_user_id).join(User2, CDR.to_user_id == User2.id_).\ + filter( User.clid == form.userName.data ): + Id.append(call.CDR.id_) + fromUser.append( call.User.clid) + toUser.append( call.User2.clid) + answer.append ( call.CDR.answer) + billSec.append ( call.CDR.billsec ) + + leng = len(Id) + + return render_template('bill.html',form = form,Id = Id, fromUser = fromUser, answer = answer , billSec = billSec, toUser = toUser,leng = leng ) + + +if __name__ == '__main__': + app.debug = True + +## app.run() + app.run(host='0.0.0.0') + + diff --git a/models.py b/models.py index a743052..ba6b124 100644 --- a/models.py +++ b/models.py @@ -7,10 +7,13 @@ ForeignKey, DateTime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, relationship +## Database path. +from config import engine + Base = declarative_base() -engine = create_engine('sqlite:////tmp/celcombiller.db') + Session = sessionmaker(bind=engine) session = Session() diff --git a/templates/bill.html b/templates/bill.html new file mode 100644 index 0000000..784a45d --- /dev/null +++ b/templates/bill.html @@ -0,0 +1,31 @@ + +{% block body %} +
+ + + +{% if form.userName.data:%} +| Call ID | From User | To User | Answer Time | Call Duration | + + {% for i in range(leng) %} +
| {% print Id[i] %} | +{% print fromUser[i] %} | +{% print toUser[i] %} | +{% print answer[i] %} | +{% print billSec[i] %} | +