BES-6 Delete note

This commit is contained in:
Th3NiKo 2020-01-15 16:47:34 +01:00
parent e78332631c
commit 54c5370b5d
5 changed files with 34 additions and 6 deletions

View File

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

View File

@ -11,7 +11,10 @@
</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> <button id="delete_note" class="btn-danger">Usuń</button></div>
<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>
{% endif %}

View File

@ -14,5 +14,6 @@ urlpatterns = [
path('addnote/', views.add, name="add"),
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('create/', views.create_note, name="create_note"),
path('delete_note/<note_id>', views.delete_note, name="delete_note")
]

View File

@ -123,4 +123,28 @@ def create_note(request):
add_date=date.today())
note.save()
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)
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.