<commit_before>""" 
 FableMe.com
 A LittleLite Web Application
 
 print.py

"""

import logging
import urllib
import datetime

import fableme.db.schema as schema

from fableme.fabulator import Fabulator
from fableme.abstract import FablePage
from fableme.generatorhelper import GeneratorProxy
from google.appengine.api import mail

from google.appengine.ext.webapp.util import login_required
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

class Print(FablePage):
    """ /print page """ 
    
    def _prepare(self, user, fable_id):
        """ read the template """
        self.fable = Fabulator(user, fable_id) 
        self.ebookproxy = GeneratorProxy('unknown', self.fable.the_fable)
        self.fable_contents = self.ebookproxy.load_template()
    
    @login_required    
    def get(self):
        """ http get handler """
        fable_id = self.request.get('id') 
        self._prepare(self.the_user, long(fable_id))
        self.template_values['fable_id'] = fable_id
        self.template_values['fable_contents'] = self.fable_contents
        self.template_values['title'] = self.fable.the_fable.localized_title
        self.render()
        
    def __init__(self, request, response):
        FablePage.__init__(self, request, response, 'print.html')
        
class PrinteBook():
    """ /print/book page """ 
    
    def _prepare(self, user, fable_id, fable_format):
        """ read the template and prepare for pdf creation """
        self.fable = Fabulator(user, fable_id) 
        self.ebookproxy = GeneratorProxy(fable_format, self.fable.the_fable)
        self.fable_contents = self.ebookproxy.load_template()
    
    def _build(self, fable_id, fable_format, user):
        self._prepare(user, long(fable_id), fable_format)
        self.ebookproxy.prepare_ebook()
        
    def _download_url(self, fable_format):
        blobkey = self.ebookproxy.save_ebook()
        nick = self.fable.the_fable.name
        titlebrief = self.fable.the_fable.template['title_brief']
        lastmod = self.fable.the_fable.modified.strftime("%d%m%y%H%M%S")
        userid = self.user_db.nickname
        lang = self.fable.the_fable.language
        return '/serve/%s?brief=%s&nick=%s&lastmod=%s&userid=%s&title=%s&lang=%s&fmt=%s' % ( blobkey, titlebrief, nick, lastmod, userid, titlebrief, lang, fable_format )
      
    def printBook(self, fable_id, fable_format):
        logging.info('Initiating process print ebook id='+fable_id)
        self._build(fable_id, fable_format, self.user)
        # Update DB fable
        dbfable = schema.DbFable.get_fable(self.user, long(fable_id))
        dbfable.bought = True
        link_url = self._download_url(fable_format)
        dbfable.downlink = link_url
        dbfable.purchased = datetime.datetime.now()
        dbfable.put()
        logging.info('Ended process pring ebook id='+fable_id)
        dbuser = schema.DbFableUser.get_from_user(self.user)
        logging.info('Sending email advice to '+dbuser.email)
        self.sendmail(dbuser, link_url)
        
    def sendmail(self, dbuser, ebook_link):     
        receiver = 'user'
        if (dbuser.name):
            receiver = dbuser.name.title()
        elif (dbuser.nickname):
            receiver = dbuser.nickname.title()
        to_field = receiver + ' <' + dbuser.email + '>'
        body_field = """
Dear [name],

The eBook you recently purchased from FableMe.com is ready.  You can now visit
<a href="http://www.fableme.com/">http://www.fableme.com/</a> and sign in using your Google Account to
download it.

You can also directly download it, by clicking the link below:
<a href="[link]">[link]</a>

Your FableMe Team
        """
        <commit_msg>eMail fix<commit_after>""" 
 FableMe.com
 A LittleLite Web Application
 
 print.py

"""

import logging
import urllib
import datetime

import fableme.db.schema as schema

from fableme.fabulator import Fabulator
from fableme.abstract import FablePage
from fableme.generatorhelper import GeneratorProxy
from google.appengine.api import mail

from google.appengine.ext.webapp.util import login_required
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers

class Print(FablePage):
    """ /print page """ 
    
    def _prepare(self, user, fable_id):
        """ read the template """
        self.fable = Fabulator(user, fable_id) 
        self.ebookproxy = GeneratorProxy('unknown', self.fable.the_fable)
        self.fable_contents = self.ebookproxy.load_template()
    
    @login_required    
    def get(self):
        """ http get handler """
        fable_id = self.request.get('id') 
        self._prepare(self.the_user, long(fable_id))
        self.template_values['fable_id'] = fable_id
        self.template_values['fable_contents'] = self.fable_contents
        self.template_values['title'] = self.fable.the_fable.localized_title
        self.render()
        
    def __init__(self, request, response):
        FablePage.__init__(self, request, response, 'print.html')
        
class PrinteBook():
    """ /print/book page """ 
    
    def _prepare(self, user, fable_id, fable_format):
        """ read the template and prepare for pdf creation """
        self.fable = Fabulator(user, fable_id) 
        self.ebookproxy = GeneratorProxy(fable_format, self.fable.the_fable)
        self.fable_contents = self.ebookproxy.load_template()
    
    def _build(self, fable_id, fable_format, user):
        self._prepare(user, long(fable_id), fable_format)
        self.ebookproxy.prepare_ebook()
        
    def _download_url(self, fable_format):
        blobkey = self.ebookproxy.save_ebook()
        nick = self.fable.the_fable.name
        titlebrief = self.fable.the_fable.template['title_brief']
        lastmod = self.fable.the_fable.modified.strftime("%d%m%y%H%M%S")
        userid = self.user_db.nickname
        lang = self.fable.the_fable.language
        return '/serve/%s?brief=%s&nick=%s&lastmod=%s&userid=%s&title=%s&lang=%s&fmt=%s' % ( blobkey, titlebrief, nick, lastmod, userid, titlebrief, lang, fable_format )
      
    def printBook(self, fable_id, fable_format):
        logging.info('Initiating process print ebook id='+fable_id)
        self._build(fable_id, fable_format, self.user)
        # Update DB fable
        dbfable = schema.DbFable.get_fable(self.user, long(fable_id))
        dbfable.bought = True
        link_url = self._download_url(fable_format)
        dbfable.downlink = link_url
        dbfable.purchased = datetime.datetime.now()
        dbfable.put()
        logging.info('Ended process pring ebook id='+fable_id)
        dbuser = schema.DbFableUser.get_from_user(self.user)
        logging.info('Sending email advice to '+dbuser.email)
        self.sendmail(dbuser, link_url)
        
    def sendmail(self, dbuser, ebook_link):     
        receiver = 'user'
        if (dbuser.name):
            receiver = dbuser.name.title()
        elif (dbuser.nickname):
            receiver = dbuser.nickname.title()
        to_field = receiver + ' <' + dbuser.email + '>'
        body_field = """
Dear [name],

The eBook you recently purchased from FableMe.com is ready.  You can now visit
http://www.fableme.com/ and sign in using your Google Account to
download it. To do so, navigate to your Account page, under the tab 'Purchased eBooks'.

You can also directly download it, by clicking the link below:
[link]

Your FableMe Team
        """
        
{'fraction_non_alphanumeric': 0.10996793158738642, 'fraction_numerical': 0.0, 'mean_word_length': 2.992, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6502529', 'n_tokens_mistral': 2403, 'n_tokens_neox': 2384, 'n_words': 512}