Esempio n. 1
0
def create_one_license(selling_shop,data_transfer_plan,prefix):
    # read current index
    bulk_license_manager = BulkLicenseManagement.objects.all()[0]
    current_index = bulk_license_manager.current_index

    # set up username and password
    username = prefix + str(current_index)
    password = generate_md5_hash(selling_shop.username + str(current_index))
    password = password[: ( len(password)/2  ) ]
    new_user = User(username=username, password=password)

    current_index2 = BulkLicenseManagement.objects.all()[0].current_index
    if current_index != current_index2:
        raise DatabaseError()

    else:
        new_user = User.objects.create_user(username=username, password=password)

        # create a userprofile for the new customer
        new_user_profile = UserProfile()
        new_user_profile.user = new_user
        new_user_profile.is_shop = False
        new_user_profile.email_subscription = False
        new_user_profile.save()

        # create CustomerProfile
        new_customer_profile = CustomerProfile()
        new_customer_profile.user_profile = new_user_profile
        new_customer_profile.save()

        #create purchase object
        new_purchase = Purchase()
        new_purchase.user = new_user
        new_purchase.data_transfer_plan = data_transfer_plan
        new_purchase.remaining_allowance_frequency = data_transfer_plan.freq
        new_purchase.selling_shop = selling_shop
        # purchase.shop_debited = false is by default.

        new_purchase.save()
        new_purchase.follow_up_number = generate_md5_hash(str(new_purchase.id))

        new_purchase.save()

        #increase the current index
        current_index = current_index + 1
        bulk_license_manager.current_index = current_index
        bulk_license_manager.save()

        return (username,password)