Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from sqlalchemy import create_engine
engine = create_engine('sqlite://PATH TO THE DATABASE')
23 changes: 23 additions & 0 deletions forms.py
Original file line number Diff line number Diff line change
@@ -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()])

97 changes: 97 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -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')


5 changes: 4 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
31 changes: 31 additions & 0 deletions templates/bill.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<html>
{% block body %}
<form method="POST" action="/bill">
{{ form.csrf_token }}
User Clid {{ form.userName(size=20) }}
<input type="submit" value="Go">
</form>



{% if form.userName.data:%}
<table border='1'>
<tr><td> Call ID </td> <td> From User </td> <td> To User </td> <td> Answer Time </td> <td> Call Duration </td>

{% for i in range(leng) %}
<tr>
<td>{% print Id[i] %} </td>
<td>{% print fromUser[i] %} </td>
<td>{% print toUser[i] %} </td>
<td>{% print answer[i] %} </td>
<td>{% print billSec[i] %} </td>
</tr>
{% endfor %}

</table>
{%endif%}
{% print typ %}

{% endblock %}

</html>
16 changes: 16 additions & 0 deletions templates/new_user.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<form method="POST" action="/new_user">
{{ form.csrf_token }}
Clid: {{ form.clid(size=20) }}
New User: {{ form.name(size=20) }}
Balance: {{ form.balance(size=20) }}
<input type="submit" value="Ir">
</form>



{% if form.clid.data: %}
{% print form.clid.data %}

{% print form.name.data %}
{% print form.balance.data %}
{% endif %}
20 changes: 20 additions & 0 deletions templates/show_entries.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_entry') }}" method=post class=add-entry>
<dl>
<dt>Title:
<dd><input type=text size=30 name=title>
<dt>Text:
<dd><textarea name=text rows=5 cols=40></textarea>
<dd><input type=submit value=Share>
</dl>
</form>
{% endif %}
<ul class=entries>
{% for entry in entries %}
<li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}
{% else %}
<li><em>Unbelievable. No entries here so far</em>
{% endfor %}
</ul>
{% endblock %}
5 changes: 5 additions & 0 deletions users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from models import session, User
session.add_all([User(clid='1000', name='PSB', balance=100),
User(clid='2000', name='LaPS', balance=100)
])
session.commit()