Exemplo n.º 1
0
 def convert_to(self, from_model):
     try:
         return {
             'id': from_model.id,
             'title': from_model.title,
             'slug': from_model.slug,
             'parent_cate_id': from_model.category.parent.id,
             'category_id': from_model.category.id,
             'category_name': from_model.category.name,
             'category_slug': from_model.category.slug,
             'description': from_model.description,
             'content': from_model.content,
             'mark': from_model.mark,
             'enable_comment': from_model.enable_comment,
             'login_required': from_model.login_required,
             'views_count': from_model.views_count,
             'comment_count': from_model.comment_count,    # TODO: get from comment model?
             'publish_date': datetime2timestamp(from_model.publish_date, convert_to_utc=True),
             'thumbnail_url': (from_model.thumbnail.path.url if from_model.thumbnail.path else from_model.thumbnail.url) if from_model.thumbnail else 'http://xianglong.qiniudn.com/default_article_image.gif',
             'tags': from_model.get_tags(),
             'tag_ids': [tag.id for tag in from_model.tags.all()],
         }
     except Exception, e:
         logger.exception('Convert article failed, error: %s' % e)
         return {}
Exemplo n.º 2
0
 def convert_to(self, from_model):
     try:
         return {
             'id':
             from_model.id,
             'user_id':
             from_model.user.id if from_model.user else 0,
             'username':
             from_model.user.username if from_model.user else '',
             'article_id':
             from_model.article.id,
             'author':
             from_model.author,
             'author_email':
             from_model.author_email,
             'author_website':
             from_model.author_website,
             'author_ip':
             from_model.author_ip,
             'author_agent':
             from_model.author_agent,
             'parent_id':
             from_model.parent.id if from_model.parent else 0,
             'comment_date':
             datetime2timestamp(from_model.comment_date,
                                convert_to_utc=True),
             'content':
             from_model.content,
         }
     except Exception, e:
         logger.exception('Convert comment failed, error: %s' % e)
         return {}
Exemplo n.º 3
0
 def get_hottest_articles(self, count=5, has_login=False):
     """ 获取最热门的文章 """
     cond = {'login_required': False} if not has_login else {}
     # 只获取前6个月的数据
     date_time = datetime.datetime.now() - datetime.timedelta(days=180)
     cond['publish_date'] = {'$gt': datetime2timestamp(date_time, convert_to_utc=True)}
     article_infos = self._db.articles.find(cond, sort=[('views_count', self.ORDER_DESC)], skip=0, limit=count, fields=ARTICLE_BRIEF_INFO_FIELDS)
     return article_infos
Exemplo n.º 4
0
def preview_article(request, slug):
    try:
        article = Article.objects.get(slug__exact=slug)
    except Article.DoesNotExist:
        logger.warning('Invaild article slug: %s' % slug)
        return _render_404_response(request)

    publish_date = datetime2timestamp(article.publish_date,
                                      convert_to_utc=True)
    # get previous and next articles
    prev_a = blog_db.get_prev_article(publish_date)
    next_a = blog_db.get_next_article(publish_date)

    context_infos = {
        'page_title': article.title,
        'prev_a': prev_a,
        'next_a': next_a,
    }
    article_infos = {
        'id':
        article.id,
        'title':
        article.title,
        'slug':
        article.slug,
        'category_id':
        article.category.id,
        'category_name':
        article.category.name,
        'category_slug':
        article.category.slug,
        'description':
        article.description,
        'content':
        article.content,
        'mark':
        article.mark,
        'enable_comment':
        False,
        'login_required':
        article.login_required,
        'views_count':
        article.views_count,
        'publish_date':
        publish_date,
        'thumbnail_url': (article.thumbnail.path.url if article.thumbnail.path
                          else article.thumbnail.url) if article.thumbnail else
        'http://xianglong.qiniudn.com/default_article_image.gif',
        'tags':
        article.get_tags(),
    }
    context_infos.update(_process_single_article(article_infos))

    return _render_response(request, 'detail', context_infos)
Exemplo n.º 5
0
 def get_hottest_articles(self, count=5, has_login=False):
     """ 获取最热门的文章 """
     cond = {'login_required': False} if not has_login else {}
     # 只获取前6个月的数据
     date_time = datetime.datetime.now() - datetime.timedelta(days=180)
     cond['publish_date'] = {
         '$gt': datetime2timestamp(date_time, convert_to_utc=True)
     }
     article_infos = self._db.articles.find(
         cond,
         sort=[('views_count', self.ORDER_DESC)],
         skip=0,
         limit=count,
         fields=ARTICLE_BRIEF_INFO_FIELDS)
     return article_infos
Exemplo n.º 6
0
 def convert_to(self, from_model):
     try:
         return {
             'id':
             from_model.id,
             'title':
             from_model.title,
             'slug':
             from_model.slug,
             'parent_cate_id':
             from_model.category.parent.id,
             'category_id':
             from_model.category.id,
             'category_name':
             from_model.category.name,
             'category_slug':
             from_model.category.slug,
             'description':
             from_model.description,
             'content':
             from_model.content,
             'mark':
             from_model.mark,
             'enable_comment':
             from_model.enable_comment,
             'login_required':
             from_model.login_required,
             'views_count':
             from_model.views_count,
             'comment_count':
             from_model.comment_count,  # TODO: get from comment model?
             'publish_date':
             datetime2timestamp(from_model.publish_date,
                                convert_to_utc=True),
             'thumbnail_url':
             (from_model.thumbnail.path.url if from_model.thumbnail.path
              else from_model.thumbnail.url) if from_model.thumbnail else
             'http://xianglong.qiniudn.com/default_article_image.gif',
             'tags':
             from_model.get_tags(),
             'tag_ids': [tag.id for tag in from_model.tags.all()],
         }
     except Exception, e:
         logger.exception('Convert article failed, error: %s' % e)
         return {}
Exemplo n.º 7
0
 def convert_to(self, from_model):
     try:
         return {
             'id': from_model.id,
             'user_id': from_model.user.id if from_model.user else 0,
             'username': from_model.user.username if from_model.user else '',
             'article_id': from_model.article.id,
             'author': from_model.author,
             'author_email': from_model.author_email,
             'author_website': from_model.author_website,
             'author_ip': from_model.author_ip,
             'author_agent': from_model.author_agent,
             'parent_id': from_model.parent.id if from_model.parent else 0,
             'comment_date': datetime2timestamp(from_model.comment_date, convert_to_utc=True),
             'content': from_model.content,
         }
     except Exception, e:
         logger.exception('Convert comment failed, error: %s' % e)
         return {}
Exemplo n.º 8
0
def preview_article(request, slug):
    try:
        article = Article.objects.get(slug__exact=slug)
    except Article.DoesNotExist:
        logger.warning('Invaild article slug: %s' % slug)
        return _render_404_response(request)

    publish_date = datetime2timestamp(article.publish_date, convert_to_utc=True)
    # get previous and next articles
    prev_a = blog_db.get_prev_article(publish_date)
    next_a = blog_db.get_next_article(publish_date)

    context_infos = {
        'page_title': article.title,
        'prev_a': prev_a,
        'next_a': next_a,
    }
    article_infos = {
        'id': article.id,
        'title': article.title,
        'slug': article.slug,
        'category_id': article.category.id,
        'category_name': article.category.name,
        'category_slug': article.category.slug,
        'description': article.description,
        'content': article.content,
        'mark': article.mark,
        'enable_comment': False,
        'login_required': article.login_required,
        'views_count': article.views_count,
        'publish_date': publish_date,
        'thumbnail_url': (article.thumbnail.path.url if article.thumbnail.path else article.thumbnail.url)
        if article.thumbnail else 'http://xianglong.qiniudn.com/default_article_image.gif',
        'tags': article.get_tags(),
    }
    context_infos.update(_process_single_article(article_infos))

    return _render_response(request, 'detail', context_infos)