aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/forms.py
blob: 4dc79d266e448b72a09700a29c48de03c47c53fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import NON_FIELD_ERRORS

from utils.widgets import RelatedFieldWidgetCanAdd

from .models import Note, Notebook


class BaseNoteForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop("user", None)
        super(BaseNoteForm, self).__init__(*args, **kwargs)


class NoteForm(BaseNoteForm):
    class Meta:
        model = Note
        fields = ['title', 'body_text', 'body_html', 'body_qjson', 'notebook', 'url', 'tags']
        labels = {
            "body": _("Note"),
        }

    def __init__(self, *args, **kwargs):
        user = kwargs.pop("user", None)
        super(NoteForm, self).__init__(*args, **kwargs)
        self.fields['notebook'].widget = RelatedFieldWidgetCanAdd(Notebook, related_url="notebooks:list")
        self.fields['notebook'].queryset = Notebook.objects.filter(owner__username=user)


class NotebookForm(BaseNoteForm):
    class Meta:
        model = Notebook
        fields = ['owner', 'name', 'color_rgb']
        widgets = {'owner': forms.HiddenInput()}
        labels = {
            "name": _("Notebook Name"),
            "color_rgb": _("Notebook Color"),
        }
        error_messages = {
            NON_FIELD_ERRORS: {
                'unique_together': "You already have a notebook by that name, please choose a different name",
            }
        }

    def __init__(self, *args, **kwargs):
        user = kwargs.pop("user", None)
        super(NotebookForm, self).__init__(*args, **kwargs)
        self.fields['owner'].initial = user