diff --git a/BestNotesProject/settings.py b/BestNotesProject/settings.py index c51932f..11ee46f 100644 --- a/BestNotesProject/settings.py +++ b/BestNotesProject/settings.py @@ -128,18 +128,17 @@ USE_TZ = True STATIC_URL = '/static/' -TINYMCE_JS_URL = "https://cdn.tiny.cloud/1/no-api-key/tinymce/5.1.4-66/tinymce.min.js" +TINYMCE_JS_URL = "https://cdn.tiny.cloud/1/r9o35nobg5gdj46hhxw3ohwbjh4yct23kx405ozckt8fj2k2/tinymce/5.1.4-66/tinymce.min.js" TINYMCE_DEFAULT_CONFIG = { 'height' : 500, 'skin': 'oxide-dark', + 'branding': False, 'content_css': 'dark', - 'plugins': "image,imagetools,media,codesample,link,code", - 'cleanup_on_startup': True, + 'plugins': "image,imagetools,media,codesample,link,code,wordcount", 'menubar': False, - 'toolbar': "styleselect |undo redo | bold italic | alignleft aligncenter alignright | link image media codesample code", + 'contextmenu': "cut, copy, link, media ", + 'toolbar': "styleselect | undo redo | bold italic underline superscript subscript| fontselect fontsizeselect forecolor backcolor | alignleft aligncenter alignright | link image media codesample code", 'image_caption': True, - 'image_advtab': True, - 'custom_undo_redo_levels': 10, - 'file_browser_callback' : "myFileBrowser" + 'image_advtab': True } \ No newline at end of file diff --git a/bestnotes/models.py b/bestnotes/models.py index 880ef0e..8c5bba7 100644 --- a/bestnotes/models.py +++ b/bestnotes/models.py @@ -39,17 +39,17 @@ def save_studentprofile(sender, instance, **kwargs): class Subject(models.Model): - name = models.CharField(max_length=30) + name = models.CharField(max_length=40) student = models.ForeignKey(StudentProfile, on_delete=models.CASCADE) class Topic(models.Model): - name = models.CharField(max_length=30) + name = models.CharField(max_length=40) subject = models.ForeignKey(Subject, on_delete=models.CASCADE) add_date = models.DateField() class Note(models.Model): - name = models.CharField(max_length=30) + name = models.CharField(max_length=60) content = tinymce_models.HTMLField("Content") text = models.TextField(blank=False) topic = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True) diff --git a/bestnotes/static/shownote.js b/bestnotes/static/shownote.js index bc793fe..a3476b2 100644 --- a/bestnotes/static/shownote.js +++ b/bestnotes/static/shownote.js @@ -3,11 +3,12 @@ tinymce.init({ selector: '.tmpTextArea', menubar: false, skin: 'oxide-dark', + branding: false, content_css: 'dark', plugins: [ 'autoresize advlist autolink lists link image charmap print preview anchor', - 'searchreplace visualblocks code fullscreen', - 'insertdatetime media table contextmenu paste code' + 'searchreplace visualblocks codesample fullscreen', + 'insertdatetime media table paste code' ], readonly: 1, toolbar: false, diff --git a/bestnotes/templates/add.html b/bestnotes/templates/add.html index f2509b1..08bc796 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/base.html b/bestnotes/templates/base.html index 2c5c69d..5c1b8de 100644 --- a/bestnotes/templates/base.html +++ b/bestnotes/templates/base.html @@ -2,15 +2,13 @@ {% load static %} - {{ form.media }} - - + diff --git a/bestnotes/templates/note.html b/bestnotes/templates/note.html index 3eb80f0..c719285 100644 --- a/bestnotes/templates/note.html +++ b/bestnotes/templates/note.html @@ -11,7 +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 7718acf..b7a18ac 100644 --- a/bestnotes/urls.py +++ b/bestnotes/urls.py @@ -14,5 +14,9 @@ urlpatterns = [ path('addnote/', views.add, name="add"), path('notes/', views.notes_all, name="notes"), path('notes/', views.notes_name, name="notes_id"), - path('create/', views.create_note, name="create_note") + path('create/', views.create_note, name="create_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 40af1c9..2eb184e 100644 --- a/bestnotes/views.py +++ b/bestnotes/views.py @@ -59,7 +59,7 @@ def topics_by_subject_id(request,subject_id): topics_under_subject = all_topics.filter(subject__id=subject_id) #get topics under given subject if len(topics_under_subject) > 0: - subject_name = all_topics[0].subject.name # Get note with given id + subject_name = topics_under_subject[0].subject.name # Get note with given id context = { 'subject_name': subject_name, @@ -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'], @@ -123,4 +121,77 @@ def create_note(request): add_date=date.today()) note.save() url = reverse('subject') - return HttpResponseRedirect(url) \ No newline at end of file + return HttpResponseRedirect(url) + +def delete_note(request, note_id): + #Get values + note = Note.objects.get(pk=note_id) + student = StudentProfile.objects.get(user=request.user.id) + #Check if student has possibility do delete this note + if note.user.id == student.id: + note.delete() + delete_empty_categories() + #Redirect + 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() + for topic in topics: + if not topic.note_set.all().exists(): + topic.delete() + #Same for subjects + subjects = Subject.objects.all() + for subject in subjects: + if not subject.topic_set.all().exists(): + subject.delete() \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 6ff44df..b30d9fa 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ