Skip to content
This repository was archived by the owner on May 20, 2018. It is now read-only.

Commit 67d4628

Browse files
committed
Merge pull request #160 from makalaaneesh/master
In reference to #156 . Added a TODO view
2 parents 8118e10 + ed57971 commit 67d4628

File tree

7 files changed

+211
-43
lines changed

7 files changed

+211
-43
lines changed

app/main/views.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,16 @@ def edit(id):
182182
db.session.add(old_item)
183183
db.session.commit()
184184

185+
186+
187+
# adding the id tags to the html elements
188+
todo_objs = Todo.query.filter_by(note_id = note.id).all()
189+
todo_ids = [item.id for item in todo_objs]
190+
185191
body_html_list = note.body_html.split("\n")
186192
for i, element in enumerate(body_html_list):
187193
if '<li class="task-list-item">' in element:
188-
todo_id = Todo.get_todo_item_id_from_li(element)
194+
todo_id = Todo.get_todo_item_id(element, todo_objs)
189195
new_element = Todo.add_id_to_li_element(element, str(todo_id))
190196
# count = count + 1
191197
body_html_list[i] = new_element
@@ -368,6 +374,7 @@ def checkuncheck():
368374
new_body_html = request.form["body_html"]
369375
todo_item = request.form["todo_item"]
370376
todo_item_id = request.form["todo_item_id"]
377+
property_ = request.form["property"]
371378

372379
note = Note.query.get_or_404(note_id)
373380
if current_user != note.author:
@@ -381,7 +388,12 @@ def checkuncheck():
381388
db.session.add(note)
382389
todo = Todo.query.get_or_404(todo_item_id)
383390
todo.updated_date = datetime.now()
384-
todo.is_checked = not todo.is_checked
391+
if property_ == "check":
392+
todo.is_checked = True
393+
else:
394+
todo.is_checked = False
395+
if todo.checked_date:
396+
todo.checked_date = None
385397
if todo.is_checked is True:
386398
todo.checked_date = datetime.now()
387399
db.session.add(todo)
@@ -390,6 +402,20 @@ def checkuncheck():
390402
return jsonify(**results)
391403

392404

405+
@main.route('/todolist', methods=['GET'])
406+
@login_required
407+
def todolist():
408+
notes = Note.query.filter_by(
409+
author_id=current_user.id,
410+
is_deleted=False).order_by(
411+
Note.updated_date.asc()).all()
412+
if len(notes) == 0:
413+
notes = None
414+
flash("Your TODO list is empty!")
415+
return render_template('app/todolist.html', notes=notes)
416+
417+
418+
393419
@main.route('/shutdown')
394420
def server_shutdown():
395421
if not current_app.testing:

app/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Note(db.Model):
219219
is_favorite = db.Column(db.Boolean, default=False)
220220

221221
todo_items = db.relationship(
222-
"Todo", backref="note", cascade="all")
222+
"Todo", backref="note", cascade="all, delete-orphan")
223223
tags = db.relationship(
224224
"Tag", secondary=note_tag,
225225
backref="Note", passive_deletes=True)
@@ -347,12 +347,14 @@ def add_id_to_li_element(li, id):
347347
return new_li
348348

349349
@staticmethod
350-
def get_todo_item_id_from_li(li):
350+
def get_todo_item_id(li, todo_objs):
351351
li = li.encode("utf-8")
352352
element = html.fromstring(li.strip())
353353
todo_item = element.text_content()
354-
todo = Todo.query.filter_by(title=todo_item).first()
355-
return todo.id
354+
for item in todo_objs:
355+
if item.title == todo_item:
356+
return item.id
357+
return -1
356358

357359
@staticmethod
358360
def toggle_checked_property_markdown(markdown_body, item):

app/static/js/braindump.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
11
$(function () {
22
$('[data-toggle="tooltip"]').tooltip()
33
})
4+
5+
function checkUncheck (checkbox) {
6+
// body...
7+
var property;
8+
if ($(checkbox).is(':checked') == false){
9+
$(checkbox).removeAttr("checked");
10+
property = "uncheck";
11+
}
12+
else{
13+
$(checkbox).attr("checked","");
14+
property="check";
15+
}
16+
17+
var notediv = $(checkbox).parents(".tab-pane");
18+
19+
var note_id = ($(notediv).attr('id'));// note id
20+
var todo_item = ($(checkbox).parent().text()); // todo item text
21+
var content_div = $(checkbox).parents(".note-content");
22+
var body_html = content_div.html();// body_html of the note
23+
var todo_item_id = $(checkbox).parent().attr("id");
24+
console.log("id is "+ todo_item_id);
25+
$.ajax({
26+
url: "/checkuncheck/",
27+
cache: false,
28+
type: "POST",
29+
data: {note_id : note_id, property: property, body_html: body_html, todo_item: todo_item,todo_item_id:todo_item_id},
30+
success: function(result){
31+
console.log(result);
32+
},
33+
error: function(result){
34+
console.log(result);
35+
}
36+
});
37+
}
38+
39+

app/templates/app/_note.html

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,40 +50,3 @@ <h1> {{ note.title }} </h1>
5050
</div>
5151
</div>
5252
{% endfor %}
53-
54-
<script type="text/javascript">
55-
function checkUncheck (checkbox) {
56-
// body...
57-
var property;
58-
if ($(checkbox).is(':checked') == false){
59-
$(checkbox).removeAttr("checked");
60-
property = "uncheck";
61-
}
62-
else{
63-
$(checkbox).attr("checked","");
64-
property="check";
65-
}
66-
67-
var notediv = $(checkbox).parents(".tab-pane");
68-
69-
var note_id = ($(notediv).attr('id'));// note id
70-
var todo_item = ($(checkbox).parent().text()); // todo item text
71-
var content_div = $(checkbox).parents(".note-content");
72-
var body_html = content_div.html();// body_html of the note
73-
var todo_item_id = $(checkbox).parent().attr("id");
74-
console.log("id is "+ todo_item_id);
75-
$.ajax({
76-
url: "{{ url_for('.checkuncheck') }}",
77-
cache: false,
78-
type: "POST",
79-
data: {note_id : note_id, property: property, body_html: body_html, todo_item: todo_item,todo_item_id:todo_item_id},
80-
success: function(result){
81-
console.log(result);
82-
},
83-
error: function(result){
84-
console.log(result);
85-
}
86-
});
87-
}
88-
89-
</script>

app/templates/app/app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@
4747
{% block scripts %}
4848
{{ super() }}
4949
<script src="{{ url_for('static', filename='js/libs/marked.js') }}"></script>
50+
<script src="{{ url_for('static', filename='js/braindump.js') }}"></script>
5051
{% endblock %}

app/templates/app/app_base.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<li class="sidebar-item" title="Favorites">
3535
<a href="/favorites"><span class="nav-item glyphicon glyphicon-star" aria-hidden="true"></span><br><span class="nav-description">Favorites</span></a>
3636
</li>
37+
<li class="sidebar-item" title="TODO">
38+
<a href="/todolist"><span class="nav-item glyphicon glyphicon-th-list" aria-hidden="true"></span><br><span class="nav-description">TODO</span></a>
39+
</li>
3740
<li class="sidebar-item" title="Settings">
3841
<a href="/settings"><span class="nav-item glyphicon glyphicon-cog" aria-hidden="true"></span><br><span class="nav-description">Settings</span></a>
3942
</li>

app/templates/app/todolist.html

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
{% extends "app/app_base.html" %}
2+
{% import "bootstrap/wtf.html" as wtf %}
3+
{% import "_macros.html" as macros %}
4+
5+
{% block title %}BrainDump{% endblock %}
6+
7+
{% block page_content %}
8+
9+
{% if notes %}
10+
<div class="col-sm-11">
11+
<h1 class="top"> TODO list </h1>
12+
<hr>
13+
14+
<p> Hi {{ current_user.username }}! Here are all your TODOs. </p>
15+
16+
{% for note in notes %}
17+
<div id="note{{note.id}}" hidden>
18+
{{note.body_html}}
19+
</div>
20+
{% endfor %}
21+
22+
<div class="container">
23+
<div class="row">
24+
<div class="col-s-12">
25+
<div class="table-responsive">
26+
<table class="table table-bordered table-hover">
27+
28+
<thead>
29+
<tr>
30+
<th>TODO</th>
31+
<th>Note</th>
32+
<th>Created</th>
33+
<th>Completed</th>
34+
</tr>
35+
</thead>
36+
37+
<tbody>
38+
{% for note in notes|sort(attribute='created_date') %}
39+
{% for todo in note.todo_items|sort(attribute='created_date') %}
40+
<tr>
41+
<td class="col-sm-2">
42+
{% if todo.is_checked %}
43+
<input type="checkbox" onchange="checkUncheckDirect(this,{{ note.id }},{{todo.id}},'{{todo.title}}')" checked="checked">
44+
{{ todo.title }}
45+
{% else %}
46+
<input type="checkbox" onchange="checkUncheckDirect(this,{{ note.id }},{{todo.id}},'{{todo.title}}')" >
47+
{{ todo.title }}
48+
{% endif %}
49+
</td>
50+
<td><a href="{{ url_for('.note', id=note.id) }}"><h5>{{ note.title | truncate(100) }}</h5></a></td>
51+
<td>{{ moment(todo.created_date).format("MMMM DD, YYYY") }}</td>
52+
<td>{% if todo.checked_date %}
53+
{{ moment(todo.checked_date).format("MMMM DD, YYYY") }}
54+
{% else %}
55+
Not yet completed!
56+
{% endif %}
57+
</td>
58+
</tr>
59+
{% endfor %}
60+
{% endfor %}
61+
62+
63+
</tbody>
64+
65+
</table>
66+
</div>
67+
</div>
68+
</div>
69+
</div>
70+
</div>
71+
72+
73+
</div>
74+
{% endif %}
75+
{% endblock %}
76+
77+
{% block scripts %}
78+
{{ super() }}
79+
<script src="{{ url_for('static', filename='js/libs/marked.js') }}"></script>
80+
<script src="{{ url_for('static', filename='js/libs/bootstrap-tagsinput.js') }}"></script>
81+
<script src="{{ url_for('static', filename='js/libs/releases.js') }}"></script>
82+
<script src="{{ url_for('static', filename='js/libs/ace/ace.js') }}"></script>
83+
<script src="{{ url_for('static', filename='js/braindump.js') }}"></script>
84+
<script src="{{ url_for('static', filename='js/braindump_editor.js') }}"></script>
85+
<script>
86+
87+
88+
function checkUncheckDirect(checkbox, note_id, todo_item_id, todo_item){
89+
90+
var property;
91+
if ($(checkbox).is(':checked') == false){
92+
$(checkbox).removeAttr("checked");
93+
property = "uncheck";
94+
95+
}
96+
else{
97+
$(checkbox).attr("checked","");
98+
property="check";
99+
}
100+
101+
var temp = $("#tempdiv"+note_id);
102+
console.log("len"+ temp.length);
103+
if (temp.length == 0){
104+
var body_html = $('#note'+note_id).text();
105+
temp = document.createElement('div');
106+
temp.setAttribute("id", "tempdiv"+note_id);
107+
temp.innerHTML = body_html;
108+
$('#note'+note_id).append(temp);
109+
}
110+
else{
111+
temp = temp.html();
112+
}
113+
var li = $('#'+todo_item_id).find("input");
114+
if (property == "uncheck")
115+
$(li).attr('checked', false);
116+
else
117+
$(li).attr('checked', true);
118+
119+
body_html = $("#tempdiv"+note_id).html();
120+
console.log("id is "+todo_item_id);
121+
122+
$.ajax({
123+
url: "/checkuncheck/",
124+
cache: false,
125+
type: "POST",
126+
data: {note_id : note_id, property: property, body_html: body_html, todo_item: todo_item,todo_item_id:todo_item_id},
127+
success: function(result){
128+
console.log(result);
129+
},
130+
error: function(result){
131+
console.log(result);
132+
}
133+
});
134+
135+
}
136+
</script>
137+
{% endblock %}

0 commit comments

Comments
 (0)