class ProductModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "title": u"商品名稱", "spec": u"商品規格", "image_url": u"圖片網址", "quantity": u"庫存數量", "date": u"可購買期限", "price": u"價格", "limit_1": u"已出售數量", "limit_2": u"可供購買數量", } title = Fields.StringProperty(default=u"未命名的商品") spec = Fields.StringProperty(default=u"") image_url = Fields.StringProperty(default=u"") date = Fields.DateTimeProperty() price = Fields.FloatProperty(default=0.0) limit_1 = Fields.IntegerProperty(default=0) limit_2 = Fields.IntegerProperty(default=99999) store = Fields.CategoryProperty(kind=StoreModel) group_info = Fields.CategoryProperty(kind=GroupInfoModel) @classmethod def get_by_feature(cls, store, group_info, title, spec, price): p = float(price) return cls.query(cls.title == title, cls.spec == spec, cls.price == p, cls.store == store, cls.group_info == group_info).get()
class OrderInfoModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "store": u"商家", "purchaser": u"購買人", "purchaser_name": u"購買人姓名", "purchaser_mobile": u"購買人手機", "receiver_name": u"收件人姓名", "receiver_address": u"收件人地址", "receiver_mobile": u"收件人手機", "state": u"訂單狀態", "cash_flow_state": u"金流狀態", "freight_flow_state": u"物流狀態", "official_order": u"正式訂單", "official_order_date": u"正式訂單成立時間", } order_info_id = Fields.StringProperty() store = Fields.CategoryProperty(kind=StoreModel) purchaser = Fields.CategoryProperty(kind=UserInfoModel) purchaser_name = Fields.StringProperty(default=u"") purchaser_mobile = Fields.StringProperty(default=u"") receiver = Fields.CategoryProperty(kind=UserInfoModel) receiver_name = Fields.StringProperty(default=u"") receiver_address = Fields.StringProperty(default=u"") receiver_mobile = Fields.StringProperty(default=u"") state = Fields.IntegerProperty(default=0) cash_flow_state = Fields.IntegerProperty(default=0) freight_flow_state = Fields.IntegerProperty(default=0) path_event = Fields.StringProperty(default="") path_forecast = Fields.StringProperty(default="") path_now = Fields.StringProperty(default="") path_next = Fields.StringProperty(default="") process_now = Fields.StringProperty(default="") process_1 = Fields.TextProperty(default="") process_2 = Fields.TextProperty(default="") process_3 = Fields.TextProperty(default="") process_4 = Fields.TextProperty(default="") process_5 = Fields.TextProperty(default="") official_order = Fields.BooleanProperty(default=False) official_order_date = Fields.DateTimeProperty() def details(self): from order_detail import OrderDetailModel return OrderDetailModel.query(OrderDetailModel.order_info == self.key ).order(-OrderDetailModel.sort).fetch() def messages(self): from message import MessageModel return MessageModel.query(MessageModel.order_info == self.key).order( -MessageModel.sort).fetch()
class OrderItemModel(BasicModel): class Meta: behaviors = (Searchable,) label_name = { "title": u"摘要", "order_detail": u"所屬訂單細節", } not_show = { "title": u"摘要", "order_detail": u"所屬訂單細節", } title = Fields.StringProperty(default=u"未命名的商品") order_detail = Fields.CategoryProperty(kind=OrderDetailModel) product = Fields.CategoryProperty(kind=ProductModel) quantity = Fields.IntegerProperty()
class OnlineCodeModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "title": u"團體名稱", "customer": u"所屬客戶", "code_type": u"類型", "source": u"原始碼", } title = Fields.StringProperty(default=u"未命名") target = Fields.CategoryProperty(kind=OnlineCodeTargetModel) code_type = Fields.StringProperty() source = Fields.TextProperty() vision = Fields.IntegerProperty(default=0) @classmethod def all_with_target(cls, target, code_type): return cls.query(cls.code_type == code_type, cls.target == target.key).order(-cls.sort) @classmethod def get_source(cls, target, code_type, vision): return cls.query(cls.code_type == code_type, cls.target == target.key, cls.vision == int(vision)).order(-cls.sort).get()
class BannerModel(BasicModel): class Meta: label_name = { "name": u"名稱", "description": u"描述", "link": u"連結網址", "link_title": u"連結標題", "image": u"圖片", "is_enable": u"啟用", "category": u"分類", } name = Fields.StringProperty() description = Fields.TextProperty() link = Fields.StringProperty() link_title = Fields.StringProperty() image = Fields.ImageProperty() is_enable = Fields.BooleanProperty(default=True) category = Fields.CategoryProperty(required=True, kind=BannerCategoryModel) @classmethod def all_enable(cls, category=None, *args, **kwargs): cat = None if category: cat = BannerCategoryModel.get_by_name(category) if cat is None: return cls.query(cls.is_enable == True).order(-cls.sort) else: return cls.query(cls.category == cat.key, cls.is_enable == True).order(-cls.sort)
class OrderDetailModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "title": u"訂購方式", "order_info": u"所屬訂單", } title = Fields.StringProperty(default=u"") order_info = Fields.CategoryProperty(kind=OrderInfoModel) group_info = Fields.CategoryProperty(kind=GroupInfoModel) shipping_fee = Fields.FloatProperty() sum = Fields.FloatProperty() def items(self): from order_item import OrderItemModel return OrderItemModel.query(OrderItemModel.order_detail == self.key ).order(-OrderItemModel.sort).fetch()
class StoreClientModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "title": u"團購名稱", "store": u"商家", } store = Fields.CategoryProperty(kind=UserInfoModel) client_id = Fields.StringProperty(default=u"")
class WebMenuModel(BasicModel): class Meta: label_name = { "title": u"通用名稱", "name": u"識別碼", "title_lang_zhtw": u"繁中名稱", "title_lang_zhcn": u"簡中名稱", "title_lang_enus": u"英文名稱", "page_url": u"網址", "category": u"分類", "is_enable": u"顯示於前台", } title = Fields.StringProperty(required=True) name = Fields.StringProperty() title_lang_zhtw = Fields.StringProperty(default=u"未命名") title_lang_zhcn = Fields.StringProperty(default=u"未命名") title_lang_enus = Fields.StringProperty(default=u"未命名") page_url = Fields.StringProperty(required=True) is_enable = Fields.BooleanProperty(default=True) category = Fields.CategoryProperty(kind=WMM) @classmethod def get_by_url(cls, url, *args, **kwargs): return cls.query(cls.page_url==url) @classmethod def get_title(cls, url, *args, **kwargs): item_q = cls.get_by_url(url) item = item_q.get() if item is None: return "" if "lang" in kwargs: if kwargs["lang"] == "zhtw": return item.title_lang_zhtw if kwargs["lang"] == "zhcn": return item.title_lang_zhcn if kwargs["lang"] == "enus": return item.title_lang_enus return item.title @classmethod def get_by_name(cls, name): return cls.query(cls.name==name).get() @classmethod def all_enable(cls, category=None, *args, **kwargs): cat = None if category: cat = cls.get_by_name(category) if cat is None: return cls.query(cls.category==None, cls.is_enable==True).order(-cls.sort) else: return cls.query(cls.category==cat.key, cls.is_enable==True).order(-cls.sort)
class MobileModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "number": u"電話", "code": u"驗証碼", "password": u"密碼", "store": u"所屬商家", "is_enable": u"啟用/停用", } hide_filed = ["account", "store"] number = Fields.StringProperty(required=True) code = Fields.StringProperty() password = Fields.StringProperty() store = Fields.CategoryProperty(kind=StoreModel) account = Fields.CategoryProperty(kind=UserInfoModel) is_enable = Fields.BooleanProperty(default=True) @classmethod def format_mobile_number(cls, value=u''): """ 處理手機格式 :param value: 輸入值 :param taiwan_format: 台灣手機 :return: 正確的手機格式 或 None """ if value.startswith("09") is True: if len(value) == 10: return "+886" + value[1:] if value.startswith("+886") is True: if len(value) == 13: return value return None @classmethod def get_by_number(cls, number): return cls.query(cls.number == number).get() @classmethod def get_or_create_by_number(cls, number, name=u"未命名使用者", store_name=u"未命名店家"): number = cls.format_mobile_number(number) if number is None: raise u"手機格式有誤" mobile = cls.get_by_number(number) if mobile is None: mobile = MobileModel() mobile.number = number mobile.put() if mobile.store is None: s = StoreModel() s.title = store_name s.process_main = '[{"id":"process_order_check"},{"id":"process_payment"},{"id":"process_send_goods"},{"id":"process_order_end"}]' s.put() mobile.store = s.key mobile.put() if mobile.account is None: a = UserInfoModel() a.user_name = name a.put() mobile.account = a.key mobile.put() return mobile @classmethod def already_register_check_and_login(cls, mobile, code): r = cls.query(cls.number == mobile, cls.password == code).get() if r is None: r = cls.query(cls.number == mobile, cls.code == code).get() if r is None: return None return r @classmethod def all_enable(cls): return cls.query(cls.is_enable == True).order(-cls.sort) @classmethod def all_enable_with_category(cls, cat): return cls.query(cls.is_enable == True, cls.store == cat.key).order(-cls.sort) @classmethod def get_prev_one_with_enable(cls, item): return cls.query(cls.is_enable == True, cls.sort > item.sort).order(cls.sort).get() @classmethod def get_next_one_with_enable(cls, item): return cls.query(cls.is_enable == True, cls.sort < item.sort).order(-cls.sort).get()
class ApplicationUserModel(BasicModel): class Meta: label_name = { "name": u"名稱", "account": u"帳號", "password": u"密碼", "is_enable": u"啟用", "avatar": u"頭像", "role": u"角色" } name = Fields.StringProperty(required=True) account = Fields.StringProperty(required=True) password = Fields.StringProperty(required=True) avatar = Fields.ImageProperty() is_enable = Fields.BooleanProperty(default=True) role = Fields.CategoryProperty(kind=ApplicationUserRoleModel, required=True) @classmethod def init(cls, name, account, password, prohibited_actions, avatar): if ApplicationUserRoleModel.has_record() is False: su_role = ApplicationUserRoleModel() su_role.name = "super_monkey" su_role.title = u"超級猴子" su_role.level = 9999 su_role.put() admin_role = ApplicationUserRoleModel() admin_role.name = "super_user" admin_role.title = u"超級管理員" admin_role.level = 999 admin_role.prohibited_actions = prohibited_actions admin_role.put() else: su_role = ApplicationUserRoleModel.get_role("super_monkey") admin_role = ApplicationUserRoleModel.get_role("super_user") cls.create_account(u"猴子", "iammonkey", "iammonkey", su_role.key, avatar) return cls.create_account(name, account, password, admin_role.key, avatar) @classmethod def get_login(cls, account, password, is_enable=True): a = cls.query(cls.account == account, cls.password == password, cls.is_enable == is_enable).get() if a is None: return None role = None if a.role is not None: role = a.role.get() if role is not None and role.is_enable is False: return None return a @classmethod def has_record(cls): r = cls.query().get() if r is not None: return True else: return False @classmethod def create_account(cls, name, account, password, role, avatar=None): n = cls() n.name = name n.account = account n.password = password n.role = role n.avatar = avatar n.put() return n @classmethod def get_list(cls): return cls.query(cls.account != "iammonkey").order( cls.account, -cls.sort, -cls._key)
class MessageModel(BasicModel): class Meta: behaviors = (Searchable, ) label_name = { "title": u"團購名稱", "store": u"商家", } store = Fields.CategoryProperty(kind=StoreModel) user_a = Fields.CategoryProperty(kind=UserInfoModel) user_b = Fields.CategoryProperty(kind=UserInfoModel) order_info = Fields.CategoryProperty(kind=OrderInfoModel) store_text = Fields.StringProperty() user_a_text = Fields.StringProperty() user_b_text = Fields.StringProperty() message_type = Fields.StringProperty() message_from = Fields.StringProperty() store_read = Fields.BooleanProperty(default=False) user_a_read = Fields.BooleanProperty(default=False) user_b_read = Fields.BooleanProperty(default=False) @classmethod def read(cls, order_info, store_key, account_key): r = ndb.Key(urlsafe=order_info).get() if r is None: return li = cls.query(cls.order_info == r.key).fetch() to_put = [] for item in li: if item.store == store_key and item.store_read is False: item.store_read = True to_put.append(item) else: if r.purchaser == account_key and item.user_a_read is False: r.user_a_read = True to_put.append(item) elif item.user_b_read is False: r.user_b_read = True to_put.append(item) ndb.put_multi(to_put) @classmethod def insert(cls, msg_from, msg_type, store, purchaser, receiver, order_info=None): msg = cls() if order_info is not None: msg.order_info = order_info.key msg.store = store["key"] msg.store_text = store["message"] msg.user_a = purchaser["key"] msg.user_a_text = purchaser["message"] msg.user_b = receiver["key"] msg.user_b_text = u"" if purchaser["key"] != purchaser["key"]: msg.user_b_text = receiver["message"] msg.message_from = msg_from msg.message_type = msg_type msg.put() return msg