def put_info_into_es(): root = os.path.dirname(os.path.abspath(__file__)) config_path = os.path.join(root, 'config', 'database.yaml') yaml = pyaml.yaml.safe_load(open(config_path)) config = yaml[yaml['stage']] client = MongoClient(config['mongo']['host']) blogtb = client['simpleblog']['simpleblog'] es = ES.connect_host() for blog in blogtb.find(): # print blog['title'], keep_only_words(blog['content']) try: data = { 'id': unicode(blog['_id']), 'content': keep_only_words(blog['content']), 'author': unicode(blog['author']), 'title': blog['title'], 'create_time': format_datetime(blog['create_time'], '%Y-%m-%d'), # 'create_time': format_datetime(blog['create_time'], '%Y/%m/%d %H:%M:%S'), 'update_time': format_datetime(blog['update_time'], '%Y-%m-%d'), # 'update_time': format_datetime(blog['update_time'], '%Y/%m/%d %H:%M:%S'), 'visible': blog['visible'], } except Exception, e: print blog['title'] print e continue # print format_datetime(blog['create_time'], '%Y/%m/%d %H:%M:%S') es.index('simpleblog', 'blogpost', body=json.dumps(data), id=unicode(blog['_id']))
def get_prev_next(cls, blog_id, current_user): """ 获得前一篇文章的id,根据时间倒叙排序 blog_id :return: """ id_list = [] for item in cls.objects(delete_time=None, visible=cls.VISIBLE_ALL).only('id', 'create_time').all(): id_list.append((unicode(item.id), format_datetime(item.create_time))) if current_user.is_authenticated: for item in cls.objects(Q(delete_time=None) & (Q(visible=cls.VISIBLE_LOGIN) | (Q(visible=cls.VISIBLE_OWNER) & Q(author=current_user.id))))\ .only('id', 'create_time').all(): id_list.append((unicode(item.id), format_datetime(item.create_time))) id_list.sort(key=lambda x: x[1]) id_list = map(lambda x: x[0], id_list) try: ind = id_list.index(blog_id) except ValueError: ind = -1 if ind == -1 or len(id_list) <= 1: return None, None elif ind == 0: return None, id_list[1] elif ind == len(id_list) - 1: return id_list[-2], None else: return id_list[ind - 1], id_list[ind + 1]
def as_dict(self): dic = dict(self.to_mongo()) dic['_id'] = unicode(self.id) dic['id'] = unicode(self.id) dic['name_count'] = '%s (%d)' % (self.name, self.count) dic['last_use_time'] = format_datetime(self.last_use_time) dic['create_time'] = format_datetime(self.create_time) dic['update_time'] = format_datetime(self.update_time) dic['delete_time'] = format_datetime(self.delete_time) return dic
def get_all_month(): res_list = set() for item in Blog.objects(delete_time=None): res_list.add(format_datetime(item.create_time, '%Y-%m')) res_list = list(res_list) res_list.sort(reverse=True) return res_list
def post_save(cls, sender, document, **kwargs): data = { 'id': unicode(document.id), 'content': keep_only_words(document.content), 'author': unicode(document.author), 'title': document.title, 'create_time': format_datetime(document.create_time, '%Y-%m-%d'), 'update_time': format_datetime(document.update_time, '%Y-%m-%d'), 'visible': document.visible, 'tags': document.tags } try: es = ES.connect_host() es.index('simpleblog', 'blogpost', body=json.dumps(data), id=unicode(document.id)) except Exception, e: print 'es connect failed, %s' % e
def as_dict(self, with_permisson=True): dic = dict(self.to_mongo()) dic['_id'] = unicode(self.id) dic['id'] = unicode(self.id) try: dic['author'] = BlogUser.objects.with_id(ObjectId(self.author)).username except AttributeError: dic['author'] = unicode(self.author) dic['last_view_user'] = unicode(self.last_view_user) dic['create_time'] = format_datetime(self.create_time) dic['update_time'] = format_datetime(self.update_time) dic['delete_time'] = format_datetime(self.delete_time) dic['last_comment_time'] = format_datetime(self.last_comment_time) dic['summary'] = clean_all_html(self.content)[:20].lstrip(' ') dic['comment_num'] = len(self.comment) if self.comment else 0 if with_permisson: dic['could_delete'] = True dic['could_edit'] = True dic['visible_text'] = self.VISIBLE_TYPE_DICT.get(self.visible, '') return dic
def as_json(self): return { 'username': self.username, 'nickname': self.nickname, '_id': unicode(self['id']), 'email': self.email, 'privileges_text': self.privileges_text(), 'last_login_time_display': format_datetime(self.last_login_time), 'last_login_ip': self.last_login_ip, 'status': self.status, 'status_display': self.get_status_display() }
def as_dict(self, with_permisson=False, cur_user=None): dic = dict(self.to_mongo()) dic['create_time'] = format_datetime(self.created_at, '%Y-%m-%d %H:%M:%S') dic['update_time'] = format_datetime(self.updated_at, '%Y-%m-%d %H:%M:%S') dic['delete_time'] = format_datetime(self.deleted_at, '%Y-%m-%d %H:%M:%S')