
from __future__ import absolute_import, unicode_literals

import datetime

from flask import render_template, Blueprint

from zhihudaily.utils import make_request
from zhihudaily.cache import cache


image_ui = Blueprint('image_ui', __name__, template_folder='templates')


@image_ui.route('/withimage')
@cache.cached(timeout=900)
def with_image():
    """The page for 图片 UI."""
    r = make_request('http://news.at.zhihu.com/api/1.2/news/latest')
    (display_date, date, news_list) = get_news_info(r)
    news_list = handle_image(news_list)
    day_before = (
        datetime.datetime.strptime(date, '%Y%m%d') - datetime.timedelta(1)
    ).strftime('%Y%m%d')
    return render_template('with_image.html', lists=news_list,
                           display_date=display_date,
                           day_before=day_before,
                           is_today=True)


from __future__ import absolute_import, unicode_literals


from flask import render_template, Blueprint, json

from zhihudaily.cache import cache
from zhihudaily.models import Zhihudaily
from zhihudaily.utils import Date


image_ui = Blueprint('image_ui', __name__, template_folder='templates')


@image_ui.route('/withimage')
@cache.cached(timeout=900)
def with_image():
    """The page for 图片 UI."""
    day = Date()
    news = Zhihudaily.select().where(Zhihudaily.date == int(day.today)).get()

    return render_template('with_image.html',
                           lists=json.loads(news.json_news),
                           display_date=news.display_date,
                           day_before=day.day_before,
                           is_today=True)

Switch to use database for image ui
Switch to use database for image ui

  
  from __future__ import absolute_import, unicode_literals
  
- import datetime
  
- from flask import render_template, Blueprint
+ from flask import render_template, Blueprint, json
  
- from zhihudaily.utils import make_request
  from zhihudaily.cache import cache
+ from zhihudaily.models import Zhihudaily
+ from zhihudaily.utils import Date
  
  
  image_ui = Blueprint('image_ui', __name__, template_folder='templates')
  
  
  @image_ui.route('/withimage')
  @cache.cached(timeout=900)
  def with_image():
      """The page for 图片 UI."""
+     day = Date()
+     news = Zhihudaily.select().where(Zhihudaily.date == int(day.today)).get()
+ 
-     r = make_request('http://news.at.zhihu.com/api/1.2/news/latest')
-     (display_date, date, news_list) = get_news_info(r)
-     news_list = handle_image(news_list)
-     day_before = (
-         datetime.datetime.strptime(date, '%Y%m%d') - datetime.timedelta(1)
-     ).strftime('%Y%m%d')
-     return render_template('with_image.html', lists=news_list,
+     return render_template('with_image.html',
+                            lists=json.loads(news.json_news),
-                            display_date=display_date,
+                            display_date=news.display_date,
-                            day_before=day_before,
+                            day_before=day.day_before,
                             is_today=True)
  
Switch to use database for image ui
## Code Before:

from __future__ import absolute_import, unicode_literals

import datetime

from flask import render_template, Blueprint

from zhihudaily.utils import make_request
from zhihudaily.cache import cache


image_ui = Blueprint('image_ui', __name__, template_folder='templates')


@image_ui.route('/withimage')
@cache.cached(timeout=900)
def with_image():
    """The page for 图片 UI."""
    r = make_request('http://news.at.zhihu.com/api/1.2/news/latest')
    (display_date, date, news_list) = get_news_info(r)
    news_list = handle_image(news_list)
    day_before = (
        datetime.datetime.strptime(date, '%Y%m%d') - datetime.timedelta(1)
    ).strftime('%Y%m%d')
    return render_template('with_image.html', lists=news_list,
                           display_date=display_date,
                           day_before=day_before,
                           is_today=True)

## Instruction:
Switch to use database for image ui
## Code After:

from __future__ import absolute_import, unicode_literals


from flask import render_template, Blueprint, json

from zhihudaily.cache import cache
from zhihudaily.models import Zhihudaily
from zhihudaily.utils import Date


image_ui = Blueprint('image_ui', __name__, template_folder='templates')


@image_ui.route('/withimage')
@cache.cached(timeout=900)
def with_image():
    """The page for 图片 UI."""
    day = Date()
    news = Zhihudaily.select().where(Zhihudaily.date == int(day.today)).get()

    return render_template('with_image.html',
                           lists=json.loads(news.json_news),
                           display_date=news.display_date,
                           day_before=day.day_before,
                           is_today=True)
