From fa8bde22eadc25bc7bc87370f922f428519d1abf Mon Sep 17 00:00:00 2001 From: renzon Date: Wed, 3 Jul 2019 15:32:51 -0300 Subject: [PATCH] Added tag "never-watched-video" to new leads close #1298 --- .travis.yml | 2 +- pythonpro/conftest.py | 3 ++ pythonpro/dashboard/conftest.py | 1 - pythonpro/dashboard/facade.py | 5 +++ .../tests/test_interaction_creation.py | 31 ++++++++++++++++++- pythonpro/dashboard/urls.py | 2 +- pythonpro/dashboard/views.py | 9 ++++-- pythonpro/mailchimp/facade.py | 7 ++++- pythonpro/mailchimp/tests.py | 3 +- .../templates/topics/topic_detail.html | 2 -- 10 files changed, 55 insertions(+), 10 deletions(-) delete mode 100644 pythonpro/dashboard/conftest.py create mode 100644 pythonpro/dashboard/facade.py diff --git a/.travis.yml b/.travis.yml index 29a26620..f2bf17d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ before_script: script: - flake8 . - - pytest --cov=pythonpro + - pytest pythonpro --cov=pythonpro after_success: - codecov notifications: diff --git a/pythonpro/conftest.py b/pythonpro/conftest.py index 54966152..07a78f0a 100644 --- a/pythonpro/conftest.py +++ b/pythonpro/conftest.py @@ -40,3 +40,6 @@ def client_with_client(client, django_user_model, logged_user): assign_role(logged_user, 'client') client.force_login(logged_user) return client + + +pytest_plugins = ['pythonpro.modules.tests.test_topics_view'] diff --git a/pythonpro/dashboard/conftest.py b/pythonpro/dashboard/conftest.py deleted file mode 100644 index 27dde779..00000000 --- a/pythonpro/dashboard/conftest.py +++ /dev/null @@ -1 +0,0 @@ -from pythonpro.modules.tests.test_topics_view import topic, chapter, section, module # noqa \ No newline at end of file diff --git a/pythonpro/dashboard/facade.py b/pythonpro/dashboard/facade.py new file mode 100644 index 00000000..08f87cbb --- /dev/null +++ b/pythonpro/dashboard/facade.py @@ -0,0 +1,5 @@ +from pythonpro.dashboard.models import TopicInteraction as _TopicInteracion + + +def has_watched_any_topic(user) -> bool: + return _TopicInteracion.objects.filter(user=user).exists() diff --git a/pythonpro/dashboard/tests/test_interaction_creation.py b/pythonpro/dashboard/tests/test_interaction_creation.py index 115a5c23..7df73457 100644 --- a/pythonpro/dashboard/tests/test_interaction_creation.py +++ b/pythonpro/dashboard/tests/test_interaction_creation.py @@ -1,11 +1,32 @@ import pytest from django.urls import reverse +from model_mommy import mommy from pythonpro.dashboard.models import TopicInteraction @pytest.fixture -def resp(client_with_lead, topic, logged_user): +def remove_tags_mock(mocker): + return mocker.patch('pythonpro.dashboard.views.remove_tags') + + +@pytest.fixture +def resp(client_with_lead, topic, logged_user, remove_tags_mock): + return client_with_lead.post( + reverse('dashboard:topic_interaction'), + data={ + 'topic': topic.id, + 'topic_duration': 200, + 'total_watched_time': 120, + 'max_watched_time': 60 + }, + secure=True + ) + + +@pytest.fixture +def resp_with_interaction(client_with_lead, topic, logged_user, remove_tags_mock): + mommy.make(TopicInteraction, user=logged_user, topic=topic) return client_with_lead.post( reverse('dashboard:topic_interaction'), data={ @@ -18,6 +39,14 @@ def resp(client_with_lead, topic, logged_user): ) +def test_user_first_video(resp, remove_tags_mock, logged_user): + remove_tags_mock.assert_called_once_with(logged_user.email, 'never-watched-video') + + +def test_user_not_first_video(resp_with_interaction, remove_tags_mock): + assert remove_tags_mock.call_count == 0 + + def test_topic_interaction_status_code(resp): return resp.status_code == 200 diff --git a/pythonpro/dashboard/urls.py b/pythonpro/dashboard/urls.py index cf6004b1..a692b48c 100644 --- a/pythonpro/dashboard/urls.py +++ b/pythonpro/dashboard/urls.py @@ -4,6 +4,6 @@ app_name = 'dashboard' urlpatterns = [ - path('topic_interaction', views.topic_interation, name='topic_interaction'), + path('topic_interaction', views.topic_interaction, name='topic_interaction'), path('', views.home, name='home'), ] diff --git a/pythonpro/dashboard/views.py b/pythonpro/dashboard/views.py index ecdf6d99..0a36a70f 100644 --- a/pythonpro/dashboard/views.py +++ b/pythonpro/dashboard/views.py @@ -3,8 +3,10 @@ from django.shortcuts import render from django.views.decorators.csrf import csrf_exempt +from pythonpro.dashboard.facade import has_watched_any_topic from pythonpro.dashboard.forms import TopicInteractionForm from pythonpro.dashboard.models import TopicInteraction +from pythonpro.mailchimp.facade import remove_tags @login_required @@ -15,10 +17,13 @@ def home(request): @login_required @csrf_exempt -def topic_interation(request): +def topic_interaction(request): data = dict(request.POST.items()) - data['user'] = request.user.id + user = request.user + data['user'] = user.id form = TopicInteractionForm(data) if form.is_valid(): + if not has_watched_any_topic(user): + remove_tags(user.email, 'never-watched-video') form.save() return JsonResponse({'msg': 'ok'}) diff --git a/pythonpro/mailchimp/facade.py b/pythonpro/mailchimp/facade.py index 05eea4b0..9c239ff4 100644 --- a/pythonpro/mailchimp/facade.py +++ b/pythonpro/mailchimp/facade.py @@ -37,7 +37,8 @@ def _create_or_update(name: str, email: str, role: str): }, 'interests': { role_id: True - } + }, + 'tags': ['never-watched-video'] } return _members_client.create(_list_id, data) @@ -46,6 +47,10 @@ def tag_as(email: str, *tags): return _members_client.tags.update(_list_id, email, {'tags': [{'name': tag, 'status': 'active'} for tag in tags]}) +def remove_tags(email: str, *tags): + return _members_client.tags.update(_list_id, email, {'tags': [{'name': tag, 'status': 'inactive'} for tag in tags]}) + + def _update_member_role(email: str, role: str) -> dict: member = _members_client.get(_list_id, email) interests = member['interests'] diff --git a/pythonpro/mailchimp/tests.py b/pythonpro/mailchimp/tests.py index cb4151aa..a3bf8e21 100644 --- a/pythonpro/mailchimp/tests.py +++ b/pythonpro/mailchimp/tests.py @@ -44,7 +44,8 @@ def test_creation(existing_lead, resps): }, 'interests': { roles_to_ids[facade._LEAD]: True - } + }, + 'tags': ['never-watched-video'] } facade.create_or_update_lead('Renzo', 'host@python.pro.br') assert json.loads(resps.calls[-1].request.body) == expected_on_request diff --git a/pythonpro/modules/templates/topics/topic_detail.html b/pythonpro/modules/templates/topics/topic_detail.html index cd40bf2f..77f6f845 100644 --- a/pythonpro/modules/templates/topics/topic_detail.html +++ b/pythonpro/modules/templates/topics/topic_detail.html @@ -119,8 +119,6 @@

{{ topic.title }}

(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d); })(); - {% else %} -
Membros possuem acesso a fórum de discussão aqui ;)
{% endif %}