Esempio n. 1
0
    def get(self):
        default_size = 10
        summary_length = 200
        cate_id = self.get_query_argument('cate', None)
        size = self.get_query_argument('size', default_size)
        categories = Category.list(self.current_user.id)
        user_id = self.current_user.id
        if cate_id:
            drafts = Post.drafts_by_category(user_id, int(cate_id), 0, int(size))
            count = Post.count_drafts_by_category(user_id, cate_id)
        else:
            drafts = Post.drafts(user_id, 0, int(size))
            count = Post.count_drafts(user_id)
        for draft in drafts:
            draft['author'] = self.current_user
            _html = markdown.markdown(draft.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] + '...'
            draft['cover'] = img
            draft['summary'] = _text

        self.render('drafts.html', cate_id=cate_id, categories=categories, drafts=drafts)
Esempio n. 2
0
    def get(self):
        default_size = 10
        summary_length = 200
        need_pagination = False
        cate_id = self.get_query_argument('cate', None)
        size = self.get_query_argument('size', default_size)
        categories = Category.list(self.current_user.id)
        user_id = self.current_user.id
        if cate_id:
            posts = Post.list_by_category(user_id, int(cate_id), 0, int(size))
            count = Post.count_posts_by_category(user_id, cate_id)
        else:
            count = Post.count_posts(user_id)
            posts = Post.list(user_id, 0, int(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')
                last_modified = post.last_modified
                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'] = self.current_user

        self.render('posts.html', cate_id=cate_id, categories=categories,
            posts=posts, page_size=size, need_pagination=int(need_pagination))
Esempio n. 3
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))
Esempio n. 4
0
    def post(self):

        subject = self.request.get("subject")
        content = self.request.get("content")

        if subject and content:
            post = Post(subject = subject, content = content)
            post.put()
            self.top_posts(update=True)
            self.redirect("/blog/"+str(post.key().id()))
        else:
            error = "we need both a subject and content!"
            self.render_new_post(subject,content,error)
Esempio n. 5
0
def post_links():
    session = Session()
    data = json.loads(request.data)

    for link in data['links']:
        post = Post.get_or_create(session, link['reddit_id'])
        subreddit = Subreddit.get_or_create(session, link['sub_name'])
        user = User.get_or_create(session, link['authorname'])

        post.subreddit_id   = subreddit.id
        post.author_id      = user.id

        post.domain = link['domain']
        post.title  = link['title']
        post.url    = link['url']
        post.score  = link['score']
        post.downs  = link['downs']
        post.ups    = link['ups']

        post.is_self = link['is_self']
        post.over_18 = link['over_18']
        post.thumbnail = link['thumbnail']
        post.created = float(link['created'])
        post.scraped_time = Util.now()

        session.add(post)

    session.commit()
    session.close()
    return jsonify({'received':True})
Esempio n. 6
0
def index():
    if current_user.is_authenticated():
        date_string = make_datestring(today())
        try:
            post = Post.objects.get(
                date_string=date_string, owner=current_user.id
            )
        except Post.DoesNotExist:
            post = Post(date_string=date_string, owner=current_user.id)
            post.save()
        return render_template(
            'index.html',
            post=post,
            past_status=past_status(),
            user=current_user
        )
    return render_template('unauth_index.html')
Esempio n. 7
0
 def post(self, id, update=False):
     post_id = int(id)
     res = self.client.get(id)
     if res is None or update:
         post = Post.get_by_id(post_id)
         last_update = datetime.utcnow()
         res = (post,last_update)
         self.client.set(id, res)
     return res
Esempio n. 8
0
def update(session, user, praw_post, score):
    post = Post.get_or_create(session, praw_post.id)
    post.update_from_praw(praw_post)
    post.author_id = user.id
    Util.update_subreddit(Subreddit, post, session, praw_post.subreddit.display_name)
    vote = Vote.get_or_create(session, user, post, score)
    vote.vote = score

    session.add(vote)
    session.add(post)
Esempio n. 9
0
    def get(self, post_id=None):
        user_id = self.current_user.id
        post = None
        if post_id:
            _post = Post.get_post(post_id)
            if _post and _post.user_id == user_id:
                post = _post
        get_categories = 'select id, name from tb_category where visible = 1 and user_id = %s'
        categories = db.query(get_categories, user_id)
        referer = self.request.headers.get('Referer') or '/'

        self.render('edit.html', categories=categories, post=post, referer=referer)
Esempio n. 10
0
def past_status():
    post_map = dict()
    posts = Post.objects(owner=current_user.id).order_by('-date').limit(29)
    for post in posts:
        post_map[post.date_string] = (
            post.completed, post.length, post.url_string()
        )
    return [
        post_map.get(
            make_datestring(today() - timedelta(days=i)), (False, 0, None)
        )
        for i in reversed(range(1, 29))
    ]
Esempio n. 11
0
    def get(self):
        res = {'data': None}
        need_pagination = False
        summary_length = 260
        offset = self.get_query_argument('offset', 0)
        size = self.get_query_argument('size', 10)
        user_id = self.current_user.id
        count = Post.count_posts(user_id)
        posts = Post.list(user_id, int(offset), int(size))
        if posts:
            need_pagination = count > (int(offset) + int(size))
            for post in posts:
                _html = markdown.markdown(post.content)
                soup = BeautifulSoup(_html, 'html.parser')
                _text = soup.get_text()
                if _text and len(_text) > summary_length:
                    _text = _text[0:summary_length] + '...'
                post['summary'] = _text
                post['author'] = self.current_user

            data = {'posts': posts, 'need_pagination': need_pagination}

            self.send_result(True, data, None)
Esempio n. 12
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)
Esempio n. 13
0
def main(notify):
    session = Session()
    gen = r.get_subreddit("all").get_top(limit=3000)

    start = session.query(Post).count()
    notify("Getting posts, initial count: %d" % start)
    count = 0

    for post in gen:
        count += 1

        p = Post.get_or_create(session, post.id)
        p.update_from_praw(post)

        author_name = Util.patch_author(post.author)
        Util.update_user(User, p, session, author_name)
        Util.update_subreddit(Subreddit, p, session, post.subreddit.display_name)

        session.add(p)
        session.commit()

    diff = session.query(Post).count() - start
    notify("Added %d posts" % diff)
Esempio n. 14
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)
Esempio n. 15
0
def create_post():
    session = Session()
    post = Post.create('woops', sync=True, session=session)
    session.close()

    return post.slug
Esempio n. 16
0
def get_past():
    if not current_user.is_authenticated():
        return redirect(url_for("index"))

    posts = Post.objects(owner=current_user.id, length__gt=0).order_by('-date')
    return render_template('past.html', posts=posts)
Esempio n. 17
0
 def delete(self, post_id):
     if Post.pretend_delete(post_id):
         self.send_result(True, error_code=None)
         return
     self.send_result()