Пример #1
0
 def to_dict(self):
     return {
         'id': self.id,
         'title': self.title,
         'link': self.article_url,
         'text': self.preview_text,
         'date': format_datetime(self.published, 'dd MMM yyyy HH:mm'),
         'elapsedDate': relative_date_format(self.published),
         'imageUrl': self.preview_image_src,
         'viewsCount': self.views_count,
         'favoritesCount': self.favorites_count
     }
Пример #2
0
def relative_date_format(dt):
    diff = datetime.now() - dt
    s = diff.seconds
    if diff.days > 30 or diff.days < 0:
        return format_datetime(dt, "dd MMM yyyy HH:mm")
    elif diff.days == 1:
        return ngettext("%(days)d day ago", "%(days)d days ago", 1, days=1)
    elif diff.days > 1:
        return ngettext("%(days)d day ago", "%(days)d days ago", diff.days, days=diff.days)
    elif s <= 1:
        return gettext("just now")
    elif s < 60:
        return ngettext("%(seconds)d second ago", "%(seconds)d seconds ago", s, seconds=s)
    elif s < 120:
        return ngettext("%(minutes)d minute ago", "%(minutes)d minutes ago", 1, minutes=1)
    elif s < 3600:
        return ngettext("%(minutes)d minute ago", "%(minutes)d minutes ago", s / 60, minutes=(s / 60))
    elif s < 7200:
        return ngettext("%(hours)d hour ago", "%(hours)d hours ago", 1, hours=1)
    else:
        return ngettext("%(hours)d hour ago", "%(hours)d hours ago", s / 3600, hours=(s / 3600))
Пример #3
0
def generate_file(task_id, app_url='http://localhost:9000', file_format='PDF', lang='en'):

    from rdr.modules.users.session import session_user
    from rdr.modules.feeds.models import OfflineReadQueueTask
    task = OfflineReadQueueTask.query.filter(OfflineReadQueueTask.id == task_id).one()
    user = task.user
    session_user.auth(user)

    from rdr.application.i18n import format_datetime, gettext, ngettext, set_global_lang
    set_global_lang(lang)

    from rdr.application.database import db
    from rdr.modules.feeds.models import Article, OfflineReadQueue
    from rdr.modules.feeds.articles.status import UserArticleRecord

    articles = Article.query \
        .join(OfflineReadQueue, (OfflineReadQueue.user_id == user.id) & (OfflineReadQueue.article_id == Article.id)) \
        .options(db.joinedload(Article.statuses),
                 db.joinedload(Article.feed)) \
        .filter((Article.active == True)).order_by(OfflineReadQueue.add_date.asc())

    records = UserArticleRecord.wrap_articles_list(articles)
    dicts = [x.to_dict(full_text=True) for x in records]

    if not dicts:
        task.status = OfflineReadQueueTask.STATUS_REJECTED
        db.session.commit()
        return False

    from jinja2 import Environment, FileSystemLoader
    import os
    from datetime import datetime

    env = Environment(extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape', 'jinja2.ext.with_'], autoescape=True, loader=FileSystemLoader(
        os.path.join(os.path.dirname(os.path.realpath(__file__)),
                     '..',
                     '..',
                     'modules',
                     'feeds',
                     'templates',
                     'offline-reading')
        )
    )
    env.install_gettext_callables(gettext, ngettext, True)
    template = env.get_template('pdf-std.jhtml')
    content = template.render(articles=dicts,
                              url=app_url,
                              username=user.username,
                              gen_date=format_datetime(datetime.now(), 'dd MMM yyyy HH:mm'))

    import pdfkit
    from tempfile import NamedTemporaryFile

    pdf_opts = {
        'page-size': 'Letter'
    }
    f = NamedTemporaryFile(delete=False)
    pdfkit.from_string(content, f.name, options=pdf_opts)
    task.status = OfflineReadQueueTask.STATUS_FILE_GENERATED
    task.out_file = f.name
    db.session.commit()
    return True