class saleCode(Document): __collection__ = 'code' __database__ = 'shop' structure = { 'appkey': unicode, # appkey 'salerange': OR(float, int), # 优惠幅度 'saleprice': OR(float, int), # 优惠价格 'count': int, # 总数量 'usedcount': int, # 已使用数量 'products': [{ #产品 '_id': unicode, 'title': unicode, #产品名称 'imgs': [], #产品图片 }], #可使用优惠卷的产品id 'validdate': unicode, #有效日期 'code': unicode, # 优惠码 'del': int, # 0 存在 1删除 } validators = { 'appkey': max_length(200), } default_values = {'salerange': 0, 'saleprice': 0, 'del': 0} required = ['appkey'] use_dot_notation = True
class AppStoreApp(App): use_schemaless = True structure = { 'trackId': OR(int, basestring), 'account': Account, 'price': OR(float, basestring), } required_fields = ['trackId', 'account'] indexes = [{ 'fields':['bundleId', 'version'], 'unique':True, },{ 'fields':['name'] }] def clean_doc(self): cp = super(AppStoreApp, self).clean_doc() if 'account' in self and self.account: cp['account'] = str(self.account['uniqueIdentifier']) return cp @classmethod def rebuild_doc_dict(cls, db, docDict): docDict = super(AppStoreApp, cls).rebuild_doc_dict(db, docDict) if 'account' in docDict: acc = db.Account.find_one({ 'uniqueIdentifier': str(docDict['account']) }) docDict['account'] = acc return docDict
class Img2(Document): __collection__ = 'col' __database__ = 'img' structure = { 'url': unicode, #路径 'type': IS(u'AD', u'RSC', u'OTHER'), # ad 广告 rsc 资源 other 'size': OR(int, float, unicode), #大小 'pf': unicode, #后缀 'name': unicode, # 后缀 'appkey': unicode, 'userid': unicode, 'date': OR(unicode, datetime.datetime), 'del': int, #0 存在 1删除 } required = ['url', 'pf', 'appkey'] validators = { 'url': max_length(100), 'userid': max_length(100), 'name': max_length(100), 'pf': max_length(10), } default_values = { 'del': 0, 'type': 'RSC', 'date': datetime.datetime.now(), } use_dot_notation = True @connection.register class Ad_imgs(Document): __collection__ = 'col' __database__ = 'ads' structure = { 'des': unicode, # 描述 'imgs': [{ 'url': unicode, 'tolink': unicode }], 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'del': int, # 0 存在 1删除 } required = ['appkey'] validators = {} default_values = { 'del': 0, 'date': datetime.datetime.now(), } use_dot_notation = True
class Type(Document): __collection__ = 'type' __database__ = 'app' structure = { 'level': int, 'name': unicode, 'dec': unicode, 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'type': IS(u'article', u'article_hot', u'shop', u'shop_hot', u'user'), 'parentID': unicode, 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'del': int, #0 存在 1删除 } validators = { 'name': max_length(50), 'dec': max_length(10000), 'reserved_1': max_length(10000), 'reserved_2': max_length(10000), 'reserved_3': max_length(10000), 'reserved_4': max_length(10000), } default_values = { 'del': 0, 'date': datetime.datetime.now(), 'level': 1, 'parentID': u'', } use_dot_notation = True
class Count(RootDocument): __collection__ = "Analysis" use_schemaless = True structure = { "name": unicode, # Name of the field to keep a count of; must be unique "total": OR(int, float) # The total count } required_fields = ["name"] def incrementBy(self, num): """ Increment the count by num. """ response = self.connection["yr-metrics"].Analysis.update( {"name": self.name}, {"$inc": { "total": num }}, upsert=True) if response: return response else: return True def __repr__(self): """ Return a string representation of this object. """ return "<Analysis for %r> %d" % (self.name, self.total)
class UserType(Document): __collection__ = 'type' __database__ = 'app' structure = { 'level': int, 'level_name': unicode, 'level_dec': unicode, 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'del': int, #0 存在 1删除 } validators = { 'level_name': max_length(50), 'level_dec': max_length(10000), 'reserved_1': max_length(10000), 'reserved_2': max_length(10000), 'reserved_3': max_length(10000), 'reserved_4': max_length(10000), } default_values = {'del': 0, 'date': datetime.datetime.now(), 'level': 1} use_dot_notation = True
class Fenxiao(Document): __collection__ = 'fenxiao' __database__ = 'shop' structure = { 'appkey': unicode, # appkey 'status': int, #0关闭 1开启 'fx1': OR(float, int), #1级分销 'fx2': OR(float, int), # 1级分销 'fx3': OR(float, int), # 1级分销 } validators = { 'appkey': max_length(200), } default_values = {} required = ['appkey'] use_dot_notation = True
class Comment(Document): __collection__ = 'col' __database__ = 'comment' structure = { 'oid': unicode, #文章或产品id 'level': int, #评价级别 1- 5 'type': IS(0, 1), #0留言 1评论 'content': unicode, #内容 'imgs': [unicode], 'userId': unicode, 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'answer': unicode, 'reserved_1': unicode, # 预留字段1 'reserved_2': unicode, # 预留字段1 'reserved_3': unicode, # 预留字段1 'reserved_4': unicode, # 预留字段1 'del': int, #0 存在 1删除 } required = ['oid', 'type', 'content', 'appkey', 'userId'] validators = { 'oid': max_length(100), 'content': max_length(1000), 'answer': max_length(1000), } default_values = { 'del': 0, 'type': 0, 'date': datetime.datetime.now(), } use_dot_notation = True
class Product(Document): __collection__ = 'product' __database__ = 'shop' structure = { 'title': unicode, #标题 'price': OR(float, int), #销售价 'costprice': OR(float, int), # 成本价 用于计算分销提成 'overview': unicode, #简介\概述 'type': unicode, #类型 'colour': list, # 颜色 大小 'size': list, # 大小 'repertory': int, # 库存 'imgs': list, #主图list 地址 'describe': unicode, #描述 'describe_html': unicode, # 描述html 'recommend': unicode, #推荐 'buycount': int, #购买数量 'collectcount': int, #收藏数量 'appkey': unicode, 'author': unicode, #所有者 上传者 'date': OR(unicode, datetime.datetime), 'status': int, #0未审核 1,审核,2,未上架 3上架 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'del': int, #0 存在 1删除 } validators = { 'title': max_length(200), 'overview': max_length(200), 'type': max_length(200), 'author': max_length(200), 'describe': max_length(20000), } default_values = { 'status': 0, 'del': 0, 'date': datetime.datetime.now(), 'buycount': 0, 'collectcount': 0, 'price': 0, 'costprice': 0, } use_dot_notation = True
class APP_User(Document): __collection__ = 'user' __database__ = 'app' structure = { 'name': unicode, 'password': unicode, 'phone': unicode, 'email': unicode, 'vip': unicode, 'qq': unicode, 'integral': OR(int, float), #用户积分点数之类 'wachat': unicode, 'icon': unicode, 'nickname': unicode, 'referee': unicode, #我的推荐人 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'status': int, #用户状态 1.正常 2.禁用 3.自定义 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'del': int, #0 存在 1删除 } validators = { 'name': max_length(50), 'password': max_length(120), 'phone': max_length(50), 'email': max_length(120), 'qq': max_length(120), 'wachat': max_length(120), 'nickname': max_length(120), 'icon': max_length(120), 'reserved_1': max_length(120), 'reserved_2': max_length(120), 'reserved_3': max_length(120), 'reserved_4': max_length(120), } default_values = { 'del': 0, 'status': 1, 'integral': 0, } use_dot_notation = True
def test_or_operator(self): from mongokit import OR assert repr(OR(unicode, str)) == "<unicode or str>" failed = False try: class BadMyDoc(SchemaDocument): structure = {"bla":OR(unicode,str)} except StructureError, e: self.assertEqual(str(e), "BadMyDoc: <type 'str'> in <unicode or str> is not an authorized type (type found)") failed = True
class WanNeng(Document): __collection__ = 'wanneng' __database__ = 'wanneng' structure = { 'appkey': unicode, 'w1': unicode, 'w2': unicode, 'w3': unicode, 'w4': unicode, 'w5': unicode, 'w6': unicode, 'w7': unicode, 'w8': unicode, 'w9': unicode, 'w10': unicode, 'w11': unicode, 'w12': unicode, 'w13': unicode, 'w14': unicode, 'w15': unicode, 'w16': unicode, 'w17': unicode, 'date': OR(unicode, datetime.datetime), 'del': int, # 0 存在 1删除 } validators = { 'w1': max_length(500), 'w2': max_length(500), 'w3': max_length(500), 'w4': max_length(500), 'w5': max_length(500), 'w6': max_length(500), 'w7': max_length(500), 'w8': max_length(500), 'w9': max_length(500), 'w10': max_length(500), 'w11': max_length(500), 'w12': max_length(500), 'w13': max_length(500), 'w14': max_length(500), 'w15': max_length(500), 'w16': max_length(500), 'w17': max_length(500), } default_values = { 'del': 0, 'date': datetime.datetime.now(), } use_dot_notation = True
class CompanyInfo(MongoBaseItem): __database__ = "rongmofang" __collection__ = "companyinfo" use_autorefs = True structure = { # "cid": int, "company_id": OR(unicode, basestring), # "company_name": unicode, # "compnay_description": unicode, "register_time": datetime, "register_capital": unicode, # 注册资本 "assets": unicode, # 净资产 "property": unicode, # 公司性质 "industry": unicode, # 所在行业 "introduction": unicode, # 企业简介 "assets_situation": unicode, "law_situation": unicode, # 涉诉情况 "credit_situation": unicode, # 征信情况 "financial_situation": [{ 'year': int, 'main_income': float, # 主营收入 'profit': float, # 净利润 'total_capital': float, # 总资产 'pure_capital': float, # 净资产 "balance_propery": float, # 资产负债率 }], # 转让类企业信息 "profit_amount": unicode, # 收益权金额 "profit_limit": unicode, # 收益权期限 "profit_detial": unicode, # 收益权详情 "projects": [Project] }
class Event(RootDocument): __collection__ = "Events" structure = { "realm": unicode, # Which property is this coming from (ie - ADP, Turnstyle, etc) "description": unicode, # Description of event or origin (ie - Shoutcast Server) "name": unicode, # Name of data point (ie - SONGTITLE) "datum": OR(unicode, int, float), # The actual data being recorded "source": unicode, # IP Address or other source for event "useragent": unicode, # User Agent info on source "dt": datetime.datetime, # The datetime the event was recorded "dtm": datetime.datetime # The datetime the event recording ended } required_fields = ["realm", "description", "datum"] def __repr__(self): return "<Event %r>" % (self.name)
def test_or_operator(self): from mongokit import OR assert repr(OR(str, str)) == "<unicode or str>" failed = False try: class BadMyDoc(SchemaDocument): structure = {"bla": OR(unicode, str)} except StructureError as e: self.assertEqual( str(e), "BadMyDoc: <type 'str'> in <unicode or str> is not an authorized type (type found)" ) failed = True self.assertEqual(failed, True) from datetime import datetime class MyDoc(SchemaDocument): structure = {"foo": OR(str, int), "bar": OR(str, datetime)} mydoc = MyDoc() assert str(mydoc.structure['foo']) == '<unicode or int>' assert str(mydoc.structure['bar']) == '<unicode or datetime>' assert mydoc == {'foo': None, 'bar': None} mydoc['foo'] = 3.0 self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = u"foo" mydoc.validate() mydoc['foo'] = 3 mydoc.validate() mydoc['foo'] = 'bar' self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = datetime.now() self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = u"foo" mydoc['bar'] = datetime.now() mydoc.validate() mydoc['bar'] = u"today" mydoc.validate() mydoc['bar'] = 25 self.assertRaises(SchemaTypeError, mydoc.validate)
def test_or_operator(self): from mongokit import OR assert repr(OR(int, str)) == "<int or str>" failed = False try: class BadMyDoc(SchemaDocument): structure = {"bla": OR(int, tuple)} except StructureError as e: self.assertEqual( str(e), "BadMyDoc: <%s 'tuple'> in <int or tuple> is not an authorized type (type found)" % ('type' if six.PY2 else 'class')) failed = True self.assertEqual(failed, True) from datetime import datetime class MyDoc(SchemaDocument): structure = {"foo": OR(str, int), "bar": OR(str, datetime)} mydoc = MyDoc() assert str(mydoc.structure['foo']) == '<%s or int>' % str.__name__ assert str(mydoc.structure['bar']) == '<%s or datetime>' % str.__name__ assert mydoc == {'foo': None, 'bar': None} mydoc['foo'] = 3.0 self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = "foo" mydoc.validate() mydoc['foo'] = 3 mydoc.validate() mydoc['foo'] = six.b('bar') self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = datetime.now() self.assertRaises(SchemaTypeError, mydoc.validate) mydoc['foo'] = "foo" mydoc['bar'] = datetime.now() mydoc.validate() mydoc['bar'] = "today" mydoc.validate() mydoc['bar'] = 25 self.assertRaises(SchemaTypeError, mydoc.validate)
class Ad_imgs(Document): __collection__ = 'col' __database__ = 'ads' structure = { 'des': unicode, # 描述 'imgs': [{ 'url': unicode, 'tolink': unicode }], 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'del': int, # 0 存在 1删除 } required = ['appkey'] validators = {} default_values = { 'del': 0, 'date': datetime.datetime.now(), } use_dot_notation = True
class Article(Document): __collection__ = 'article' __database__ = 'app' structure = { 'title': unicode, 'overview': unicode, 'author': unicode, 'type': unicode, 'appkey': unicode, 'date': OR(unicode, datetime.datetime), 'source': int, #0 转发 1原创 'recommend': unicode, #推荐类型 'content': unicode, #markdown代码 'htmlcontent': unicode, #html代码 'status': int, #0未审核 1,审核,2,保存 3发布 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'reserved_5': unicode, # 预留字段1 'reserved_6': unicode, # 预留字段1 'reserved_7': unicode, # 预留字段1 'del': int, #0 存在 1删除 } validators = { 'title': max_length(50), 'overview': max_length(520), 'author': max_length(50), 'type': max_length(120), 'recommend': max_length(120), 'content': max_length(10000), 'htmlcontent': max_length(10000), } default_values = { 'del': 0, 'date': datetime.datetime.now(), 'status': 0, 'source': 0, } use_dot_notation = True
class Collection(Document): __collection__ = 'collection' __database__ = 'shop' structure = { 'appkey': unicode, 'type': IS(u'collection', u'shopcard'), 'userId': unicode, 'productId': unicode, #产品Id 'date': OR(unicode, datetime.datetime), 'reserved_1': unicode, #预留字段1 'reserved_2': unicode, #预留字段1 'reserved_3': unicode, #预留字段1 'reserved_4': unicode, #预留字段1 'del': int, #0 存在 1删除 } validators = { 'appkey': max_length(200), 'userId': max_length(200), } default_values = { 'del': 0, 'date': datetime.datetime.now(), } use_dot_notation = True
class MyDoc(SchemaDocument): structure = { "foo":OR(unicode,int), "bar":OR(unicode, datetime) }
class BadMyDoc(SchemaDocument): structure = {"bla":OR(unicode,str)}
class BadMyDoc(SchemaDocument): structure = {"bla": OR(int, tuple)}
class Order(Document): __collection__ = 'order' __database__ = 'shop' structure = { 'user':unicode, #订单客户 'price': OR(float,int), #订单实付价格 'product': [{ #订单产品 '_id':unicode, 'title':unicode, #产品名称 'imgs':[], #产品图片 'costprice': OR(float, int), # 成本单价 'price': OR(float,int), # 产品单价 'saleprice': OR(float, int), # 产品促销价 'saleCode':unicode, # 优惠码 'colour': unicode, # 颜色 大小 'size': unicode, # 大小 'count':int, #购买数量 }], 'date': OR(unicode, datetime.datetime), 'receiveinfo':{ 'name': unicode, # 收件人名称 'mphone': unicode, # 手机号码 'phone': unicode, # 电话 'province': unicode, # 省 'city': unicode, # 市 'area': unicode, # 区域 'address': unicode, # 详细地址 'remake': unicode, # 备注 }, #收货信息 'status':IS(0,1,2,3,4), #0 待付款 1已付款 2,已发货 3.交易完成 4关闭交易 'refund': { #退款信息 'status': IS(0,1,2,3,4,5), # 退款状态 0 无退款 1申请退款 2同意退款 3拒绝退款 4退款完成,5关闭退款 'remake':[{ #卖退款备注 'user':unicode, 'msg':unicode, 'date': OR(unicode, datetime.datetime), }], 'price': OR(float, int), #退款金额 'products': list, # 退款产品 'express':{ #快递信息 'name':unicode, #快递名称 'code': unicode, # 快递单号 }, 'date': OR(unicode, datetime.datetime), }, 'express':{ #物流信息 'name': unicode, # 快递名称 'code': unicode, # 快递单号 }, 'remake': unicode, # 备注 'appkey':unicode, 'reserved_1':unicode, #预留字段1 支付订单号 'reserved_2':unicode, #预留字段1 'reserved_3':unicode, #预留字段1 'reserved_4':unicode, #预留字段1 'del': int,#0 存在 1删除 } validators = { 'user': max_length(200), 'refund.remake': max_length(200), 'refund.express.name': max_length(200), 'refund.express.code': max_length(200), 'remake': max_length(200), 'express.code': max_length(200), 'express.name': max_length(200), } default_values = { 'del': 0, 'date': datetime.datetime.now(), 'status': 0, 'refund.status': 0, } required = ['user', 'price', 'receiveinfo', 'status'] use_dot_notation = True
class MyDoc(SchemaDocument): structure = { "foo": OR(six.text_type, int), "bar": OR(six.text_type, datetime), "foobar": OR(string_type, int), }
class Video(Document): __collection__ = "assets" __database__ = MONGODB_DB use_schemaless = True structure = { "@context": dict, "@graph": { "dc:creator": basestring, "dc:date": IsoDate(), "dc:identifier": basestring, "dc:type": basestring, "dc:coverage": basestring, "dc:rights": { "read": list, "write": list }, "ma:averageBitRate": int, "ma:isCopyrightedBy": { "@id": basestring, "name": unicode }, "ma:date": int, "ma:description": unicode, "ma:duration": int, "ma:features": [{ "@id": basestring, "name": unicode }], "ma:frameHeight": int, "ma:frameRate": OR(float, int), "ma:frameSizeUnit": basestring, "ma:frameWidth": int, "ma:hasContributor": [{ "@id": basestring, "name": unicode }], "ma:hasGenre": { "@id": basestring, "name": unicode }, "ma:hasLanguage": [basestring], "ma:isMemberOf": [dict], "ma:isRelatedTo": [basestring], "ma:hasRelatedResource": [dict], "ma:hasKeyword": [basestring], "ma:hasPolicy": [basestring], "ma:locator": [{ "location": basestring, "ma:hasFormat": basestring, "ma:hasCompression": { "@id": basestring, "name": basestring } }], "ma:title": unicode } } required_fields = [ "@context", "@graph.dc:creator", "@graph.dc:date", "@graph.dc:type", "@graph.ma:date", "@graph.ma:hasLanguage", "@graph.ma:title", "@graph.ma:duration" ] default_values = { "@context": { "dc": "http://purl.org/dc/elements/1.1/", "dc:identifier": "@id", "dc:type": "@type", "ma": "http://www.w3.org/ns/ma-ont/", "hummedia": "http://humanities.byu.edu/hummedia/" }, "@graph.dc:creator": "Hummedia", "@graph.dc:date": datetime.datetime.utcnow(), "@graph.dc:type": "hummedia:type/humvideo", "@graph.ma:title": u"New Hummedia Video", "@graph.ma:date": 1900, "@graph.ma:duration": 0, "@graph.ma:hasLanguage": ["en"], "@graph.dc:coverage": "private", "@graph.ma:frameRate": 23.976, "@graph.ma:averageBitRate": 768000, "@graph.ma:frameHeight": 360, "@graph.ma:frameWidth": 640, "@graph.ma:frameSizeUnit": "px" } def make_part(self, vid, host, part): from helpers import uri_pattern, resolve_type from config import HOST resource = uri_pattern(vid.get("pid"), host + "/video") thepart = { "ma:title": vid["ma:title"], "pid": vid.get("pid"), "resource": resource } thepart['type'] = resolve_type(vid['dc:type']) if part != "snippet": thepart["fromYt"] = [] thepart["ma:image"] = [] for location in vid["ma:locator"]: if location['@id'] is None: continue if resolve_type(vid["dc:type"]) == "humvideo": poster = uri_pattern(location["@id"] + ".jpg", HOST + "/posters") thumb = uri_pattern(location["@id"] + "_thumb.jpg", HOST + "/posters") thepart["ma:image"].append({ "poster": poster, "thumb": thumb }) elif resolve_type(vid['dc:type']) == 'humaudio': pass else: thepart["ma:image"].append({"ytId": location["@id"]}) thepart["fromYt"].append(location["@id"]) for att in [ "ma:date", "ma:description", "ma:hasLanguage", "ma:hasPolicy", "ma:isMemberOf" ]: thepart[att] = vid.get(att) for annot in thepart["ma:isMemberOf"]: coll = connection[MONGODB_DB].assetgroups.find_one( {"_id": annot["@id"]}) if coll is not None: annot["title"] = coll["@graph"]["dc:title"] return thepart