| author | veltr |
| Fri, 26 Oct 2012 18:38:28 +0200 | |
| branch | new_rest_api |
| changeset 878 | 4a5c54368447 |
| parent 295 | 454449cd5e11 |
| child 1190 | 129d45eec68c |
| permissions | -rw-r--r-- |
| 269 | 1 |
from django import forms |
2 |
from django.contrib.auth.models import User, Group |
|
|
293
4fd110c8fa26
Projects and contents can be shared or not during edition and creation
verrierj
parents:
274
diff
changeset
|
3 |
from django.forms.widgets import HiddenInput, MultipleHiddenInput |
| 269 | 4 |
|
5 |
class LazyMultipleChoiceField(forms.MultipleChoiceField): |
|
6 |
||
7 |
def validate(self, value): |
|
8 |
pass |
|
9 |
||
10 |
class ShareForm(forms.ModelForm): |
|
11 |
read_list = LazyMultipleChoiceField(required=False, widget=MultipleHiddenInput()) |
|
12 |
write_list = LazyMultipleChoiceField(required=False, widget=MultipleHiddenInput()) |
|
|
293
4fd110c8fa26
Projects and contents can be shared or not during edition and creation
verrierj
parents:
274
diff
changeset
|
13 |
share = forms.BooleanField(required=False, initial=False, widget=HiddenInput()) |
| 269 | 14 |
|
15 |
def clean(self): |
|
| 271 | 16 |
super(ShareForm, self).clean() |
17 |
||
18 |
try: |
|
19 |
read_list = self.data.getlist("read_list") |
|
20 |
write_list = self.data.getlist("write_list") |
|
21 |
except AttributeError: |
|
22 |
# This will be raised for content creation. As data is preprocessed, self.data |
|
23 |
# is a dict and not a querydict, so getlist won't work |
|
24 |
read_list = self.data.get('read_list', []) |
|
25 |
write_list = self.data.get('write_list', []) |
|
| 269 | 26 |
|
27 |
elems = read_list + write_list |
|
28 |
users = [e.split('-')[0] for e in elems if e.split('-')[1] == 'user' ] |
|
29 |
groups = [e.split('-')[0] for e in elems if e.split('-')[1] == 'group' ] |
|
30 |
||
31 |
users = User.objects.filter(id__in=users) |
|
32 |
groups = Group.objects.filter(id__in=groups) |
|
33 |
||
34 |
def create_real_lists(list, users, groups): |
|
35 |
new_list = [] |
|
36 |
for e in list: |
|
37 |
id, cls_name = e.split('-') |
|
38 |
if cls_name == 'user': |
|
39 |
new_list.append(users.get(id=id)) |
|
40 |
elif cls_name == 'group': |
|
41 |
new_list.append(groups.get(id=id)) |
|
42 |
return new_list |
|
| 295 | 43 |
|
| 271 | 44 |
new_read_list = create_real_lists(read_list, users, groups) |
| 269 | 45 |
new_write_list = create_real_lists(write_list, users, groups) |
46 |
||
47 |
self.cleaned_data["read_list"] = new_read_list |
|
48 |
self.cleaned_data["write_list"] = new_write_list |
|
| 295 | 49 |
|
50 |
if self.data.get("share", False) == "True": |
|
51 |
self.cleaned_data["share"] = True |
|
52 |
else: |
|
53 |
self.cleaned_data["share"] = False |
|
| 269 | 54 |
return self.cleaned_data |