Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################################
  2. # To Do: How can we refactor code to make that faster?
  3. ######################################################
  4.  
  5. def func(moderator_ids):
  6.     Post.objects.filter(user__pk__in=moderator_ids) \
  7.     .update(updated_at=timezone.now())
  8.  
  9. #
  10. ######################################################
  11.  
  12.  
  13. ######################################################
  14. # To Do: What problems can we encounter with? How can you impove the code?
  15. ######################################################
  16.  
  17. # problem #1: what if paypal request fails?
  18. # problem #2: race condition
  19. def withdraw(user_id, amount):
  20.     balance = UserBalance.objects.get(user_id=user_id)
  21.  
  22.     pp_result = send_request_to_paypal(balance.paypal_id, amount)
  23.     if not pp_result.all_ok:
  24.         raise FokkenPaypal(':(')
  25.  
  26.     (
  27.         UserBalance.objects.filter(user_id=user_id)
  28.         .update(amount=F('amount') - amount)
  29.     )
  30.  
  31. #
  32. ######################################################
  33.  
  34.  
  35. ######################################################
  36. # To Do:
  37. # 1. Write a code of model `Webinar`, that contains the fields:
  38. # - title
  39. # - is_published
  40. # - starts_at
  41. #
  42. # 2. Please, write a code of the function `get_nearest_webinar`. The function returns a published webinar that starts in the near future.
  43. # 3. Write a code of the function `count_published_webinars`. That returns a number of published webinars.
  44. # 4. How you could change the code if the count of published webinars is displayed on the really visited page?
  45. #
  46. # P.S.: If you need use additional classes you are welcome.
  47. ######################################################
  48.  
  49.  
  50. class Webinar(models.Model):
  51.     title = models.TextField()
  52.     is_published = models.BooleanField(default=False)
  53.     starts_at = models.DatetimeField()
  54.  
  55.  
  56. # should be Webinar's method but whatever
  57. def get_nearest_webinar():
  58.     return (
  59.         Webinar.objects.filter(is_published=True, starts_at__gt=timezone.now())
  60.         .order_by('starts_at')
  61.         .first()
  62.     )
  63.  
  64. @lru_cache  # for the really visited page. Or use django cache
  65. def count_published_webinars():
  66.     return Webinar.objects.filter(is_published=True).count()
  67.  
  68. #
  69. ######################################################
  70.  
  71.  
  72. ######################################################
  73. # Please, add the processing of probably exceptions and logging:
  74. ######################################################
  75. #
  76.  
  77. log = logging.getLogger()
  78.  
  79. @login_required()
  80. def disable_subscription(request):
  81.     account = get_object_or_404(Account, user=request.user)
  82.  
  83.     if request.method == 'POST':
  84.         if request.POST.get('do_disable'):
  85.             account.switch_to_free()
  86.             log.info(f'account pk:{account.pk} set free')
  87.             return HttpResponseRedirect(reverse('app:my_account'))
  88.  
  89.         # I assume if there is no do_disable or do_disable=False that means
  90.         # some one os hacky hacky so who cares
  91.  
  92.     return render(request, 'theme/disable_subscription.html')
  93.  
  94. #
  95. ######################################################
  96.  
  97.  
  98. ######################################################
  99. # There is a really visited site. Many uWSGI workers. What problem will you probably encounter?
  100. # What changes may you suggest?
  101. ######################################################
  102. #
  103.  
  104. def increment_views(webinar_id):
  105.     # oh no, race condition! Lets fix it, lol!
  106.     (
  107.         Webinar.objects.filter(pk=webinar_id)
  108.         .update(views_count=F('views_count') + 1)
  109.     )
  110.     #...
  111. #
  112. ######################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement