Esempio n. 1
0
def key():
    return key_generator(size=6, chars=ascii_lowercase + digits, )
Esempio n. 2
0
    def form_valid(self, form, ):
        # print form
        # print form.cleaned_data
        # print form.cleaned_data.get('POST_NAME', None, )
        if form.cleaned_data.get('name', None, ):
            self.coupon_group = CouponGroup.objects.create(**form.cleaned_data)
            how_much_coupons = form.cleaned_data.get('how_much_coupons', 0, )
            start_of_the_coupon = form.cleaned_data.get('start_of_the_coupon', datetime.now(), )
            # start_of_the_coupon = datetime.strptime(start_of_the_coupon, '%d.%m.%Y %H:%M:%S', )
            start_of_the_coupon = start_of_the_coupon.strftime('%Y-%m-%d %H:%M:%S')
            end_of_the_coupon = form.cleaned_data.get('end_of_the_coupon', datetime.now(), )
            # end_of_the_coupon = datetime.strptime(end_of_the_coupon, '%d.%m.%Y %H:%M:%S', )
            end_of_the_coupon = end_of_the_coupon.strftime('%Y-%m-%d %H:%M:%S')
            cursor = connection.cursor()
            name = form.cleaned_data.get('name', None, )
            number_of_possible_uses = form.cleaned_data.get('number_of_possible_uses', 0, )
            percentage_discount = form.cleaned_data.get('percentage_discount', 0, )
            if SERVER:
                ins = '''insert into Coupon (name, coupon_group_id, `key`, number_of_possible_uses, number_of_uses, percentage_discount, start_of_the_coupon, end_of_the_coupon, created_at, updated_at)
                         values ('%s', %d, '%s', %d, 0, %d, '%s', '%s', NOW(), NOW())'''
            else:
                ins = '''insert into Coupon (name, coupon_group_id, key, number_of_possible_uses, number_of_uses, percentage_discount, start_of_the_coupon, end_of_the_coupon, created_at, updated_at)
                         values ('%s', %d, '%s', %d, 0, %d, '%s', '%s', datetime('now'), datetime('now'))'''
            for i in range(how_much_coupons, ):
                success = 0
                unsuccess = 0
                ok = True
                while ok:
                    # print 'success: %d unsuccess: %d' % (success, unsuccess, )
                    # print 'SERVER: %s' % SERVER
                    key = key_generator(size=6, chars=ascii_lowercase + digits, )
                    insert = ins % (name,
                                    self.coupon_group.id,
                                    key,
                                    int(number_of_possible_uses),
                                    int(percentage_discount),
                                    start_of_the_coupon,
                                    end_of_the_coupon, )
                    # print insert
                    try:
                        with transaction.atomic():
                            cursor.execute(insert, )
                            # Captcha_Key.objects.create(image=image, )
                    except IntegrityError as s:
                        print 'IntegrityError', ' Key: ', key
                        print type(s, )
                        print s
                        unsuccess += 1
                    except Exception as inst:
                        print type(inst, )
                        print inst
                        unsuccess += 1
                    else:
                        success += 1
                        ok = False

#                Coupon.objects.create(name=name,
#                                      key=key_generator(size=6, chars=ascii_lowercase + digits, ),
#                                      coupon_group=self.coupon_group,
#                                      number_of_possible_uses=number_of_possible_uses,
#                                      percentage_discount=percentage_discount,
#                                      start_of_the_coupon=start_of_the_coupon,
#                                      end_of_the_coupon=end_of_the_coupon, )
        return super(CouponGroupCreateEdit, self, ).form_valid(form, )
Esempio n. 3
0
def hash_16(size=16, ):
    return key_generator(size=size, )
Esempio n. 4
0
def hash_64(size=64, ):
    return key_generator(size=size, )
Esempio n. 5
0
def hash_8(size=8, ):
    return key_generator(size=size, )