Some refactoring and documentation (docstrings).

master
Justyna Ilczuk 2013-01-27 15:23:52 +01:00
parent b0ed3cc800
commit b910e2649e
5 changed files with 87 additions and 94 deletions

View File

@ -75,46 +75,11 @@ def home(subject= "mechanika", datestring= "14grudnia2012"):
other_dates.remove(datestring) other_dates.remove(datestring)
thumbs_info = {"subject": subject, "datestring": datestring, "other_dates": other_dates} thumbs_info = {"subject": subject, "datestring": datestring, "other_dates": other_dates}
print thumbs
thumbs = [dict(href=thumb.href, src=thumb.src) for thumb in thumbs] thumbs = [dict(href=thumb.href, src=thumb.src) for thumb in thumbs]
main_page_src=thumbs[0]["href"] main_page_src=thumbs[0]["href"]
print all_dates
return render_template('index.html', thumbs=thumbs, main_page_src=main_page_src,thumbs_info=thumbs_info, return render_template('index.html', thumbs=thumbs, main_page_src=main_page_src,thumbs_info=thumbs_info,
all_dates=all_dates, all_subjects=all_subjects) all_dates=all_dates, all_subjects=all_subjects)
def prepare_all_dates():
all_dates = [date["datestring"] for date in get_all_dates()]
datastrings = [date["datestring"] for date in get_all_dates()]
all_dates = [convert_datestring_to_date(date) for date in datastrings]
sorted_dates = {}
for date, datestring in zip(all_dates, datastrings):
if not date.datetime.year in sorted_dates:
sorted_dates[date.datetime.year] = {}
if not date.datetime.month in sorted_dates[date.datetime.year]:
sorted_dates[date.datetime.year][date.datetime.month] = []
sorted_dates[date.datetime.year][date.datetime.month].append({"day" : date.datetime.day, "source": datestring,
"weekday": weekday(date.datetime.weekday())})
sorted = sort_number_strings(sorted_dates[date.datetime.year][date.datetime.month])
sorted_dates[date.datetime.year][date.datetime.month] = sorted
print sorted_dates
return sorted_dates
def sort_number_strings(data):
number_data = [int(day["day"]) for day in data]
number_data.sort()
print "sorted date" + str(number_data)
return [{"day": number, "source": data[get_index_for(number, data)]["source"],
"weekday": data[get_index_for(number, data)]["weekday"] } for i, number in enumerate(number_data)]
def weekday(number):
weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return weekdays[number]
def get_index_for(number, data):
index = 0
for i, day in enumerate(data):
if day["day"] == number:
index = i
return index
@app.route('/subject/<subject>') @app.route('/subject/<subject>')
def show_subject(subject): def show_subject(subject):
@ -168,7 +133,6 @@ def get_thumbs(subject, datestring=""):
else: else:
query_result = query_db("select pages.* from pages left join notes on notes.id = pages.note_id \ query_result = query_db("select pages.* from pages left join notes on notes.id = pages.note_id \
where notes.subject = ? and datestring = ?", [subject, datestring]) where notes.subject = ? and datestring = ?", [subject, datestring])
print query_result
return [create_thumb(subject, datestring, t["name"], t["tags"]) for t in query_result ] return [create_thumb(subject, datestring, t["name"], t["tags"]) for t in query_result ]
def get_possible_dates(subject): def get_possible_dates(subject):
@ -176,20 +140,57 @@ def get_possible_dates(subject):
return dates return dates
def get_notes_for_day(datestring): def get_notes_for_day(datestring):
"""returns pages which where written that day (daystring) with other info""" """return pages which where written that day (daystring) with other info"""
query_result = query_db("select pages.*,notes.subject from pages left join notes on notes.id = pages.note_id \ query_result = query_db("select pages.*,notes.subject from pages left join notes on notes.id = pages.note_id \
where datestring = ?", [datestring]) where datestring = ?", [datestring])
return [{"subject": t["subject"], "datestring": datestring, "name": t["name"]}for t in query_result ] return [{"subject": t["subject"], "datestring": datestring, "name": t["name"]}for t in query_result ]
def get_all_dates(): def get_all_dates():
"""return all dates from database"""
dates = query_db("select distinct datestring from notes ") dates = query_db("select distinct datestring from notes ")
return dates return dates
def get_all_subjects(): def get_all_subjects():
"""return all subject from database"""
subjects = query_db("select distinct subject from notes ") subjects = query_db("select distinct subject from notes ")
return subjects return subjects
def prepare_all_dates():
"""return all dates in well formated form of nested dictionaries"""
all_dates = [date["datestring"] for date in get_all_dates()]
datastrings = [date["datestring"] for date in get_all_dates()]
all_dates = [convert_datestring_to_date(date) for date in datastrings]
sorted_dates = {}
for date, datestring in zip(all_dates, datastrings):
if not date.datetime.year in sorted_dates:
sorted_dates[date.datetime.year] = {}
if not date.datetime.month in sorted_dates[date.datetime.year]:
sorted_dates[date.datetime.year][date.datetime.month] = []
sorted_dates[date.datetime.year][date.datetime.month].append({"day" : date.datetime.day, "source": datestring,
"weekday": weekday(date.datetime.weekday())})
sorted = sort_number_strings(sorted_dates[date.datetime.year][date.datetime.month])
sorted_dates[date.datetime.year][date.datetime.month] = sorted
return sorted_dates
def sort_number_strings(data):
"""return array of dictionaries sorted by day treated as a number, i.e. 12 is after 3."""
number_data = [int(day["day"]) for day in data]
number_data.sort()
return [{"day": number, "source": data[get_index_for(number, data)]["source"],
"weekday": data[get_index_for(number, data)]["weekday"] } for i, number in enumerate(number_data)]
def weekday(number):
"""return name of the weekday for a specific number in range 0-6"""
weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return weekdays[number]
def get_index_for(number, data):
"""get index of dictionary which day value equals number"""
index = 0
for i, day in enumerate(data):
if day["day"] == number:
index = i
return index
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()

View File

@ -9,22 +9,22 @@ $("document").ready( function(){
</script> </script>
<div class="container "> <div class="container ">
<h1>About</h1> <h2>About</h2>
<p> <p>
This application is a reader of notes. You can easily browse any notes which are uploaded and indexed. This application is a reader of notes. You can easily browse any notes which are uploaded and indexed.
</p> </p>
<p> <p>
Later on easy download, uploading, searching, tagging and commenting will be implemented. Later on easy download, uploading, searching, tagging and commenting will be implemented.
</p> </p>
<h1>User guide</h1> <h2>User guide</h2>
<p> <p>
Select a subject from navigation bar menu. Then you will be able to see, which notes are uploaded.There is link, view and download buttons click on anything you are interested in. Select a subject from navigation bar menu. Then you will be able to see, which notes are uploaded.There is link, view and download buttons click on anything you are interested in.
</p> </p>
<h1>Why? What for? Motivation</h1> <h2>Why? What for? Motivation</h2>
<p> <p>
I like digital notes. But I encountered some problems, it wasn't very comfortable. I wanted to improve it. I wanted to share, to have easy access and automated updates and maintainence. So I made nooz. I am still working on it. I like digital notes. But I encountered some problems, it wasn't very comfortable. I wanted to improve it. I wanted to share, to have easy access and automated updates and maintainence. So I made nooz. I am still working on it.
</p> </p>
<h1>How was it made?</h1> <h2>How was it made?</h2>
<p> <p>
Whole app is written in python. I used flask, amazing, fast, simple python web framework as backend. To make it look somehow I used twitter bootstrap. As database I use sqlite3. Whole app is written in python. I used flask, amazing, fast, simple python web framework as backend. To make it look somehow I used twitter bootstrap. As database I use sqlite3.
</p> </p>

View File

@ -75,10 +75,10 @@ $("document").ready( function(){
</a> </a>
<ul class="pager"> <ul class="pager">
<li class="previous"> <li class="previous">
<a href="" id="older">&larr; Older</a> <a href="" id="older">&larr; Previous</a>
</li> </li>
<li class="next"> <li class="next">
<a href="" id="newer">Newer &rarr;</a> <a href="" id="newer">Next &rarr;</a>
</li> </li>
</ul> </ul>
</li> </li>

View File

@ -7,38 +7,35 @@
<meta name="description" content="Home page of Attero"> <meta name="description" content="Home page of Attero">
<meta name="author" content="Justyna Ilczuk" > <meta name="author" content="Justyna Ilczuk" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
<script src="{{ url_for('static', filename='bootstrap/js/bootstrap.js') }}"></script> <script src="{{ url_for('static', filename='bootstrap/js/bootstrap.js') }}"></script>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap/css/bootstrap.css') }}"> <link rel=stylesheet type=text/css href="{{ url_for('static', filename='bootstrap/css/bootstrap.css') }}">
<script type=text/javascript> <script type=text/javascript>
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }}; $SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
</script> </script>
<script > <script >
$("document").ready( function() { $("document").ready( function() {
$('.submenu').hover(function () { $('.submenu').hover(function () {
console.log("hover"); console.log("hover");
$(this).children('ul').removeClass('submenu-hide').addClass('submenu-show'); $(this).children('ul').removeClass('submenu-hide').addClass('submenu-show');
}, function () { }, function () {
console.log("hide"); console.log("hide");
$(this).children('ul').removeClass('.submenu-show').addClass('submenu-hide'); $(this).children('ul').removeClass('.submenu-show').addClass('submenu-hide');
}).find("a:first").append(" &raquo; "); }).find("a:first").append(" &raquo; ");
}); });
</script>
</script>
</head> </head>
<style type="text/css"> <style type="text/css">
body { body {
padding-top: 40px; padding-top: 20px;
padding-bottom: 40px; padding-bottom: 40px;
} }
@ -90,7 +87,7 @@ $("document").ready( function() {
</div> </div>
<div class="navbar well-small"> <div class="navbar well-small navbar-inverse">
<div class="navbar-inner"> <div class="navbar-inner">
<ul class="nav"> <ul class="nav">
<li class="active" id="home"><a href="/view" >Home</a></li> <li class="active" id="home"><a href="/view" >Home</a></li>
@ -136,40 +133,37 @@ $("document").ready( function() {
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="nav-header">Year</li> <li class="nav-header">Year</li>
{%- for key, year in all_dates.items() recursive %} {%- for key, year in all_dates.items() recursive %}
<li class="dropdown submenu"> <li class="dropdown submenu">
<a href="#" class="dropdown-toggle" id="{{key}}" data-toggle="dropdown">{{key}}</a> <a href="#" class="dropdown-toggle" id="{{key}}" data-toggle="dropdown">{{key}}</a>
{%- if year %} {%- if year %}
<ul class="dropdown-menu submenu-hide"> <ul class="dropdown-menu submenu-hide">
<li class="nav-header">Month</li> <li class="nav-header">Month</li>
{% for month, days in year.items() %} {% for month, days in year.items() %}
<li class="dropdown submenu"> <li class="dropdown submenu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">{{month}}</a> <a href="#" class="dropdown-toggle" data-toggle="dropdown">{{month}}</a>
<ul class="dropdown-menu submenu-show submenu-hide"> <ul class="dropdown-menu submenu-show submenu-hide">
<li class="nav-header">Day</li> <li class="nav-header">Day</li>
{% for day in days %} {% for day in days %}
<li><a href="/date/{{day.source}}">{{day.weekday}}, {{day.day}}</a></li> <li><a href="/date/{{day.source}}">{{day.weekday}}, {{day.day}}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
{%-endif%}
</li>
{%- endfor %}
{%-endif%}
</li>
{%- endfor %}
</ul> </ul>
</div> </div>
</ul> </ul>
</li> </li>
<li> <li>
<li id="search" ><a href="/search" >Advanced search</a></li> <li id="search" ><a href="/search" >Advanced search</a></li>
</li> </li>
@ -177,8 +171,6 @@ $("document").ready( function() {
</div> </div>
</div> </div>
<br />
<br />
{% block body %} {% block body %}
{% endblock %} {% endblock %}

View File

@ -9,7 +9,7 @@ $("document").ready( function(){
</script> </script>
<div class="container "> <div class="container ">
<h1>Search</h1> <h2>Search</h2>
</div> </div>
{% endblock %} {% endblock %}