Skip to content

Commit

Permalink
#704 check purchase (#709)
Browse files Browse the repository at this point in the history
* Create SuccessPage

* Assert success_page.is_success() in check_purchase

* Setup auto-retry for the check_purchase

* Create TGReport

* Review fixes

* Add the comment to check_purchase's report policy

* Apply linter rules

* Fix typos
  • Loading branch information
ArtemijRodionov committed Jan 28, 2019
1 parent 9cae226 commit a1e74b2
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 14 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Unidecode==1.0.22
ua-parser==0.8.0
user-agents==1.1.0
sorl-thumbnail==12.5.0
python-telegram-bot==11.1.0
https://github.com/selwin/django-user_agents/archive/master.zip
https://github.com/fidals/refarm-site/archive/0.4.28.zip
12 changes: 12 additions & 0 deletions shopelectro/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.conf import settings
from telegram import Bot


class TelegramReport:

def __init__(self):
self.bot = Bot(settings.TG_BOT_TOKEN)

def send(self, text: str):
for id in settings.TG_REPORT_ADDRESSEES:
self.bot.send_message(id, text)
2 changes: 1 addition & 1 deletion shopelectro/selenium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"""

from .driver import SiteDriver
from .pages import CategoryPage, OrderPage
from .pages import CategoryPage, OrderPage, SuccessPage
1 change: 1 addition & 0 deletions shopelectro/selenium/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

from .category import CategoryPage
from .order import OrderPage
from .success import SuccessPage
13 changes: 13 additions & 0 deletions shopelectro/selenium/pages/success.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from shopelectro.selenium.pages import Page

from pages.models import CustomPage


class SuccessPage(Page):

@property
def path(self):
CustomPage.objects.get(slug='order-success').url

def is_success(self):
return 'Заказ принят' in self.driver.find_element_by_tag_name('h1').text
6 changes: 6 additions & 0 deletions shopelectro/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,9 @@ def get_robots_content():

PRODUCTS_ON_PAGE_PC = 48
PRODUCTS_ON_PAGE_MOB = 12

TG_BOT_TOKEN = os.environ.get('TG_BOT_TOKEN')
TG_REPORT_ADDRESSEES = os.environ.get(
'TG_REPORT_ADDRESSEES', '@shopelectro_reports'
).split(',')
CHECK_PURCHASE_RETRIES = int(os.environ.get('CHECK_PURCHASE_RETRIES', '3'))
40 changes: 27 additions & 13 deletions shopelectro/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from django.conf import settings
from django.core.management import call_command
from selenium.common.exceptions import WebDriverException

from shopelectro import selenium
from shopelectro.celery import app
from shopelectro.report import TelegramReport
from shopelectro.models import CategoryPage
from shopelectro.management.commands._update_catalog import utils

Expand Down Expand Up @@ -61,18 +63,30 @@ def update_catalog():
collect_static()
]

# @todo #690:60m Handle errors in check_purchase.
# Report failed attempts. Schedule it in the celery beat.
# @todo #690:30m Schedule check_purchase in the celery beat.


@app.task
def check_purchase():
driver = selenium.SiteDriver(site_url=settings.BASE_URL)
category_page = selenium.CategoryPage(driver, CategoryPage.objects.first().url)
category_page.load()
category_page.add_to_cart()

order_page = selenium.OrderPage(driver)
order_page.load()
order_page.fill_contacts()
order_page.make_order()
@app.task(
bind=True,
autoretry_for=(WebDriverException, AssertionError),
retry_kwargs={'max_retries': settings.CHECK_PURCHASE_RETRIES},
)
def check_purchase(self):
try:
driver = selenium.SiteDriver(site_url=settings.BASE_URL)
category_page = selenium.CategoryPage(driver, CategoryPage.objects.first().slug)
category_page.load()
category_page.add_to_cart()

order_page = selenium.OrderPage(driver)
order_page.load()
order_page.fill_contacts()
order_page.make_order()

success_page = selenium.SuccessPage(driver)
assert success_page.is_success()
except (WebDriverException, AssertionError) as err:
if self.request.retries + 1 > self.max_retries:
# report on the last attempt
TelegramReport().send(f'Can\'t buy a product. Got the error: {err}')
raise err

2 comments on commit a1e74b2

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on a1e74b2 Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 690-44aa8d80 disappeared from shopelectro/tasks.py, that's why I closed #704. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on a1e74b2 Jan 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 690-7e329879 discovered in shopelectro/tasks.py and submitted as #712. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.