Example #1
0
def pantone():
    page = 1
    per_page = 100
    is_end = False
    total = 1
    while not is_end:
        # 倒序
        data = Color.objects(pantone='').order_by('created_at').paginate(
            page=page, per_page=per_page)
        if not len(data.items):
            print("get data is empty! \n")
            break

        for i, color in enumerate(data.items):
            if color.pantone:
                print('pantone值存在跳过:', str(color._id))
                continue
            pantone = get_pantone(color)
            ok = color.update(pantone=pantone)
            if ok:
                print('更新成功:%s' % str(color._id))
            else:
                print('更新失败:%s' % str(color._id))

        total += 1
        print("current page %s: \n" % page)
        page += 1
        if len(data.items) < per_page:
            is_end = True
    print("is over execute count %s\n" % total)
Example #2
0
 def put(self, name):
     args = parser.parse_args()
     color = Color.get_by_id(name)
     if color:
         color = create_color(name, args.r, args.g, args.b)
         color.put()
         return color.to_dict()
     else:
         return 'Color not found.'
Example #3
0
def color_submit():
    meta = metaInit.copy()
    meta['referer_url'] = request.environ.get(
        'HTTP_REFERER') if request.environ.get('HTTP_REFERER') else ''
    id = request.args.get('id', None)
    meta['data'] = None
    if id:
        item = Color.objects(_id=ObjectId(id)).first()
        meta['data'] = item

    form = SaveForm()

    #current_app.logger.debug(id)

    return render_template('admin/color/submit.html', meta=meta, form=form)
Example #4
0
def color_list():
    meta = metaInit.copy()
    query = {}
    page = int(request.args.get('page', 1))
    per_page = 100
    status = int(request.args.get('status', 0))
    deleted = int(request.args.get('deleted', 0))
    page_url = url_for('admin.color_list', page="#p#", status=status)
    if status == -1:
        meta['css_disable'] = 'active'
        query['status'] = 0
    elif status == 1:
        query['status'] = 1
        meta['css_verify'] = 'active'
    elif status == 5:
        query['status'] = 5
        meta['css_success'] = 'active'
    else:
        meta['css_all'] = 'active'

    query['deleted'] = deleted
    current_app.logger.debug(type(g.user))

    data = Color.objects(**query).order_by('-created_at').paginate(
        page=page, per_page=per_page)
    total_count = Color.objects(**query).count()

    # 过滤数据
    rows = t_color_list(data)

    meta['data'] = rows
    meta['total_count'] = total_count
    pager = Pager(page, per_page, total_count, page_url)
    meta['pager'] = pager.render_view()

    return render_template('admin/color/list.html', meta=meta)
Example #5
0
def color_delete():
    meta = {}

    ids = request.values.get('ids', '')
    type = request.values.get('type', 1)
    if not ids:
        return jsonify(success=False, message='缺少请求参数!')

    try:
        arr = ids.split(',')
        for d in arr:
            color = Color.objects(_id=ObjectId(d)).first()
            if color:
                color.mark_delete() if color else None
    except (Exception) as e:
        return jsonify(success=False, message=str(e))

    return jsonify(success=True,
                   message='操作成功!',
                   data={
                       'ids': ids,
                       'type': type
                   },
                   redirect_to=url_for('admin.color_list'))
Example #6
0
 def get(self, name):
     color = Color.get_by_id(name)
     if color:
         return color.to_dict()
     return 'Color not found.'
Example #7
0
 def get(self):
     color_query = Color.query()
     colors = color_query.fetch()
     colors = [color.to_dict() for color in colors]
     return colors
Example #8
0
def get_tones():
    query = {}
    query['deleted'] = 0
    query['kind'] = 1
    #query['color_ids'] = []
    page = 1
    per_page = 100
    is_end = False
    total = 1
    asset_url = current_app.config['ASSET_URL']
    while not is_end:
        # 倒序
        data = Image.objects(**query).order_by('-created_at').paginate(
            page=page, per_page=per_page)
        if not len(data.items):
            print("get data is empty! \n")
            break
        for i, image in enumerate(data.items):
            if image.color_ids:
                print('色值已存在,跳过...')
                continue
            img_url = ''
            if image.path:
                img_url = os.path.join(asset_url, image.path)
            else:
                print('没有上传图片,跳过..')
                continue
                img_url = image.img_url

            try:
                response = requests.get(img_url)
            except:
                print('网络超时访问图片地址失败', str(image._id))
                continue
            try:
                if response.status_code == 200:
                    img = read_file(response)
                else:
                    print('读取图片失败:%s', response.status_code)
                    continue
            except:
                print('读取文件失败: %s', str(image._id))
                continue
            try:
                img.reduce_size(img.raw_image.size[1])
                img.unstack_pixel()
                img.extract_tones(4)
                color_ids = []
                for j in range(len(img.tones_str)):
                    color = Color.objects(rgb=img.tones_str[j]).first()
                    if color:
                        color_ids.append(str(color._id))
                    else:
                        color = Color(rgb=img.tones_str[j], hex=img.hex[j])
                        ok = color.save()
                        color_ids.append(str(ok._id))
                ok = image.update(color_ids=color_ids)
                if ok:
                    print('更新成功: %s' % str(image._id))
                else:
                    print('更新失败:%s' % str(image._id))
            except:
                print('解析图片失败:%s', str(image._id))
                continue
        total += 1
        print("current page %s: \n" % page)
        page += 1
        if len(data.items) < per_page:
            is_end = True
    print("is over execute count %s\n" % total)