mirror of
				https://gitlab.crans.org/bde/nk20
				synced 2025-11-04 01:12:08 +01:00 
			
		
		
		
	Added a 'trust back' button, front can be improved
This commit is contained in:
		@@ -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)
 | 
			
		||||
})
 | 
			
		||||
 
 | 
			
		||||
@@ -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 = '<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 Meta:
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user