Added a 'trust back' button, front can be improved

This commit is contained in:
Nicolas Margulies 2022-09-21 11:53:31 +02:00
parent 7afc583282
commit 6a69590a82
2 changed files with 51 additions and 15 deletions

View File

@ -1,7 +1,7 @@
/** /**
* On form submit, create a new friendship * On form submit, create a new friendship
*/ */
function create_trust (e) { function form_create_trust (e) {
// Do not submit HTML form // Do not submit HTML form
e.preventDefault() e.preventDefault()
@ -14,25 +14,35 @@ function create_trust (e) {
addMsg(gettext("You can't add yourself as a friend"), "danger") addMsg(gettext("You can't add yourself as a friend"), "danger")
return return
} }
$.post('/api/note/trust/', { create_trust(formData.get('trusting'), trusted_alias.note)
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)
})
}).fail(function (xhr, _textStatus, _error) { }).fail(function (xhr, _textStatus, _error) {
errMsg(xhr.responseJSON) errMsg(xhr.responseJSON)
}) })
} }
/** /**
* On click of "delete", delete the alias * Create a trust between users
* @param button_id:Integer Alias id to remove * @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) { function delete_button (button_id) {
$.ajax({ $.ajax({
@ -42,6 +52,7 @@ function delete_button (button_id) {
}).done(function () { }).done(function () {
addMsg(gettext('Friendship successfully deleted'), 'success') addMsg(gettext('Friendship successfully deleted'), 'success')
$('#trust_table').load(location.pathname + ' #trust_table') $('#trust_table').load(location.pathname + ' #trust_table')
$('#trusted_table').load(location.pathname + ' #trusted_table')
}).fail(function (xhr, _textStatus, _error) { }).fail(function (xhr, _textStatus, _error) {
errMsg(xhr.responseJSON) errMsg(xhr.responseJSON)
}) })
@ -49,5 +60,5 @@ function delete_button (button_id) {
$(document).ready(function () { $(document).ready(function () {
// Attach event // Attach event
document.getElementById('form_trust').addEventListener('submit', create_trust) document.getElementById('form_trust').addEventListener('submit', form_create_trust)
}) })

View File

@ -185,6 +185,31 @@ class TrustedTable(tables.Table):
show_header = False show_header = False
trusting = tables.Column(attrs={'td': {'class': 'text-center'}}) 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 = '<button id="'
val += str(record.pk)
val += '" class="btn btn-success btn-sm" \
onclick="create_trust(' + str(record.trusted.pk) + ',' + \
str(record.trusting.pk) + ')">'
val += str(_("Trust back"))
val += '</button>'
return mark_safe(val)
class AliasTable(tables.Table): class AliasTable(tables.Table):
class Meta: class Meta: