def test_null(self): IntegerModel.objects.create(big=100) obj = IntegerModel.objects.annotate( null_mod_small=Mod("small", "normal"), null_mod_normal=Mod("normal", "big"), ).first() self.assertIsNone(obj.null_mod_small) self.assertIsNone(obj.null_mod_normal)
def test_null(self): IntegerModel.objects.create(big=100) obj = IntegerModel.objects.annotate( null_mod_small=Mod('small', 'normal'), null_mod_normal=Mod('normal', 'big'), ).first() self.assertIsNone(obj.null_mod_small) self.assertIsNone(obj.null_mod_normal)
def test_integer(self): IntegerModel.objects.create(small=20, normal=15, big=1) obj = IntegerModel.objects.annotate( small_mod=Mod("small", "normal"), normal_mod=Mod("normal", "big"), big_mod=Mod("big", "small"), ).first() self.assertIsInstance(obj.small_mod, float) self.assertIsInstance(obj.normal_mod, float) self.assertIsInstance(obj.big_mod, float) self.assertEqual(obj.small_mod, math.fmod(obj.small, obj.normal)) self.assertEqual(obj.normal_mod, math.fmod(obj.normal, obj.big)) self.assertEqual(obj.big_mod, math.fmod(obj.big, obj.small))
def add_base64_padding(apps, schema_editor): U2fDevice = apps.get_model('otp_u2f', 'U2fDevice') credential_qs = U2fDevice.objects.annotate( padding=Value(4) - Mod(Length('credential'), Value(4)) ) credential_qs.filter(padding=1).update( credential=Concat(F('credential'), Value('='))) credential_qs.filter(padding=2).update( credential=Concat(F('credential'), Value('=='))) credential_qs.filter(padding=3).update( credential=Concat(F('credential'), Value('==='))) public_key_qs = U2fDevice.objects.annotate( padding=Value(4) - Mod(Length('public_key'), Value(4)) ) public_key_qs.filter(padding=1).update( public_key=Concat(F('public_key'), Value('='))) public_key_qs.filter(padding=2).update( public_key=Concat(F('public_key'), Value('=='))) public_key_qs.filter(padding=3).update( public_key=Concat(F('public_key'), Value('===')))
def test_float(self): FloatModel.objects.create(f1=-25, f2=0.33) obj = FloatModel.objects.annotate(f_mod=Mod("f1", "f2")).first() self.assertIsInstance(obj.f_mod, float) self.assertAlmostEqual(obj.f_mod, math.fmod(obj.f1, obj.f2))
def test_decimal(self): DecimalModel.objects.create(n1=Decimal("-9.9"), n2=Decimal("4.6")) obj = DecimalModel.objects.annotate(n_mod=Mod("n1", "n2")).first() self.assertIsInstance(obj.n_mod, Decimal) self.assertAlmostEqual(obj.n_mod, Decimal(math.fmod(obj.n1, obj.n2)))
def test_decimal(self): DecimalModel.objects.create(n1=Decimal('-9.9'), n2=Decimal('4.6')) obj = DecimalModel.objects.annotate(n_mod=Mod('n1', 'n2')).first() self.assertIsInstance(obj.n_mod, Decimal) self.assertAlmostEqual(obj.n_mod, Decimal(math.fmod(obj.n1, obj.n2)))
def get_need_async_objects_queryset(self, bucket, id_gt: int = 0, limit: int = 1000, meet_time=None, id_mod_div: int = None, id_mod_equal: int = None, backup_num: int = None): """ 获取需要同步的对象的查询集, id正序排序 :param bucket: bucket instance :param id_gt: 查询id大于id_gt的数据,实现分页续读 :param limit: 获取数据的数量 :param meet_time: 查询upt大于此时间的对象 :param id_mod_div: object id求余的除数,和参数id_mod_equal一起使用 :param id_mod_equal: object id求余的余数相等的筛选条件,仅在参数id_mod有效时有效 :param backup_num: 筛选条件,只查询指定备份点编号需要同步的对象,默认查询所有备份点需要同步的对象 :return: QuerySet """ table_name = bucket.get_bucket_table_name() bfm = BucketFileManagement(collection_name=table_name) object_class = bfm.get_obj_model_class() backup_nums = [] if backup_num is not None: if isinstance(backup_num, list): backup_nums = backup_num else: backup_nums.append(backup_num) else: for backup in bucket.backup_buckets.all(): if backup.status == BackupBucket.Status.START: if backup.backup_num in backup.BackupNum.values: backup_nums.append(backup.backup_num) if not backup_nums: return object_class.objects.none() queryset = object_class.objects.filter(fod=True, id__gt=id_gt).all() if meet_time is None: meet_time = self._get_meet_time() queryset = queryset.filter(Q(upt__lt=meet_time) | Q(upt__isnull=True)) if backup_nums == [ BackupBucket.BackupNum.ONE, ]: queryset = queryset.filter( Q(async1__isnull=True) | Q(upt__gt=F('async1'))).order_by('id') elif backup_nums == [ BackupBucket.BackupNum.TWO, ]: queryset = queryset.filter( Q(async2__isnull=True) | Q(upt__gt=F('async2'))).order_by('id') else: queryset = queryset.filter( Q(async1__isnull=True) | Q(upt__gt=F('async1')) | Q(async2__isnull=True) | Q(upt__gt=F('async2'))).order_by('id') if id_mod_div is not None and id_mod_equal is not None: if id_mod_div >= 1 and (0 <= id_mod_equal < id_mod_div): queryset = queryset.annotate( id_mod=Mod('id', id_mod_div)).filter(id_mod=id_mod_equal) return queryset[0:limit]