diff --git a/bestnotes/templates/add.html b/bestnotes/templates/add.html index f2509b1..825e76a 100644 --- a/bestnotes/templates/add.html +++ b/bestnotes/templates/add.html @@ -6,7 +6,7 @@ {% block content %} {% include 'navbar.html' %} -
{% csrf_token %} +{% csrf_token %}
{% render_field form.name class="form-control mb-2 mt-4 bg-dark text-light" id="name" placeholder="Wpisz nazwe notatki" %} {% render_field form.subject class="form-control mb-2 bg-dark text-light" name="subject" id="subject" placeholder="Wpisz przedmiot" %} diff --git a/bestnotes/templates/note.html b/bestnotes/templates/note.html index 38b6551..c719285 100644 --- a/bestnotes/templates/note.html +++ b/bestnotes/templates/note.html @@ -11,10 +11,14 @@
{% if note.user.user.id == user.id %}
-
- -
- +
+
+ +
+
+ +
+
{% endif %} diff --git a/bestnotes/templates/update.html b/bestnotes/templates/update.html new file mode 100644 index 0000000..eba55bb --- /dev/null +++ b/bestnotes/templates/update.html @@ -0,0 +1,24 @@ +{% extends 'base.html' %} +{% load widget_tweaks %} +{% load static %} +{% block css %} +{% endblock %} +{% block content %} +{% include 'navbar.html' %} + +
{% csrf_token %} +
+ {% render_field form.name class="form-control mb-2 mt-4 bg-dark text-light" id="name" placeholder="Wpisz nazwe notatki" %} + {% render_field form.subject class="form-control mb-2 bg-dark text-light" name="subject" id="subject" placeholder="Wpisz przedmiot" %} + {% render_field form.topic class="form-control mb-2 bg-dark text-light" id="topic" placeholder="Wpisz temat" %} +
+ {% render_field form.content id="content" %} +
+ +
+ + + + + +{% endblock %} \ No newline at end of file diff --git a/bestnotes/urls.py b/bestnotes/urls.py index f63da4c..3d73c72 100644 --- a/bestnotes/urls.py +++ b/bestnotes/urls.py @@ -15,5 +15,7 @@ urlpatterns = [ path('notes/', views.notes_all, name="notes"), path('notes/', views.notes_name, name="notes_id"), path('create/', views.create_note, name="create_note"), - path('delete_note/', views.delete_note, name="delete_note") + path('delete_note/', views.delete_note, name="delete_note"), + path('update_note/', views.update_note, name="update_note"), + path('update_note_id/', views.update_note_id, name="update_note_id") ] diff --git a/bestnotes/views.py b/bestnotes/views.py index 436c439..eb37aa4 100644 --- a/bestnotes/views.py +++ b/bestnotes/views.py @@ -97,7 +97,6 @@ def create_note(request): #Check if subject exist, if not create it subject = Subject() - print(form.data['subject']) if Subject.objects.filter(name=form.data['subject']): subject = Subject.objects.filter(name=form.data['subject'])[0] else: @@ -113,7 +112,6 @@ def create_note(request): add_date=date.today()) topic.save() - #Create note note = Note(name=form.data['name'], content=form.data['content'], @@ -137,6 +135,55 @@ def delete_note(request, note_id): url = reverse('subject') return HttpResponseRedirect(url) + + +#Update note view html +def update_note(request, note_id): + + note = Note.objects.get(pk=note_id) + form = EditorForm(initial={'content': note.content, + 'subject':note.topic.subject.name, + 'topic':note.topic.name, + 'name':note.name}) + + context = { + 'note':note, + 'form':form + } + return render(request, "update.html", context) + +def update_note_id(request, note_id): + note = Note.objects.get(pk=note_id) + form = EditorForm(request.POST) + student = StudentProfile.objects.get(user=request.user.id) + + if request.method == 'POST': + subject = Subject() + if Subject.objects.filter(name=form.data['subject']): + subject = Subject.objects.filter(name=form.data['subject'])[0] + else: + subject = Subject(name=form.data['subject'], student=student) + subject.save() + #Now topic, in addition check if exist in subject + topic = Topic() + if Topic.objects.filter(name=form.data['topic'], subject=subject): + topic = Topic.objects.filter(name=form.data['topic'], subject=subject)[0] + else: + topic = Topic(name=form.data['topic'], + subject=subject, + add_date=date.today()) + topic.save() + + #Update note + note.content = form.data['content'] + note.name = form.data['name'] + note.topic = topic + note.save(force_update=True) + + delete_empty_categories() + url = reverse('note', args=[note_id]) + return HttpResponseRedirect(url) + def delete_empty_categories(): #Get all topics topics = Topic.objects.all() @@ -147,4 +194,4 @@ def delete_empty_categories(): subjects = Subject.objects.all() for subject in subjects: if not subject.topic_set.all().exists(): - subject.delete() + subject.delete() \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 05e3bab..f11f6bb 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ