Beispiel #1
0
class LogicAddDevice(object):
    def __init__(self):
        self.device = DBOHHODevice()

    def add_device(self, application_id, identity_id, mac_address, tx_power):

        data_dict = dict()
        data_dict["application_id"] = application_id
        data_dict["identity_id"] = identity_id
        data_dict["mac_address"] = mac_address
        data_dict["tx_power"] = tx_power

        self.device.set_identity(identity_id)
        device = self.device.get_by_identity()
        if not device:
            add = self.device.add(data_dict)
            if add:
                result = Result.result_success()
                device = self.device.get_by_identity()
                result["device_id"] = device.id
            else:
                result = Result.result_failed()
        else:
            result = Result.result_exist(DEVICE_EXIST)
            result["device_id"] = device.id
        return result
Beispiel #2
0
class Device(object):
    def __init__(self, identity_id=None, mac_address=None):
        self.device = DBOHHODevice(identity_id, mac_address)
        self.relation = DBOHHOUserAndDeviceRelation()
        self.imei = DBOHHOUserAndDeviceIMEI()
        self.user = DBOHHOUser()
        self.countryCode = DBOHHOCountryCode()

    def set_identity(self, identity_id):
        self.device.set_identity(identity_id)

    def get_identity(self):
        return self.device.identity_id

    def set_mac_address(self, mac_address):
        self.device.set_mac_address(mac_address)

    def get_mac_address(self):
        return self.device.mac_address

    def get_by_identity(self):
        return self.device.get_by_identity()

    def bind_device(self, user_id, type=1):
        try:
            OHHOLog.print_log("bind device start")
            device = self.get_by_identity()
            if device and self.device.check_by_mac():
                relation = self.relation.get_by_device(device.id)
                if relation and self.relation.is_valid(relation, True):
                    if relation.user_id == int(user_id):
                        result = Result.result_exist(RELATION_EXIST)
                    else:
                        user = self.user.get_by_id(relation.user_id)
                        cellphone = user.cellphone
                        country_code_id = user.country_code_id
                        country_code_obj = self.countryCode.get_by_id(
                            country_code_id)
                        if country_code_obj:
                            country_code = country_code_obj.country_code
                        else:
                            country_code = "+86"
                        result = Result.result_device_used_by_other()
                        result["country_code"] = country_code
                        result["cellphone_number"] = cellphone
                else:
                    success = self.relation.add_without_commit({
                        "device_id":
                        device.id,
                        "user_id":
                        user_id,
                        "type":
                        type
                    })
                    if success:
                        self.relation.commit()
                        result = Result.result_success()
                    else:
                        self.relation.rollback()
                        result = Result.result_failed()
            else:
                result = Result.result_not_exist()
            OHHOLog.print_log("bind device end")
            return result
        except Exception as ex:
            OHHOLog.print_log(ex)
            return Result.result_failed(ex)

    def unbind_device(self, user_id):
        device = self.device.get_by_identity()
        if device and self.device.check_by_mac():
            OHHOLog.print_log(device.id)
            relation = self.relation.get_by_device(device.id)
            if not relation:
                return Result.result_success(RELATION_NOT_EXIST)
            elif relation.user_id == int(user_id):
                if not self.relation.is_valid(relation, True):
                    return Result.result_success(RELATION_EXIST)
                else:
                    success = self.relation.delete(relation)
                    if success:
                        return Result.result_success()
                    else:
                        return Result.result_failed()
            else:
                return Result.result_failed(USED_BY_OTHER)
        else:
            return Result.result_failed(INVALID_DEVICE)

    def is_device_valid(self):
        device = self.device.get_by_identity()
        # OHHOLog.print_log(self.device.get_identity())
        # OHHOLog.print_log(device)
        if device and self.device.check_by_mac():
            relation = self.relation.get_by_device(device.id)
            if relation and self.relation.is_valid(relation, True):
                result = Result.result_device_used_by_other()
                user = DBOHHOUser()
                user_instance = user.get_by_id(relation.user_id)
                if user_instance:
                    result["cellphone_number"] = user_instance.cellphone
                    code = DBOHHOCountryCode.get_by_id(
                        user_instance.country_code_id)
                    if code:
                        result["country_code"] = code.country_code
                    else:
                        result["country_code"] = "+86"
                return result
            else:
                return Result.result_success()
        else:
            return Result.result_failed(INVALID_DEVICE)

    def get_relation_by_device(self):
        device = self.device.get_by_identity()
        if device:
            relation = self.relation.get_by_device(device.id)
            if self.relation.is_valid(relation, True):
                return relation
            else:
                return None
        return None

    def get_primary_relation_by_user(self, user_id):
        query = self.relation.get_query()
        query = self.relation.get_valid(query)
        relations = self.relation.get_by_user(query, user_id)
        relations = self.relation.get_primary(relations)
        return relations

    def relation_update(self, instance, data_dict):
        return self.relation.update(instance, data_dict)

    def get_all_device(self):
        return self.device.get_query()

    def find_by_identity(self, query):
        return self.device.find_by_identity(query)

    def find_by_mac_address(self, query):
        return self.device.find_by_mac_address(query)

    def get_some_devices(self, query, offset, limit):
        return self.device.get_some(query, offset, limit)

    def get_by_id(self, device_id):
        return self.device.get_by_id(device_id)

    def update(self, instance, data):
        return self.device.update(instance, data)

    def add(self, data):
        return self.device.add(data)

    def delete(self, instance):
        return self.device.delete(instance)

    def is_empty(self, query):
        count = query.count()
        if count > 0:
            return False
        return True

    def set_lost(self, user_id, identity_id):
        self.device.set_identity(identity_id)
        device = self.device.get_by_identity()
        if device:
            query = self.relation.get_valid_by_device(device.id)
            if query:
                query = self.relation.get_by_user(query, user_id)
                query = self.relation.get_not_lost(query)
                OHHOLog.print_log(query.count())
            if query:
                success = self.relation.set_lost(query)
                if success:
                    return Result.result_success()
                else:
                    return Result.result_failed()
            else:
                return Result.result_failed("this device is not yours!")
        else:
            return Result.result_failed("this device is invalid")

    def cancel_lost(self, user_id, identity_id):
        self.device.set_identity(identity_id)
        device = self.device.get_by_identity()
        if device:
            query = self.relation.get_valid_by_device(device.id)
            if query:
                query = self.relation.get_by_user(query, user_id)
                OHHOLog.print_log(query.count())
                # query = self.relation.get_valid(query)
                # OHHOLog.print_log(query.count())
                query = self.relation.get_lost(query)
                OHHOLog.print_log(query.count())
            if query:
                success = self.relation.cancel_lost(query)
                if success:
                    return Result.result_success()
                else:
                    return Result.result_failed()
            else:
                return Result.result_failed("this device is not yours!")
        else:
            return Result.result_failed("this device is invalid")

    def get_valid_relation_by_user(self, user_id):
        query = self.relation.get_query()
        query = self.relation.get_valid(query)
        query = self.relation.get_by_user(query, user_id)
        return query

    def get_device_information(self, device_id):
        device = self.device.get_by_id(device_id)
        result = self.device.get_information(device)
        imei = self.get_imei_by_deivce(device_id)
        if imei:
            result["imei"] = imei.imei
        else:
            result["imei"] = ""
        return result

    def get_imei_by_deivce(self, device_id):
        query = self.imei.get_query()
        instance = self.imei.get_by_device_id(query, device_id)
        # query = self.imei.order_by_id_desc(query)
        # instance = self.imei.first(query)
        return instance
class LogicAddUserNew(object):
    def __init__(self):
        self.user = DBOHHOUser()
        self.user_and_device_relation = DBOHHOUserAndDeviceRelation()
        self.password = Password()
        self.token = Token()
        self.user_extension = DBOHHOUserAccuracyExtension()
        self.interest = DBOHHOInterest()
        self.map = DBOHHOMapInformation()
        self.device = DBOHHODevice()
        self.record_user_and_match_condition = DBOHHORecordUserAndMatchCondition(
        )
        self.match_condition = DBOHHORecordMatchCondition()
        self.im_user = DBOHHOIMUser()
        self.im_user_relation = DBOHHOIMUserRelation()
        self.user_configuration = DBOHHOUserConfiguration()
        self.imei = IMEI()
        self.user_and_device_imei = DBOHHOUserAndDeviceIMEI()
        self.friend = Friend()
        self.apply = DBOHHORecordMatchApply()
        self.country_code = DBOHHOCountryCode()
        self.user_and_cellphone_relation = DBOHHOUserAndCellphoneRelation()

        self.user_favourite_book = DBOHHOUserFavouriteBook()
        self.user_favourite_movie = DBOHHOUserFavouriteMovie()
        self.user_favourite_sport = DBOHHOUserFavouriteSport()
        self.user_favourite_music = DBOHHOUserFavouriteMusic()
        self.user_icon = DBOHHOUserIcon()
        self.user_description = DBOHHOUserDescription()
        self.user_impression = DBOHHOUserImpression()

        # self.cellphone = 10000000000
        # self.identity_id = 9000000000

    def add_user(self):
        cellphone = 10000000000
        identity_id = 9000000000
        for i in range(10):
            cellphone = cellphone + 1
            identity_id = identity_id + 1
            while self.user.get_by_cellphone(cellphone):
                cellphone = cellphone + 1
            user_id = self.add_user_table(str(cellphone))
            self.add_user_token_table(user_id)
            self.add_user_extension(user_id)
            self.add_user_configuration(user_id)
            # self.add_user_and_cellphone_relation(user_id)
            self.device.set_identity(identity_id)
            while self.device.get_by_identity():
                identity_id = identity_id + 1
            device_id = self.add_device(identity_id)
            self.add_user_and_device_imei(user_id, device_id)
            self.add_user_and_device_relation(user_id, device_id)
            match_condition_id = self.add_match_condition()
            print("match_condition_id:")
            print(match_condition_id)
            self.add_user_match_condition(user_id, match_condition_id)

    def add_device(self, identity_id):
        data = dict()
        data["identity_id"] = identity_id
        self.device.add(data)
        query = self.device.get_query()
        query = self.device.order_by_id_desc(query)
        obj = Operation.first(query)
        return obj.id

    def add_match_condition(self):
        data = dict()
        data["sex"] = OhhoRandom.get_sex()
        data["small_age"] = 0
        data["big_age"] = 150
        data["interest"] = OhhoRandom.get_user_match_interest()
        self.match_condition.add(data)
        query = self.match_condition.get_query()
        query = self.match_condition.order_by_id_desc(query)
        obj = Operation.first(query)
        return obj.id

    def add_user_and_device_relation(self, user_id, device_id):
        data = dict()
        data["user_id"] = user_id
        data["device_id"] = device_id
        self.user_and_device_relation.add(data)

    def add_user_and_device_imei(self, user_id, device_id):
        data = dict()
        data["user_id"] = user_id
        data["device_id"] = device_id
        data["imei"] = OhhoRandom.get_imei()
        self.user_and_device_imei.add(data)

    def add_user_and_cellphone_relation(self, user_id):
        data = dict()
        data["user_id"] = user_id
        data["cellphone_id"] = OhhoRandom.get_cellphone_id()
        self.user_and_cellphone_relation.add(data)

    def add_user_configuration(self, user_id):
        data = dict()
        data["user_id"] = user_id
        data["is_match"] = 1
        data["is_online"] = 1
        self.user_configuration.add(data)

    def add_user_match_condition(self, user_id, match_condition_id):
        data = dict()
        data["user_id"] = user_id
        data["match_condition_id"] = match_condition_id
        self.record_user_and_match_condition.add(data)

    def add_user_extension(self, user_id):
        data = dict()
        data["user_id"] = user_id
        data["sex"] = OhhoRandom.get_sex()
        data["birthday"] = OhhoRandom.get_birthday()
        data["interest"] = OhhoRandom.get_user_interest()
        self.user_extension.add(data)

    def add_user_token_table(self, user_id):
        token = OHHORandom.get_nonce()
        token_dict = dict()
        token_dict["user_id"] = user_id
        token_dict["token"] = token
        self.token.token.add(token_dict)

    def add_user_table(self, cellphone="00000000000"):
        """添加用户 参数 """
        data = dict()
        password = "******"
        country_code_id = 159
        username = OHHOUUID.get_uuid1_string()
        self.password.set_password(password)
        encryption_password = self.password.encryption()
        data["username"] = username
        data["password"] = encryption_password
        data["cellphone"] = cellphone
        data["country_code_id"] = country_code_id
        self.user.add(data)
        user = self.user.get_by_username(username)
        return user.id