Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ before_script:

script:
- flake8 .
- pytest --cov=pythonpro
- pytest pythonpro --cov=pythonpro
after_success:
- codecov
notifications:
Expand Down
3 changes: 3 additions & 0 deletions pythonpro/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
1 change: 0 additions & 1 deletion pythonpro/dashboard/conftest.py

This file was deleted.

5 changes: 5 additions & 0 deletions pythonpro/dashboard/facade.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 30 additions & 1 deletion pythonpro/dashboard/tests/test_interaction_creation.py
Original file line number Diff line number Diff line change
@@ -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={
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pythonpro/dashboard/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
]
9 changes: 7 additions & 2 deletions pythonpro/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'})
7 changes: 6 additions & 1 deletion pythonpro/mailchimp/facade.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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']
Expand Down
3 changes: 2 additions & 1 deletion pythonpro/mailchimp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pythonpro/modules/templates/topics/topic_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ <h1 class="mt-4 mb-3">{{ topic.title }}</h1>
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
})();
</script>
{% else %}
<div id='discourse-comments'>Membros possuem acesso a fórum de discussão aqui ;)</div>
{% endif %}
</div>
</div>
Expand Down