More optimisation

This commit is contained in:
Yohann D'ANELLO 2020-03-20 02:05:41 +01:00
parent 6fc43e651e
commit 0ce6cd88b3
2 changed files with 22 additions and 17 deletions

View File

@ -84,10 +84,7 @@ class AliasSerializer(serializers.ModelSerializer):
read_only_fields = ('note', ) read_only_fields = ('note', )
def get_note(self, alias): def get_note(self, alias):
if PermissionBackend().has_perm(get_current_authenticated_user(), "note.view_note", alias.note): return alias.note.id
return NotePolymorphicSerializer().to_representation(alias.note)
else:
return alias.note.id
class NotePolymorphicSerializer(PolymorphicSerializer): class NotePolymorphicSerializer(PolymorphicSerializer):

View File

@ -61,12 +61,22 @@ function li(id, text) {
* @param profile_pic_field * @param profile_pic_field
*/ */
function displayNote(note, alias, user_note_field=null, profile_pic_field=null) { function displayNote(note, alias, user_note_field=null, profile_pic_field=null) {
let img = note == null ? null : note.display_image; if (!note.display_image) {
if (img == null) note.display_image = 'https://nk20.ynerant.fr/media/pic/default.png';
img = '/media/pic/default.png'; $.getJSON("/api/note/note/" + note.id + "/?format=json", function(new_note) {
if (note !== null && alias !== note.name) note.display_image = new_note.display_image.replace("http:", "https:");
note.name = new_note.name;
note.balance = new_note.balance;
displayNote(note, alias, user_note_field, profile_pic_field);
});
return;
}
let img = note.display_image;
if (alias !== note.name)
alias += " (aka. " + note.name + ")"; alias += " (aka. " + note.name + ")";
if (note !== null && user_note_field !== null) if (user_note_field !== null)
$("#" + user_note_field).text(alias + (note.balance == null ? "" : (" : " + pretty_money(note.balance)))); $("#" + user_note_field).text(alias + (note.balance == null ? "" : (" : " + pretty_money(note.balance))));
if (profile_pic_field != null) if (profile_pic_field != null)
$("#" + profile_pic_field).attr('src', img); $("#" + profile_pic_field).attr('src', img);
@ -173,15 +183,13 @@ function autoCompleteNote(field_id, alias_matched_id, note_list_id, notes, notes
aliases.results.forEach(function (alias) { aliases.results.forEach(function (alias) {
let note = alias.note; let note = alias.note;
if (typeof note === "number") { note = {
note = { id: note,
id: note, name: alias.name,
name: alias.name, alias: alias,
balance: null balance: null
}; };
}
aliases_matched_html += li(alias_prefix + "_" + alias.id, alias.name); aliases_matched_html += li(alias_prefix + "_" + alias.id, alias.name);
note.alias = alias;
notes.push(note); notes.push(note);
}); });