BES-6 Updating note, no protections

This commit is contained in:
Th3NiKo 2020-01-16 11:16:59 +01:00
parent 54c5370b5d
commit 1553d6be08
6 changed files with 86 additions and 9 deletions

View File

@ -6,7 +6,7 @@
{% block content %}
{% 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">
{% 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" %}

View File

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

@ -15,5 +15,7 @@ urlpatterns = [
path('notes/', views.notes_all, name="notes"),
path('notes/<subject_id>', views.notes_name, name="notes_id"),
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")
]

View File

@ -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()

Binary file not shown.