Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
Merge pull request #93 from ponyriders/92-fsk18-list-in-notifications
Browse files Browse the repository at this point in the history
closes #92
  • Loading branch information
dArignac committed Jan 16, 2016
2 parents 4c08525 + 9591c55 commit fb2d3dd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

TO_BE_ANNOUNCED
---------------
**Features:**

- list FSK 18 products in notification mail if region is Germany `#92 <https://github.com/ponyriders/django-amazon-price-monitor/issues/92>`__

`0.6 <https://pypi.python.org/pypi/django-amazon-price-monitor/0.6>`__
----------------------------------------------------------------------
**Features:**
Expand Down
4 changes: 2 additions & 2 deletions price_monitor/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ProductAdmin(admin.ModelAdmin):

"""Admin for the model Product"""

list_display = ('asin', 'title', 'artist', 'status', 'date_updated', 'date_last_synced', )
list_filter = ('status', )
list_display = ('asin', 'title', 'artist', 'audience_rating', 'status', 'date_updated', 'date_last_synced', )
list_filter = ('status', 'audience_rating', )
search_fields = ('asin', )
readonly_fields = ('current_price', 'highest_price', 'lowest_price',)

Expand Down
5 changes: 3 additions & 2 deletions price_monitor/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@
settings,
'PRICE_MONITOR_I18N_EMAIL_NOTIFICATION_BODY',
ugettext_lazy(
'The price limit of {price_limit:0.2f} {currency:s} has been reached for the article "{product_title:s}" - '
'the current price is {price:0.2f} {currency:s}.'
'The price limit of {price_limit:0.2f} {currency:s} has been reached for the article "{product_title:s}"\n'
'Current price is {price:0.2f} {currency:s} ({price_date:s}).'
'\n\n'
'Please support our platform by using this affiliate link for buying the product: {url_product_amazon:s}'
'\n'
'Adjust the price limits for the products here: {url_product_detail:s}'
'\n\n'
'{additional_text:s}'
'\n'
'Regards,'
'\n'
Expand Down
12 changes: 12 additions & 0 deletions price_monitor/models/Product.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ def get_title(self):
self.title if self.title is not None and len(self.title) > 0 else _('Unsynchronized Product'),
)

def get_detail_url(self):
"""
Returns the url to a product detail view.
As the frontend is AngularJS, we cannot use any Django reverse functionality.
:return: the link
"""
return '{base_url:s}/#/products/{asin:s}'.format(
base_url=app_settings.PRICE_MONITOR_BASE_URL,
asin=self.asin,
)

def __str__(self):
"""
Expand Down
42 changes: 41 additions & 1 deletion price_monitor/product_advertising_api/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
)
from django.utils import timezone

from django.utils.translation import ugettext

from price_monitor import app_settings
from price_monitor.models import (
Price,
Expand Down Expand Up @@ -311,7 +313,7 @@ def run(self, product_pk, price_pk, subscription_pk, **kwargs):

logger.info('Trying to send notification email to %s...', subscription.email_notification.email)
try:
send_mail(product, subscription, price)
send_mail(product, subscription, price, self.get_audience_rating_info(product))
except SMTPServerDisconnected:
logger.exception('SMTP server was disconnected.')
else:
Expand All @@ -321,3 +323,41 @@ def run(self, product_pk, price_pk, subscription_pk, **kwargs):
return True

return False

def get_audience_rating_info(self, product):
"""
Checks, if the product matches specific audience rating and includes additional information.
If the region is DE and the product is a FSK 18 one, additionally get all other FSK 18 products and put them into a mailable list.
see https://github.com/ponyriders/django-amazon-price-monitor/issues/92
As we do not currently have any use cases that could be generalized to something using the audience rating this is a country specific implementation.
:param product: the product to check
:type product: price_monitor.models.Product
:return: an additional mail text or empty string if product and installation do not match prerequisites.
:rtype: str
"""
if app_settings.PRICE_MONITOR_AMAZON_PRODUCT_API_REGION == 'DE' and product.audience_rating == 'Freigegeben ab 18 Jahren':
# mail text
mail_text = ''

# fetch all other products with FSK 18
for p in Product.objects.filter(audience_rating='Freigegeben ab 18 Jahren').exclude(pk=product.pk).order_by('current_price'):
mail_text += '{title:s}\n'.format(title=p.get_title())
mail_text += '{price:0.2f} {currency:s} ({price_date:s})\n'.format(
price=p.current_price.value,
currency=p.current_price.currency,
price_date=p.current_price.date_seen.strftime('%b %d, %Y %H:%M %p %Z'),
)
mail_text += '{offer_url:s}\n'.format(offer_url=p.offer_url)
mail_text += '{product_detail_url:s}\n\n'.format(product_detail_url=product.get_detail_url())

# prepend introduction if there were results
if mail_text:
mail_text = '\n{intro:s}\n\n'.format(
intro=ugettext('As this is a FSK 18 article, here are your other subscribed FSK 18 articles:')
) + mail_text

# return
return mail_text
return ''
8 changes: 6 additions & 2 deletions price_monitor/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def get_product_detail_url(asin):
)


def send_mail(product, subscription, price):
def send_mail(product, subscription, price, additional_text=''):
"""
Sends an email using the appropriate settings for formatting aso.
Expand All @@ -50,16 +50,20 @@ def send_mail(product, subscription, price):
:type subscription: price_monitor.models.Subscription
:param price: the current price
:type price: price_monitor.models.Price
:param additional_text: additional text to include in mail
:type additional_text: str
"""
django_send_mail(
_(app_settings.PRICE_MONITOR_I18N_EMAIL_NOTIFICATION_SUBJECT) % {'product': product.title},
_(app_settings.PRICE_MONITOR_I18N_EMAIL_NOTIFICATION_BODY).format(
price_limit=subscription.price_limit,
currency=price.currency,
price=price.value,
price_date=price.date_seen.strftime('%b %d, %Y %H:%M %p %Z'),
product_title=product.get_title(),
url_product_amazon=product.offer_url,
url_product_detail=get_product_detail_url(product.asin),
url_product_detail=product.get_detail_url(),
additional_text=additional_text,
),
app_settings.PRICE_MONITOR_EMAIL_SENDER,
[subscription.email_notification.email],
Expand Down
11 changes: 8 additions & 3 deletions tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ def test_set_failed_to_sync(self):
self.assertEqual(2, p.status)

def test_get_image_urls(self):
"""
Tests the Product.get_image_urls method.
"""
"""Tests the Product.get_image_urls method."""

# FIXME usually you would test a HTTP and a HTTPS setup but override_settings does not work with our app_settings (the setting does not get overwritten)

Expand Down Expand Up @@ -58,3 +56,10 @@ def test_get_image_urls(self):
'{0}{1}'.format(app_settings.PRICE_MONITOR_AMAZON_SSL_IMAGE_DOMAIN, '/ponyriders/django-amazon-price-monitor/large.png'),
p.get_image_urls()['large']
)

def test_get_detail_url(self):
"""Tests the get_detail_url method"""
p = Product.objects.create(
asin='ASIN0054321',
)
assert p.get_detail_url() == str(app_settings.PRICE_MONITOR_BASE_URL + '/#/products/ASIN0054321')

0 comments on commit fb2d3dd

Please sign in to comment.