Ejemplo n.º 1
0
 def __get_post(table_name, id=None, title=None):
     get_sql = """
                 select id, title, content, user_id, category_id, public, visible,
                 if(updated is NULL, created, updated) last_modified from {0}
                 where deleted = 0 """.format(table_name)
     post = None
     if id:
         get_sql += 'and id = %s'
         post = db.get(get_sql, id)
     if title:
         get_sql += 'and title = %s'
         post = db.get(get_sql, title)
     if post:
         post['last_modified_format'] = post.last_modified.strftime('%Y-%m-%d %H:%M:%S')
         post['last_modified'] = time.mktime(post.last_modified.timetuple())
     return post
Ejemplo n.º 2
0
    def post(self):
        nick = self.get_body_argument('nick', None)
        password = self.get_body_argument('password', None)
        password_confirm = self.get_body_argument('password_confirm', None)
        invite_code = self.get_body_argument('invite_code', None)
        if nick and password and password_confirm and invite_code:
            length = len(password)
            if length >= 6 and length <= 18 and password == password_confirm:
                if Member.isExist(nick):
                    self.send_result(error_code=constants.error_code['user_has_exist'])
                    return
                query_invite_code = 'select count(1) count from tb_invite where code = %s'
                code_num = db.get(query_invite_code, invite_code)
                if code_num and code_num.count:
                    md5 = hashlib.md5()
                    md5.update(password)
                    password_md5 = md5.hexdigest()
                    now = datetime.datetime.now()
                    insert_sql = 'insert into tb_user (nick, password, created) values (%s, %s, %s)'
                    try:
                        member_id = db.insert(insert_sql, nick, password_md5, now)
                        self.send_result(True, error_code=None)
                        return
                    except:
                        pass
                        # TODO add log
                elif not code_num.count:
                    self.send_result(error_code=constants.error_code['invite_code_not_exist'])
                    return

        self.send_result()
Ejemplo n.º 3
0
 def post(self):
     old_password = self.get_body_argument('old_password', None)
     new_password = self.get_body_argument('new_password', None)
     new_password_confirm = self.get_body_argument('new_password_confirm',
                                                   None)
     if old_password and new_password and new_password_confirm:
         if len(new_password) >= 6 and len(new_password) <= 18:
             if new_password == new_password_confirm:
                 md5 = hashlib.md5()
                 md5.update(old_password)
                 password_md5 = md5.hexdigest().upper()
                 query_user = '******'
                 user = db.get(query_user, self.current_user.nick)
                 if user.password.upper() == password_md5:
                     md5 = hashlib.md5()
                     md5.update(new_password)
                     new_password_md5 = md5.hexdigest().upper()
                     update_user = '******'
                     row = db.update(update_user, new_password_md5,
                                     self.current_user.nick)
                     if row:
                         self.send_result(True, error_code=None)
                         return
                     self.send_result()
                     return
                 self.send_result(
                     error_code=constants.error_code['wrong_password'])
                 return
             self.send_result(
                 error_code=constants.error_code['password_confirm_failed'])
             return
         self.send_result(
             error_code=constants.error_code['illegal_password'])
         return
     self.send_result(error_code=constants.error_code['missing_parameters'])
Ejemplo n.º 4
0
    def post(self):
        nick = self.get_body_argument('nick', None)
        password = self.get_body_argument('password', None)
        password_confirm = self.get_body_argument('password_confirm', None)
        invite_code = self.get_body_argument('invite_code', None)
        if nick and password and password_confirm and invite_code:
            length = len(password)
            if length >= 6 and length <= 18 and password == password_confirm:
                if Member.isExist(nick):
                    self.send_result(
                        error_code=constants.error_code['user_has_exist'])
                    return
                query_invite_code = 'select count(1) count from tb_invite where code = %s'
                code_num = db.get(query_invite_code, invite_code)
                if code_num and code_num.count:
                    md5 = hashlib.md5()
                    md5.update(password)
                    password_md5 = md5.hexdigest()
                    now = datetime.datetime.now()
                    insert_sql = 'insert into tb_user (nick, password, created) values (%s, %s, %s)'
                    try:
                        member_id = db.insert(insert_sql, nick, password_md5,
                                              now)
                        self.send_result(True, error_code=None)
                        return
                    except:
                        pass
                        # TODO add log
                elif not code_num.count:
                    self.send_result(error_code=constants.
                                     error_code['invite_code_not_exist'])
                    return

        self.send_result()
Ejemplo n.º 5
0
    def get(self, nick=None):
        posts = None
        if not nick:
            self.redirect('/posts')
            return

        select_user = '******'
        user = db.get(select_user, nick)
        if user:
            default_size = 10
            summary_length = 200
            need_pagination = False
            size = self.get_query_argument('size', default_size)
            count = Post.count_posts(user.id)
            posts = Post.list(user.id, 0, default_size)
            if posts:
                need_pagination = count > len(posts)
                for post in posts:
                    _html = markdown.markdown(post.content)
                    soup = BeautifulSoup(_html, 'html.parser')
                    img = soup.find('img')
                    if img:
                        img['class'] = 'inner-img-limit'
                    _text = soup.get_text()
                    if _text and len(_text) > summary_length:
                        _text = _text[0:summary_length] + '...'
                    post['cover'] = img
                    post['summary'] = _text
                    post['author'] = user

            self.render('main.html', user=user, posts=posts,
                page_size=size, need_pagination=int(need_pagination))
Ejemplo n.º 6
0
 def post(self):
     old_password = self.get_body_argument('old_password', None)
     new_password = self.get_body_argument('new_password', None)
     new_password_confirm = self.get_body_argument('new_password_confirm', None)
     if old_password and new_password and new_password_confirm:
         if len(new_password) >= 6 and len(new_password) <= 18:
             if new_password == new_password_confirm:
                 md5 = hashlib.md5()
                 md5.update(old_password)
                 password_md5 = md5.hexdigest().upper()
                 query_user = '******'
                 user = db.get(query_user, self.current_user.nick)
                 if user.password.upper() == password_md5:
                     md5 = hashlib.md5()
                     md5.update(new_password)
                     new_password_md5 = md5.hexdigest().upper()
                     update_user = '******'
                     row = db.update(update_user, new_password_md5, self.current_user.nick)
                     if row:
                         self.send_result(True, error_code=None)
                         return
                     self.send_result()
                     return
                 self.send_result(error_code=constants.error_code['wrong_password'])
                 return
             self.send_result(error_code=constants.error_code['password_confirm_failed'])
             return
         self.send_result(error_code=constants.error_code['illegal_password'])
         return
     self.send_result(error_code=constants.error_code['missing_parameters'])
Ejemplo n.º 7
0
    def get(self, nick=None):
        posts = None
        if not nick:
            self.redirect('/posts')
            return

        select_user = '******'
        user = db.get(select_user, nick)
        if user:
            default_size = 10
            summary_length = 200
            need_pagination = False
            size = self.get_query_argument('size', default_size)
            count = Post.count_posts(user.id)
            posts = Post.list(user.id, 0, default_size)
            if posts:
                need_pagination = count > len(posts)
                for post in posts:
                    _html = markdown.markdown(post.content)
                    soup = BeautifulSoup(_html, 'html.parser')
                    img = soup.find('img')
                    if img:
                        img['class'] = 'inner-img-limit'
                    _text = soup.get_text()
                    if _text and len(_text) > summary_length:
                        _text = _text[0:summary_length] + '...'
                    post['cover'] = img
                    post['summary'] = _text
                    post['author'] = user

            self.render('main.html',
                        user=user,
                        posts=posts,
                        page_size=size,
                        need_pagination=int(need_pagination))
Ejemplo n.º 8
0
    def get_current_user(self):
        current_nick = self.get_secure_cookie('current_user')
        current_user = None
        if current_nick:
            query = 'select id, nick from tb_user where nick = %s'
            current_user = db.get(query, current_nick)

        return current_user
Ejemplo n.º 9
0
 def __count(table_name, visible=1, deleted=0, user_id=None, category_id=None):
     count_sql = 'select count(1) count from {0} where visible = %s and deleted = %s '.format(table_name)
     if user_id and category_id:
         count_sql += 'and user_id = %s and category_id = %s'
         count_res = db.get(count_sql, visible, deleted, user_id, category_id)
         return count_res.count
     elif user_id:
         count_sql += 'and user_id = %s'
         count_res = db.get(count_sql, visible, deleted, user_id)
         return count_res.count
     elif category_id:
         count_sql += 'and category_id = %s'
         count_res = db.get(count_sql, visible, deleted, category_id)
         return count_res.count
     else:
         count_res = db.get(count_sql, visible, deleted)
         return count_res.count
Ejemplo n.º 10
0
 def __get_post(table_name, id=None, title=None):
     get_sql = """
                 select id, title, content, user_id, category_id, public, visible,
                 if(updated is NULL, created, updated) last_modified from {0}
                 where deleted = 0 """.format(table_name)
     post = None
     if id:
         get_sql += 'and id = %s'
         post = db.get(get_sql, id)
     if title:
         get_sql += 'and title = %s'
         post = db.get(get_sql, title)
     if post:
         post['last_modified_format'] = post.last_modified.strftime(
             '%Y-%m-%d %H:%M:%S')
         post['last_modified'] = time.mktime(post.last_modified.timetuple())
     return post
Ejemplo n.º 11
0
 def post(self):
     category = self.get_body_argument('cate_name')
     if category:
         query_exist = 'select count(*) count from tb_category where name = %s and user_id = %s'
         num = db.get(query_exist, category, self.current_user.id)
         if num and num.count:
             self.write({'success': False, 'error_code': constants.error_code['category_already_exist']})
             self.finish()
             return
         query_new = 'insert into tb_category (name, user_id, visible) values (%s, %s, %s)'
         id = db.insert(query_new, category, self.current_user.id, 1)
         if id:
             self.write({'success': True, 'category_id': id})
             self.finish()
Ejemplo n.º 12
0
 def __count(table_name,
             visible=1,
             deleted=0,
             user_id=None,
             category_id=None):
     count_sql = 'select count(1) count from {0} where visible = %s and deleted = %s '.format(
         table_name)
     if user_id and category_id:
         count_sql += 'and user_id = %s and category_id = %s'
         count_res = db.get(count_sql, visible, deleted, user_id,
                            category_id)
         return count_res.count
     elif user_id:
         count_sql += 'and user_id = %s'
         count_res = db.get(count_sql, visible, deleted, user_id)
         return count_res.count
     elif category_id:
         count_sql += 'and category_id = %s'
         count_res = db.get(count_sql, visible, deleted, category_id)
         return count_res.count
     else:
         count_res = db.get(count_sql, visible, deleted)
         return count_res.count
Ejemplo n.º 13
0
 def get(self):
     count_code = 'select count(1) count from tb_invite where inviter_id = %s and used = 0'
     count_obj = db.get(count_code, self.current_user.id)
     if count_obj.count >= 3:
         self.send_result(error_code=constants.error_code['invitation_reached_limit'])
         return
     code = ''
     for i in range(8):
         code += str(random.randint(0, 9))
     insert_code = 'insert into tb_invite (inviter_id, code, created, used) values (%s, %s, %s, %s)'
     id = db.insert(insert_code, self.current_user.id, code, datetime.datetime.now(), 0)
     if id:
         self.send_result(True, code, None)
         return
     self.send_result()
Ejemplo n.º 14
0
 def get(self):
     category = self.get_query_argument('cate_name', None)
     if category:
         query_exist = 'select count(*) count from tb_category where name = %s and user_id = %s'
         num = db.get(query_exist, category, self.current_user.id)
         if num and num.count:
             self.write({'success': True, 'exist': True})
             self.finish()
             return
         elif not num.count:
             self.write({'success': True, 'exist': False})
             self.finish()
             return
     self.write({'success': False, 'error_code': constants.error_code['missing_parameters']})
     self.finish()
Ejemplo n.º 15
0
 def get(self):
     count_code = 'select count(1) count from tb_invite where inviter_id = %s and used = 0'
     count_obj = db.get(count_code, self.current_user.id)
     if count_obj.count >= 3:
         self.send_result(
             error_code=constants.error_code['invitation_reached_limit'])
         return
     code = ''
     for i in range(8):
         code += str(random.randint(0, 9))
     insert_code = 'insert into tb_invite (inviter_id, code, created, used) values (%s, %s, %s, %s)'
     id = db.insert(insert_code, self.current_user.id, code,
                    datetime.datetime.now(), 0)
     if id:
         self.send_result(True, code, None)
         return
     self.send_result()
Ejemplo n.º 16
0
 def get(self):
     category = self.get_query_argument('cate_name', None)
     if category:
         query_exist = 'select count(*) count from tb_category where name = %s and user_id = %s'
         num = db.get(query_exist, category, self.current_user.id)
         if num and num.count:
             self.write({'success': True, 'exist': True})
             self.finish()
             return
         elif not num.count:
             self.write({'success': True, 'exist': False})
             self.finish()
             return
     self.write({
         'success': False,
         'error_code': constants.error_code['missing_parameters']
     })
     self.finish()
Ejemplo n.º 17
0
    def get(self, id):
        post = Post.get_post(id)
        if post:
            if not ord(post.public):
                if not self.current_user:
                    self.write_error(403, '您没有该文章的阅读权限')
                    return
                elif self.current_user.id != post.user_id:
                    self.write_error(403, '您没有该文章的阅读权限')
                    return
            query_author = 'select id, nick from tb_user where id = %s'
            author = db.get(query_author, post.user_id)
            post['author'] = author
            post = json.dumps(post, cls=modules.utils.JSONEncoder)
            self.send_result(True, post, None)
            return

        self.write_error(404)
Ejemplo n.º 18
0
    def get(self, id):
        post = Post.get_post(id)
        if post:
            if not ord(post.public):
                if not self.current_user:
                     self.write_error(403, '您没有该文章的阅读权限')
                     return
                elif self.current_user.id != post.user_id:
                    self.write_error(403, '您没有该文章的阅读权限')
                    return
            query_author = 'select id, nick from tb_user where id = %s'
            author = db.get(query_author, post.user_id)
            post['author'] = author
            post = json.dumps(post, cls=modules.utils.JSONEncoder)
            self.send_result(True, post, None)
            return

        self.write_error(404)
Ejemplo n.º 19
0
 def post(self):
     category = self.get_body_argument('cate_name')
     if category:
         query_exist = 'select count(*) count from tb_category where name = %s and user_id = %s'
         num = db.get(query_exist, category, self.current_user.id)
         if num and num.count:
             self.write({
                 'success':
                 False,
                 'error_code':
                 constants.error_code['category_already_exist']
             })
             self.finish()
             return
         query_new = 'insert into tb_category (name, user_id, visible) values (%s, %s, %s)'
         id = db.insert(query_new, category, self.current_user.id, 1)
         if id:
             self.write({'success': True, 'category_id': id})
             self.finish()
Ejemplo n.º 20
0
 def post(self):
     post_id = self.get_body_argument('post_id', None)
     if post_id:
         has_liked = self.get_cookie('like')
         if not has_liked:
             now = datetime.datetime.now()
             if self.current_user:
                 has_user_liked = 'select count(1) count from tb_interaction where user_id = %s and post_id = %s'
                 liked_count = db.get(has_user_liked, self.current_user.id, int(post_id))
                 if not liked_count.count:
                     add_like = 'insert into tb_interaction (user_id, post_id, `like`, unlike, created) values (%s, %s, %s, %s, %s)'
                     like_id = db.insert(add_like, self.current_user.id, post_id, 1, 0, now)
                     if like_id:
                         #self.set_cookie('like_post_{0}'.format(post_id), '1', expires_days=999)
                         self.send_result(True, error_code=None)
                         return
             else:
                 add_like = 'insert into tb_interaction (post_id, `like`, unlike, created) values (%s, %s, %s, %s)'
                 like_id = db.insert(add_like, post_id, 1, 0, now)
                 if like_id:
                     #self.set_cookie('like_post_{0}'.format(post_id), '1', expires_days=999)
                     self.send_result(True, error_code=None)
Ejemplo n.º 21
0
    def get(self, id):
        read_mode = self.get_query_argument('mode', None)
        post = Post.get_post(id)
        if post:
            if not ord(post.public):
                if not self.current_user:
                    raise tornado.web.HTTPError(403)
                    return
                elif self.current_user.id != post.user_id:
                    raise tornado.web.HTTPError(403)
                    return
            query_author = 'select id, nick from tb_user where id = %s'
            author = db.get(query_author, post.user_id)
            post['author'] = author
        else:
            raise tornado.web.HTTPError(404)

        template_name = 'post.html'
        if self.is_mobile:
            template_name = 'read_focus.html'
        elif read_mode and read_mode == 'focus_read':
                template_name = 'read_focus.html'

        self.render(template_name, post=post)
Ejemplo n.º 22
0
 def post(self):
     post_id = self.get_body_argument('post_id', None)
     if post_id:
         has_liked = self.get_cookie('like')
         if not has_liked:
             now = datetime.datetime.now()
             if self.current_user:
                 has_user_liked = 'select count(1) count from tb_interaction where user_id = %s and post_id = %s'
                 liked_count = db.get(has_user_liked, self.current_user.id,
                                      int(post_id))
                 if not liked_count.count:
                     add_like = 'insert into tb_interaction (user_id, post_id, `like`, unlike, created) values (%s, %s, %s, %s, %s)'
                     like_id = db.insert(add_like, self.current_user.id,
                                         post_id, 1, 0, now)
                     if like_id:
                         #self.set_cookie('like_post_{0}'.format(post_id), '1', expires_days=999)
                         self.send_result(True, error_code=None)
                         return
             else:
                 add_like = 'insert into tb_interaction (post_id, `like`, unlike, created) values (%s, %s, %s, %s)'
                 like_id = db.insert(add_like, post_id, 1, 0, now)
                 if like_id:
                     #self.set_cookie('like_post_{0}'.format(post_id), '1', expires_days=999)
                     self.send_result(True, error_code=None)
Ejemplo n.º 23
0
 def getByNick(cls, nick):
     query = 'select id, nick, password, created, updated from tb_user where nick = %s'
     member = db.get(query, nick)
     return member
Ejemplo n.º 24
0
 def getById(cls, id):
     query = 'select id, nick, password, created, updated from tb_user where id = %s'
     member = db.get(query, id)
     return member
Ejemplo n.º 25
0
 def isExist(cls, nick):
     query_count = 'select count(nick) count from tb_user where nick = %s'
     count_obj = db.get(query_count, nick)
     return count_obj.count > 0
Ejemplo n.º 26
0
 def get(cls, category_id):
     get_one = 'select id, name from tb_category where nick = %s'
     category = db.get(get_one)
     return category
Ejemplo n.º 27
0
 def get(cls, category_id):
     get_one = 'select id, name from tb_category where nick = %s'
     category = db.get(get_one)
     return category