# Path: src/gthnk/models/day.py
# class Day(db.Model, CRUDMixin):
#     """
#     A Day consists of the Entry objects that were created on that day.
# 
#     This is just a convenient way of referring to Entry objects in the database.
#     This object is also capable of creating a string that is parsable by the JournalBuffer
#     """
# 
#     id = db.Column(db.Integer, primary_key=True)
#     date = db.Column(db.Date, unique=True)
# 
#     pages = db.relationship("Page", order_by="Page.sequence",
#         collection_class=ordering_list('sequence', reorder_on_append=True),
#         backref=db.backref("day"))
# 
#     def add_page(self, binary):
#         from .page import Page
#         page = Page.create(day=self)
#         page.set_image(binary=binary)
#         self.pages.append(page)
#         self.pages.reorder()
#         db.session.commit()
#         return(page)
# 
#     def attach(self, binary):
#         # determine the format of the file
#         ext = puremagic.from_string(binary)
# 
#         page = None
# 
#         # if the attachment is a PDF
#         if ext == ".pdf":
#             # use PyPDF2 to read the stream
#             pdf = PdfFileReader(BytesIO(binary))
#             # if it is a multi-page PDF
#             if pdf.getNumPages() > 1:
#                 # add the pages individually
#                 for pdf_page in pdf.pages:
#                     output = PdfFileWriter()
#                     output.addPage(pdf_page)
# 
#                     pdf_page_buf = BytesIO()
#                     output.write(pdf_page_buf)
#                     page = self.add_page(pdf_page_buf.getvalue())
#             # if it is just a single page PDF
#             else:
#                 # then add the original bytestream
#                 page = self.add_page(binary)
#         # if the attachment is a recognized image
#         elif ext in [".png", ".jfif", ".gif", ".jpeg", ".jpg"]:
#             page = self.add_page(binary)
#         # could not recognize file
#         else:
#             pass
# 
#         if page:
#             return(page)
# 
#     def render_pdf(self):
#         outpdf = PdfFileWriter()
#         for page in self.pages:
#             if page.extension == "pdf":
#                 # the page is already a PDF so append directly
#                 outpdf.addPage(PdfFileReader(BytesIO(page.binary)).getPage(0))
#             else:
#                 # otherwise, the page is an image that needs to be converted to PDF first
#                 buf = BytesIO()
#                 img = Image.open(BytesIO(page.binary))
#                 img.convert("RGB").save(buf, format="pdf")
#                 # once image is PDF, it can be appended
#                 outpdf.addPage(PdfFileReader(buf).getPage(0))
# 
#         pdf_page_buf = BytesIO()
#         outpdf.write(pdf_page_buf)
#         return(pdf_page_buf.getvalue())
# 
#     def yesterday(self):
#         return(self.query.filter(Day.date < self.date).order_by(desc(Day.date)).first())
# 
#     def tomorrow(self):
#         return(self.query.filter(Day.date > self.date).order_by(Day.date).first())
# 
#     def render(self):
#         from .entry import Entry
#         buf = self.date.strftime("%Y-%m-%d")
#         for entry in self.entries.order_by(Entry.timestamp).all():
#             buf += str(entry)
#         buf += "\n\n"
#         return(buf)
# 
#     def render_markdown(self):
#         buf = self.render()
#         buf = re.sub(r'(\d\d\d\d-\d\d-\d\d)\n', '# \g<1>\n', buf)
#         buf = re.sub(r'(\d\d\d\d)\n', '## \g<1>\n', buf)
# 
#         img_fmt = "[![{sequence}](../thumbnail/{attachment})](../attachment/{attachment})\n"
# 
#         if len(self.pages) > 0:
#             buf += "# Attachments\n\n"
#             for page in self.pages:
#                 buf += img_fmt.format(
#                     sequence=page.sequence,
#                     thumbnail=page.filename(extension="jpg"),
#                     attachment=page.filename()
#                 )
# 
#         return buf
# 
#     def __repr__(self):
#         return("<Day: {}>".format(self.date))
# 
#     def __unicode__(self):
#         return(self.render())
# 
#     def __str__(self):
#         return(self.__unicode__())
# 
# def latest():
#     return(Day.query.order_by(desc(Day.date)).first())
# 
# Path: src/gthnk/utils/slugify.py
# def slugify(value):
#     """
#     Normalizes string, converts to lowercase, removes non-alpha characters,
#     and converts spaces to hyphens.
#     
#     From Django's "django/template/defaultfilters.py".
#     """
#     # import unicodedata
#     # if not isinstance(value, unicode):
#     #     value = unicode(value)
#     # value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
#     
#     value = _slugify_strip_re.sub('', value).strip().lower()
# 
#     return _slugify_hyphenate_re.sub('-', value)
# 
# @jinja2.contextfilter
# def _slugify(string):
#     if not string:
#         return ""
#     return slugify(string)
# 
# Path: src/gthnk/adaptors/journal_buffer.py
# class TextFileJournalBuffer(JournalBuffer):
#     """
#     provide functions for loading content from a text file
#     """
#     def process_one(self, filename):
#         if os.path.isfile(filename):
#             with io.open(filename, "r", encoding="utf-8") as f:
#                 contents = f.read()
#             self.parse(contents)
# 
#     def process_list(self, filename_list):
#         for filename in filename_list:
#             self.process_one(filename)


import os
import re
import flask
import logging
import datetime
from flask_login import login_required
from ..models.day import Day, latest
from ..utils.slugify import slugify, _slugify
from ..adaptors.journal_buffer import TextFileJournalBuffer

day = flask.Blueprint('day', __name__)

day.add_app_template_filter(slugify)

@day.route("/nearest/<date>")
@login_required
def nearest_day_view(date):

# Path: src/gthnk/models/day.py
# class Day(db.Model, CRUDMixin):
#     """
#     A Day consists of the Entry objects that were created on that day.
# 
#     This is just a convenient way of referring to Entry objects in the database.
#     This object is also capable of creating a string that is parsable by the JournalBuffer
#     """
# 
#     id = db.Column(db.Integer, primary_key=True)
#     date = db.Column(db.Date, unique=True)
# 
#     pages = db.relationship("Page", order_by="Page.sequence",
#         collection_class=ordering_list('sequence', reorder_on_append=True),
#         backref=db.backref("day"))
# 
#     def add_page(self, binary):
#         from .page import Page
#         page = Page.create(day=self)
#         page.set_image(binary=binary)
#         self.pages.append(page)
#         self.pages.reorder()
#         db.session.commit()
#         return(page)
# 
#     def attach(self, binary):
#         # determine the format of the file
#         ext = puremagic.from_string(binary)
# 
#         page = None
# 
#         # if the attachment is a PDF
#         if ext == ".pdf":
#             # use PyPDF2 to read the stream
#             pdf = PdfFileReader(BytesIO(binary))
#             # if it is a multi-page PDF
#             if pdf.getNumPages() > 1:
#                 # add the pages individually
#                 for pdf_page in pdf.pages:
#                     output = PdfFileWriter()
#                     output.addPage(pdf_page)
# 
#                     pdf_page_buf = BytesIO()
#                     output.write(pdf_page_buf)
#                     page = self.add_page(pdf_page_buf.getvalue())
#             # if it is just a single page PDF
#             else:
#                 # then add the original bytestream
#                 page = self.add_page(binary)
#         # if the attachment is a recognized image
#         elif ext in [".png", ".jfif", ".gif", ".jpeg", ".jpg"]:
#             page = self.add_page(binary)
#         # could not recognize file
#         else:
#             pass
# 
#         if page:
#             return(page)
# 
#     def render_pdf(self):
#         outpdf = PdfFileWriter()
#         for page in self.pages:
#             if page.extension == "pdf":
#                 # the page is already a PDF so append directly
#                 outpdf.addPage(PdfFileReader(BytesIO(page.binary)).getPage(0))
#             else:
#                 # otherwise, the page is an image that needs to be converted to PDF first
#                 buf = BytesIO()
#                 img = Image.open(BytesIO(page.binary))
#                 img.convert("RGB").save(buf, format="pdf")
#                 # once image is PDF, it can be appended
#                 outpdf.addPage(PdfFileReader(buf).getPage(0))
# 
#         pdf_page_buf = BytesIO()
#         outpdf.write(pdf_page_buf)
#         return(pdf_page_buf.getvalue())
# 
#     def yesterday(self):
#         return(self.query.filter(Day.date < self.date).order_by(desc(Day.date)).first())
# 
#     def tomorrow(self):
#         return(self.query.filter(Day.date > self.date).order_by(Day.date).first())
# 
#     def render(self):
#         from .entry import Entry
#         buf = self.date.strftime("%Y-%m-%d")
#         for entry in self.entries.order_by(Entry.timestamp).all():
#             buf += str(entry)
#         buf += "\n\n"
#         return(buf)
# 
#     def render_markdown(self):
#         buf = self.render()
#         buf = re.sub(r'(\d\d\d\d-\d\d-\d\d)\n', '# \g<1>\n', buf)
#         buf = re.sub(r'(\d\d\d\d)\n', '## \g<1>\n', buf)
# 
#         img_fmt = "[![{sequence}](../thumbnail/{attachment})](../attachment/{attachment})\n"
# 
#         if len(self.pages) > 0:
#             buf += "# Attachments\n\n"
#             for page in self.pages:
#                 buf += img_fmt.format(
#                     sequence=page.sequence,
#                     thumbnail=page.filename(extension="jpg"),
#                     attachment=page.filename()
#                 )
# 
#         return buf
# 
#     def __repr__(self):
#         return("<Day: {}>".format(self.date))
# 
#     def __unicode__(self):
#         return(self.render())
# 
#     def __str__(self):
#         return(self.__unicode__())
# 
# def latest():
#     return(Day.query.order_by(desc(Day.date)).first())
# 
# Path: src/gthnk/utils/slugify.py
# def slugify(value):
#     """
#     Normalizes string, converts to lowercase, removes non-alpha characters,
#     and converts spaces to hyphens.
#     
#     From Django's "django/template/defaultfilters.py".
#     """
#     # import unicodedata
#     # if not isinstance(value, unicode):
#     #     value = unicode(value)
#     # value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore')
#     
#     value = _slugify_strip_re.sub('', value).strip().lower()
# 
#     return _slugify_hyphenate_re.sub('-', value)
# 
# @jinja2.contextfilter
# def _slugify(string):
#     if not string:
#         return ""
#     return slugify(string)
# 
# Path: src/gthnk/adaptors/journal_buffer.py
# class TextFileJournalBuffer(JournalBuffer):
#     """
#     provide functions for loading content from a text file
#     """
#     def process_one(self, filename):
#         if os.path.isfile(filename):
#             with io.open(filename, "r", encoding="utf-8") as f:
#                 contents = f.read()
#             self.parse(contents)
# 
#     def process_list(self, filename_list):
#         for filename in filename_list:
#             self.process_one(filename)


# Path: src/gthnk/blueprints/day.py
import os
import re
import flask
import logging
import datetime
from flask_login import login_required
from ..models.day import Day, latest
from ..utils.slugify import slugify, _slugify
from ..adaptors.journal_buffer import TextFileJournalBuffer


day = flask.Blueprint('day', __name__)

day.add_app_template_filter(slugify)

@day.route("/nearest/<date>")
@login_required
def nearest_day_view(date):

    day = Day.find(date=datetime.datetime.strptime(date, "%Y-%m-%d").date())