family: don't force async cell rendering (#19996)
This commit is contained in:
parent
0fe9f9ad0d
commit
83120c469a
|
@ -18,9 +18,8 @@ from django.utils.translation import ugettext_lazy as _
|
|||
|
||||
from combo.data.models import CellBase
|
||||
from combo.data.library import register_cell_class
|
||||
from combo.utils import NothingInCacheException
|
||||
|
||||
from .utils import is_family_enabled, get_family_json
|
||||
from .utils import is_family_enabled, get_family
|
||||
|
||||
|
||||
@register_cell_class
|
||||
|
@ -44,15 +43,8 @@ class FamilyInfosCell(CellBase):
|
|||
user = self.get_concerned_user(context)
|
||||
if not user or user.is_anonymous():
|
||||
return {}
|
||||
response = get_family_json('family/',
|
||||
user=user,
|
||||
response = get_family(user=user,
|
||||
raise_if_not_cached=not(context.get('synchronous')))
|
||||
if response.status_code == 200:
|
||||
return {'family': response.json()}
|
||||
return {'error': _('An error occured while retrieving family details.')}
|
||||
|
||||
def render(self, context):
|
||||
self.context = context
|
||||
if not context.get('synchronous'):
|
||||
raise NothingInCacheException()
|
||||
return super(FamilyInfosCell, self).render(context)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.http import urlencode
|
||||
|
||||
from combo.utils import requests
|
||||
|
||||
|
@ -25,10 +26,36 @@ def get_passerelle_service():
|
|||
def is_family_enabled():
|
||||
return get_passerelle_service() and hasattr(settings, 'FAMILY_SERVICE')
|
||||
|
||||
def get_family_json(endpoint, **kwargs):
|
||||
def remote_service(endpoint, **kwargs):
|
||||
path = settings.FAMILY_SERVICE.get('root') + endpoint
|
||||
return requests.get(path,
|
||||
cache_duration=0, # because link/unlink cannot invalidate cache
|
||||
remote_service=get_passerelle_service(),
|
||||
headers={'accept': 'application/json'},
|
||||
**kwargs)
|
||||
|
||||
|
||||
def get_family(**kwargs):
|
||||
endpoint = 'family/'
|
||||
return remote_service(endpoint, **kwargs)
|
||||
|
||||
|
||||
def link_family(user, family_id, family_code):
|
||||
endpoint = 'family/link/'
|
||||
kwargs = {
|
||||
'user': user,
|
||||
'invalidate_cache': True,
|
||||
'params': {
|
||||
'login': family_id,
|
||||
'password': family_code,
|
||||
}
|
||||
}
|
||||
return remote_service(endpoint, **kwargs)
|
||||
|
||||
|
||||
def unlink_family(user):
|
||||
endpoint = 'family/unlink/'
|
||||
kwargs = {
|
||||
'user': user,
|
||||
'invalidate_cache': True
|
||||
}
|
||||
return remote_service(endpoint, **kwargs)
|
||||
|
|
|
@ -17,12 +17,11 @@
|
|||
|
||||
from django.views.generic import FormView, TemplateView
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.http import urlencode
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.contrib import messages
|
||||
|
||||
from .forms import FamilyLinkForm
|
||||
from .utils import get_family_json
|
||||
from .utils import link_family, unlink_family
|
||||
|
||||
ERROR_MESSAGES = {
|
||||
100: _('Wrong credentials: make sure you typed them correctly.'),
|
||||
|
@ -41,11 +40,8 @@ class FamilyLinkView(FormView):
|
|||
return '.'
|
||||
|
||||
def form_valid(self, form):
|
||||
endpoint = 'family/link/?' + urlencode({
|
||||
'login': form.cleaned_data['family_id'],
|
||||
'password': form.cleaned_data['family_code'],
|
||||
})
|
||||
response = get_family_json(endpoint, user=self.request.user)
|
||||
response = link_family(self.request.user, form.cleaned_data['family_id'],
|
||||
form.cleaned_data['family_code'])
|
||||
|
||||
if not response.ok or response.json().get('err'):
|
||||
error_code = response.json().get('err')
|
||||
|
@ -61,8 +57,7 @@ class FamilyUnlinkView(TemplateView):
|
|||
template_name = 'family/unlink_confirm.html'
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
endpoint = 'family/unlink/'
|
||||
response = get_family_json(endpoint, user=request.user)
|
||||
response = unlink_family(request.user)
|
||||
|
||||
if not response.ok or response.json().get('err'):
|
||||
messages.error(request, _('An error occured when unlinking.'))
|
||||
|
|
|
@ -52,3 +52,6 @@ if 'DISABLE_MIGRATIONS' in os.environ:
|
|||
return 'notmigrations'
|
||||
|
||||
MIGRATION_MODULES = DisableMigrations()
|
||||
|
||||
|
||||
FAMILY_SERVICE = {'root': '/'}
|
||||
|
|
|
@ -395,27 +395,88 @@ def test_familyinfos_cell_with_placeholders(app, admin_user):
|
|||
family_cell = FamilyInfosCell(page=page, placeholder='content', order=0)
|
||||
family_cell.save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>Hello anonymous user</p>', order=0,
|
||||
restricted_to_unlogged=True, public=True).save()
|
||||
restricted_to_unlogged=True, public=True).save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>You are not linked</p>',
|
||||
order=1, public=False, restricted_to_unlogged=False).save()
|
||||
order=1, public=False, restricted_to_unlogged=False).save()
|
||||
|
||||
resp = app.get(reverse('combo-public-ajax-page-cell',
|
||||
kwargs={'page_pk': page.pk, 'cell_reference': family_cell.get_reference()}))
|
||||
assert "<p>Hello anonymous user</p>" in resp.content
|
||||
assert "<p>You are not linked</p>" not in resp.content
|
||||
|
||||
app = login(app)
|
||||
with override_settings(FAMILY_SERVICE={'root': '/family/'}):
|
||||
with mock.patch('combo.utils.requests.get') as requests_get:
|
||||
mocked_response = mock.Mock()
|
||||
mocked_response.status_code = 200
|
||||
mocked_response.json.return_value = {'err': 0, 'data': None}
|
||||
requests_get.return_value = mocked_response
|
||||
with mock.patch('combo.utils.requests.send') as requests_send:
|
||||
resp = app.get('/')
|
||||
assert "<p>Hello anonymous user</p>" in resp.content
|
||||
assert "<p>You are not linked</p>" not in resp.content
|
||||
|
||||
app = login(app)
|
||||
data = {'err': 0, 'data': None}
|
||||
requests_send.return_value = mock.Mock(json=lambda: data, content=json.dumps(data), status_code=200)
|
||||
# make sure no data are loaded
|
||||
resp = app.get('/')
|
||||
resp.html.body.find('div', {'class': 'familyinfoscell'}).text.strip() == 'Loading...'
|
||||
# put data in cache
|
||||
resp = app.get(reverse('combo-public-ajax-page-cell',
|
||||
kwargs={'page_pk': page.pk, 'cell_reference': family_cell.get_reference()}))
|
||||
resp = app.get('/')
|
||||
assert "<p>Hello anonymous user</p>" not in resp.content
|
||||
assert "<p>You are not linked</p>" in resp.content
|
||||
|
||||
|
||||
@mock.patch('combo.utils.requests.get')
|
||||
def test_familycell_link(requests_get, app, settings, admin_user):
|
||||
settings.FAMILY_SERVICE = {'root': '/family/'}
|
||||
Page.objects.all().delete()
|
||||
page = Page(title='Family', slug='index', template_name='standard')
|
||||
page.save()
|
||||
family_cell = FamilyInfosCell(page=page, placeholder='content', order=0)
|
||||
family_cell.save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>Hello anonymous user</p>', order=0,
|
||||
restricted_to_unlogged=True, public=True).save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>You are not linked</p>',
|
||||
order=1, public=False, restricted_to_unlogged=False).save()
|
||||
app = login(app)
|
||||
# when linking fails
|
||||
requests_get.return_value = mock.Mock(ok=False, json=lambda: {'err': 102})
|
||||
resp = app.get('/family/link/')
|
||||
resp.form['family_id'] = '123'
|
||||
resp.form['family_code'] = 'l33t'
|
||||
resp = resp.form.submit().follow()
|
||||
messages = resp.context['messages']
|
||||
assert len(messages._loaded_messages) == 1
|
||||
assert messages._loaded_messages[0].message == 'Failed to link to family. Please check your credentials and retry later.'
|
||||
requests_get.return_value = mock.Mock(ok=True, json=lambda: {})
|
||||
resp.form['family_id'] = '123'
|
||||
resp.form['family_code'] = 'l33t'
|
||||
resp = resp.form.submit().follow()
|
||||
messages = resp.context['messages']
|
||||
assert len(messages._loaded_messages) == 2
|
||||
assert messages._loaded_messages[1].message == 'Your account was successfully linked.'
|
||||
|
||||
|
||||
@mock.patch('combo.utils.requests.get')
|
||||
def test_familycell_unlink(requests_get, app, settings, admin_user):
|
||||
settings.FAMILY_SERVICE = {'root': '/family/'}
|
||||
Page.objects.all().delete()
|
||||
page = Page(title='Family', slug='index', template_name='standard')
|
||||
page.save()
|
||||
family_cell = FamilyInfosCell(page=page, placeholder='content', order=0)
|
||||
family_cell.save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>Hello anonymous user</p>', order=0,
|
||||
restricted_to_unlogged=True, public=True).save()
|
||||
TextCell(page=page, placeholder='family_unlinked_user', text='<p>You are not linked</p>',
|
||||
order=1, public=False, restricted_to_unlogged=False).save()
|
||||
app = login(app)
|
||||
requests_get.return_value = mock.Mock(ok=False, json=lambda: {'err': 102})
|
||||
resp = app.get('/family/unlink/')
|
||||
resp = resp.form.submit().follow()
|
||||
messages = resp.context['messages']
|
||||
assert len(messages._loaded_messages) == 1
|
||||
assert messages._loaded_messages[0].message == 'An error occured when unlinking.'
|
||||
requests_get.return_value = mock.Mock(ok=True, json=lambda: {})
|
||||
resp = app.get('/family/unlink/')
|
||||
resp = resp.form.submit().follow()
|
||||
messages = resp.context['messages']
|
||||
assert len(messages._loaded_messages) == 2
|
||||
assert messages._loaded_messages[1].message == 'Your account was successfully unlinked.'
|
||||
|
||||
|
||||
def test_synchronous_placeholder(app):
|
||||
page = Page(title=u'foo', slug='foo', template_name='standard', order=0)
|
||||
page.save()
|
||||
|
|
Loading…
Reference in New Issue