applications: add links to element's definition (#70442)
gitea-wip/hobo/pipeline/head There was a failure building this commit Details
gitea/hobo/pipeline/head Something is wrong with the build of this commit Details

This commit is contained in:
Lauréline Guérin 2022-10-24 16:56:58 +02:00
parent 55988f5867
commit 7215ca84bd
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
3 changed files with 89 additions and 4 deletions

View File

@ -77,6 +77,16 @@ class Element(models.Model):
def __repr__(self):
return '<Element %s/%s>' % (self.type, self.slug)
def get_redirect_url(self):
if self.type == 'roles':
return
if not self.cache.get('urls'):
return
if self.cache['urls'].get('redirect'):
return self.cache['urls']['redirect']
if self.cache['urls'].get('export'):
return '%sredirect/' % self.cache['urls']['export']
class Relation(models.Model):
application = models.ForeignKey(Application, on_delete=models.CASCADE)

View File

@ -25,13 +25,17 @@
<ul class="objects-list single-links">
{% for relation in relations %}
{% if not relation.auto_dependency %}
<li><a>{{ relation.element.name }} <span class="extra-info">- {{ relation.element.type_label }}</span></a>
<a rel="popup" class="delete" href="{% url 'application-delete-element' app_slug=app.slug pk=relation.id %}">{% trans "remove" %}</a></li>
<li>
<a {% if relation.element.get_redirect_url %}href="{{ relation.element.get_redirect_url }}"{% endif %}>{{ relation.element.name }} <span class="extra-info">- {{ relation.element.type_label }}</span></a>
<a rel="popup" class="delete" href="{% url 'application-delete-element' app_slug=app.slug pk=relation.id %}">{% trans "remove" %}</a>
</li>
{% endif %}
{% endfor %}
{% for relation in relations %}
{% if relation.auto_dependency %}
<li class="auto-dependency"><a>{{ relation.element.name }} <span class="extra-info">- {{ relation.element.type_label }}</span></a></li>
<li class="auto-dependency">
<a {% if relation.element.get_redirect_url %}href="{{ relation.element.get_redirect_url }}"{% endif %}>{{ relation.element.name }} <span class="extra-info">- {{ relation.element.type_label }}</span></a>
</li>
{% endif %}
{% endfor %}
</ul>

View File

@ -10,7 +10,7 @@ from httmock import HTTMock
from test_manager import login
from webtest import Upload
from hobo.applications.models import Application
from hobo.applications.models import Application, Element, Relation
from hobo.environment.models import Authentic, Wcs
pytestmark = pytest.mark.django_db
@ -63,6 +63,13 @@ WCS_AVAILABLE_OBJECTS = {
"minor": True,
"urls": {"list": "https://wcs.example.invalid/api/export-import/wscalls/"},
},
{
"id": "roles",
"text": "Roles",
"singular": "Role",
"minor": True,
"urls": {"list": "https://wcs.example.invalid/api/export-import/roles/"},
},
]
}
@ -75,6 +82,7 @@ WCS_AVAILABLE_FORMS = {
"urls": {
"export": "https://wcs.example.invalid/api/export-import/forms/test-form/",
"dependencies": "https://wcs.example.invalid/api/export-import/forms/test-form/dependencies/",
"redirect": "https://wcs.example.invalid/api/export-import/forms/test-form/redirect/",
},
},
{
@ -232,6 +240,69 @@ def test_create_application(app, admin_user, settings):
assert b'"icon": "foo' in resp.content
def test_redirect_application_element(app, admin_user, settings):
Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')
settings.KNOWN_SERVICES = {
'wcs': {
'foobar': {
'title': 'Foobar',
'url': 'https://wcs.example.invalid/',
'orig': 'example.org',
'secret': 'xxx',
}
}
}
application = Application.objects.create(name='Test', slug='test')
element = Element.objects.create(
type='forms',
slug='test-form',
name='Test 1 form',
cache={
'urls': {'redirect': 'https://wcs.example.invalid/api/export-import/forms/test-form/redirect/'}
},
)
Relation.objects.create(application=application, element=element)
element = Element.objects.create(
type='forms',
slug='test2-form',
name='Test 2 form',
cache={'urls': {'export': 'https://wcs.example.invalid/api/export-import/forms/test2-form/'}},
)
Relation.objects.create(application=application, element=element)
element = Element.objects.create(
type='forms',
slug='test3-form',
name='Test 3 form',
cache={},
)
Relation.objects.create(application=application, element=element)
element = Element.objects.create(
type='roles',
slug='test',
name='Test',
cache={'urls': {'redirect': 'https://wcs.example.invalid/api/export-import/roles/test/redirect/'}},
)
Relation.objects.create(application=application, element=element)
login(app)
with HTTMock(mocked_http):
resp = app.get('/applications/manifest/test/')
print(resp)
assert 'https://wcs.example.invalid/api/export-import/forms/test-form/redirect/' in resp
assert (
'https://wcs.example.invalid/api/export-import/forms/test2-form/redirect/' in resp
) # no redirect url, but it's ok from export url
assert (
'https://wcs.example.invalid/api/export-import/forms/test3-form/redirect/' not in resp
) # no urls
assert (
'https://wcs.example.invalid/api/export-import/roles/test/redirect/' not in resp
) # not for roles
def test_delete_application(app, admin_user, settings):
Wcs.objects.create(base_url='https://wcs.example.invalid', slug='foobar', title='Foobar')