add deploy settings, test for task with message bonus

prod
Dmitriy Shesterkin 9 years ago
parent ebfe8b6055
commit 57fc5c0070
  1. 1
      .gitignore
  2. 4
      conf/deploy/common.py
  3. 4
      conf/deploy/stage.py
  4. 7
      conf/deploy/stage.py.example
  5. 12
      requirements/base.txt
  6. 14
      requirements/test.txt
  7. 32
      src/customer/context_processors.py
  8. 60
      src/customer/utils.py
  9. 34
      src/factories/models.py
  10. 11
      src/tests/fixtures/models.py
  11. 5
      src/tests/test_tasks.py
  12. 58
      src/tests/test_utils.py

1
.gitignore vendored

@ -198,4 +198,3 @@ target/
*.css.map *.css.map
################################################################################ ################################################################################

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
PROJECT_NAME = 'dokumentor'
REPO = 'git@bitbucket.org:Air51/dokumentor_dev.git'
BRANCH = 'develop'

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
USER = 'mitri4'
PASS = '9091324913Dasha'
HOSTS = ['mitri4.pro:8022']

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
from deploy.common import * # noqa
USER = 'username'
PASS = 'password'
HOSTS = ['domain_name:ssh_port']

@ -71,16 +71,4 @@ django-redis==4.8.0
redis==2.10.5 redis==2.10.5
trans==2.1.0 trans==2.1.0
python-decouple==3.0 python-decouple==3.0
flake8==3.3.0
numpy==1.13.0 numpy==1.13.0
mock==2.0.0
mockredispy==2.9.3
pytest-django==3.1.2
pytest-sugar==0.8.0
factory-boy==2.8.1
django-test-plus==1.0.17
Faker==0.7.15
coverage==4.4.1
pytest-flake8==0.8.1
pycodestyle==2.3.1
pyflakes==1.5.0

@ -0,0 +1,14 @@
-r base.txt
freezegun-0.3.9
mock==2.0.0
mockredispy==2.9.3
pytest-django==3.1.2
pytest-sugar==0.8.0
factory-boy==2.8.1
django-test-plus==1.0.17
Faker==0.7.15
coverage==4.4.1
pytest-flake8==0.8.1
pycodestyle==2.3.1
pyflakes==1.5.0
flake8==3.3.0

@ -3,6 +3,7 @@ import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from django.core.cache import cache from django.core.cache import cache
from customer.models import License from customer.models import License
from customer.utils import get_display_message_for_bonus
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -14,11 +15,6 @@ def license_check_soon_ends(request):
days_left = cache.get(f'days_left_{request.user.username}', None) days_left = cache.get(f'days_left_{request.user.username}', None)
cur_license = cache.get(f'cur_license_{request.user.username}', None) cur_license = cache.get(f'cur_license_{request.user.username}', None)
print(license_cookie)
print(license_15days)
print(days_left)
print(cur_license)
if not days_left or not cur_license: if not days_left or not cur_license:
now = datetime.today() now = datetime.today()
cur_license = License.objects.filter( cur_license = License.objects.filter(
@ -66,10 +62,22 @@ def license_check_soon_ends(request):
def confirm_user_bonus(request): def confirm_user_bonus(request):
if request.user: if request.user:
bonus_url = '#' context = {}
message = f'У вас есть ещё <b>10 дней</b>, чтобы получить <a href="{bonus_url}">бонус</a>' close_bonus_cookie = request.COOKIES.get('close_message_bonus')
test_message = 'This message is personal confirm for <b>current user</b>' confirm_bonus_days = cache.get(f'confirm_bonus_days_{request.user.username}', None)
return {
'confirm_bonus': True, if confirm_bonus_days >= 0:
'message_bonus': message message = get_display_message_for_bonus(confirm_bonus_days)
} context['confirm_bonus'] = True
context['message'] = message
return context
if not close_bonus_cookie:
pass
else:
return context
# return {
# 'confirm_bonus': True,
# 'message_bonus': message
# }

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from datetime import timedelta from datetime import timedelta
from pytils import numeral
from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from django.template.loader import render_to_string from django.template.loader import render_to_string
@ -9,11 +11,10 @@ from commons.utils import get_site_url
def check_one_profile(profile, now, manual=False): def check_one_profile(profile, now, manual=False):
from customer.models import License from customer.models import License
profile_is_active = profile.active profile_is_active = profile.active
licenses = License.objects.\ licenses = License.objects. \
filter(company=profile, date_from__lte=now, date_to__gte=now, filter(company=profile, date_from__lte=now, date_to__gte=now,
status__in=[-1, 1, 2], deleted=False) status__in=[-1, 1, 2], deleted=False)
licenses.filter(status=1).update(status=2) licenses.filter(status=1).update(status=2)
@ -98,3 +99,58 @@ def raise_if_no_profile(request):
raise Exception( raise Exception(
f"Profile not found for user: {request.user.pk}, '{request.user.username}'" f"Profile not found for user: {request.user.pk}, '{request.user.username}'"
) )
def check_confirm_bonus_to_user(user):
"""
Check user for displaying a message with an offer for
purchase of licenses for additional bonus licenses
Show message only for new users in the period
from 5 to 15 day
If user already have licenses don't display message
:param user: DockUser instance
:return: count of days remaining for getting bonus,
integer or False if nothing to use
"""
from customer.models import License
lic = License.objects.filter(
company=user.profile,
status__in=[1, 2],
deleted=False
)
if lic:
return False
today = timezone.now().date()
date_join_start = today - timezone.timedelta(days=15)
date_join_end = today - timezone.timedelta(days=6)
if date_join_start <= user.date_joined.date() <= date_join_end:
delta = user.date_joined.date() - date_join_start
return delta.days
return False
def get_display_message_for_bonus(day_count):
"""
Get text to display the message depending on count of days
:param day_count: Count of days, integer
:return: Message for display in page, string
"""
message = ''
if type(day_count) == str:
return message
if day_count > 10:
return message
if day_count == 0:
message = f'Сегодня последний день что бы получить <a href="#">бонус</a>'
if day_count > 0:
message = f'У вас есть ещё <b>{day_count} ' \
f'{numeral.choose_plural(day_count, "день, дня, дней")}</b>, ' \
f'чтобы получить <a href="#">бонус</a>'
return message

@ -6,7 +6,7 @@ import factory.fuzzy
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from myauth.models import ConfirmEmail from myauth.models import ConfirmEmail
from customer.models import UserProfile from customer.models import UserProfile, License
from functools import partial from functools import partial
USER_PASSWORD = 'test' USER_PASSWORD = 'test'
@ -74,3 +74,35 @@ class ConfirmEmailFactory(factory.django.DjangoModelFactory):
class Meta: class Meta:
model = ConfirmEmail model = ConfirmEmail
class LicenseFactory(factory.django.DjangoModelFactory):
company = factory.SubFactory(ProfileFactory)
term = factory.Iterator([1, 6, 12, 24])
date_from = Faker(
'past_datetime',
start_date='-30d',
tzinfo=pytz.UTC
)
date_to = Faker(
'future_datetime',
end_date='+30d',
tzinfo=pytz.UTC
)
payform = factory.Iterator([0, 1])
status = factory.Iterator([1, 2])
order_date = Faker(
'past_datetime',
start_date='-30d',
tzinfo=pytz.UTC
)
paid_date = Faker(
'past_datetime',
start_date='-30d',
tzinfo=pytz.UTC
)
pay_sum = factory.Iterator([1000, 2000])
deleted = False
class Meta:
model = License

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest import pytest
from factories.models import UserFactory, ConfirmEmailFactory from factories.models import UserFactory, ConfirmEmailFactory, LicenseFactory
@pytest.fixture @pytest.fixture
@ -17,3 +17,12 @@ def confirm_email():
def profile(): def profile():
_user = UserFactory() _user = UserFactory()
return _user.profile return _user.profile
@pytest.fixture()
def lic():
_user = UserFactory()
_lic = LicenseFactory()
_lic.company = _user.profile
_lic.save()
return _lic

@ -1,8 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import pytest import pytest
from freezegun import freeze_time
from django.utils import timezone from django.utils import timezone
from myauth.models import DokUser, ConfirmEmail from myauth.models import DokUser, ConfirmEmail
from customer.models import UserProfile from customer.models import UserProfile
@ -23,6 +26,7 @@ dates_lt_five = [timezone.now() - timezone.timedelta(days=5),
dates_bonus = [timezone.now() - timezone.timedelta(days=5)] dates_bonus = [timezone.now() - timezone.timedelta(days=5)]
@freeze_time("2017-06-28 00:21:34", tz_offset=2)
@pytest.mark.parametrize('create_date', dates_gte_five) @pytest.mark.parametrize('create_date', dates_gte_five)
@pytest.mark.django_db @pytest.mark.django_db
def test_delete_not_activated_users_great_five_days(user, create_date): def test_delete_not_activated_users_great_five_days(user, create_date):
@ -61,6 +65,7 @@ def test_delete_not_activated_users_less_five_day(user, create_date):
assert ConfirmEmail.objects.count() == 1 assert ConfirmEmail.objects.count() == 1
@freeze_time("2017-06-28 00:21:34", tz_offset=2)
@pytest.mark.parametrize('create_date', dates_bonus) @pytest.mark.parametrize('create_date', dates_bonus)
@pytest.mark.django_db @pytest.mark.django_db
def test_send_offer_for_get_bonus(user, create_date): def test_send_offer_for_get_bonus(user, create_date):

@ -4,7 +4,19 @@ import pytest
from django.utils import timezone from django.utils import timezone
from commons.utils import get_site_url from commons.utils import get_site_url
from customer.utils import check_one_profile from customer.utils import check_one_profile, check_confirm_bonus_to_user
from customer.utils import get_display_message_for_bonus
dates = [timezone.now() - timezone.timedelta(days=6),
timezone.now() - timezone.timedelta(days=10),
timezone.now() - timezone.timedelta(days=15)]
dates_not_in_range = [timezone.now() - timezone.timedelta(days=5),
timezone.now() - timezone.timedelta(days=16)]
days = [1, 5, 10, 4, 6, 0]
not_days = ['1', 11, 'test']
def test_utils_with_request(mocked_request): def test_utils_with_request(mocked_request):
@ -17,6 +29,50 @@ def test_utils_without_request():
assert len(url.split('//')) == 2 assert len(url.split('//')) == 2
@pytest.mark.parametrize('create_date', dates)
@pytest.mark.django_db
def test_check_confirm_bonus_to_user(user, create_date):
user.date_joined = create_date
user.save()
day_remain = check_confirm_bonus_to_user(user)
assert day_remain >= 0
@pytest.mark.parametrize('create_date', dates_not_in_range)
@pytest.mark.django_db
def test_check_confirm_bonus_to_user_not_in_range(user, create_date):
user.date_joined = create_date
user.save()
day_remain = check_confirm_bonus_to_user(user)
assert day_remain is False
@pytest.mark.parametrize('create_date', dates)
@pytest.mark.django_db
def test_check_confirm_bonus_to_user_with_license(lic, create_date):
user = lic.company.users.first()
user.date_joined = create_date
user.save()
day_remain = check_confirm_bonus_to_user(user)
assert day_remain is False
@pytest.mark.parametrize('count_day', days)
def test_get_display_message_for_bonus(count_day):
text = get_display_message_for_bonus(count_day)
assert text != ''
if count_day == 0:
assert 'последний' in text
if count_day != 0:
assert 'последний' not in text
@pytest.mark.parametrize('count_day', not_days)
def test_get_display_message_for_bonus_not_valid(count_day):
text = get_display_message_for_bonus(count_day)
assert text == ''
@pytest.mark.django_db @pytest.mark.django_db
def test_check_one_profile(profile): def test_check_one_profile(profile):
check_one_profile(profile, timezone.now(), manual=False) check_one_profile(profile, timezone.now(), manual=False)

Loading…
Cancel
Save