Merge branch 'BES-6'

This commit is contained in:
Th3NiKo 2020-01-19 19:01:20 +01:00
commit fd3812b24b
10 changed files with 126 additions and 22 deletions

View File

@ -128,18 +128,17 @@ USE_TZ = True
STATIC_URL = '/static/' 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 = { TINYMCE_DEFAULT_CONFIG = {
'height' : 500, 'height' : 500,
'skin': 'oxide-dark', 'skin': 'oxide-dark',
'branding': False,
'content_css': 'dark', 'content_css': 'dark',
'plugins': "image,imagetools,media,codesample,link,code", 'plugins': "image,imagetools,media,codesample,link,code,wordcount",
'cleanup_on_startup': True,
'menubar': False, '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_caption': True,
'image_advtab': True, 'image_advtab': True
'custom_undo_redo_levels': 10,
'file_browser_callback' : "myFileBrowser"
} }

View File

@ -39,17 +39,17 @@ def save_studentprofile(sender, instance, **kwargs):
class Subject(models.Model): class Subject(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=40)
student = models.ForeignKey(StudentProfile, on_delete=models.CASCADE) student = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)
class Topic(models.Model): class Topic(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=40)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE) subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
add_date = models.DateField() add_date = models.DateField()
class Note(models.Model): class Note(models.Model):
name = models.CharField(max_length=30) name = models.CharField(max_length=60)
content = tinymce_models.HTMLField("Content") content = tinymce_models.HTMLField("Content")
text = models.TextField(blank=False) text = models.TextField(blank=False)
topic = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True) topic = models.ForeignKey(Topic, on_delete=models.SET_NULL, null=True)

View File

@ -3,11 +3,12 @@ tinymce.init({
selector: '.tmpTextArea', selector: '.tmpTextArea',
menubar: false, menubar: false,
skin: 'oxide-dark', skin: 'oxide-dark',
branding: false,
content_css: 'dark', content_css: 'dark',
plugins: [ plugins: [
'autoresize advlist autolink lists link image charmap print preview anchor', 'autoresize advlist autolink lists link image charmap print preview anchor',
'searchreplace visualblocks code fullscreen', 'searchreplace visualblocks codesample fullscreen',
'insertdatetime media table contextmenu paste code' 'insertdatetime media table paste code'
], ],
readonly: 1, readonly: 1,
toolbar: false, toolbar: false,

View File

@ -6,7 +6,7 @@
{% block content %} {% block content %}
{% include 'navbar.html' %} {% include 'navbar.html' %}
<form action="/bestnotes/create/" method="post">{% csrf_token %} <form action={% url 'create_note' %} method="post">{% csrf_token %}
<div class="form-group"> <div class="form-group">
{% render_field form.name class="form-control mb-2 mt-4 bg-dark text-light" id="name" placeholder="Wpisz nazwe notatki" %} {% 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.subject class="form-control mb-2 bg-dark text-light" name="subject" id="subject" placeholder="Wpisz przedmiot" %}

View File

@ -2,15 +2,13 @@
<html lang="pl"> <html lang="pl">
{% load static %} {% load static %}
<head> <head>
<!-- Required meta tags -->
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{{ form.media }} {{ form.media }}
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="{% static 'navbar.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'navbar.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'all.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'all.css' %}">
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script> <script src="https://cdn.tiny.cloud/1/r9o35nobg5gdj46hhxw3ohwbjh4yct23kx405ozckt8fj2k2/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

View File

@ -11,7 +11,14 @@
</div> </div>
{% if note.user.user.id == user.id %} {% if note.user.user.id == user.id %}
<div class="container"> <div class="container">
<div class="row d-flex justify-content-center"><button id="edit_note" class="btn-success mr-1">Edytuj</button> <button id="delete_note" class="btn-danger">Usuń</button></div> <div class="row d-flex justify-content-center">
<form action="{% url 'update_note' note_id=note.id %}">
<button type="submit" id="edit_note" class="btn-success mr-1">Edytuj</button>
</form>
<form action="{% url 'delete_note' note_id=note.id %}">
<button type="submit" id="delete_note" class="btn-danger">Usuń</button>
</form>
</div>
</div> </div>
{% endif %} {% endif %}

View File

@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% load widget_tweaks %}
{% load static %}
{% block css %}
{% endblock %}
{% block content %}
{% include 'navbar.html' %}
<form action="{% url 'update_note_id' note_id=note.id %}" method="post">{% csrf_token %}
<div class="form-group">
{% 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" %}
<div>
{% render_field form.content id="content" %}
<div>
<input type="submit" class="btn-primary mt-1" value="Aktualizuj notatke">
</div>
</form>
{% endblock %}

View File

@ -14,5 +14,9 @@ urlpatterns = [
path('addnote/', views.add, name="add"), path('addnote/', views.add, name="add"),
path('notes/', views.notes_all, name="notes"), path('notes/', views.notes_all, name="notes"),
path('notes/<subject_id>', views.notes_name, name="notes_id"), path('notes/<subject_id>', 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/<note_id>', views.delete_note, name="delete_note"),
path('update_note/<note_id>', views.update_note, name="update_note"),
path('update_note_id/<note_id>', views.update_note_id, name="update_note_id")
] ]

View File

@ -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 topics_under_subject = all_topics.filter(subject__id=subject_id) #get topics under given subject
if len(topics_under_subject) > 0: 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 = { context = {
'subject_name': subject_name, 'subject_name': subject_name,
@ -97,7 +97,6 @@ def create_note(request):
#Check if subject exist, if not create it #Check if subject exist, if not create it
subject = Subject() subject = Subject()
print(form.data['subject'])
if Subject.objects.filter(name=form.data['subject']): if Subject.objects.filter(name=form.data['subject']):
subject = Subject.objects.filter(name=form.data['subject'])[0] subject = Subject.objects.filter(name=form.data['subject'])[0]
else: else:
@ -113,7 +112,6 @@ def create_note(request):
add_date=date.today()) add_date=date.today())
topic.save() topic.save()
#Create note #Create note
note = Note(name=form.data['name'], note = Note(name=form.data['name'],
content=form.data['content'], content=form.data['content'],
@ -123,4 +121,77 @@ def create_note(request):
add_date=date.today()) add_date=date.today())
note.save() note.save()
url = reverse('subject') url = reverse('subject')
return HttpResponseRedirect(url) 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()

Binary file not shown.