Ejemplo n.º 1
0
    def move(self):
        """

        :return:
        """
        a_from = request.form.get('from')
        a_to = request.form.get('to')
        if a_from and a_to:
            b_from = Banner.objects(id=a_from).first_or_404()
            b_to = Banner.objects(id=a_to).first_or_404()
            b_from.order, b_to.order = b_to.order, b_from.order
            b_from.save()
            b_to.save()
            return jsonify(message='OK', bfrom=b_from.order, bto=b_to.order)
        return jsonify(message='Failed')
Ejemplo n.º 2
0
    def set(self, id):
        """

        :param id:
        :return:
        """
        banner = Banner.objects(id=id).first()
        date_from = request.form.get('from')
        date_until = request.form.get('until')
        if date_from:
            try:
                dt = datetime.datetime.strptime(date_from, '%Y-%m-%d %H:%M:%S')
            except ValueError:
                flash(gettext('invalid date format. example: 2020-01-09 12:11:11'), 'error')
            else:
                banner.date_from = dt
        if date_until:
            try:
                dt = datetime.datetime.strptime(date_until, '%Y-%m-%d %H:%M:%S')
            except ValueError:
                flash(gettext('invalid date format. example: 2020-01-09 12:11:11'), 'error')
            else:
                banner.date_until = dt

        banner.target = request.form.get('target').strip()
        banner.banner_type = request.form.get('banner_type').strip()
        banner.save()
        flash('successfully updated')
        return redirect(redirect_url())
Ejemplo n.º 3
0
    def upload(self):
        """

        :return:
        """
        if request.method == 'GET':
            return self.render('admin/content/upload.html')

        target = request.form.get('boardid', '')
        banner_type = request.form.get('banner_type')
        img = request.files.get('image')
        name, ext = img.filename.rsplit('.', 1)
        name = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')
        filename = secure_filename(name)
        path = '{}/{}.{}'.format('banner', filename, ext)
        url = jobs.image.upload('maybe-img', path, image=img.read(), make_thumbnails=True)

        if banner_type == 'URL':
            if target.startswith('http') == False:
                flash(gettext('upload failed. if you select board, please fill in board id, otherwise fill in url.'))
                return redirect(redirect_url())

        # 广播
        elif banner_type == 'BOARD':
            try:
                board = Board.objects(id=target).first()
            except ValidationError:
                flash('upload failed, please fill in a valid board id')
                return redirect(redirect_url())

        Banner(banner_type=banner_type, target=target, img=url, published=True).save()
        flash(gettext('upload successfully'))

        return redirect(url_for('bannerview.index'))
Ejemplo n.º 4
0
 def unpublish(self, id):
     """
     发布
     :param id:
     :return:
     """
     banner = Banner.objects(id=id).first()
     banner.published = False
     banner.save()
     return redirect(redirect_url())
Ejemplo n.º 5
0
    def delete(self, id):
        """
        根据id删除
        :param id:
        :return:
        """
        banner = Banner.objects(id=id).first()
        if banner:
            banner.delete()

        return redirect(redirect_url())
Ejemplo n.º 6
0
    def upload_img(self, id):
        """
        上传轮播图
        :param id:
        :return:
        """
        banner = Banner.objects(id=id).first()
        img = request.files.get('img')
        if img:
            name, extension = img.filename.rsplit('.', 1)
            name = datetime.datetime.now().strftime('%Y-%m-%d-%H%M%S')
            filename = secure_filename(name)
            path = '{}/{}.{}'.format('banner', filename, extension)
            url = jobs.image.upload('maybe-img', path, image=img.read(), make_thumbnails=True)
            banner.img = url
            banner.save()

        return redirect(redirect_url())
Ejemplo n.º 7
0
 def index(self):
     banners = Banner.objects().order_by('-order')
     return self.render('admin/content/banner.html', banner=banners)