diff --git a/notes/forms.py b/notes/forms.py new file mode 100644 index 0000000..2d9aa57 --- /dev/null +++ b/notes/forms.py @@ -0,0 +1,8 @@ +from django import forms + +from notes.models import Note + +class PostNoteForm(forms.ModelForm): + class Meta: + model = Note + fields = ["to_user", "note"] diff --git a/notes/templates/notes/base.html b/notes/templates/notes/base.html index 279b75d..405ff0d 100644 --- a/notes/templates/notes/base.html +++ b/notes/templates/notes/base.html @@ -10,7 +10,7 @@
Appunti - + Post a note diff --git a/notes/templates/notes/note_form.html b/notes/templates/notes/note_form.html new file mode 100644 index 0000000..086c54e --- /dev/null +++ b/notes/templates/notes/note_form.html @@ -0,0 +1,11 @@ +{% extends "notes/base.html" %} + +{% block title %}Post a Note{% endblock %} + +{% block body %} +

Post a Note

+
{% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/notes/urls.py b/notes/urls.py index 9c583da..eeafaf8 100644 --- a/notes/urls.py +++ b/notes/urls.py @@ -6,6 +6,7 @@ from notes import views, api_views urlpatterns = [ path("", views.HomePage.as_view(), name="home"), + path("post-a-note/", views.PostNoteView.as_view(), name="post-a-note"), path("login/", LoginView.as_view(), name='login'), path("api/notes/", api_views.NoteListView.as_view(), name="api-notes-list"), path("api-auth/", include("rest_framework.urls", namespace="rest_framework")), diff --git a/notes/views.py b/notes/views.py index 198f1ca..5b6620f 100644 --- a/notes/views.py +++ b/notes/views.py @@ -1,6 +1,11 @@ +from datetime import timedelta from typing import Any from django.contrib.auth.mixins import LoginRequiredMixin -from django.views.generic import TemplateView +from django.urls import reverse_lazy +from django.utils import timezone +from django.views.generic import CreateView, TemplateView +from django.views.generic.edit import FormMixin +from notes.models import Note # Create your views here. class HomePage(LoginRequiredMixin, TemplateView): @@ -10,3 +15,21 @@ class HomePage(LoginRequiredMixin, TemplateView): ctx = super().get_context_data(**kwargs) ctx['notes'] = self.request.user.alive_received_notes return ctx + + +class PostNoteView(LoginRequiredMixin, CreateView): + model = Note + fields = ["to_user", "note"] + success_url = reverse_lazy("post-a-note") + + def get_form(self, form_class=None): + form = super().get_form(form_class) + form.fields['to_user'].queryset = self.request.user.allowed_notes_to.all() + return form + + def form_valid(self, form): + self.object = note = form.save(commit=False) + note.expiry = timezone.now() + timedelta(seconds=note.to_user.expiry_seconds) + note.from_user = self.request.user + note.save() + return FormMixin.form_valid(self, form)