From fb0340abd92544d81558c5d40c707c08ba120a3f Mon Sep 17 00:00:00 2001 From: jvnsoares Date: Tue, 2 Jun 2015 21:15:11 -0300 Subject: [PATCH 1/9] Several files added: forms.py: the forms template hello.py: test file to flask templates: the html templates uses.py: script to populate the database Changes: The path to the database was changed to testes. --- forms.py | 23 ++++++++ hello.py | 108 ++++++++++++++++++++++++++++++++++++ models.py | 2 +- templates/WTF.html | 7 +++ templates/bill.html | 31 +++++++++++ templates/new_user.html | 16 ++++++ templates/show_entries.html | 20 +++++++ templates/test1.html | 34 ++++++++++++ test.py | 28 ++++++++++ users.py | 5 ++ 10 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 forms.py create mode 100644 hello.py create mode 100644 templates/WTF.html create mode 100644 templates/bill.html create mode 100644 templates/new_user.html create mode 100644 templates/show_entries.html create mode 100644 templates/test1.html create mode 100644 test.py create mode 100644 users.py 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/hello.py b/hello.py new file mode 100644 index 0000000..c0be076 --- /dev/null +++ b/hello.py @@ -0,0 +1,108 @@ +## 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 import create_engine +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 ) + + + + +@app.route('/WTF', methods=('GET', 'POST')) +def submit(): + form = MyForm(csrf_enabled=False) + if not form.name.data: + return render_template('WTF.html', form=form) + else: + return render_template('WTF.html', form=form,test2=form.name.data) + +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..89a43ed 100644 --- a/models.py +++ b/models.py @@ -10,7 +10,7 @@ Base = declarative_base() -engine = create_engine('sqlite:////tmp/celcombiller.db') +engine = create_engine('sqlite:////home/vitor/celcombiller/celcombiller.db') Session = sessionmaker(bind=engine) session = Session() diff --git a/templates/WTF.html b/templates/WTF.html new file mode 100644 index 0000000..33c592d --- /dev/null +++ b/templates/WTF.html @@ -0,0 +1,7 @@ +
+ {{ form.csrf_token }} + {{ form.name.label }} {{ form.name(size=20) }} + {{ form.test.label }} {{ form.name(size=20) }} +{% print test2%} + +
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 %} +
+ {{ form.csrf_token }} + User Clid {{ form.userName(size=20) }} + +
+ + + +{% if form.userName.data:%} + + + + {% for i in range(leng) %} + + + + + + + + {% endfor %} + +
Call ID From User To User Answer Time Call Duration
{% print Id[i] %} {% print fromUser[i] %} {% print toUser[i] %} {% print answer[i] %} {% print billSec[i] %}
+{%endif%} +{% print typ %} + +{% endblock %} + + diff --git a/templates/new_user.html b/templates/new_user.html new file mode 100644 index 0000000..436e5d7 --- /dev/null +++ b/templates/new_user.html @@ -0,0 +1,16 @@ +
+ {{ form.csrf_token }} + Clid: {{ form.clid(size=20) }} + New User: {{ form.name(size=20) }} + Balance: {{ form.balance(size=20) }} + +
+ + + +{% if form.clid.data: %} + {% print form.clid.data %} + + {% print form.name.data %} + {% print form.balance.data %} +{% endif %} diff --git a/templates/show_entries.html b/templates/show_entries.html new file mode 100644 index 0000000..92e76a8 --- /dev/null +++ b/templates/show_entries.html @@ -0,0 +1,20 @@ +{% block body %} + {% if session.logged_in %} +
+
+
Title: +
+
Text: +
+
+
+
+ {% endif %} + +{% endblock %} diff --git a/templates/test1.html b/templates/test1.html new file mode 100644 index 0000000..06f55cd --- /dev/null +++ b/templates/test1.html @@ -0,0 +1,34 @@ +{% block body %} +

Login

+{% if error %}

Error: {{ error }}{% endif %} +

+
+
Username: +
+
Password: +
+
+ {{haha}} +
+
+ + {% for variable in range(10) %} + + + + + {% endfor %} +
hahhaha{% print variable %}
+ + {% for i in range(length) %} + + + + + + {% endfor %} + +
{% print names[i] %} {% print clids[i] %} {% print balances[i] %}
+ + +{% endblock %} diff --git a/test.py b/test.py new file mode 100644 index 0000000..d82d39d --- /dev/null +++ b/test.py @@ -0,0 +1,28 @@ +## Flask imports +from flask import Flask,render_template,url_for,request + +# flask-wtf imports +from wtforms.validators import DataRequired + +from forms import billForm,MyForm + + +## sqlalchemy imports +import sqlalchemy +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker + + +## Celcombiller imports +from models import CDR, User,session + +app = Flask(__name__) + +aaa = session.query(CDR).all() + +print aaa[0] + + +bbb= aaa[0] + +print bb.from diff --git a/users.py b/users.py new file mode 100644 index 0000000..493e6e9 --- /dev/null +++ b/users.py @@ -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() From 50d0da1241ca5586edaabf2d14d422e0f009fce9 Mon Sep 17 00:00:00 2001 From: jvnsoares Date: Tue, 2 Jun 2015 21:25:57 -0300 Subject: [PATCH 2/9] Useless files deleted hello.py renomed to main.py --- hello.py => main.py | 0 templates/WTF.html | 7 ------- templates/test1.html | 34 ---------------------------------- test.py | 28 ---------------------------- 4 files changed, 69 deletions(-) rename hello.py => main.py (100%) delete mode 100644 templates/WTF.html delete mode 100644 templates/test1.html delete mode 100644 test.py diff --git a/hello.py b/main.py similarity index 100% rename from hello.py rename to main.py diff --git a/templates/WTF.html b/templates/WTF.html deleted file mode 100644 index 33c592d..0000000 --- a/templates/WTF.html +++ /dev/null @@ -1,7 +0,0 @@ -
- {{ form.csrf_token }} - {{ form.name.label }} {{ form.name(size=20) }} - {{ form.test.label }} {{ form.name(size=20) }} -{% print test2%} - -
diff --git a/templates/test1.html b/templates/test1.html deleted file mode 100644 index 06f55cd..0000000 --- a/templates/test1.html +++ /dev/null @@ -1,34 +0,0 @@ -{% block body %} -

Login

-{% if error %}

Error: {{ error }}{% endif %} -

-
-
Username: -
-
Password: -
-
- {{haha}} -
-
- - {% for variable in range(10) %} - - - - - {% endfor %} -
hahhaha{% print variable %}
- - {% for i in range(length) %} - - - - - - {% endfor %} - -
{% print names[i] %} {% print clids[i] %} {% print balances[i] %}
- - -{% endblock %} diff --git a/test.py b/test.py deleted file mode 100644 index d82d39d..0000000 --- a/test.py +++ /dev/null @@ -1,28 +0,0 @@ -## Flask imports -from flask import Flask,render_template,url_for,request - -# flask-wtf imports -from wtforms.validators import DataRequired - -from forms import billForm,MyForm - - -## sqlalchemy imports -import sqlalchemy -from sqlalchemy import create_engine -from sqlalchemy.orm import sessionmaker - - -## Celcombiller imports -from models import CDR, User,session - -app = Flask(__name__) - -aaa = session.query(CDR).all() - -print aaa[0] - - -bbb= aaa[0] - -print bb.from From 1f5f9a9aee10d419bfd3157821bb6f6422ed55d1 Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Tue, 2 Jun 2015 21:29:28 -0300 Subject: [PATCH 3/9] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5bd7891..1c34de2 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 model.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 From d5d4d48f6171f606bd0746053c214189b535a025 Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Tue, 2 Jun 2015 21:30:09 -0300 Subject: [PATCH 4/9] Update models.py --- models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/models.py b/models.py index 89a43ed..878bc03 100644 --- a/models.py +++ b/models.py @@ -10,6 +10,7 @@ Base = declarative_base() +## Database path. engine = create_engine('sqlite:////home/vitor/celcombiller/celcombiller.db') Session = sessionmaker(bind=engine) session = Session() From 66f24cd7dc92811fceb01ba093c237b8653d1a03 Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Tue, 2 Jun 2015 21:31:52 -0300 Subject: [PATCH 5/9] Update main.py --- main.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/main.py b/main.py index c0be076..0f3feff 100644 --- a/main.py +++ b/main.py @@ -89,16 +89,6 @@ def bill(): return render_template('bill.html',form = form,Id = Id, fromUser = fromUser, answer = answer , billSec = billSec, toUser = toUser,leng = leng ) - - -@app.route('/WTF', methods=('GET', 'POST')) -def submit(): - form = MyForm(csrf_enabled=False) - if not form.name.data: - return render_template('WTF.html', form=form) - else: - return render_template('WTF.html', form=form,test2=form.name.data) - if __name__ == '__main__': app.debug = True From ff4210118cd35cf02711bd5df1550cb6fcc92d3d Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Wed, 8 Jul 2015 08:44:28 -0300 Subject: [PATCH 6/9] Create config.py Configuration archive --- config.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 config.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..57013a2 --- /dev/null +++ b/config.py @@ -0,0 +1 @@ +engine = create_engine('sqlite://PATH TO THE DATABASE') From c0672266cbcf19ac16eabcd1342d2f07e63f5339 Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Wed, 8 Jul 2015 08:45:06 -0300 Subject: [PATCH 7/9] Update models.py Added import config --- models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/models.py b/models.py index 878bc03..ba6b124 100644 --- a/models.py +++ b/models.py @@ -7,11 +7,13 @@ ForeignKey, DateTime from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker, relationship +## Database path. +from config import engine + Base = declarative_base() -## Database path. -engine = create_engine('sqlite:////home/vitor/celcombiller/celcombiller.db') + Session = sessionmaker(bind=engine) session = Session() From abcc8fc743cc200a165c0d4f583061186ed54604 Mon Sep 17 00:00:00 2001 From: Vitor Nunes Date: Wed, 8 Jul 2015 08:45:58 -0300 Subject: [PATCH 8/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c34de2..08f1253 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ $ pip install --allow-external pyst --allow-unverified pyst -r requirements.txt ## Database setup -You have to change the database path in the file model.py +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. From 8964c30128e9354c41a31dd5f8c8fa723e59dbe4 Mon Sep 17 00:00:00 2001 From: jvnsoares Date: Wed, 8 Jul 2015 09:01:04 -0300 Subject: [PATCH 9/9] Path to the database moved to config.py --- config.py | 1 + main.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py b/config.py index 57013a2..e98d122 100644 --- a/config.py +++ b/config.py @@ -1 +1,2 @@ +from sqlalchemy import create_engine engine = create_engine('sqlite://PATH TO THE DATABASE') diff --git a/main.py b/main.py index 0f3feff..8aafada 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,6 @@ ## sqlalchemy imports import sqlalchemy -from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker,aliased