1
1
from django import forms
2
+ from django_tools .middlewares import ThreadLocal
3
+ from django .utils .translation import ugettext as _
4
+
2
5
from . import models
6
+ from etsd .keys .models import PublicKey
7
+
8
+
9
+ def has_active_public_key (authority ):
10
+ return PublicKey .objects .filter (authority = authority , status = "ACTIVE" ).exists ()
3
11
4
12
5
13
class ParticipantInlineForm (forms .ModelForm ):
@@ -9,6 +17,48 @@ class Meta:
9
17
10
18
def __init__ (self , * args , ** kwargs ):
11
19
super (ParticipantInlineForm , self ).__init__ (* args , ** kwargs )
12
-
20
+ user = ThreadLocal .get_current_user ()
21
+
13
22
new_choices = list (self .fields ["kind" ].choices )
14
23
self .fields ["kind" ].choices = new_choices [:3 ]
24
+
25
+ auth_qs = self .fields ["authority" ].queryset
26
+ self .fields ["authority" ].queryset = auth_qs .exclude (id = user .get_authority ().id )
27
+
28
+ def clean (self ):
29
+ authority = self .cleaned_data .get ("authority" )
30
+ if authority and not has_active_public_key (authority ):
31
+ self .add_error (
32
+ "authority" ,
33
+ _ ("This authority does not have an active public key!" ),
34
+ )
35
+
36
+
37
+ class MessageCreateForm (forms .ModelForm ):
38
+ class Meta :
39
+ model = models .Message
40
+ fields = ["category" , "kind" , "available_to_sender" , "rel_message" ]
41
+
42
+ def clean (self ):
43
+ user = ThreadLocal .get_current_user ()
44
+ my_authority = user .get_authority ()
45
+ if self .cleaned_data ["available_to_sender" ] and not has_active_public_key (
46
+ my_authority
47
+ ):
48
+ self .add_error (
49
+ "available_to_sender" ,
50
+ _ ("Your authority does not have an active public key!" ),
51
+ )
52
+
53
+ kind = self .cleaned_data .get ("kind" )
54
+ rel_message = self .cleaned_data .get ("rel_message" )
55
+ if kind == "NEW" and rel_message :
56
+ self .add_error (
57
+ "rel_message" ,
58
+ _ ("You cannot add a related message when message kind is New!" ),
59
+ )
60
+ if kind != "NEW" and not rel_message :
61
+ self .add_error (
62
+ "rel_message" ,
63
+ _ ("You must provide a related message when message kind is not New!" ),
64
+ )
0 commit comments