From 6a69590a828b2b65206a8677745f273ad4100a47 Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Wed, 21 Sep 2022 11:53:31 +0200 Subject: [PATCH] Added a 'trust back' button, front can be improved --- apps/member/static/member/js/trust.js | 41 +++++++++++++++++---------- apps/note/tables.py | 25 ++++++++++++++++ 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/apps/member/static/member/js/trust.js b/apps/member/static/member/js/trust.js index a16bed08..15b5359f 100644 --- a/apps/member/static/member/js/trust.js +++ b/apps/member/static/member/js/trust.js @@ -1,7 +1,7 @@ /** * On form submit, create a new friendship */ -function create_trust (e) { +function form_create_trust (e) { // Do not submit HTML form e.preventDefault() @@ -14,25 +14,35 @@ function create_trust (e) { addMsg(gettext("You can't add yourself as a friend"), "danger") return } - $.post('/api/note/trust/', { - csrfmiddlewaretoken: formData.get('csrfmiddlewaretoken'), - trusting: formData.get('trusting'), - trusted: trusted_alias.note - }).done(function () { - // Reload table - $('#trust_table').load(location.pathname + ' #trust_table') - addMsg(gettext('Friendship successfully added'), 'success') - }).fail(function (xhr, _textStatus, _error) { - errMsg(xhr.responseJSON) - }) + create_trust(formData.get('trusting'), trusted_alias.note) }).fail(function (xhr, _textStatus, _error) { errMsg(xhr.responseJSON) }) } /** - * On click of "delete", delete the alias - * @param button_id:Integer Alias id to remove + * Create a trust between users + * @param trusting:Integer trusting note id + * @param trusted:Integer trusted note id + */ +function create_trust(trusting, trusted) { + $.post('/api/note/trust/', { + trusting: trusting, + trusted: trusted, + csrfmiddlewaretoken: CSRF_TOKEN + }).done(function () { + // Reload tables + $('#trust_table').load(location.pathname + ' #trust_table') + $('#trusted_table').load(location.pathname + ' #trusted_table') + addMsg(gettext('Friendship successfully added'), 'success') + }).fail(function (xhr, _textStatus, _error) { + errMsg(xhr.responseJSON) + }) +} + +/** + * On click of "delete", delete the trust + * @param button_id:Integer Trust id to remove */ function delete_button (button_id) { $.ajax({ @@ -42,6 +52,7 @@ function delete_button (button_id) { }).done(function () { addMsg(gettext('Friendship successfully deleted'), 'success') $('#trust_table').load(location.pathname + ' #trust_table') + $('#trusted_table').load(location.pathname + ' #trusted_table') }).fail(function (xhr, _textStatus, _error) { errMsg(xhr.responseJSON) }) @@ -49,5 +60,5 @@ function delete_button (button_id) { $(document).ready(function () { // Attach event - document.getElementById('form_trust').addEventListener('submit', create_trust) + document.getElementById('form_trust').addEventListener('submit', form_create_trust) }) diff --git a/apps/note/tables.py b/apps/note/tables.py index 6c684d69..3e6e4f0a 100644 --- a/apps/note/tables.py +++ b/apps/note/tables.py @@ -185,6 +185,31 @@ class TrustedTable(tables.Table): show_header = False trusting = tables.Column(attrs={'td': {'class': 'text-center'}}) + trust_back = tables.Column( + verbose_name=_("Trust back"), + accessor="pk", + attrs={ + 'td': { + 'class': 'col-sm-1', + 'id': lambda record: "trust_back_" + str(record.pk), + } + }, + ) + + def render_trust_back(self, record): + user_note = record.trusted + trusting_note = record.trusting + if Trust.objects.filter(trusted=trusting_note, trusting=user_note) : + return "" + val = '' + return mark_safe(val) + class AliasTable(tables.Table): class Meta: