BES-55 Adding notes with tinymce, fix showing too many subjects
This commit is contained in:
parent
032b4c34c2
commit
be72c875c3
@ -44,6 +44,7 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'widget_tweaks',
|
'widget_tweaks',
|
||||||
|
'tinymce',
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@ -125,3 +126,18 @@ USE_TZ = True
|
|||||||
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
# https://docs.djangoproject.com/en/2.2/howto/static-files/
|
||||||
|
|
||||||
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_DEFAULT_CONFIG = {
|
||||||
|
'height' : 500,
|
||||||
|
'plugins': "image,imagetools,media,codesample,link,code",
|
||||||
|
'cleanup_on_startup': True,
|
||||||
|
'menubar': False,
|
||||||
|
'toolbar': "styleselect |undo redo | bold italic | alignleft aligncenter alignright | link image media codesample code",
|
||||||
|
'image_caption': True,
|
||||||
|
'image_advtab': True,
|
||||||
|
'custom_undo_redo_levels': 10,
|
||||||
|
'file_browser_callback' : "myFileBrowser"
|
||||||
|
}
|
@ -20,5 +20,6 @@ from bestnotes.views import homepage
|
|||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('bestnotes/', include('bestnotes.urls')),
|
path('bestnotes/', include('bestnotes.urls')),
|
||||||
path('', homepage, name='homepage'),
|
path('', homepage, name='homepage'),
|
||||||
|
path(r'^tinymce/', include('tinymce.urls')),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
]
|
]
|
||||||
|
8
bestnotes/forms.py
Normal file
8
bestnotes/forms.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
from django import forms
|
||||||
|
from tinymce.widgets import TinyMCE
|
||||||
|
|
||||||
|
class EditorForm(forms.Form):
|
||||||
|
content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}),required=True)
|
||||||
|
subject = forms.CharField(label="subject", max_length=30, required=True)
|
||||||
|
topic = forms.CharField(label="topic", max_length=30, required=True)
|
||||||
|
name = forms.CharField(label="name", max_length=30, required=True)
|
20
bestnotes/migrations/0004_note_content.py
Normal file
20
bestnotes/migrations/0004_note_content.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 2.2.7 on 2019-12-22 12:22
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
import tinymce.models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('bestnotes', '0003_note'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='note',
|
||||||
|
name='content',
|
||||||
|
field=tinymce.models.HTMLField(default='test', verbose_name='Content'),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
]
|
@ -5,6 +5,8 @@ from django.db import models
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.signals import post_save
|
from django.db.models.signals import post_save
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
|
from tinymce import models as tinymce_models
|
||||||
|
|
||||||
|
|
||||||
class StudentProfile(models.Model):
|
class StudentProfile(models.Model):
|
||||||
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
||||||
@ -48,6 +50,7 @@ class Topic(models.Model):
|
|||||||
|
|
||||||
class Note(models.Model):
|
class Note(models.Model):
|
||||||
name = models.CharField(max_length=30)
|
name = models.CharField(max_length=30)
|
||||||
|
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)
|
||||||
user = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)
|
user = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)
|
||||||
|
22
bestnotes/templates/add.html
Normal file
22
bestnotes/templates/add.html
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{% extends 'base.html' %}
|
||||||
|
{% load widget_tweaks %}
|
||||||
|
{% load static %}
|
||||||
|
{% block css %}
|
||||||
|
{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
{% include 'navbar.html' %}
|
||||||
|
|
||||||
|
<form action="/bestnotes/create/" method="post">{% csrf_token %}
|
||||||
|
<div class="form-group">
|
||||||
|
{% render_field form.name class="form-control mb-2 mt-4" id="name" placeholder="Wpisz nazwe notatki" %}
|
||||||
|
{% render_field form.subject class="form-control mb-2" name="subject" id="subject" placeholder="Wpisz przedmiot" %}
|
||||||
|
{% render_field form.topic class="form-control mb-2" id="topic" placeholder="Wpisz temat" %}
|
||||||
|
<div>
|
||||||
|
{% render_field form.content id="content" %}
|
||||||
|
<input type="submit" class="btn-primary" value="Dodaj">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
{% endblock %}
|
@ -5,7 +5,7 @@
|
|||||||
<!-- Required meta tags -->
|
<!-- 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 }}
|
||||||
<!-- Bootstrap CSS -->
|
<!-- 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' %}">
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
<ul class="navbar-nav collapse navbar-collapse" id="navbarNav">
|
<ul class="navbar-nav collapse navbar-collapse" id="navbarNav">
|
||||||
<li id="home" class="nav-item nav-link"><a class="nolink" href="{% url 'homepage' %}"><i class="fa fa-home fa-2x"></a></i></li>
|
<li id="home" class="nav-item nav-link"><a class="nolink" href="{% url 'homepage' %}"><i class="fa fa-home fa-2x"></a></i></li>
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
<li id="add_note" class="nav-item nav-link"><a class="nolink" href="#"><i class="fa fa-pencil-square-o fa-2x"></a></i></li>
|
<li id="add_note" class="nav-item nav-link"><a class="nolink" href="{% url 'add' %}"><i class="fa fa-pencil-square-o fa-2x"></a></i></li>
|
||||||
<li id="all_subjects" class="nav-item nav-link active"><a class="nolink" href="{% url 'subject' %}"><i class="fa fa-list-ul fa-2x"></i></a></li>
|
<li id="all_subjects" class="nav-item nav-link active"><a class="nolink" href="{% url 'subject' %}"><i class="fa fa-list-ul fa-2x"></i></a></li>
|
||||||
<li id="logout" class="nav-item nav-link"><a class="nolink" href="{% url 'logout' %}"><i class="fa fa-sign-out fa-2x"></i></a></li>
|
<li id="logout" class="nav-item nav-link"><a class="nolink" href="{% url 'logout' %}"><i class="fa fa-sign-out fa-2x"></i></a></li>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -11,7 +11,8 @@ urlpatterns = [
|
|||||||
path('note/<id>', views.note_id, name="note"),
|
path('note/<id>', views.note_id, name="note"),
|
||||||
path('notes_by_topic/<topic_id>', views.notes_by_topic_id, name="notes"),
|
path('notes_by_topic/<topic_id>', views.notes_by_topic_id, name="notes"),
|
||||||
path('topics_by_subject_id/<subject_id>', views.topics_by_subject_id, name="topics"),
|
path('topics_by_subject_id/<subject_id>', views.topics_by_subject_id, name="topics"),
|
||||||
|
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")
|
||||||
]
|
]
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from bestnotes.models import Note, Topic, Subject
|
from bestnotes.models import Note, Topic, Subject, StudentProfile
|
||||||
# Create your views here.
|
from datetime import date
|
||||||
|
from .forms import EditorForm
|
||||||
|
from django.urls import reverse
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
|
|
||||||
def homepage(request):
|
def homepage(request):
|
||||||
#return HttpResponse("BestNotes' index will be here.")
|
#return HttpResponse("BestNotes' index will be here.")
|
||||||
return render(request, "homepage.html", {})
|
return render(request, "homepage.html", {})
|
||||||
|
|
||||||
|
def add(request):
|
||||||
|
form = EditorForm()
|
||||||
|
return render(request, "add.html", {"form": form})
|
||||||
|
|
||||||
|
|
||||||
def subject(request):
|
def subject(request):
|
||||||
return render(request, "subjects.html", {})
|
return render(request, "subjects.html", {})
|
||||||
@ -79,6 +85,42 @@ def subjects_all(request):
|
|||||||
student_subjects.append(note.topic.subject)
|
student_subjects.append(note.topic.subject)
|
||||||
context = {
|
context = {
|
||||||
'subjects' : all_subjects,
|
'subjects' : all_subjects,
|
||||||
'student_subjects': student_subjects
|
'student_subjects': set(student_subjects)
|
||||||
}
|
}
|
||||||
return render(request, "subjects.html", context)
|
return render(request, "subjects.html", context)
|
||||||
|
|
||||||
|
def create_note(request):
|
||||||
|
form = EditorForm(request.POST)
|
||||||
|
if request.method == 'POST':
|
||||||
|
#Get actual user as Student
|
||||||
|
student = StudentProfile.objects.get(user=request.user.id)
|
||||||
|
|
||||||
|
#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:
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
#Create note
|
||||||
|
note = Note(name=form.data['name'],
|
||||||
|
content=form.data['content'],
|
||||||
|
text="Empty",
|
||||||
|
topic=topic,
|
||||||
|
user=student,
|
||||||
|
add_date=date.today())
|
||||||
|
note.save()
|
||||||
|
url = reverse('subject')
|
||||||
|
return HttpResponseRedirect(url)
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user