# Path: lib/messages.py
# def parse_line(line, id):
#     # Lines consist of comma separated fields.
#     parts = line.split(',', 4)
# 
#     try:
#         if not isinstance(parts[4], unicode):
#             parts[4] = unicode(parts[4], encoding='utf-8')
# 
#         timestamp = int(parts[0])
#         counter = int(parts[1])
#     except (ValueError, IndexError):
#         return None
# 
#     return {
#         'id': id,
#         'timestamp': timestamp,
#         'counter': counter,
#         'type': parts[2],
#         'color': parts[3],
#         'line': parts[4]
#     }
# 
# Path: lib/model.py
# class Log(Base):
#     __tablename__ = 'logs'
#     id = Column(Integer, primary_key=True)
#     url = Column(String(100), unique=True)
#     page_count = Column(Integer, default=1)
#     time_created = Column(DateTime(), nullable=False, default=now)
#     time_saved = Column(DateTime(), nullable=False, default=now)
# 
# class LogPage(Base):
#     __tablename__ = 'log_pages'
#     log_id = Column(Integer, ForeignKey('logs.id'), primary_key=True, autoincrement=False)
#     number = Column(Integer, primary_key=True)
#     content = Column(UnicodeText, nullable=False)
# 
# Path: lib/requests.py
# def connect_mysql():
#     g.mysql = sm()
# 
# def disconnect_mysql(response=None):
#     g.mysql.close()
#     del g.mysql
#     return response


import datetime
import os
from flask import Flask, g, request, render_template, redirect, url_for, abort
from sqlalchemy import and_
from sqlalchemy.orm.exc import NoResultFound
from webhelpers import paginate
from lib.messages import parse_line
from lib.model import Log, LogPage
from lib.requests import connect_mysql, disconnect_mysql
from werkzeug.contrib.fixers import ProxyFix
@app.route('/logs/group/<chat>')
def old_view_log(chat):
    return redirect(url_for('view_log', chat=chat))

@app.route('/logs/<log_id>')
def view_log_by_id(log_id=None):
    try:
        log_id = int(log_id)
    except ValueError:
        abort(400)
    try:
        log = g.mysql.query(Log).filter(Log.id==log_id).one()
    except NoResultFound:
        abort(404)
    if log.url is not None:
        return redirect(url_for('view_log', chat=log.url))
    abort(404)

@app.route('/chat/<chat>/log')
def view_log(chat=None):

    try:
        log = g.mysql.query(Log).filter(Log.url==chat).one()
    except NoResultFound:
        abort(404)

    current_page = request.args.get('page') or log.page_count
    mode = request.args.get('mode') or 'normal'

    try:

# Path: lib/messages.py
# def parse_line(line, id):
#     # Lines consist of comma separated fields.
#     parts = line.split(',', 4)
# 
#     try:
#         if not isinstance(parts[4], unicode):
#             parts[4] = unicode(parts[4], encoding='utf-8')
# 
#         timestamp = int(parts[0])
#         counter = int(parts[1])
#     except (ValueError, IndexError):
#         return None
# 
#     return {
#         'id': id,
#         'timestamp': timestamp,
#         'counter': counter,
#         'type': parts[2],
#         'color': parts[3],
#         'line': parts[4]
#     }
# 
# Path: lib/model.py
# class Log(Base):
#     __tablename__ = 'logs'
#     id = Column(Integer, primary_key=True)
#     url = Column(String(100), unique=True)
#     page_count = Column(Integer, default=1)
#     time_created = Column(DateTime(), nullable=False, default=now)
#     time_saved = Column(DateTime(), nullable=False, default=now)
# 
# class LogPage(Base):
#     __tablename__ = 'log_pages'
#     log_id = Column(Integer, ForeignKey('logs.id'), primary_key=True, autoincrement=False)
#     number = Column(Integer, primary_key=True)
#     content = Column(UnicodeText, nullable=False)
# 
# Path: lib/requests.py
# def connect_mysql():
#     g.mysql = sm()
# 
# def disconnect_mysql(response=None):
#     g.mysql.close()
#     del g.mysql
#     return response


# Path: main.py
import datetime
import os
from flask import Flask, g, request, render_template, redirect, url_for, abort
from sqlalchemy import and_
from sqlalchemy.orm.exc import NoResultFound
from webhelpers import paginate
from lib.messages import parse_line
from lib.model import Log, LogPage
from lib.requests import connect_mysql, disconnect_mysql
from werkzeug.contrib.fixers import ProxyFix

@app.route('/logs/group/<chat>')
def old_view_log(chat):
    return redirect(url_for('view_log', chat=chat))

@app.route('/logs/<log_id>')
def view_log_by_id(log_id=None):
    try:
        log_id = int(log_id)
    except ValueError:
        abort(400)
    try:
        log = g.mysql.query(Log).filter(Log.id==log_id).one()
    except NoResultFound:
        abort(404)
    if log.url is not None:
        return redirect(url_for('view_log', chat=log.url))
    abort(404)

@app.route('/chat/<chat>/log')
def view_log(chat=None):

    try:
        log = g.mysql.query(Log).filter(Log.url==chat).one()
    except NoResultFound:
        abort(404)

    current_page = request.args.get('page') or log.page_count
    mode = request.args.get('mode') or 'normal'

    try:

        log_page = g.mysql.query(LogPage).filter(and_(LogPage.log_id==log.id, LogPage.number==current_page)).one()