Source

OMG_agenda / application_werk.py

Full commit
#-*-coding:utf-8 -*-

import os
import codecs
from werkzeug.wrappers import BaseRequest, BaseResponse, Response
from werkzeug.formparser import parse_form_data
from werkzeug.utils import redirect
from werkzeug.wsgi import wrap_file
from jinja2 import Template
import csv
from datetime import datetime,date
import templates


##UTILS##

def get_data(mode="a",file="week.csv"):
    """ process an org-agenda to get the data into a csv file """
    
    os.system("""
emacs -batch -l dummy_org_conf -eval '(org-batch-agenda-csv "%s" org-agenda-include-diary t org-agenda-files (quote ("~/upload/")))' > %s"""% (mode,file)
)

def read_data(file):
    """ read data from a csv file return a well list of well formated entry"""
    reader = csv.reader(open(file))
    agenda_list = []
    for elem in reader:
        agenda_list.append(format_dict(elem))
    temp_dates = [elem['date'] for elem in agenda_list]
    dates = []
    for elem in temp_dates:
        if elem not in dates:
            dates.append(elem)
    
    return {'agenda':agenda_list,'dates':dates}
                        

def format_dict(line):
    """ get a line of csv file and process data into a clean dictionary """
    elem_dict = {
        'category':codecs.utf_8_decode(line[0])[0],
        'head':codecs.utf_8_decode(line[1])[0],
        'type':codecs.utf_8_decode(line[2])[0],
        'todo':codecs.utf_8_decode(line[3])[0],
        'tag':codecs.utf_8_decode(line[4])[0].split(':'),
        'date':datetime.strptime(codecs.utf_8_decode(line[5])[0],'%Y-%m-%d'),
        'time':codecs.utf_8_decode(line[6])[0],
        'extra':codecs.utf_8_decode(line[7])[0],
        'priority1':codecs.utf_8_decode(line[8])[0],
        'priorityn':codecs.utf_8_decode(line[9])[0]
        }
    print elem_dict
    return elem_dict

##VIEWS

def display_week(request):
    try :
        agenda=read_data("week.csv")
    except:
        return  redirect('/upload/')
    template = Template("%s %s" %(templates.header,templates.agenda))
    return BaseResponse(template.render(
            today = date.today(),
            agenda = agenda,
            ,
            name="Weekly"
            ),
                        mimetype='text/html'
                        )

def generate_data(request,mode):
    get_data(mode)
    return  redirect('/')



def display_upload(request):
    template = Template("%s %s"%(templates.header,templates.upload_form))
    return BaseResponse(template.render(),mimetype='text/html')

def save_data(request):
    f = request.files.get('org')
    f.save('upload/%s'%f.filename)
    template = Template("%s %s"%(templates.header,templates.sucess))
    return BaseResponse(template.render(),mimetype='text/html')

def application(environ, start_response):
    request = BaseRequest(environ)
    if request.path == '/':
        response = display_week(request)

    elif request.path == '/generate/week/':
        response = generate_data(request,"a")

    elif request.path == '/upload/':
        if request.method == 'GET':
            response = display_upload(request)

        if request.method == 'POST':
            response = save_data(request)
            
    else:
        response = Response('Go to hell',status=404)
        
    return response(environ, start_response)

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 8000, application,use_debugger=True, use_reloader=True,static_files={"/jquery.js":"jquery.js"})