Пример #1
0
def edit(id):
    compose = Compose.query.get_or_404(id)
    if current_user != compose.author and not current_user.can(Permission.ADMINISTER):
        abort(403)
    form = ComposeForm()
    if form.validate_on_submit():
        compose.body = form.body.data
        db.session.add(compose)
        db.session.commit()
        flash('The article has been updated.')
        return redirect(url_for('main.compose', id=compose.id))
    form.body.data = compose.body
    return render_template('edit.html', form = form, compose = compose)
Пример #2
0
def edit(id):
    compose = Compose.query.get_or_404(id)
    if current_user != compose.author and not current_user.can(
            Permission.ADMINISTER):
        abort(403)
    form = ComposeForm()
    if form.validate_on_submit():
        compose.body = form.body.data
        db.session.add(compose)
        db.session.commit()
        flash('The article has been updated.')
        return redirect(url_for('main.compose', id=compose.id))
    form.body.data = compose.body
    return render_template('edit.html', form=form, compose=compose)
Пример #3
0
def open_message_thread(recipients, subject, template, sender, context={}, send=True, message=None):
    body = ""
    if template:
        t = get_template(template)
        body = t.render(Context({}))
    else:
        body = message

    from forms import ComposeForm  # temporary here to remove circular dependence

    compose_form = ComposeForm(data={"recipient": recipients, "subject": subject, "body": body})
    if compose_form.is_valid():
        (thread, new_message) = compose_form.save(sender=sender, send=send)

    return (thread, new_message)
Пример #4
0
def open_message_thread(recipients, subject, template,
                        sender, context={}, send=True, message=None):
    body = ''
    if template:
        t = get_template(template)
        body = t.render(Context(context))
    else:
        body = message

    from forms import ComposeForm  # temporary here to remove circular dependence
    compose_form = ComposeForm(data={
        "recipient": recipients,
        "subject": subject,
        "body": body
    })
    if compose_form.is_valid():
        (thread, new_message) = compose_form.save(sender=sender, send=send)

    return (thread, new_message)
Пример #5
0
def show_entries():
    form = ComposeForm()
    page = request.args.get('page', 1, type = int)
    pagination = Compose.query.order_by(Compose.timestamp.desc()).paginate(
                 page, per_page = 10,
                 error_out = False)
    composes = pagination.items

    if current_user.can(Permission.WRITE_ARTICLES) and \
       form.validate_on_submit():
        new_compose = Compose(title = form.title.data,
                              body = form.body.data,
                              author = current_user._get_current_object())
        db.session.add(new_compose)
        db.session.commit()
        flash('New entry was successfully posted')
        return redirect(url_for('main.show_entries'))

    return render_template('show_entries.html', form=form, entries=composes,
                           pagination = pagination, Permission=Permission,
                           pagedown = pagedown)
Пример #6
0
def show_entries():
    form = ComposeForm()
    page = request.args.get('page', 1, type=int)
    pagination = Compose.query.order_by(Compose.timestamp.desc()).paginate(
        page, per_page=10, error_out=False)
    composes = pagination.items

    if current_user.can(Permission.WRITE_ARTICLES) and \
       form.validate_on_submit():
        new_compose = Compose(title=form.title.data,
                              body=form.body.data,
                              author=current_user._get_current_object())
        db.session.add(new_compose)
        db.session.commit()
        flash('New entry was successfully posted')
        return redirect(url_for('main.show_entries'))

    return render_template('show_entries.html',
                           form=form,
                           entries=composes,
                           pagination=pagination,
                           Permission=Permission,
                           pagedown=pagedown)
Пример #7
0
 def initialize(self, *args,  **kwargs):
     self.form = ComposeForm(self.request.arguments)
Пример #8
0
class ComposeHandler(MainHandler):
    _re_tags_separator = re.compile(r'(\/|\\|\,|\ |\|){1,}')
    _time_delta = datetime.timedelta(hours=8)

    def initialize(self, *args,  **kwargs):
        self.form = ComposeForm(self.request.arguments)

    def UTC2BeiJingTime(self, time):
        '''the server may be anywhere in the world,
        so we manually adjust the time'''
        return time + self._time_delta

    def BeiJing2UTCTime(self, time):
        return time - self._time_delta

    def separate_tags(self, tags_str):
        # first,converted to lowercase and
        # all of the separator is replaced with ` `
        # second, strip repalced_str  (sometimes user input tags :"linux,")
        # third, split with ` `
        # more flexible than direct invoke `re.split`
        return self._re_tags_separator.sub(
            ' ', tags_str.lower()).strip().split()

    @tornado.web.authenticated
    @gen.coroutine
    def get(self, title):
        post = yield Post.asyncQuery(title=title).first()
        self.form.create_time.data = \
            self.UTC2BeiJingTime(datetime.datetime.utcnow())
        if post:
            self.form.post_id.data = post.id
            self.form.title.data = post.title
            self.form.content.data = post.content
            self.form.markdown.data = post.markdown
            self.form.create_time.data = \
                self.UTC2BeiJingTime(post.create_time)
            self.form.category.data = post.category.name
            self.form.tags.data = ','.join(post.tags)
        self.render("compose.html", form=self.form)

    @tornado.web.authenticated
    @gen.coroutine
    def post(self, title):
        if self.form.validate():
            post = None

            if self.form.post_id.data:
                post = yield Post.asyncQuery(id=self.form.post_id.data).first()
            if post:
                post.modified_time = datetime.datetime.utcnow()
            else:
                post = Post()
                post.create_time = \
                    self.BeiJing2UTCTime(self.form.create_time.data)

            title = self.form.title.data.replace(' ', '-')
            content = self.form.content.data
            markdown_text = self.form.markdown.data
            tags = self.separate_tags(self.form.tags.data)
            category_str = self.form.category.data.capitalize()
            category = yield Category.asyncQuery(
                name=category_str).first()
            if not category:
                category = Category()
                category.name = category_str
                yield category.save()

            post.title = title
            post.content = content
            post.markdown = markdown_text
            post.category = category
            post.tags = tags
            post.author = self.current_user

            yield post.save()
            self.flash("compose finsh!")
            return self.redirect('/blog')
        self.render("compose.html", form=self.form)
Пример #9
0
 def initialize(self, *args, **kwargs):
     self.form = ComposeForm(self.request.arguments)
Пример #10
0
class ComposeHandler(MainHandler):
    _re_tags_separator = re.compile(r'(\/|\\|\,|\ |\|){1,}')
    _time_delta = datetime.timedelta(hours=8)

    def initialize(self, *args, **kwargs):
        self.form = ComposeForm(self.request.arguments)

    def UTC2BeiJingTime(self, time):
        '''the server may be anywhere in the world,
        so we manually adjust the time'''
        return time + self._time_delta

    def BeiJing2UTCTime(self, time):
        return time - self._time_delta

    def separate_tags(self, tags_str):
        # first,converted to lowercase and
        # all of the separator is replaced with ` `
        # second, strip repalced_str  (sometimes user input tags :"linux,")
        # third, split with ` `
        # more flexible than direct invoke `re.split`
        return self._re_tags_separator.sub(' ',
                                           tags_str.lower()).strip().split()

    @tornado.web.authenticated
    @gen.coroutine
    def get(self, title):
        post = yield Post.asyncQuery(title=title).first()
        self.form.create_time.data = \
            self.UTC2BeiJingTime(datetime.datetime.utcnow())
        if post:
            self.form.post_id.data = post.id
            self.form.title.data = post.title
            self.form.content.data = post.content
            self.form.markdown.data = post.markdown
            self.form.create_time.data = \
                self.UTC2BeiJingTime(post.create_time)
            self.form.category.data = post.category.name
            self.form.tags.data = ','.join(post.tags)
        self.render("compose.html", form=self.form)

    @tornado.web.authenticated
    @gen.coroutine
    def post(self, title):
        if self.form.validate():
            post = None

            if self.form.post_id.data:
                post = yield Post.asyncQuery(id=self.form.post_id.data).first()
            if post:
                post.modified_time = datetime.datetime.utcnow()
            else:
                post = Post()
                post.create_time = \
                    self.BeiJing2UTCTime(self.form.create_time.data)

            title = self.form.title.data.replace(' ', '-')
            content = self.form.content.data
            markdown_text = self.form.markdown.data
            tags = self.separate_tags(self.form.tags.data)
            category_str = self.form.category.data.capitalize()
            category = yield Category.asyncQuery(name=category_str).first()
            if not category:
                category = Category()
                category.name = category_str
                yield category.save()

            post.title = title
            post.content = content
            post.markdown = markdown_text
            post.category = category
            post.tags = tags
            post.author = self.current_user

            yield post.save()
            self.flash("compose finsh!")
            return self.redirect('/blog')
        self.render("compose.html", form=self.form)