2013-01-19 08:33:44 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# all the imports
|
|
|
|
from __future__ import with_statement
|
|
|
|
from contextlib import closing
|
|
|
|
import sqlite3
|
|
|
|
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, jsonify
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
import re
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
from subprocess import Popen, PIPE
|
|
|
|
import smtplib
|
2013-01-26 19:41:39 +00:00
|
|
|
from note import Note, Page, convert_datestring_to_date
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
# configuration
|
2013-01-19 21:45:02 +00:00
|
|
|
DATABASE = 'notes.db'
|
2013-01-19 08:33:44 +00:00
|
|
|
DEBUG = True
|
|
|
|
SECRET_KEY = 'development key'
|
|
|
|
USERNAME = 'admin'
|
|
|
|
PASSWORD = 'dupa.8'
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config.from_object(__name__)
|
|
|
|
|
|
|
|
class Thumb:
|
2013-01-19 21:45:02 +00:00
|
|
|
def __init__(self, source, href, tags):
|
|
|
|
self.src = source
|
|
|
|
self.href = href
|
|
|
|
self.tags = tags
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-19 21:45:02 +00:00
|
|
|
#DB functions
|
2013-01-19 08:33:44 +00:00
|
|
|
def connect_db():
|
2013-01-19 21:45:02 +00:00
|
|
|
return sqlite3.connect(app.config['DATABASE'])
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def init_db():
|
2013-01-19 21:45:02 +00:00
|
|
|
with closing(connect_db()) as db:
|
|
|
|
with app.open_resource('schema.sql') as f:
|
|
|
|
db.cursor().executescript(f.read())
|
|
|
|
db.commit()
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def query_db(query, args=(), one=False):
|
2013-01-19 21:45:02 +00:00
|
|
|
"""
|
|
|
|
Simple wrapper around query
|
|
|
|
"""
|
2013-01-19 08:33:44 +00:00
|
|
|
cur = g.db.execute(query, args)
|
|
|
|
rv = [dict((cur.description[idx][0], value)
|
|
|
|
for idx, value in enumerate(row)) for row in cur.fetchall()]
|
|
|
|
return (rv[0] if rv else None) if one else rv
|
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
2013-01-19 21:45:02 +00:00
|
|
|
try:
|
|
|
|
g.db = g._db.cursor()
|
|
|
|
except AttributeError:
|
|
|
|
g._db = connect_db()
|
|
|
|
g.db = g._db.cursor()
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
@app.teardown_request
|
|
|
|
def teardown_request(exception):
|
2013-01-19 21:45:02 +00:00
|
|
|
try:
|
|
|
|
g.db.close()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
#VIEWS
|
2013-01-19 11:05:28 +00:00
|
|
|
@app.route('/')
|
2013-01-19 08:33:44 +00:00
|
|
|
@app.route('/view')
|
|
|
|
@app.route('/view/<subject>/<datestring>')
|
2013-01-26 19:41:39 +00:00
|
|
|
def home(subject= "mechanika", datestring= "14grudnia2012"):
|
2013-01-28 12:19:14 +00:00
|
|
|
other_dates = prepare_other_dates(subject)
|
2013-01-26 19:41:39 +00:00
|
|
|
all_dates = prepare_all_dates()
|
2013-01-19 21:45:02 +00:00
|
|
|
all_subjects = [sub["subject"] for sub in get_all_subjects()]
|
2013-01-26 19:41:39 +00:00
|
|
|
thumbs = get_thumbs(subject, datestring)
|
2013-01-19 21:45:02 +00:00
|
|
|
if datestring in other_dates:
|
|
|
|
other_dates.remove(datestring)
|
|
|
|
thumbs_info = {"subject": subject, "datestring": datestring, "other_dates": other_dates}
|
2013-01-26 19:41:39 +00:00
|
|
|
|
2013-01-19 21:45:02 +00:00
|
|
|
thumbs = [dict(href=thumb.href, src=thumb.src) for thumb in thumbs]
|
|
|
|
main_page_src=thumbs[0]["href"]
|
|
|
|
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)
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-26 19:41:39 +00:00
|
|
|
|
2013-01-19 08:33:44 +00:00
|
|
|
@app.route('/subject/<subject>')
|
|
|
|
def show_subject(subject):
|
2013-02-08 17:03:00 +00:00
|
|
|
dates = prepare_other_dates(subject)
|
2013-01-26 19:41:39 +00:00
|
|
|
all_dates = prepare_all_dates()
|
2013-01-19 21:45:02 +00:00
|
|
|
all_subjects = [sub["subject"] for sub in get_all_subjects()]
|
|
|
|
return render_template('subject.html', subject=subject, dates=dates, all_dates=all_dates, all_subjects=all_subjects)
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
@app.route('/date/<datestring>')
|
|
|
|
def show_date(datestring):
|
2013-01-26 19:41:39 +00:00
|
|
|
all_dates = prepare_all_dates()
|
2013-01-19 21:45:02 +00:00
|
|
|
all_subjects = [sub["subject"] for sub in get_all_subjects()]
|
|
|
|
notes = get_notes_for_day(datestring)
|
|
|
|
separated_notes = {}
|
|
|
|
for note in notes:
|
|
|
|
if not note["subject"] in separated_notes:
|
|
|
|
separated_notes[note["subject"]] = []
|
|
|
|
separated_notes[note["subject"]].append(note)
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-19 21:45:02 +00:00
|
|
|
return render_template('date.html', date=datestring, all_dates=all_dates, all_subjects=all_subjects, separated_notes=separated_notes)
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
@app.route('/search')
|
|
|
|
def show_search():
|
2013-01-26 19:41:39 +00:00
|
|
|
all_dates = prepare_all_dates()
|
2013-01-19 21:45:02 +00:00
|
|
|
all_subjects = [sub["subject"] for sub in get_all_subjects()]
|
|
|
|
return render_template('search.html', all_dates=all_dates, all_subjects=all_subjects )
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
@app.route('/about')
|
|
|
|
def about():
|
2013-01-26 19:41:39 +00:00
|
|
|
all_dates = prepare_all_dates()
|
2013-01-19 21:45:02 +00:00
|
|
|
all_subjects = [sub["subject"] for sub in get_all_subjects()]
|
|
|
|
return render_template('about.html', all_dates=all_dates, all_subjects=all_subjects )
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-19 21:45:02 +00:00
|
|
|
#HELPERS
|
2013-01-19 08:33:44 +00:00
|
|
|
def add_note(subject, datestring, notes):
|
|
|
|
g.db.execute('insert into notes (subject, datestring ) values (?, ?)', [subject, datestring])
|
|
|
|
note_id = g.db.lastrowid
|
|
|
|
for note in notes:
|
2013-01-19 21:45:02 +00:00
|
|
|
g.db.execute('insert into pages (note_id, name, tags ) values (?, ?, ?)', [note_id, note["name"], note["tags"]])
|
2013-01-19 08:33:44 +00:00
|
|
|
g._db.commit()
|
|
|
|
|
2013-02-08 17:03:00 +00:00
|
|
|
def create_thumb(subject, datestring, name, tags, author_string):
|
|
|
|
src = "notes/" + author_string + "/mini/" + subject + "/" + datestring + "/" + name
|
|
|
|
href = "notes/" + author_string + "/" + subject + "/" + datestring + "/" + name
|
2013-01-19 21:45:02 +00:00
|
|
|
thumb = Thumb(src, href, tags)
|
|
|
|
return thumb
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def get_thumbs(subject, datestring=""):
|
2013-01-19 21:45:02 +00:00
|
|
|
if(datestring==""):
|
|
|
|
query_result = query_db("select pages.* from pages left join notes on notes.id = pages.note_id where notes.subject = ?", [subject,])
|
|
|
|
else:
|
2013-02-08 17:03:00 +00:00
|
|
|
query_result = query_db("select pages.*, notes.author_string from pages left join notes on notes.id = pages.note_id \
|
2013-01-19 21:45:02 +00:00
|
|
|
where notes.subject = ? and datestring = ?", [subject, datestring])
|
2013-02-08 17:03:00 +00:00
|
|
|
return [create_thumb(subject, datestring, t["name"], t["tags"], t["author_string"]) for t in query_result ]
|
2013-01-19 21:45:02 +00:00
|
|
|
|
2013-01-19 08:33:44 +00:00
|
|
|
def get_possible_dates(subject):
|
2013-01-19 21:45:02 +00:00
|
|
|
dates = query_db("select distinct datestring from notes where subject = ? ", [subject])
|
|
|
|
return dates
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def get_notes_for_day(datestring):
|
2013-01-27 14:23:52 +00:00
|
|
|
"""return pages which where written that day (daystring) with other info"""
|
2013-02-08 17:03:00 +00:00
|
|
|
query_result = query_db("select pages.*,notes.subject,notes.author_string, notes.author from pages left join notes on notes.id = pages.note_id \
|
2013-01-19 21:45:02 +00:00
|
|
|
where datestring = ?", [datestring])
|
2013-02-08 17:03:00 +00:00
|
|
|
return [{"subject": t["subject"], "author_string":t["author_string"], "author":t["author"], "datestring": datestring, "name": t["name"]}for t in query_result ]
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def get_all_dates():
|
2013-01-27 14:23:52 +00:00
|
|
|
"""return all dates from database"""
|
2013-01-19 21:45:02 +00:00
|
|
|
dates = query_db("select distinct datestring from notes ")
|
|
|
|
return dates
|
2013-01-19 08:33:44 +00:00
|
|
|
|
|
|
|
def get_all_subjects():
|
2013-01-27 14:23:52 +00:00
|
|
|
"""return all subject from database"""
|
2013-01-19 21:45:02 +00:00
|
|
|
subjects = query_db("select distinct subject from notes ")
|
|
|
|
return subjects
|
|
|
|
|
2013-01-27 14:23:52 +00:00
|
|
|
def prepare_all_dates():
|
|
|
|
"""return all dates in well formated form of nested dictionaries"""
|
2013-01-28 12:19:14 +00:00
|
|
|
datestrings = [date["datestring"] for date in get_all_dates()]
|
|
|
|
all_dates = [convert_datestring_to_date(date) for date in datestrings]
|
|
|
|
return sort_dates(datestrings, all_dates)
|
|
|
|
|
|
|
|
def prepare_other_dates(subject):
|
|
|
|
datestrings = [date["datestring"] for date in get_possible_dates(subject)]
|
|
|
|
dates = [convert_datestring_to_date(date) for date in datestrings]
|
|
|
|
return sort_dates(datestrings, dates)
|
|
|
|
|
|
|
|
def sort_dates(datestrings, dates):
|
2013-01-27 14:23:52 +00:00
|
|
|
sorted_dates = {}
|
2013-01-28 12:19:14 +00:00
|
|
|
for date, datestring in zip(dates, datestrings):
|
2013-01-27 14:23:52 +00:00
|
|
|
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
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-27 14:23:52 +00:00
|
|
|
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
|
2013-01-19 08:33:44 +00:00
|
|
|
|
2013-01-19 21:45:02 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|
2013-01-19 08:33:44 +00:00
|
|
|
|