BES-6 Updating note, no protections
This commit is contained in:
parent
54c5370b5d
commit
1553d6be08
@ -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 'update_note_id' note_id=note.id %} 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" %}
|
||||||
|
@ -11,10 +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>
|
<div class="row d-flex justify-content-center">
|
||||||
<form action="{% url 'delete_note' note_id=note.id%}">
|
<form action="{% url 'update_note' note_id=note.id %}">
|
||||||
<button type="submit" id="delete_note" class="btn-danger">Usuń</button></div>
|
<button type="submit" id="edit_note" class="btn-success mr-1">Edytuj</button>
|
||||||
</form>
|
</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 %}
|
||||||
|
|
||||||
|
24
bestnotes/templates/update.html
Normal file
24
bestnotes/templates/update.html
Normal 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 %}
|
@ -15,5 +15,7 @@ urlpatterns = [
|
|||||||
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('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")
|
||||||
]
|
]
|
||||||
|
@ -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'],
|
||||||
@ -137,6 +135,55 @@ def delete_note(request, note_id):
|
|||||||
url = reverse('subject')
|
url = reverse('subject')
|
||||||
return HttpResponseRedirect(url)
|
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():
|
def delete_empty_categories():
|
||||||
#Get all topics
|
#Get all topics
|
||||||
topics = Topic.objects.all()
|
topics = Topic.objects.all()
|
||||||
|
BIN
db.sqlite3
BIN
db.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user