Beispiel #1
0
def get_ago_label(lang, dt):
    """
    Convert specified datetime into a string, '*** days,months,years ago.'
    As conversion rule is made depending on creator's sense, it is sometimes different from yours.
    HACK: It's hard to read and has a room to be shortened.
    """

    now = set_timezone_local(datetime.datetime.now())
    years = now.year - dt.year
    months = now.month - dt.month
    days = now.day - dt.day
    actual_days = (now - dt).days

    if actual_days == 0:
        return common.dp_lang(lang, '今日', 'Today')

    before_today_of_each_year = months >= 0 and days >= 0
    if not before_today_of_each_year:
        years -= 1

    before_today_of_each_month = days >= 0
    if not before_today_of_each_month:
        months = 11 if months == 0 else months - 1

    labels = common.dp_lang(
        lang,
        ['年前', 'ヶ月前', '日前'],
        [' years ago', ' months ago', ' days ago'],
    )
    for num, label in zip([years, months, actual_days], labels):
        if num > 0:
            label = label.replace('s', '') if num == 1 else label
            return f'{num}{label}'
Beispiel #2
0
def search(request, lang):
    word = request.GET.get(key='s', default='')

    data = common_bizlogic.get_base_data(lang, request)
    data['is_search_page'] = True
    data['page_title'] = common.dp_lang(
        lang, '検索結果', 'Searching results') + f' | {data["page_title"]}'
    c, data['posts_dic'] = search_bizlogic.get_posts_by_search_words(
        lang, word)
    data['message'] = common.dp_lang(
        lang, f'{c}件見つかりました。',
        f'{c} posts were found.' if c > 1 else f'{c} post was found.')
    data['search_words'] = word
    return render(request, 'app/list.tpl', data)
def select_display_body(post_obj, lang=consts.Lang.JA):
    """Get body to display, with html style, by various conditions."""

    if post_obj.html:
        return post_obj.html
    return common.dp_lang(lang, post_obj.get_markdownified_body_ja(),
                          post_obj.get_markdownified_body_en())
def get_version(lang):
    version = Config.objects.filter(key=consts.ConfigKeys.SITE_VERSION).first()
    version = version.value if version else '3.0'
    return common.dp_lang(
        lang,
        f'みろりHP version {version}',
        f'Mirori-HP version {version}',
    )
Beispiel #5
0
def year(request, lang, code):
    year_obj = year_bizlogic.get_year_obj_by_code(code)

    data = common_bizlogic.get_base_data(lang, request)
    data['is_year_page'] = True
    data['page_title'] = code + common.dp_lang(
        lang, ' アーカイブ', ' Archives') + f' | {data["page_title"]}'
    data['posts_dic'] = year_bizlogic.get_posts_by_month(lang, year_obj)
    return render(request, 'app/list.tpl', data)
Beispiel #6
0
def tag(request, lang, code):
    tag_obj = tag_bizlogic.get_tag_obj_by_code(code)

    data = common_bizlogic.get_base_data(lang, request)
    data['is_tag_page'] = True
    data['page_title'] = common.dp_lang(
        lang, tag_obj.name_ja + ' アーカイブ',
        tag_obj.name_en + ' Archives') + f' | {data["page_title"]}'
    data['posts_dic'] = tag_bizlogic.get_posts_by_year(lang, tag_obj)
    return render(request, 'app/list.tpl', data)
Beispiel #7
0
def __search_posts_by_title(lang, words):
    """Search title by words and get posts of search result."""
    if not words:
        return []
    # When search words are 'a b c', try to get posts that have title contains a, b and c all.
    q = Q()
    for w in words:
        # 'icontains' searches case insensitively.
        q &= common.dp_lang(lang, Q(title_ja__icontains=w),
                            Q(title_en__icontains=w))
    return Post.available().filter(q).order_by('publish_at').reverse()
Beispiel #8
0
def __search_posts_by_title_and_body(lang, word):
    """So far it doesn't adapt multipul keywords."""
    if word == '':
        return []
    q = common.dp_lang(
        lang,
        Q(title_ja__contains=word) | Q(html__contains=word) |
        (Q(html='') & Q(body_ja__contains=word)),
        Q(title_en__contains=word) | Q(html__contains=word) |
        (Q(html='') & Q(body_en__contains=word)),
    )
    return Post.available().filter(q).order_by('publish_at').reverse()
def format_post(post_obj, lang, require_body=False):
    """Get one post by code. It occurs 404 when no post is found.
    This method organizes post data for display on tpl file.
    """

    if not post_obj:
        return

    # Here change UTC time in DB to Japan time.
    post_obj.publish_at = date_utils.convert_timezone_to_local(
        post_obj.publish_at)

    # Decide which body will be displayed.
    displayed_body = ''
    has_only_html_body_message = ''
    if require_body:
        displayed_body = select_display_body(post_obj, lang)
        # If this post has html body, it is judged that this post was moved from previous blog system automatically.
        if post_obj.html:
            has_only_html_body_message = common.dp_lang(
                lang,
                '自動移設した記事です。崩れてたらゴメン。',
                'This post was automatically moved.',
            )

    return {
        'id': post_obj.id,
        'title'     : common.dp_lang(lang, post_obj.title_ja, post_obj.title_en), # Depends on lang
        'code'      : post_obj.code,                                              # As it is
        'publish_at': date_utils.format_by_lang_Ymd(lang, post_obj.publish_at),   # Change format depends on lang
        'thumbnail' : post_obj.thumbnail,                                         # As it is
        'body'      : displayed_body,                                             # Be made above.
        'tag'       : {                                                           # As it is.
            'name': common.dp_lang(lang, post_obj.tag.name_ja, post_obj.tag.name_en),
            'code': post_obj.tag.code,
        },
        'no_en_version': lang==consts.Lang.EN and not post_obj.body_en,           # If has English body
        'has_only_html_body_message': has_only_html_body_message,
        'publish_at_ago': date_utils.get_ago_label(lang, post_obj.publish_at),
    }
def __sort_by_month(lang, post_objs):
    """Sort post objects by month."""

    posts_by_month = {}
    for p in post_objs:
        # Here change UTC time in DB to Japan time.
        p.publish_at = date_utils.convert_timezone_to_local(p.publish_at)
        m = date_utils.format_by_lang_Ym(lang, p.publish_at)
        if m not in posts_by_month:
            posts_by_month[m] = []
        posts_by_month[m].append({
            'publish_at':
            date_utils.format_by_lang_Ymd(lang, p.publish_at),
            'code':
            p.code,
            'title':
            common.dp_lang(lang, p.title_ja, p.title_en),
            'tag': {
                'name': common.dp_lang(lang, p.tag.name_ja, p.tag.name_en),
                'code': p.tag.code,
            },
        })
    return posts_by_month
Beispiel #11
0
def __sort_by_year(lang, post_objs):
    """Sort post objects by year."""

    posts_by_year = {}
    for p in post_objs:
        y = p.year.code
        if y not in posts_by_year:
            posts_by_year[y] = []
        # Here change UTC time in DB to Japan time.
        p.publish_at = date_utils.convert_timezone_to_local(p.publish_at)
        posts_by_year[y].append({
            'publish_at':
            date_utils.format_by_lang_Ymd(lang, p.publish_at),
            'code':
            p.code,
            'title':
            common.dp_lang(lang, p.title_ja, p.title_en),
            'tag': {
                'name': common.dp_lang(lang, p.tag.name_ja, p.tag.name_en),
                'code': p.tag.code,
            },
        })
    return posts_by_year
Beispiel #12
0
def search_posts_by_get_query(request):
    """Search posts by GET queries and get them.
    t: Search title only.
    """

    # Get t key from GET query.
    keywords = set(
        filter(lambda t: t != '',
               request.GET.get(key='t', default='').split(' ')))

    # Get lang key from GET query.
    lang = request.GET.get(key='lang', default=consts.Lang.JA)

    # Search posts by the keywords.
    posts = __search_posts_by_title(lang, keywords)

    # Format posts, ramaining necessary information.
    formatted_posts = []
    for post in posts:
        # Change UTC time in DB to Japanese time.
        post.publish_at = date_utils.convert_timezone_to_local(post.publish_at)
        formatted_posts.append({
            'publish_at':
            date_utils.format_by_lang_Ymd(lang, post.publish_at),
            'code':
            post.code,
            'title':
            common.dp_lang(lang, post.title_ja, post.title_en),
            'tag': {
                'name': common.dp_lang(lang, post.tag.name_ja,
                                       post.tag.name_en),
                'code': post.tag.code,
            },
        })

    return formatted_posts
def get_base_data(lang, request):
    """Make base data required on every page."""

    tags = [{
        'code': tag_obj.code,
        'name': common.dp_lang(lang, tag_obj.name_ja, tag_obj.name_en),
        'count': tag_obj.count,
    } for tag_obj in Tag.objects.filter(count__gt=0).order_by('id')]
    years = [{
        'code': year_obj.code,
        'count': year_obj.count,
    } for year_obj in Year.objects.filter(
        count__gt=0).order_by('id').reverse()]

    return {
        'lang': lang,
        'page_title': get_site_name(lang),
        'site_desc': get_site_desc(lang),
        'mainimage_fullpath': image_utils.get_default_mainimage(request),
        'tags': tags,
        'years': years,
        'site_version': get_version(lang),
    }
Beispiel #14
0
def format_by_lang_Ym(lang, date):
    """Ym format by lang."""
    return datetime.datetime.strftime(date,
                                      common.dp_lang(lang, '%Y/%m', '%b %Y'))
Beispiel #15
0
def format_by_lang_Ymd(lang, date):
    """Ymd format by lang."""
    return datetime.datetime.strftime(
        date, common.dp_lang(lang, '%Y-%m-%d', '%b %d, %Y'))
Beispiel #16
0
def format_by_lang_YmdHM(lang, date):
    """Ymd HM format by lang."""
    return datetime.datetime.strftime(
        date, common.dp_lang(lang, '%Y-%m-%d %H:%M', '%b %d, %Y %H:%M'))
def get_site_desc(lang):
    return common.dp_lang(
        lang,
        '★ 緑色さんの多目的ブログ みろりえいちぴー ごゆるりとおくつろぎあさーせ。 ★',
        '★ Midori\'s blog for multi-purposes. ★',
    )
Beispiel #18
0
def format_by_lang_md(lang, date):
    """md format by lang."""
    return datetime.datetime.strftime(date,
                                      common.dp_lang(lang, '%m/%d', '%b %d'))
def get_site_name(lang):
    return common.dp_lang(lang, 'みろりHP', 'Mirori-HP')