Beispiel #1
0
class OpenUserModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'open_user'}
    name = StringField(required=True, unique=True)
    callback_url = StringField(required=True)
    secret = StringField(required=True)
    created_time = IntegerField(required=True)
    status = StringField(required=True,
                         candidate=OPEN_USER_STATUS_CANDIDATES,
                         default="verified")

    @classmethod
    @gen.coroutine
    def CreateNewOpenUser(cls, name, callback_url):
        secret = ''.join(
            [random.choice(string.letters + string.digits) for _ in range(32)])
        data = {
            'name': name,
            'callback_url': callback_url,
            'secret': secret,
            'created_time': int(time.time() * 1000),
            'status': 'verified'
        }
        try:
            open_user_id = yield cls.insert(data)
            raise gen.Return(open_user_id)
        except UniqueError, e:
            value = ""
            if e.field == "name":
                value = name
            raise DuplicateError("'%s'" % e.field, value)
            pass
Beispiel #2
0
class FineModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'fine'}

    courier_id = ObjectIdField(required=True)
    school_id = ObjectIdField(required=True)
    console_user_id = ObjectIdField(required=True)
    amount = IntegerField(required=True)
    created_time = IntegerField(required=True)
    reason = StringField()
    description = StringField()

    @classmethod
    @gen.coroutine
    def CreateFine(cls, fine_data):
        result = yield cls.insert(fine_data)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetFinesAndCountFromCourierId(cls, courier_id, skip=None, limit=None):
        condition = {'courier_id': courier_id}
        sort_condition = [('created_time', -1)]
        query = cls.find(condition)
        count = yield query.count()
        if skip is not None and limit is not None:
            docs = yield query.sort(sort_condition).skip(skip).limit(
                limit).to_list(None)
        else:
            docs = yield query.sort(sort_condition).to_list(None)
        result = (docs, count)
        raise gen.Return(result)
Beispiel #3
0
class ItemInSubtaskModel(EmbeddedDocument):
    item_id = ObjectIdField(required=True)
    name = StringField(required=True)
    count = IntegerField(required=True)
    price = IntegerField(required=True)
    description = StringField()
    image_id = ObjectIdField(required=True)
Beispiel #4
0
class ProvinceModel(EmbeddedDocument):
    name = StringField(required=True)
    code = StringField()
    city = ListField(
        EmbeddedDocumentField(CityModel),
        required=True,
        default=[]
    )
Beispiel #5
0
class UserModel(Document):
    meta = {'db': settings.mongodb.SHARK_DB, 'collection': 'user'}

    mobile = MobileField(required=True)
    name = StringField()
    address = StringField()
    location = LocationField()
    created_time = IntegerField()

    area_level = StringField(candidate=AREA_LEVELS)
    area_name = StringField()
    device_token = EmbeddedDocumentField(DeviceTokenModel)

    @classmethod
    @gen.coroutine
    def GetUserFromId(cls, user_id):
        if isinstance(user_id, ObjectId) is False:
            user_id = ObjectId(user_id)
        result = yield cls.find_one({'_id': user_id})
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def CreateNewIfNotExists(cls, mobile):
        user = yield cls.find_one({'mobile': mobile})
        if user:
            raise gen.Return(user['_id'])
        else:
            now = int(time.time() * 1000)
            user_data = {'mobile': mobile, 'created_time': now}
            result = yield cls.insert(user_data)
            raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetUserFromMobile(cls, mobile):
        condition = {"mobile": mobile}
        user = yield cls.find_one(condition)
        raise gen.Return(user)

    @classmethod
    @gen.coroutine
    def UpdateDeviceToken(cls, user_id, device_type, device_token):
        condition = {"_id": user_id}
        setter = {"$set": {"device_token": {device_type: device_token}}}
        result = yield cls.update(condition, setter)
        raise gen.Return(result)
Beispiel #6
0
class RegionModel(Document):
    meta = {
        'db': settings.mongodb.CONSOLE_DB,
        'collection': 'regions'
    }

    name = StringField(required=True)
    code = StringField()
    province = ListField(
        EmbeddedDocumentField(ProvinceModel),
        required=True,
        default=[]
    )

    @classmethod
    @gen.coroutine
    def AllRegions(cls):
        regions = yield cls.find({}).to_list(None)
        raise gen.Return(regions)
Beispiel #7
0
class AccountModel(EmbeddedDocument):
    type = StringField(required=True, candidate=['alipay', 'bank'])
    account_name = StringField(required=True)
    account_id = StringField(required=True)
    bank_name = StringField()
    bank_province_city = StringField()
    bank_branch = StringField()
Beispiel #8
0
class LogModel(Document):
    meta = {"db": settings.mongodb.CONSOLE_DB, "collection": "log"}

    # console user id
    user_id = ObjectIdField(required=True)
    action = StringField(required=True)
    created_time = IntegerField(required=True)
    status = IntegerField(required=True)
    ip = StringField(required=True)
    arguments = GenericDictField()

    @classmethod
    @gen.coroutine
    def NewLog(cls, user_id, action, status, ip, arguments):
        data = {
            'user_id': user_id,
            'action': action,
            'created_time': int(time.time() * 1000),
            'status': status,
            'ip': ip,
            'arguments': arguments
        }
        log_id = yield cls.insert(data)
        raise gen.Return(log_id)
Beispiel #9
0
class BranchesModel(Document):
    meta = {
        'db': settings.mongodb.WUKONG_DB,
        "collection": "branches"
    }
    cityId = IntegerField(required=True)
    name = StringField(required=True)
    bankId = IntegerField(required=True)


    @classmethod
    @gen.coroutine
    def ListCityBranches(cls, cid, bid):
        condition = {
            "cityId": cid,
            "bankId": bid
        }
        branches = yield cls.find(condition).to_list(None)
        raise gen.Return(branches)
Beispiel #10
0
class UserShareModel(Document):
    meta = {'db': settings.mongodb.SHARK_DB, 'collection': 'user_share'}
    user_id = ObjectIdField(required=True)
    created_time = IntegerField(required=True)
    share_type = StringField(required=True)
    created_date_time = IntegerField(required=True)

    @classmethod
    @gen.coroutine
    def CreateUserShare(cls, user_id, share_type):
        now = datetime.datetime.now()
        timestamp = int(time.mktime(now.timetuple()) * 1000)
        today = datetime.datetime(now.year, now.month, now.day)
        created_date_time = int(time.mktime(today.timetuple()) * 1000)
        data = {
            "user_id": user_id,
            "share_type": share_type,
            "created_time": timestamp,
            "created_date_time": created_date_time
        }
        share_id = cls.insert(data)
        raise gen.Return(share_id)
Beispiel #11
0
class SchoolModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'schools'}

    name = StringField(required=True)
    region = StringField()
    province = StringField()
    city = StringField()
    location = LocationField()
    note = StringField()

    dispatch_strategy = GenericListField()

    @classmethod
    @gen.coroutine
    def GetSchoolsFromProvince(cls, province):
        condition = {'province': province}
        result = yield cls.find(condition, {"name": 1}).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetSchoolsFromCity(cls, city):
        condition = {'city': city}
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetSchoolsAndCountFromArea(cls,
                                   skip=None,
                                   limit=None,
                                   region=None,
                                   province=None,
                                   city=None,
                                   school=None):
        condition = {}
        if region:
            condition['region'] = region
        if province:
            condition['province'] = province
        if city:
            condition['city'] = city
        if school:
            condition['name'] = school
        query = cls.find(condition)
        count = yield query.count()
        if skip is not None and limit is not None:
            schools = yield query.limit(limit).skip(skip).to_list(None)
        else:
            schools = yield query.to_list(None)
        result = (schools, count)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetSchoolFromName(cls, name):
        condition = {"name": name}
        school = yield cls.find_one(condition)
        logging.info(condition)
        logging.info(school)
        raise gen.Return(school)

    @classmethod
    @gen.coroutine
    def GetSchoolFromId(cls, schoold_id):
        condition = {'_id': schoold_id}
        result = yield cls.find_one(condition)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetSchoolsFromLocation(cls, location, distance):
        condition = {
            "location": {
                "$geoWithin": {
                    "$center": [location, distance]
                }
            }
        }
        schools = yield cls.find(condition).to_list(None)
        raise gen.Return(schools)

    @classmethod
    @gen.coroutine
    def DeleteSchoolFromId(cls, schoold_id):
        condition = {'_id': schoold_id}
        result = yield cls.remove(condition)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def UpdateSchoolData(cls, school_id, school_data):
        condition = {'_id': school_id}
        updater = {'$set': school_data}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def CreateNewSchool(cls, data):
        result = yield cls.insert(data)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def SetDispatchStrategy(cls, school_id, strategy):
        condition = {'_id': school_id}
        updater = {'$addToSet': {'dispatch_strategy': strategy}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def DeleteDispatchStrategy(cls, school_id, strategy):
        condition = {'_id': school_id}
        updater = {'$pull': {'dispatch_strategy': strategy}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)
Beispiel #12
0
class CityModel(EmbeddedDocument):
    name = StringField(required=True)
    code = StringField()
Beispiel #13
0
class TaskModel(Document):
    meta = {
        'db': settings.mongodb.WUKONG_DB,
        'collection': 'task'
    }
    status = StringField(required=True, default='created', candidate=TASK_STATUS)
    shop = EmbeddedDocumentField(ShopModel)
    subtasks = GenericListField(required=True)
    pay = IntegerField()
    created_time = IntegerField(required=True)
    dispatched_time = IntegerField()
    started_time = IntegerField()
    done_time = IntegerField()
    courier_id = ObjectIdField()
    courier_name = StringField()
    courier_mobile = StringField()
    district_id = ObjectIdField()
    district_name = StringField()
    next_dispatch_time = IntegerField()

    @classmethod
    @gen.coroutine
    def GetTaskFromId(cls, task_id):
        if isinstance(task_id, ObjectId) is False:
            task_id = ObjectId(task_id)
        result = yield cls.find_one({'_id': task_id})
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetCourierTasks(cls, courier_id, limit, skip, status=None):
        if isinstance(courier_id, ObjectId) is False:
            courier_id = ObjectId(courier_id)
        condition = {
            'courier_id': courier_id
        }
        if isinstance(status, list):
            condition['status'] = {
                '$in': status
            }
        elif status:
            condition['status'] = status
        sort_condition = [
            ('created_time', -1)
        ]
        result = yield cls.find(condition).skip(skip).limit(limit).sort(sort_condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetCourierTask(cls, courier_id, task_id):
        if not isinstance(courier_id, ObjectId):
            courier_id = ObjectId(courier_id)
        if not isinstance(task_id, ObjectId):
            task_id = ObjectId(task_id)
        condition = {
            '_id': task_id,
            'courier_id': courier_id
        }
        task = yield cls.find_one(condition)
        raise gen.Return(task)

    @classmethod
    @gen.coroutine
    def StartTask(cls, courier_id, task_id):
        condition = {
            "courier_id": courier_id,
            "_id": task_id
        }
        setter = {
            "$set": {
                "status": "processing",
                "started_time": int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, setter)

    @classmethod
    @gen.coroutine
    def CreateWaitingTask(cls, district_name, district_id, shop_id, shop_location, shop_name, shop_mobile, shop_address, subtask_ids):
        '''
        my_task = {
            'district_name': district_name,
            'district_id': district_id,
            'status': 'waiting',
            'shop': {
                '_id': shop_id,
                'location': shop_location,
                'name': shop_name,
                'mobile': shop_mobile,
                'address': shop_address
            },
            'subtasks': subtask_ids,
            'created_time': int(time.time()*1000)
        }

        logging.info(my_task)
        task_id =  yield cls.insert(my_task)
        raise gen.Return(task_id)
        '''
        condition = {
            'what_the_fuck': True,
            'created_time': int(time.time()*1000)
        }

        updater = {
            '$set': {
                'district_name': district_name,
                'district_id': district_id,
                'status': 'waiting',
                'shop': {
                    '_id': shop_id,
                    'location': shop_location,
                    'name': shop_name,
                    'mobile': shop_mobile,
                    'address': shop_address
                },
                'subtasks': subtask_ids,
                'created_time': int(time.time()*1000)
            }
        }

        result = yield cls.update(condition, updater, upsert=True)
        logging.info(result)
        raise gen.Return(result['upserted'])


    @classmethod
    @gen.coroutine
    def SetTaskDispatched(cls, task_id, my_courier):
        condition = {
            '_id': task_id,
            'status': 'waiting'
        }
        updater = {
            '$set': {
                'courier_id': my_courier['_id'],
                'status': 'dispatched',
                'dispatched_time': int(time.time()*1000),
                'courier_name': my_courier.get('name', ''),
                'courier_mobile': my_courier.get('mobile', '')
            }
        }
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def MarkTaskDone(cls, task_id):
        condition = {
            "_id": task_id
        }
        setter = {
            "$set": {
                "status": "done",
                "done_time": int(time.time()*1000)
            }
        }
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetTaskFromSubtaskId(cls, subtask_id):
        condition = {
            "subtasks": subtask_id
        }
        task = yield cls.find_one(condition)
        raise gen.Return(task)

    @classmethod
    @gen.coroutine
    def GetWaitingTasks(cls):
        condition = {
            'status': 'waiting'
        }
        tasks = yield cls.find(condition).to_list(None)
        raise gen.Return(tasks)

    @classmethod
    @gen.coroutine
    def SetTaskNextDispatchTime(cls, task_id, next_time):
        condition = {
            '_id': task_id
        }
        updater = {
            '$set': {
                'next_dispatch_time': next_time
            }
        }
        result = yield cls.update(condition, updater)
        raise gen.Return(result)
Beispiel #14
0
class ShopModel(EmbeddedDocument):
    _id = ObjectIdField()
    name = StringField()
    mobile = MobileField()
    address = StringField()
    location = LocationField()
Beispiel #15
0
class ConsoleUserModel(Document):
    meta = {'db': settings.mongodb.CONSOLE_DB, 'collection': 'users'}

    # must add unique index of name in the database
    name = StringField(min_length=1, max_length=32, required=True, unique=True)
    password = StringField(required=True)
    status = StringField(required=True, candidate=STATUS_CANDIDATES)
    roles = GenericListField()
    mobile = MobileField(required=True)
    note = StringField()
    realname = StringField(min_length=1, max_length=32, required=True)

    region = StringField()
    province = StringField()
    city = StringField()
    school_name = StringField()
    school_id = ObjectIdField()

    @classmethod
    @gen.coroutine
    def CheckPassword(cls, name, pass_hash):
        condition = {'name': name, 'password': pass_hash}
        user = yield cls.find_one(condition)
        raise gen.Return(user)

    @classmethod
    @gen.coroutine
    def GetUserFromId(cls, user_id):
        condition = {'_id': user_id}
        user = yield cls.find_one(condition)
        raise gen.Return(user)

    @classmethod
    @gen.coroutine
    def GetUsersFromStatus(cls, status):
        condition = {'status': status}
        users = yield cls.find(condition).to_list(None)
        raise gen.Return(users)

    @classmethod
    @gen.coroutine
    def GetUsersAndCountFromArea(cls,
                                 skip=None,
                                 limit=None,
                                 region=None,
                                 province=None,
                                 city=None,
                                 school_name=None,
                                 other_condition=None):
        condition = {}
        if region:
            condition['region'] = region
        if province:
            condition['province'] = province
        if city:
            condition['city'] = city
        if school_name:
            condition['school_name'] = school_name
        if other_condition:
            for k, v in other_condition.items():
                condition[k] = v
        query = cls.find(condition)
        count = yield query.count()
        if skip is not None and limit is not None:
            users = yield query.limit(limit).skip(skip).to_list(None)
        else:
            users = yield query.to_list(None)
        result = (users, count)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def CreateNewUser(cls, data):
        condition = {'name': data['name']}
        result = yield cls.update(condition, data, upsert=True)
        new_user_id = result.get('upserted')
        if new_user_id:
            raise gen.Return(('created', new_user_id))
        elif result.get('updatedExisting') == True:
            raise gen.Return(('updated', None))
        else:
            raise gen.Return(('failed', None))

    @classmethod
    @gen.coroutine
    def UpdateUserInfo(cls, user_id, user_data):
        condition = {'_id': user_id}
        updater = {'$set': user_data}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetUsersByName(cls, name):
        condition = {'name': name}
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)
Beispiel #16
0
class CourierModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'courier'}
    mobile = MobileField(required=True)
    name = StringField()
    birthday = StringField()
    graduate_year = IntegerField()
    qq = StringField()
    password = StringField()
    device_token = EmbeddedDocumentField(DeviceTokenModel)
    certificate_image = ObjectIdField()
    created_time = IntegerField()
    updated_time = IntegerField()
    schedule = EmbeddedDocumentField(WeekScheduleModel)
    account = ListField(EmbeddedDocumentField(AccountModel))
    balance = IntegerField()
    location = LocationField()
    school = StringField()
    status = StringField(required=True, candidate=COURIER_STATUS)
    shop_id = ObjectIdField()
    district_id = ObjectIdField()

    delivery_buildings = GenericListField()

    debt = IntegerField()
    account_status = StringField(candidate=ACCOUNT_STATUS)
    account_locked_time = IntegerField()
    account_unlocked_time = IntegerField()

    @classmethod
    @gen.coroutine
    def GetCourierFromId(cls, courier_id):
        if isinstance(courier_id, ObjectId) is False:
            courier_id = ObjectId(courier_id)
        result = yield cls.find_one({'_id': courier_id})
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetUserFromMobile(cls, mobile):
        courier = yield cls.find_one({'mobile': mobile})
        raise gen.Return(courier)

    @classmethod
    @gen.coroutine
    def CheckPassword(cls, mobile, password):
        condition = {'mobile': mobile, 'password': password}
        courier = yield cls.find_one(condition)
        if courier:
            raise gen.Return(courier)
        else:
            raise gen.Return(False)

    @classmethod
    @gen.coroutine
    def CreateNewIfNotExists(cls, mobile):
        courier = yield cls.find_one({'mobile': mobile})
        if courier:
            raise gen.Return(False)
        else:
            now = int(time.time() * 1000)
            courier_data = {
                'mobile': mobile,
                'created_time': now,
                'updated_time': now,
                'status': 'unsubmitted'
            }
            result = yield cls.insert(courier_data)
            raise gen.Return(True)

    @classmethod
    @gen.coroutine
    def UpdateDeviceToken(cls, courier_id, device_type, device_token):
        condition = {'_id': courier_id}
        updater = {'$set': {'device_token': {device_type: device_token}}}
        yield cls.update(condition, updater)

    @classmethod
    @gen.coroutine
    def UpdateProfile(cls, courier_id, **kw):
        condition = {'_id': courier_id}
        updater = {'$set': {}}
        if 'name' in kw:
            updater['$set']['name'] = kw['name']
        if 'birthday' in kw:
            updater['$set']['birthday'] = kw['birthday']
        if 'school' in kw:
            updater['$set']['school'] = kw['school']
        if 'graduate_year' in kw:
            updater['$set']['graduate_year'] = kw['graduate_year']
        if 'qq' in kw:
            updater['$set']['qq'] = kw['qq']
        if 'certificate_image' in kw:
            updater['$set']['certificate_image'] = kw['certificate_image']
        if 'district_id' in kw:
            updater['$set']['district_id'] = kw['district_id']
        yield cls.update(condition, updater)

    @classmethod
    @gen.coroutine
    def UpdatePassword(cls, mobile, password):
        condition = {
            'mobile': mobile,
        }
        updater = {'$set': {'password': password}}
        yield cls.update(condition, updater)

    @classmethod
    @gen.coroutine
    def UpdateLocation(cls, courier_id, lgt, lat):
        condition = {'_id': courier_id}
        updater = {'$set': {'location': [lgt, lat]}}
        yield cls.update(condition, updater)

    @classmethod
    @gen.coroutine
    def GetScheduleId(cls, courier_id, week):
        condition = {'_id': courier_id}
        select = {'schedule': 1}
        sched = yield cls.find_one(condition, select)
        raise gen.Return(sched['schedule'][week])

    @classmethod
    @gen.coroutine
    def UpdateAccount(cls, mobile, account):
        condition = {'mobile': mobile}
        updater = {'$set': {'account': account}}
        yield cls.update(condition, updater)

    @classmethod
    @gen.coroutine
    def MinusWithdrawAndUpdateAccount(cls,
                                      courier_id,
                                      withdraw_type,
                                      account,
                                      name,
                                      money,
                                      update_account_flag,
                                      bank_name=None,
                                      bank_province_city=None,
                                      bank_branch=None):
        # check and update withdraw info first
        account_info = {
            "type": withdraw_type,
            "account_id": account,
            "account_name": name,
            "bank_name": bank_name,
            "bank_province_city": bank_province_city,
            "bank_branch": bank_branch
        }
        if update_account_flag:
            account_condition = {"_id": courier_id}
            account_setter = {"$push": {"account": account_info}}
            account_update_result = yield cls.update(account_condition,
                                                     account_setter)
            logging.info("[UPDATE WITHDRAW ACCOUNT] [%s,%s,%s]" %
                         (str(courier_id), str(account_info),
                          str(account_update_result)))

        withdraw_condition = {"_id": courier_id, "balance": {"$gte": money}}
        withdraw_setter = {"$inc": {"balance": 0 - int(math.fabs(money))}}
        withdraw_result = yield cls.update(withdraw_condition, withdraw_setter)
        logging.critical("[WITHDRAW] [%s,%s,%s]", str(courier_id), str(money),
                         str(withdraw_result))
        raise gen.Return(withdraw_result)

    @classmethod
    @gen.coroutine
    def ListWithdrawInfo(cls, courier_id):
        query_condition = {"_id": courier_id}
        select_condition = {"account": 1}
        result = yield cls.find_one(query_condition, select_condition)
        retval = result.get('account', [])
        raise gen.Return(retval)

    @classmethod
    @gen.coroutine
    def UpdatePasswordFromMobileAndSetStatusToVerifying(cls, mobile, password):
        condition = {'mobile': mobile}
        setter = {'$set': {'password': password, 'status': 'verifying'}}
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def SetVerifyingAfterFirstProfileUpdate(cls, courier_id):
        condition = {
            "_id": courier_id,
            "status": {
                '$in': ["unsubmitted", "failed"]
            }
        }
        setter = {"$set": {"status": "verifying"}}
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetCouriersFromIds(cls, courier_ids):
        condition = {'_id': {'$in': courier_ids}}
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetCouriersFromShopId(cls, shop_id):
        condition = {'shop_id': shop_id, 'status': 'verified'}
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetNearbyCouriers(cls, location, distance):
        condition = {
            'location': {
                '$geoWithin': {
                    '$center': [location, distance]
                }
            },
            'status': 'verified'
        }
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def AddMoneyToCourierBalance(cls, courier_id, money):
        condition = {'_id': courier_id}
        updater = {'$inc': {'balance': money}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetCouriersFromDistrictId(cls, district_id):
        condition = {'district_id': district_id, 'status': 'verified'}
        result = yield cls.find(condition).to_list(None)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def SetVerified(cls, courier_id, result):
        condition = {'_id': courier_id}
        setter = {'$set': {'status': 'verified' if result else 'failed'}}
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def UpdateDeliveryBuildings(cls, courier_id, building_ids):
        condition = {'_id': courier_id}
        setter = {'$set': {'delivery_buildings': building_ids}}
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def UpdateAccountStatus(cls, courier_id, locked):
        condition = {'_id': courier_id}
        updater = {
            '$set': {
                'account_status': 'locked' if locked else 'normal'
            }
        }
        if locked:
            updater['$set']['account_locked_time'] = int(time.time() * 1000)
        else:
            updater['$set']['account_unlocked_time'] = int(time.time() * 1000)
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def SetDebt(cls, courier_id, new_debt):
        condition = {'_id': courier_id}
        updater = {'$set': {'debt': new_debt}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def UpdateDebt(cls, courier_id, debt_change):
        condition = {'_id': courier_id}
        updater = {'$inc': {'debt': int(debt_change)}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)
Beispiel #17
0
class ActivityModel(Document):
    meta = {
        'db': settings.mongodb.SHARK_DB,
        'collection': 'activity'
    }
    user_id = ObjectIdField()
    created_time = IntegerField(required=True)
    activity_name = StringField(required=True)
    activity_result = StringField(required=True, default=DEFAULT_ACTIVITY_RESULT)
    activity_device = StringField(required=True)

    @classmethod
    @gen.coroutine
    def MarkDeviceIfNotJoinActivity(cls, device_token, activity_name, activity_result):
        condition = {
            "activity_device": device_token,
            "activity_name": activity_name
        }
        updater = {
            "$set": {
                "activity_device": device_token,
                "activity_name": activity_name,
                "created_time": int( time.time() * 1000)
            }
        }
        result = yield cls.update(condition, updater, upsert=True)
        if (result['updatedExisting'] is False) and ('upserted' in result):
            yield cls.update(condition, {"$set": {"activity_result": activity_result}})
        else:
            activity_record = yield cls.find_one(condition)
            activity_result = activity_record['activity_result']
        raise gen.Return( (True, activity_result) )

    @classmethod
    @gen.coroutine
    def GetDeviceActivityResult(cls, device_token, activity_name):
        condition = {
            "activity_device": device_token,
            "activity_name": activity_name
        }
        result = yield cls.find_one(condition)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def BindDeviceUserActivityResult(cls, device_token, user_id, activity_name):
        condition = {
            "activity_name": activity_name,
            "activity_device": device_token,
            "user_id": {"$exists": False}
        }
        updater = {
            "$set": {
                "user_id": user_id
            }
        }
        result = yield cls.update(condition, updater)
        if (result['ok'] == 1) and result['updatedExisting'] and (result['nModified'] > 0):
            raise gen.Return(True)
        else:
            raise gen.Return(False)

    @classmethod
    @gen.coroutine
    def GetUserActivity(cls, user_id, activity_name):
        condition = {
            "user_id": user_id,
            "activity_name": activity_name
        }
        activity = yield cls.find_one(condition)
        raise gen.Return(activity)
Beispiel #18
0
class DeviceTokenModel(EmbeddedDocument):
    android = StringField()
    ios = StringField()
Beispiel #19
0
class TargetModel(EmbeddedDocument):
    name = StringField(required=True, min_length=1, max_length=32)
    mobile = MobileField(required=True)
    location = LocationField(required=True)
    address = StringField(required=True, min_length=1, max_length=512)
Beispiel #20
0
class BillModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'bill'}
    courier_id = ObjectIdField()
    task_id = ObjectIdField()
    withdraw_id = ObjectIdField()
    money = IntegerField(required=True)
    type = StringField(required=True, candidate=BILL_TYPES)
    created_time = IntegerField(required=True)
    subtask_id = ObjectIdField()

    @classmethod
    @gen.coroutine
    def CreateTaskBill(cls, courier_id, task):
        task_id = task['_id']
        money = task['money']  #? task payment?
        data = {
            'courier_id': courier_id,
            'task_id': task_id,
            'type': 'task',
            'money': money,
            'created_time': int(time.time() * 1000)
        }
        bill_id = yield cls.insert(data)
        raise gen.Return(bill_id)

    @classmethod
    @gen.coroutine
    def CreateWithdrawBill(cls, courier_id, withdraw_id, money):
        money = 0 - math.fabs(money)
        data = {
            'courier_id': courier_id,
            'withdraw_id': withdraw_id,
            'money': money,
            'type': 'withdraw',
            'created_time': int(time.time() * 1000)
        }
        bill_id = yield cls.insert(data)
        raise gen.Return(bill_id)

    @classmethod
    @gen.coroutine
    def ListTodayIncomeBill(cls, courier_id):
        now = datetime.datetime.now()
        today = datetime.datetime(now.year, now.month, now.day)
        _0 = datetime.datetime.fromtimestamp(0)
        today_time = int((today - _0).total_seconds() * 1000)
        condition = {
            "created_time": {
                "$gte": today_time
            },
            "type": 'order_fee',
            "courier_id": courier_id
        }
        bills = yield cls.find(condition).to_list(None)
        raise gen.Return(bills)

    @classmethod
    @gen.coroutine
    def ListBills(cls, courier_id, limit=20, skip=0):
        query_condition = {"courier_id": courier_id}
        sort_condition = [('created_time', -1)]
        bills = yield cls\
            .find(query_condition)\
            .sort(sort_condition)\
            .limit(limit)\
            .skip(skip)\
            .to_list(None)
        raise gen.Return(bills)

    @classmethod
    @gen.coroutine
    def CreateSubtaskBill(cls, courier_id, subtask_id, pay):
        data = {
            'courier_id': courier_id,
            'subtask_id': subtask_id,
            'type': 'subtask',
            'money': pay,
            'created_time': int(time.time() * 1000)
        }
        bill_id = yield cls.insert(data)
        raise gen.Return(bill_id)

    @classmethod
    @gen.coroutine
    def CreateOrderFeeBill(cls, courier_id, subtask_id, money):
        data = {
            "courier_id": courier_id,
            "subtask_id": subtask_id,
            "money": money,
            "created_time": int(time.time() * 1000),
            "type": "order_fee"
        }
        bill_id = yield cls.insert(data)
        raise gen.Return(bill_id)

    @classmethod
    @gen.coroutine
    def CreateFineBill(cls, courier_id, withdraw_id, fine_amount):
        data = {
            "courier_id": courier_id,
            "withdraw_id": withdraw_id,
            "money": fine_amount,
            "created_time": int(time.time() * 1000),
            "type": "fine"
        }
        bill_id = yield cls.insert(data)
        raise gen.Return(bill_id)
Beispiel #21
0
class ExpendModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'expend'}

    withdraw_id = ObjectIdField(required=True)
    withdraw_amount = IntegerField(required=True)
    fine_amount = IntegerField(required=True)
    real_amount = IntegerField(required=True)
    status = StringField(required=True,
                         candidate=EXPEND_STATUS,
                         default='unprocessed')
    created_time = IntegerField(required=True)
    courier_id = ObjectIdField(required=True)
    school_id = ObjectIdField()
    courier_name = StringField()

    @classmethod
    @gen.coroutine
    def CreateExpendRecord(cls, courier_id, courier_name, withdraw_id,
                           withdraw_amount, fine_amount, expend_amount,
                           school_id, status):
        data = {
            'courier_id': courier_id,
            'courier_name': courier_name,
            'withdraw_id': withdraw_id,
            'withdraw_amount': withdraw_amount,
            'fine_amount': fine_amount,
            'real_amount': expend_amount,
            'created_time': int(time.time() * 1000),
            'status': status
        }
        if school_id:
            data['school_id'] = school_id
        result = yield cls.insert(data)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def CreateNew(cls, data):
        result = yield cls.insert(data)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetExpendFromId(cls, id_to_find):
        condition = {'_id': id_to_find}
        result = yield cls.find_one(condition)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetExpendsAndCountFromCourierId(cls,
                                        courier_id,
                                        skip=None,
                                        limit=None):
        condition = {'courier_id': courier_id}
        sort_condition = [('created_time', -1)]
        query = cls.find(condition)
        count = yield query.count()
        if skip is not None and limit is not None:
            docs = yield query.sort(sort_condition).skip(skip).limit(
                limit).to_list(None)
        else:
            docs = yield query.sort(sort_condition).to_list(None)
        result = (docs, count)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def SetProcessed(cls, expend_id):
        condition = {'_id': expend_id}
        updater = {'$set': {'status': 'processed'}}
        result = yield cls.update(condition, updater)
        raise gen.Return(result)
Beispiel #22
0
class SubtaskModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'subtask'}
    target = EmbeddedDocumentField(TargetModel)
    status = StringField(required=True,
                         default='submitted',
                         candidate=SUBTASK_STATUS)
    items = ListField(EmbeddedDocumentField(ItemInSubtaskModel), required=True)
    owner_task = ObjectIdField()
    created_time = IntegerField(required=True)
    scheduled_time = IntegerField()
    dispatched_time = IntegerField()
    start_time = IntegerField()
    done_time = IntegerField()
    shop_id = ObjectIdField()
    shop_location = LocationField()
    confirm_code = StringField(required=True)
    pay = IntegerField(required=True, default=0)
    express_no = StringField(required=True)
    app_id = ObjectIdField(required=True)
    comment = StringField(required=True, default="")

    delivery_price = IntegerField()
    order_id = ObjectIdField()

    building_id = ObjectIdField()

    @classmethod
    @gen.coroutine
    def GetSubtaskFromExpressNo(cls, express_no):
        condition = {"express_no": express_no}
        subtask = yield cls.find_one(condition)
        raise gen.Return(subtask)

    @classmethod
    @gen.coroutine
    def GetCourierSubtask(cls, courier_id, subtask_id):
        if not isinstance(courier_id, ObjectId):
            courier_id = ObjectId(courier_id)
        if not isinstance(subtask_id, ObjectId):
            subtask_id = ObjectId(subtask_id)

        condition = {'_id': subtask_id, 'courier_id': courier_id}
        task = yield cls.find_one(condition)
        raise gen.Return(task)

    @classmethod
    @gen.coroutine
    def GetSubtaskFromId(cls, subtask_id):
        if not isinstance(subtask_id, ObjectId):
            subtask_id = ObjectId(subtask_id)

        condition = {
            '_id': subtask_id,
        }
        task = yield cls.find_one(condition)
        raise gen.Return(task)

    @classmethod
    @gen.coroutine
    def CreateWaitingSubtask(cls,
                             app_id,
                             express_no,
                             shop_id,
                             shop_location,
                             target,
                             items,
                             payment,
                             delivery_price,
                             comment,
                             building_id=None):
        confirm_code = ''.join(
            [random.choice(string.digits) for _ in range(CONFIRM_CODE_LENGTH)])
        data = {
            "app_id": app_id,
            "shop_id": shop_id,
            "shop_location": shop_location,
            "target": target,
            "status": "waiting",
            "items": items,
            "created_time": int(time.time() * 1000),
            "confirm_code": confirm_code,
            "express_no": express_no,
            "pay": payment,
            "delivery_price": delivery_price,
            "comment": comment
        }
        if building_id:
            data['building_id'] = building_id
        subtask_id = yield cls.insert(data)
        raise gen.Return(subtask_id)

    @classmethod
    @gen.coroutine
    def MarkSubtasksStart(cls, subtask_ids):
        condition = {
            "_id": {
                "$in": subtask_ids
            },
        }
        setter = {
            "$set": {
                "status": "delivering",
                "start_time": int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, setter, multi=True)

    @classmethod
    @gen.coroutine
    def SetSubtasksScheduled(cls, subtask_ids, task_id):
        condition = {
            '_id': {
                '$in': subtask_ids
            },
            'status': {
                '$in': ['submitted', 'waiting']
            }
        }
        updater = {
            '$set': {
                'owner_task': task_id,
                'status': 'scheduled',
                'scheduled_time': int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, updater, multi=True)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def LockSubtaskForConfirm(cls, subtask_id):
        condition = {'_id': subtask_id}
        updater = {
            '$set': {
                'status': 'lock_for_confirm',
                'done_time': int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, updater)
        if result['updatedExisting'] and (result['ok']
                                          == 1) and (result['nModified'] > 0):
            raise gen.Return(True)
        else:
            raise gen.Return(False)

    @classmethod
    @gen.coroutine
    def UnlockSubtaskAndSetToConfirmed(cls, subtask_id):
        condition = {'_id': subtask_id, 'status': 'lock_for_confirm'}
        updater = {
            '$set': {
                'status': 'done',
                'done_time': int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, updater)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def GetSubtasksStatus(cls, subtask_ids):
        condition = {"_id": {"$in": subtask_ids}}
        statuses = yield cls.find(condition, {"status": 1}).to_list(None)
        raise gen.Return(statuses)

    @classmethod
    @gen.coroutine
    def SetSubtasksDispatched(cls, subtask_ids):
        condition = {
            '_id': {
                '$in': subtask_ids
            },
            'status': {
                '$in': ['scheduled']
            }
        }
        updater = {
            '$set': {
                'status': 'dispatched',
                'dispatched_time': int(time.time() * 1000)
            }
        }
        result = yield cls.update(condition, updater, multi=True)
        raise gen.Return(result)
Beispiel #23
0
class WithdrawModel(Document):
    meta = {'db': settings.mongodb.WUKONG_DB, 'collection': 'withdraw'}
    courier_id = ObjectIdField(required=True)
    account_type = StringField(required=True)
    account = StringField(required=True)
    name = StringField(required=True)
    money = IntegerField(required=True)
    bank_name = StringField()
    bank_province_city = StringField()
    bank_city = StringField()
    bank_branch = StringField()
    status = StringField(required=True,
                         candidate=WITHDRAW_STATUS,
                         default='unprocessed')
    created_time = IntegerField(required=True)
    trade_no = StringField()
    school_id = ObjectIdField()

    @classmethod
    @gen.coroutine
    def GetCourierWithdraws(cls, courier_id, limit=50, skip=0):
        condition = {'courier_id': courier_id}
        sort_condition = [('created_time', -1)]
        withdraws = yield cls.find(condition).limit(limit).skip(skip).sort(
            sort_condition).to_list(None)
        raise gen.Return(withdraws)

    @classmethod
    @gen.coroutine
    def CreateWithdrawRecord(cls,
                             school_id,
                             courier_id,
                             account_type,
                             account,
                             name,
                             money,
                             bank_name=None,
                             bank_province_city=None,
                             bank_branch=None):
        data = {
            'school_id': school_id,
            'courier_id': courier_id,
            'account_type': account_type,
            'account': account,
            'name': name,
            'money': money,
            'bank_name': bank_name,
            'bank_province_city': bank_province_city,
            'bank_branch': bank_branch,
            'created_time': int(time.time() * 1000)
        }
        withdraw_id = yield cls.insert(data)
        raise gen.Return(withdraw_id)

    @classmethod
    @gen.coroutine
    def GetWithdrawFromId(cls, withdraw_id):
        condition = {'_id': withdraw_id}
        withdraw = yield cls.find_one(condition)
        raise gen.Return(withdraw)

    @classmethod
    @gen.coroutine
    def MarkWithdrawProcessed(cls, withdraw_id, trade_no):
        condition = {'_id': withdraw_id, 'status': 'unprocessed'}
        setter = {'$set': {'status': 'processed'}}
        if trade_no:
            setter['$set']['trade_no'] = trade_no
        result = yield cls.update(condition, setter)
        raise gen.Return(result)

    @classmethod
    @gen.coroutine
    def ListWithdraws(cls, courier_id, limit=20, skip=0):
        query_condition = {"courier_id": courier_id}
        sort_condition = [('created_time', -1)]
        withdraws = yield cls\
            .find(query_condition)\
            .sort(sort_condition)\
            .limit(limit)\
            .skip(skip)\
            .to_list(None)
        raise gen.Return(withdraws)