Exemple #1
0
class Upload(MethodView):
    decorators = [login_required, writer_permission.require(401)]
    #template_name = 'blog_admin/post.html'
    template_name = 'we do not need template file here'  #test the editor plugin

    def get(self, slug=None, form=None, post_type='get', is_draft=False):
        #never come here
        print "using get method to upload an image is impossible"
        pass

    def post(self, slug=None, form=None, post_type="post"):
        print "you want upload an image here"
        file = request.files.get('editormd-image-file')
        if not file:
            res = {'success': 0, 'message': u'图片格式异常'}
        else:
            ex = os.path.splitext(file.filename)[1]
            filename = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + ex
            file.save(os.path.join(Config.SAVEPIC_PATH, filename))
            #返回
            res = {
                'success': 1,
                'message': u'图片上传成功',
                'url': url_for('static',
                               filename=Config.BASE_IMG_PATH + filename)
            }
        return jsonify(res)
Exemple #2
0
class Post(MethodView):
    decorators = [login_required, writer_permission.require(401)]
    #template_name = 'blog_admin/post.html'
    template_name = 'blog_admin/post_editor.html'  #test the editor plugin

    def get(self, slug=None, form=None, post_type='post', is_draft=False):
        """
        new post page
        """
        edit_flag = slug is not None or False
        post = None
        if edit_flag:
            try:
                post = models.Draft.objects.get(slug=slug)
                post.from_draft = 'true'
            except models.Draft.DoesNotExist:
                post = models.Post.objects.get_or_404(slug=slug)

            if not g.identity.can(
                    editor_permission
            ) and post.author.username != current_user.username:
                abort(401)

        display_slug = slug if slug else 'slug-value'

        if not form:
            if post:
                # post = models.Post.objects.get_or_404(slug=slug)
                print "edit post", post_type
                post.post_id = str(post.id)
                post.tags_str = ', '.join(post.tags)
                form = forms.PostForm(obj=post)
            else:
                print "new post", post_type
                form = forms.PostForm(post_type=post_type)

        categories = models.Post.objects(
            post_type=post_type).distinct('category')
        tags = models.Post.objects(post_type=post_type).distinct('tags')

        context = {
            'edit_flag': edit_flag,
            'form': form,
            'display_slug': display_slug,
            'categories': categories,
            'tags': tags
        }

        # return context
        return render_template(self.template_name, **context)

    def post(self, slug=None, post_type='post', is_draft=False):
        """
        publish the post content
        """
        article_model = article_models['post'] if request.form.get(
            'publish') else article_models['draft']

        form = forms.PostForm(obj=request.form)
        if not form.validate():  #fill the form
            return self.get(slug, form)

        # if slug:
        #     post = article_model.objects.get_or_404(slug=slug)
        # else:
        #     post = article_model()
        #     post.author = get_current_user()

        try:
            post = article_model.objects.get_or_404(slug=slug)
        except:
            post = article_model()
            post.author = get_current_user()

        post.title = form.title.data.strip()
        post.slug = form.slug.data.strip()
        post.weight = form.weight.data

        post.raw = form.raw.data.strip()
        abstract = form.abstract.data.strip()
        post.abstract = abstract if abstract else post.raw[:140]
        post.category = form.category.data.strip() if form.category.data.strip(
        ) else None
        post.tags = [tag.strip() for tag in form.tags_str.data.split(',')
                     ] if form.tags_str.data else None
        post.post_type = form.post_type.data if form.post_type.data else None

        post_urls = {
            'post': url_for('blog_admin.posts'),
            'page': url_for('blog_admin.pages'),
            'wechat': url_for('blog_admin.wechats'),
        }

        draft_urls = {
            'post': url_for('blog_admin.drafts'),
            'page': url_for('blog_admin.page_drafts'),
            'wechat': url_for('blog_admin.wechat_drafts'),
        }

        if request.form.get('publish'):
            post.is_draft = False
            msg = 'Succeed to publish the {0}'.format(post_type)
            # redirect_url = url_for('blog_admin.pages') if form.post_type.data == 'page' else url_for('blog_admin.posts')
            redirect_url = post_urls[form.post_type.data]
            post.save()

            signals.post_pubished.send(current_app._get_current_object(),
                                       post=post)

            try:
                draft = models.Draft.objects.get(slug=slug)
                draft.delete()
            except:
                pass

            try:
                post_statistic = models.PostStatistics.objects.get(post=post)
            except models.PostStatistics.DoesNotExist:
                post_statistic = models.PostStatistics()
                post_statistic.post = post
                post_statistic.verbose_count_base = random.randint(500, 5000)
                post_statistic.save()

        elif request.form.get('draft'):
            post.is_draft = True
            msg = 'Succeed to save the draft'
            # redirect_url = url_for('blog_admin.page_drafts') if form.post_type.data == 'page' else url_for('blog_admin.drafts')
            redirect_url = draft_urls[form.post_type.data]
            post.save()
        else:
            return self.get(slug, form, is_draft)

        flash(msg, 'success')
        return redirect(redirect_url)

    def delete(self, slug):
        if request.args.get('is_draft') and request.args.get(
                'is_draft').lower() == 'true':
            article_model = article_models['draft']
        else:
            article_model = article_models['post']
        post = article_model.objects.get_or_404(slug=slug)
        post_type = post.post_type
        # is_draft = post.is_draft

        try:
            post_statistic = models.PostStatistics.objects.get(post=post)
            post_statistic.delete()
        except:
            pass

        post.delete()

        redirect_url = url_for(
            'blog_admin.pages') if post_type == 'page' else url_for(
                'blog_admin.posts')
        # if is_draft:
        #     redirect_url = redirect_url + '?draft=true'

        flash('Succeed to delete the {0}'.format(post_type), 'success')

        if request.args.get('ajax'):
            return 'success'

        return redirect(redirect_url)