forked from treeio/treeio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
174 lines (136 loc) · 6.11 KB
/
forms.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# encoding: utf-8
# Copyright 2011 Tree.io Limited
# This file is part of Treeio.
# License www.tree.io/license
"""
Documents module forms
"""
from django.forms import ModelForm, CharField, TextInput, ChoiceField, Form
from treeio.documents.models import Folder, Document, File, WebLink
from treeio.core.models import Object
from django.utils.translation import ugettext as _
from treeio.core.decorators import preprocess_form
from django.core.urlresolvers import reverse
preprocess_form()
class MassActionForm(Form):
""" Mass action form for Reports """
delete = ChoiceField(label=_("With selected"), choices=(('', '-----'), ('delete', 'Delete Completely'),
('trash', 'Move to Trash')), required=False)
instance = None
def __init__(self, user, *args, **kwargs):
if 'instance' in kwargs:
self.instance = kwargs['instance']
del kwargs['instance']
super(MassActionForm, self).__init__(*args, **kwargs)
self.fields['delete'] = ChoiceField(label=_("With selected"), choices=(('', '-----'),
('delete', _('Delete Completely')),
('trash', _('Move to Trash'))), required=False)
def save(self, *args, **kwargs):
"Process form"
if self.instance:
if self.is_valid():
if self.cleaned_data['delete']:
if self.cleaned_data['delete'] == 'delete':
self.instance.delete()
if self.cleaned_data['delete'] == 'trash':
self.instance.trash = True
self.instance.save()
class FolderForm(ModelForm):
""" Folder form """
def __init__(self, user, folder_id, *args, **kwargs):
super(FolderForm, self ).__init__(*args, **kwargs)
self.fields['name'].label = _("Name")
self.fields['parent'].label = _("Parent")
self.fields['parent'].queryset = Object.filter_permitted(user, Folder.objects, mode='x')
if folder_id:
self.fields['parent'].initial = folder_id
class Meta:
"Folder"
model = Folder
fields = ('name', 'parent')
class DocumentForm(ModelForm):
""" Document form """
title = CharField(widget=TextInput(attrs={'size':'50'}))
def __init__(self, user, folder_id, *args, **kwargs):
super(DocumentForm, self ).__init__(*args, **kwargs)
self.fields['title'].label = _("Title")
self.fields['folder'].label = _("Folder")
self.fields['folder'].queryset = Object.filter_permitted(user, Folder.objects, mode='x')
self.fields['folder'].widget.attrs.update({'popuplink': reverse('documents_folder_add')})
if folder_id:
self.fields['folder'].initial = folder_id
else:
try:
self.fields['folder'].initial = self.fields['folder'].queryset[0].id
except:
pass
self.fields['body'].label = _("Body")
self.fields['body'].widget.attrs.update({'class': 'full-editor'})
class Meta:
"Document"
model = Document
fields = ('title', 'folder', 'body')
class FileForm(ModelForm):
""" File form """
name = CharField(widget=TextInput(attrs={'size':'25'}))
def __init__(self, user, folder_id, *args, **kwargs):
super(FileForm, self ).__init__(*args, **kwargs)
self.fields['name'].label = _("Name")
self.fields['folder'].label = _("Folder")
self.fields['folder'].queryset = Object.filter_permitted(user, Folder.objects, mode='x')
self.fields['folder'].widget.attrs.update({'popuplink': reverse('documents_folder_add')})
if folder_id:
self.fields['folder'].initial = folder_id
else:
try:
self.fields['folder'].initial = self.fields['folder'].queryset[0].id
except:
pass
self.fields['content'].label = _("Content")
class Meta:
"File"
model = File
fields = ('name', 'folder', 'content')
class WebLinkForm(ModelForm):
""" WebLink form """
def __init__(self, user, folder_id, *args, **kwargs):
super(WebLinkForm, self ).__init__(*args, **kwargs)
self.fields['title'].label = _("Title")
self.fields['title'].widget = TextInput(attrs = {'size':'35'})
self.fields['url'].label = _("URL")
self.fields['url'].initial = 'http://'
self.fields['url'].widget = TextInput(attrs = {'size':'50'})
self.fields['folder'].label = _("Folder")
self.fields['folder'].queryset = Object.filter_permitted(user, Folder.objects, mode='x')
self.fields['folder'].widget.attrs.update({'popuplink': reverse('documents_folder_add')})
if folder_id:
self.fields['folder'].initial = folder_id
else:
try:
self.fields['folder'].initial = self.fields['folder'].queryset[0].id
except:
pass
class Meta:
"WebLink"
model = WebLink
fields = ('title', 'folder', 'url')
class FilterForm(ModelForm):
""" Filter form definition """
def __init__(self, user, skip=[], *args, **kwargs):
super(FilterForm, self).__init__(*args, **kwargs)
self.fields['title'].label = _("Title")
self.fields['folder'].label = _("Folder")
if 'title' in skip:
del self.fields['title']
else:
self.fields['title'].required = False
if 'folder' in skip:
del self.fields['folder']
else:
self.fields['folder'].queryset = Object.filter_permitted(user, Folder.objects, mode='x')
#self.fields['folder'].required = False
self.fields['folder'].null = True
class Meta:
"Filter"
model = Document
fields = ('title', 'folder')