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