Skip to content
Snippets Groups Projects
Commit f898e20b authored by dcz2's avatar dcz2
Browse files

Notes title and deleting

parent e6c9599c
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,6 @@ export default {
refresh () {
this.key = null;
setTimeout(() => { this.key = this.lexicalUnitId; }, 0);
update_entries(); // TODO
},
swapFrames (previewedUnifiedFrameId) {
this.previewedUnifiedFrameId = this.unifiedFrameId;
......
......@@ -88,7 +88,10 @@ function setup_notes($container, $template, pk, model, refreshFunction) {
type : 'post',
url : $('#note-form-template').data('url').replace('MODEL', model).replace('PK', pk),
dataType : 'json',
data : { note: $('.note-form textarea[name=note]').val() },
data : {
title: $('.note-form input[name=title]').val(),
note: $('.note-form textarea[name=note]').val()
},
timeout : 5000,
success : function (response) {
refreshFunction();
......@@ -105,7 +108,39 @@ function setup_notes($container, $template, pk, model, refreshFunction) {
url: $('.notes-table').data('url').replace('MODEL', model).replace('PK', pk),
success: function (data) {
data.notes.map(function (note) {
$('.notes-table tbody', $container).append(`<tr><td>${note.note}</td><td>${note.owner_label}</td></tr>`);
$('.notes-table tbody', $container).append(
`
<tr role="button" data-toggle="collapse" data-target="#note-${note.pk}" class="cursor-pointer">
<td>${note.title}</td>
<td>${note.owner_label}</td>
<td>
<a
href="#"
class="btn btn-xs btn-outline-dark float-right delete-note"
data-pk="${note.pk}"
>&times;</a>
</td>
</tr>
<tr id="note-${note.pk}" class="collapse v-hidden"><td colspan="3">${note.note}</td></tr>
`
);
$('.delete-note', $container).click(function (e) {
e.preventDefault();
const pk = $(this).data("pk");
if (!confirm(gettext("Czy na pewno chcesz usunąć notatkę?"))) return false;
$.ajax({
type : 'delete',
url : `/${lang}/users/notes/${pk}/delete/`,
timeout : 5000,
success : function () {
refreshFunction();
},
error : function () {
alert(gettext('Nie udało się usunąć notatki.'));
}
});
return false;
});
});
}
})
......
......@@ -109,4 +109,4 @@ password_reset_set_password_form_helper.layout = Layout(
class NoteForm(forms.ModelForm):
class Meta:
model = Note
fields = ["note"]
fields = ["title", "note"]
......@@ -47,6 +47,7 @@ class Note(models.Model):
subject_ct = models.ForeignKey(ContentType, on_delete=models.PROTECT)
subject_id = models.PositiveIntegerField()
subject = GenericForeignKey('subject_ct', 'subject_id')
title = models.CharField(max_length=100)
note = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
......
......@@ -8,7 +8,10 @@
<a href="#" class="btn btn-xs btn-outline-dark float-right hide-note-form">&times;</a>
</div>
<div class="d-flex">
<textarea name="note" class="form-control mr-3"></textarea>
<div class="flex-grow-1 mr-3">
<input name="title" class="form-control mb-2" placeholder="{% trans 'Tytuł notatki' %}" />
<textarea name="note" class="form-control" placeholder="{% trans 'Treść notatki' %}"></textarea>
</div>
<a href="#" class="btn btn-sm btn-outline-dark ml-auto align-self-end add-note">{% trans 'Dodaj' %}</a>
</div>
</div>
......
......@@ -56,4 +56,5 @@ urlpatterns = [
),
re_path(r'^notes/(?P<model>\w+.\w+)/(?P<pk>\w+)/$', views.get_notes, name='get_notes'),
re_path(r'^notes/(?P<model>\w+.\w+)/(?P<pk>\w+)/add/$', views.add_note, name='add_note'),
path('notes/<int:pk>/delete/', views.delete_note, name='delete_note'),
]
......@@ -68,6 +68,7 @@ def get_notes(request, model, pk):
"pk": note.pk,
"owner_label": note.owner_label,
"created_at": note.created_at,
"title": note.title,
"note": note.note,
} for note in notes],
})
......@@ -84,3 +85,11 @@ def add_note(request, model, pk):
form.save()
return JsonResponse({})
return JsonResponse(form.errors.get_json_data(), status=400)
@require_http_methods(["DELETE"])
@login_required
def delete_note(request, pk):
note = get_object_or_404(Note.objects.for_user(request.user), pk=pk)
note.delete()
return JsonResponse({})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment