Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######################################################
- # To Do: How can we refactor code to make that faster?
- ######################################################
- def func(moderator_ids):
- Post.objects.filter(user__pk__in=moderator_ids) \
- .update(updated_at=timezone.now())
- #
- ######################################################
- ######################################################
- # To Do: What problems can we encounter with? How can you impove the code?
- ######################################################
- # problem #1: what if paypal request fails?
- # problem #2: race condition
- def withdraw(user_id, amount):
- balance = UserBalance.objects.get(user_id=user_id)
- pp_result = send_request_to_paypal(balance.paypal_id, amount)
- if not pp_result.all_ok:
- raise FokkenPaypal(':(')
- (
- UserBalance.objects.filter(user_id=user_id)
- .update(amount=F('amount') - amount)
- )
- #
- ######################################################
- ######################################################
- # To Do:
- # 1. Write a code of model `Webinar`, that contains the fields:
- # - title
- # - is_published
- # - starts_at
- #
- # 2. Please, write a code of the function `get_nearest_webinar`. The function returns a published webinar that starts in the near future.
- # 3. Write a code of the function `count_published_webinars`. That returns a number of published webinars.
- # 4. How you could change the code if the count of published webinars is displayed on the really visited page?
- #
- # P.S.: If you need use additional classes you are welcome.
- ######################################################
- class Webinar(models.Model):
- title = models.TextField()
- is_published = models.BooleanField(default=False)
- starts_at = models.DatetimeField()
- # should be Webinar's method but whatever
- def get_nearest_webinar():
- return (
- Webinar.objects.filter(is_published=True, starts_at__gt=timezone.now())
- .order_by('starts_at')
- .first()
- )
- @lru_cache # for the really visited page. Or use django cache
- def count_published_webinars():
- return Webinar.objects.filter(is_published=True).count()
- #
- ######################################################
- ######################################################
- # Please, add the processing of probably exceptions and logging:
- ######################################################
- #
- log = logging.getLogger()
- @login_required()
- def disable_subscription(request):
- account = get_object_or_404(Account, user=request.user)
- if request.method == 'POST':
- if request.POST.get('do_disable'):
- account.switch_to_free()
- log.info(f'account pk:{account.pk} set free')
- return HttpResponseRedirect(reverse('app:my_account'))
- # I assume if there is no do_disable or do_disable=False that means
- # some one os hacky hacky so who cares
- return render(request, 'theme/disable_subscription.html')
- #
- ######################################################
- ######################################################
- # There is a really visited site. Many uWSGI workers. What problem will you probably encounter?
- # What changes may you suggest?
- ######################################################
- #
- def increment_views(webinar_id):
- # oh no, race condition! Lets fix it, lol!
- (
- Webinar.objects.filter(pk=webinar_id)
- .update(views_count=F('views_count') + 1)
- )
- #...
- #
- ######################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement