|
|
@ -314,7 +314,7 @@ def set_user_download_times_flag(user_id, act): |
|
|
|
return cache.set(user_can_download_key, act, 3600 * 24) |
|
|
|
return cache.set(user_can_download_key, act, 3600 * 24) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_free_download_times(user_id, act='get'): |
|
|
|
def get_user_free_download_times(user_id, act='get', amount=1): |
|
|
|
now = timezone.now() |
|
|
|
now = timezone.now() |
|
|
|
user_free_download_times_key = "_".join( |
|
|
|
user_free_download_times_key = "_".join( |
|
|
|
[CACHE_KEY_TEMPLATE.get("user_free_download_times_key"), str(now.year), str(now.month), str(now.day), |
|
|
|
[CACHE_KEY_TEMPLATE.get("user_free_download_times_key"), str(now.year), str(now.month), str(now.day), |
|
|
@ -322,26 +322,26 @@ def get_user_free_download_times(user_id, act='get'): |
|
|
|
user_free_download_times = cache.get(user_free_download_times_key) |
|
|
|
user_free_download_times = cache.get(user_free_download_times_key) |
|
|
|
if user_free_download_times is not None: |
|
|
|
if user_free_download_times is not None: |
|
|
|
if act == 'set': |
|
|
|
if act == 'set': |
|
|
|
return cache.incr(user_free_download_times_key, -1) |
|
|
|
return cache.incr(user_free_download_times_key, -amount) |
|
|
|
else: |
|
|
|
else: |
|
|
|
return user_free_download_times |
|
|
|
return user_free_download_times |
|
|
|
else: |
|
|
|
else: |
|
|
|
cache.set(user_free_download_times_key, USER_FREE_DOWNLOAD_TIMES, 3600 * 24) |
|
|
|
cache.set(user_free_download_times_key, USER_FREE_DOWNLOAD_TIMES, 3600 * 24) |
|
|
|
if act == 'set': |
|
|
|
if act == 'set': |
|
|
|
return cache.incr(user_free_download_times_key, -1) |
|
|
|
return cache.incr(user_free_download_times_key, -amount) |
|
|
|
else: |
|
|
|
else: |
|
|
|
return USER_FREE_DOWNLOAD_TIMES |
|
|
|
return USER_FREE_DOWNLOAD_TIMES |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def consume_user_download_times(user_id, app_id): |
|
|
|
def consume_user_download_times(user_id, app_id, amount=1): |
|
|
|
with cache.lock("%s_%s" % ('consume_user_download_times', user_id)): |
|
|
|
with cache.lock("%s_%s" % ('consume_user_download_times', user_id)): |
|
|
|
if get_user_free_download_times(user_id) > 0: |
|
|
|
if get_user_free_download_times(user_id, 'get', amount) > 0: |
|
|
|
get_user_free_download_times(user_id, 'set') |
|
|
|
get_user_free_download_times(user_id, 'set', amount) |
|
|
|
else: |
|
|
|
else: |
|
|
|
if not check_user_can_download(user_id): |
|
|
|
if not check_user_can_download(user_id): |
|
|
|
return False |
|
|
|
return False |
|
|
|
try: |
|
|
|
try: |
|
|
|
UserInfo.objects.filter(pk=user_id).update(download_times=F('download_times') - 1) |
|
|
|
UserInfo.objects.filter(pk=user_id).update(download_times=F('download_times') - amount) |
|
|
|
except Exception as e: |
|
|
|
except Exception as e: |
|
|
|
logger.error("%s download_times less then 0. Exception:%s" % (user_id, e)) |
|
|
|
logger.error("%s download_times less then 0. Exception:%s" % (user_id, e)) |
|
|
|
disable_user_download_times_flag(user_id) |
|
|
|
disable_user_download_times_flag(user_id) |
|
|
|