<commit_before>#coding:utf-8
from cgi import FieldStorage
import json
from uuid import uuid4
from schema.product_master import ProductMasterSchema
from schema.product_image import ProductImageSchema
from schema.product_tag import ProductTagSchema
from bson.dbref import DBRef
from schema.product import ProductSchema
from bson.objectid import ObjectId
import colander

from cornice import Service
from webob import exc
from webob.response import Response

product_masters = Service(name='product_masters', path='/productMasters', description="products master",
                          cors_origins=('*',))
product_master = Service(name='product_master', path='/productMasters/{master_id}', description="product master",
                          cors_origins=('*',))


product_tags = Service(name='products_tags', path='/productTags', description="products master tag",
                        cors_origins=('*',))


products = Service(name='products', path='/products', description="products", cors_origins=('*',))
product = Service(name='product', path='/products/{product_id}', description="product detail", cors_origins=('*',))


image = Service(name='image', path='/image', description="upload image", cors_origins=('*',))


product_images = Service(name='product_images', path='/productImages', description='product images', cors_origins=('*',))
product_image = Service(name='product_image', path='/productImages/{image_id}', description='product image', cors_origins=('*',))



@image.post()
def upload_image(request):
    up = request.up
    file = request.params.get('file')
    file_ext = '.' + file.type.split('/')[-1]
    image_url = '/products/' + uuid4().hex + file_ext
    up.put(image_url, file.file, checksum=True)
    return {'image': 'http://keptrans.b0.upaiyun.com' + image_url}


class UnauthorizedView(exc.HTTPError):
    def __init__(self, msg=u'Unauthorized'):
        body = {'status': 401, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 401
        self.content_type = 'application/json'


class BadRequestView(exc.HTTPError):
    def __init__(self, msg=u'Bad request, missing data.'):
        body = {'status': 400, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 400
        self.content_type = 'application/json'


class NotFoundView(exc.HTTPError):
    def __init__(self, msg=u'Not Found.'):
        body = {'status': 404, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 404
        self.content_type = 'application/json'


product_master_error = {
    'brandNone': u'品牌不能为空',
    'categoryNone': u'所属类目不能为空',
    'coverNone': u'封面地址不能为空',
    'descNone': u'描述不能为空',
    'tags': u'标签不能为空'
}


def validate_product_master(request):
    try:
        schema = ProductMasterSchema()
        new_product = schema.deserialize(request.json_body['productMaster'])
        request.validated['productMaster'] = new_product
    except colander.Invalid as e:
        errors = e.asdict()
        for error_name, error_value in errors.items():
            if error_value == 'Required':
                request.errors.add('body', error_name, product_master_error[error_name + 'None'])
            else:
                request.errors.add('body', error_name, u'发生未知错误')
    except Exception as e:
        request.errors.add('body', 'unhandled_error', e.message)


@product_masters.post(content_type="application/json", validators=(validate_product_master,))
def add_product_master(request):
    new_product_master = request.validated['productMaster']
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = DBRef('product_tag', ObjectId(new_product_master['tags'][i]))
    db = request.db
    db['product_master'].insert(new_product_master)
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = str(new_product_master['tags'][i].id)
    new_product_master['id'] = str(new_product_master['_id'])
    del new_product_master['_id']
    return {'productMaster': new_product_master}


@product_masters.get()
def get_all_product_master(request):
    db = request.db
    tags_list = []
    products_list = []
    images_list = []

    master_list = []


    for master in db['product_master'].find():
        tag_id_list = []
        for tag in master['tags']:
            tag = db.dereference(tag)
            tag['id'] = str(tag['_id'])
            del tag['_id']
            tag_id_list.append(tag['id'])
            tags_list.append(tag)
        master['tags'] = tag_id_list
        product_id_list = []
        for pro in db['product'].find({'productMaster.$id': master['_id']}):
            pro['id'] = str(pro['_id'])
            del pro['_id']
            del pro['productMaster']
            product_id_list.append(pro['id'])
            products_list.append(pro)
        master['products'] = product_id_list
        image_id_list = []
        for img in db['product_image'].find({'productMaster.$id': master['_id']}):
            img['id'] = str(img['_id'])
            del img['_id']
            del img['productMaster']
            image_id_list.append(img['id'])
            images_list.append(img)
        master['images'] = image_id_list
        master['id'] = str(master['_id'])
        del master['_id']
        master_list.append(master)
    return {'productMasters': master_list, 'productTags': tags_list, 'productImages': images_list, 'products': products_list}


@product_master.put(content_type="application/json", validators=(validate_product_master,))
def update_product_master(request):
    master_id = ObjectId(request.matchdict['master_id'])
    new_product_master = request.validated['productMaster']
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = DBRef('product_tag', ObjectId(new_product_master['tags'][i]))
    db = request.db
    result = db['product_master'].find_and_modify(query={"_id": master_id}, update=new_product_master, new=True)
    for i in range(len(result['tags'])):
        new_product_master['tags'][i] = str(new_product_master['tags'][i].id)
    new_product_master['id'] = str(new_product_master['_id'])
    del new_product_master['_id']
    return {'productMaster': new_product_master}

<commit_msg>fix bug point to new returned productMaster<commit_after>#coding:utf-8
from cgi import FieldStorage
import json
from uuid import uuid4
from schema.product_master import ProductMasterSchema
from schema.product_image import ProductImageSchema
from schema.product_tag import ProductTagSchema
from bson.dbref import DBRef
from schema.product import ProductSchema
from bson.objectid import ObjectId
import colander

from cornice import Service
from webob import exc
from webob.response import Response

product_masters = Service(name='product_masters', path='/productMasters', description="products master",
                          cors_origins=('*',))
product_master = Service(name='product_master', path='/productMasters/{master_id}', description="product master",
                          cors_origins=('*',))


product_tags = Service(name='products_tags', path='/productTags', description="products master tag",
                        cors_origins=('*',))


products = Service(name='products', path='/products', description="products", cors_origins=('*',))
product = Service(name='product', path='/products/{product_id}', description="product detail", cors_origins=('*',))


image = Service(name='image', path='/image', description="upload image", cors_origins=('*',))


product_images = Service(name='product_images', path='/productImages', description='product images', cors_origins=('*',))
product_image = Service(name='product_image', path='/productImages/{image_id}', description='product image', cors_origins=('*',))



@image.post()
def upload_image(request):
    up = request.up
    file = request.params.get('file')
    file_ext = '.' + file.type.split('/')[-1]
    image_url = '/products/' + uuid4().hex + file_ext
    up.put(image_url, file.file, checksum=True)
    return {'image': 'http://keptrans.b0.upaiyun.com' + image_url}


class UnauthorizedView(exc.HTTPError):
    def __init__(self, msg=u'Unauthorized'):
        body = {'status': 401, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 401
        self.content_type = 'application/json'


class BadRequestView(exc.HTTPError):
    def __init__(self, msg=u'Bad request, missing data.'):
        body = {'status': 400, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 400
        self.content_type = 'application/json'


class NotFoundView(exc.HTTPError):
    def __init__(self, msg=u'Not Found.'):
        body = {'status': 404, 'message': msg}
        Response.__init__(self, json.dumps(body))
        self.status = 404
        self.content_type = 'application/json'


product_master_error = {
    'brandNone': u'品牌不能为空',
    'categoryNone': u'所属类目不能为空',
    'coverNone': u'封面地址不能为空',
    'descNone': u'描述不能为空',
    'tags': u'标签不能为空'
}


def validate_product_master(request):
    try:
        schema = ProductMasterSchema()
        new_product = schema.deserialize(request.json_body['productMaster'])
        request.validated['productMaster'] = new_product
    except colander.Invalid as e:
        errors = e.asdict()
        for error_name, error_value in errors.items():
            if error_value == 'Required':
                request.errors.add('body', error_name, product_master_error[error_name + 'None'])
            else:
                request.errors.add('body', error_name, u'发生未知错误')
    except Exception as e:
        request.errors.add('body', 'unhandled_error', e.message)


@product_masters.post(content_type="application/json", validators=(validate_product_master,))
def add_product_master(request):
    new_product_master = request.validated['productMaster']
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = DBRef('product_tag', ObjectId(new_product_master['tags'][i]))
    db = request.db
    db['product_master'].insert(new_product_master)
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = str(new_product_master['tags'][i].id)
    new_product_master['id'] = str(new_product_master['_id'])
    del new_product_master['_id']
    return {'productMaster': new_product_master}


@product_masters.get()
def get_all_product_master(request):
    db = request.db
    tags_list = []
    products_list = []
    images_list = []

    master_list = []


    for master in db['product_master'].find():
        tag_id_list = []
        for tag in master['tags']:
            tag = db.dereference(tag)
            tag['id'] = str(tag['_id'])
            del tag['_id']
            tag_id_list.append(tag['id'])
            tags_list.append(tag)
        master['tags'] = tag_id_list
        product_id_list = []
        for pro in db['product'].find({'productMaster.$id': master['_id']}):
            pro['id'] = str(pro['_id'])
            del pro['_id']
            del pro['productMaster']
            product_id_list.append(pro['id'])
            products_list.append(pro)
        master['products'] = product_id_list
        image_id_list = []
        for img in db['product_image'].find({'productMaster.$id': master['_id']}):
            img['id'] = str(img['_id'])
            del img['_id']
            del img['productMaster']
            image_id_list.append(img['id'])
            images_list.append(img)
        master['images'] = image_id_list
        master['id'] = str(master['_id'])
        del master['_id']
        master_list.append(master)
    return {'productMasters': master_list, 'productTags': tags_list, 'productImages': images_list, 'products': products_list}


@product_master.put(content_type="application/json", validators=(validate_product_master,))
def update_product_master(request):
    master_id = ObjectId(request.matchdict['master_id'])
    new_product_master = request.validated['productMaster']
    for i in range(len(new_product_master['tags'])):
        new_product_master['tags'][i] = DBRef('product_tag', ObjectId(new_product_master['tags'][i]))
    db = request.db
    result = db['product_master'].find_and_modify(query={"_id": master_id}, update=new_product_master, new=True)
    for i in range(len(result['tags'])):
        result['tags'][i] = str(result['tags'][i].id)
    result['id'] = str(result['_id'])
    del result['_id']
    return {'productMaster': result}


{'fraction_non_alphanumeric': 0.14795349577958275, 'fraction_numerical': 0.003663003663003663, 'mean_word_length': 3.7953417334860635, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6152158', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3898, 'n_words': 718}