def _upsert_comment(comment): """ 操作类型: create:创建评论 approve:通过评论 spam:标记垃圾评论 delete:删除评论 delete-forever:彻底删除评论 """ action = comment.get('action', '') meta = comment.get('meta', None) if meta and isinstance(meta, dict): from blog.models import Article, Comment a_id = meta.get('thread_key') try: if action == 'create': try: article = Article.objects.get(id=int(a_id)) except (Article.DoesNotExist, TypeError, ValueError) as e: print 'Article does not exist, ID: %s, error: %s' % (a_id, e) return c = Comment() c.article = article parent_id = meta.get('parent_id', '0') parent_c = Comment.objects.filter(duoshuo_id=parent_id) c.parent = None if parent_id == '0' or parent_c.count( ) == 0 else parent_c[0] c.duoshuo_id = meta.get('post_id', '') c.duoshuo_user_id = comment.get('user_id', '') c.author = meta.get('author_name', '') c.author_email = meta.get('author_email', '') c.author_website = meta.get('author_url', '') c.author_ip = meta.get('ip', '') c.comment_date = timestamp2datetime( comment.get('date', None), convert_to_local=True) or datetime.datetime.now() c.content = _clean_content(meta.get('message', '')) c.author_agent = '' status = meta.get('status', '') c.status = COMMENT_STATUS.APPROVED if status == 'approved' else ( COMMENT_STATUS.NOT_REVIEWED if status == 'pending' else COMMENT_STATUS.REJECTED) c.sync_status = 0 c.save() print 'Create comment, article ID: %s, comment ID: %s' % (a_id, c.id) elif action == 'approve': Comment.objects.filter(duoshuo_id__in=meta).update( status=COMMENT_STATUS.APPROVED) elif action == 'spam': Comment.objects.filter(duoshuo_id__in=meta).update( status=COMMENT_STATUS.REJECTED) elif action in ('delete', 'delete-forever'): Comment.objects.filter(duoshuo_id__in=meta).update( hided=True, status=COMMENT_STATUS.REJECTED) except Exception, e: print 'update article comment failed, exception: %s, comment: %s' % ( e, comment)
def _get_month_and_day(p_date): """ parse publish_date and get month and day """ infos = {'year': '1990', 'month': '01', 'day': '01', 'month_str': ALL_MONTHS[0], 'publish_date_str': ''} if not p_date or not isinstance(p_date, long): return infos date = timestamp2datetime(p_date, convert_to_local=True).date() date_str = date.strftime(settings.PUBLISH_DATE_FORMAT) infos['year'], infos['month'], infos['day'] = date_str.split('-') infos['month_str'], infos['publish_date_str'] = ALL_MONTHS[date.month - 1], date_str return infos
def _upsert_comment(comment): """ 操作类型: create:创建评论 approve:通过评论 spam:标记垃圾评论 delete:删除评论 delete-forever:彻底删除评论 """ action = comment.get('action', '') meta = comment.get('meta', None) if meta and isinstance(meta, dict): from blog.models import Article, Comment a_id = meta.get('thread_key') try: if action == 'create': try: article = Article.objects.get(id=int(a_id)) except (Article.DoesNotExist, TypeError, ValueError) as e: print 'Article does not exist, ID: %s, error: %s' % (a_id, e) return c = Comment() c.article = article parent_id = meta.get('parent_id', '0') parent_c = Comment.objects.filter(duoshuo_id=parent_id) c.parent = None if parent_id == '0' or parent_c.count() == 0 else parent_c[0] c.duoshuo_id = meta.get('post_id', '') c.duoshuo_user_id = comment.get('user_id', '') c.author = meta.get('author_name', '') c.author_email = meta.get('author_email', '') c.author_website = meta.get('author_url', '') c.author_ip = meta.get('ip', '') c.comment_date = timestamp2datetime(comment.get('date', None), convert_to_local=True) or datetime.datetime.now() c.content = _clean_content(meta.get('message', '')) c.author_agent = '' status = meta.get('status', '') c.status = COMMENT_STATUS.APPROVED if status == 'approved' else (COMMENT_STATUS.NOT_REVIEWED if status == 'pending' else COMMENT_STATUS.REJECTED) c.sync_status = 0 c.save() print 'Create comment, article ID: %s, comment ID: %s' % (a_id, c.id) elif action == 'approve': Comment.objects.filter(duoshuo_id__in=meta).update(status=COMMENT_STATUS.APPROVED) elif action == 'spam': Comment.objects.filter(duoshuo_id__in=meta).update(status=COMMENT_STATUS.REJECTED) elif action in ('delete', 'delete-forever'): Comment.objects.filter(duoshuo_id__in=meta).update(hided=True, status=COMMENT_STATUS.REJECTED) except Exception, e: print 'update article comment failed, exception: %s, comment: %s' % (e, comment)
def convert_from(self, to_model, data): if safe_cast(data.get('user_id', 0), int): to_model.user_id = int(data['user_id']) else: to_model.user = None if safe_cast(data.get('article_id', 0), int): to_model.article_id = int(data['article_id']) else: to_model.article = None if safe_cast(data.get('parent_id', 0), int): to_model.parent_id = int(data['parent_id']) else: to_model.comment = None to_model.author = data.get('author', '') to_model.author_email = data.get('author_email', '') to_model.author_website = data.get('author_website', '') to_model.author_ip = data.get('author_ip', '') to_model.author_agent = data.get('author_agent', '') to_model.comment_date = timestamp2datetime(data.get('comment_date', 0), convert_to_local=True) to_model.content = data.get('content', '') to_model.status = 1 return to_model