Revision e1a80257 snf-astakos-app/astakos/im/forms.py
b/snf-astakos-app/astakos/im/forms.py | ||
---|---|---|
35 | 35 |
|
36 | 36 |
from django import forms |
37 | 37 |
from django.utils.translation import ugettext as _ |
38 |
from django.contrib.auth.forms import (UserCreationForm, AuthenticationForm, |
|
39 |
PasswordResetForm, PasswordChangeForm, |
|
40 |
SetPasswordForm) |
|
38 |
from django.contrib.auth.forms import ( |
|
39 |
UserCreationForm, AuthenticationForm, |
|
40 |
PasswordResetForm, PasswordChangeForm, |
|
41 |
SetPasswordForm |
|
42 |
) |
|
41 | 43 |
from django.core.mail import send_mail |
42 | 44 |
from django.contrib.auth.tokens import default_token_generator |
43 | 45 |
from django.template import Context, loader |
... | ... | |
50 | 52 |
from django.db import transaction |
51 | 53 |
from django.utils.encoding import smart_unicode |
52 | 54 |
from django.core import validators |
55 |
from django.contrib.auth.models import AnonymousUser |
|
53 | 56 |
|
54 | 57 |
from astakos.im.models import ( |
55 | 58 |
AstakosUser, EmailChange, AstakosGroup, Invitation, GroupKind, |
56 |
Resource, PendingThirdPartyUser, get_latest_terms, RESOURCE_SEPARATOR |
|
59 |
Resource, PendingThirdPartyUser, get_latest_terms, RESOURCE_SEPARATOR, |
|
60 |
ProjectDefinition, ProjectApplication, create_application |
|
57 | 61 |
) |
58 | 62 |
from astakos.im.settings import ( |
59 | 63 |
INVITATIONS_PER_LEVEL, BASEURL, SITENAME, RECAPTCHA_PRIVATE_KEY, |
... | ... | |
902 | 906 |
except BaseException, e: |
903 | 907 |
logger.exception(e) |
904 | 908 |
return super(ExtendedSetPasswordForm, self).save(commit=commit) |
909 |
|
|
910 |
|
|
911 |
class ProjectApplicationForm(forms.ModelForm): |
|
912 |
name = forms.CharField( |
|
913 |
validators=[validators.RegexValidator( |
|
914 |
DOMAIN_VALUE_REGEX, |
|
915 |
_(astakos_messages.DOMAIN_VALUE_ERR), |
|
916 |
'invalid' |
|
917 |
)], |
|
918 |
widget=forms.TextInput(attrs={'placeholder': 'eg. foo.ece.ntua.gr'}), |
|
919 |
help_text="Name should be in the form of dns" |
|
920 |
) |
|
921 |
comments = forms.CharField(widget=forms.Textarea, required=False) |
|
922 |
|
|
923 |
class Meta: |
|
924 |
model = ProjectDefinition |
|
925 |
exclude = ('resource_grants') |
|
926 |
|
|
927 |
def __init__(self, *args, **kwargs): |
|
928 |
#update QueryDict |
|
929 |
args = list(args) |
|
930 |
qd = args.pop(0).copy() |
|
931 |
members_unlimited = qd.pop('members_unlimited', False) |
|
932 |
members_uplimit = qd.pop('members_uplimit', None) |
|
933 |
|
|
934 |
#substitue QueryDict |
|
935 |
args.insert(0, qd) |
|
936 |
|
|
937 |
super(AstakosGroupCreationForm, self).__init__(*args, **kwargs) |
|
938 |
|
|
939 |
self.fields.keyOrder = ['kind', 'name', 'homepage', 'desc', |
|
940 |
'issue_date', 'expiration_date', |
|
941 |
'moderation_enabled', 'max_participants'] |
|
942 |
def add_fields((k, v)): |
|
943 |
k = k.partition('_proxy')[0] |
|
944 |
self.fields[k] = forms.IntegerField( |
|
945 |
required=False, |
|
946 |
widget=forms.HiddenInput(), |
|
947 |
min_value=1 |
|
948 |
) |
|
949 |
map(add_fields, |
|
950 |
((k, v) for k,v in qd.iteritems() if k.endswith('_uplimit')) |
|
951 |
) |
|
952 |
|
|
953 |
def add_fields((k, v)): |
|
954 |
self.fields[k] = forms.BooleanField( |
|
955 |
required=False, |
|
956 |
#widget=forms.HiddenInput() |
|
957 |
) |
|
958 |
map(add_fields, |
|
959 |
((k, v) for k,v in qd.iteritems() if k.startswith('is_selected_')) |
|
960 |
) |
|
961 |
|
|
962 |
def clean(self): |
|
963 |
userid = self.data.get('user', None)[0] |
|
964 |
self.user = None |
|
965 |
if userid: |
|
966 |
try: |
|
967 |
self.user = AstakosUser.objects.get(id=userid) |
|
968 |
except AstakosUser.DoesNotExist: |
|
969 |
pass |
|
970 |
if not self.user: |
|
971 |
raise forms.ValidationError(_(astakos_messages.NO_APPLICANT)) |
|
972 |
super(ProjectApplicationForm, self).clean() |
|
973 |
return self.cleaned_data |
|
974 |
|
|
975 |
def resource_policies(self, clean=True): |
|
976 |
if clean: |
|
977 |
self.clean() |
|
978 |
policies = [] |
|
979 |
append = policies.append |
|
980 |
for name, uplimit in self.cleaned_data.iteritems(): |
|
981 |
subs = name.split('_uplimit') |
|
982 |
if len(subs) == 2: |
|
983 |
prefix, suffix = subs |
|
984 |
s, sep, r = prefix.partition(RESOURCE_SEPARATOR) |
|
985 |
resource = Resource.objects.get(service__name=s, name=r) |
|
986 |
|
|
987 |
# keep only resource limits for selected resource groups |
|
988 |
if self.cleaned_data.get( |
|
989 |
'is_selected_%s' % resource.group, False |
|
990 |
): |
|
991 |
append(dict(service=s, resource=r, uplimit=uplimit)) |
|
992 |
return policies |
|
993 |
|
|
994 |
def save(self, commit=True): |
|
995 |
definition = super(ProjectApplicationForm, self).save(commit=commit) |
|
996 |
definition.resource_policies=self.resource_policies(clean=False) |
|
997 |
applicant = self.user |
|
998 |
comments = self.cleaned_data.pop('comments', None) |
|
999 |
try: |
|
1000 |
precursor_application = self.instance.projectapplication |
|
1001 |
except: |
|
1002 |
precursor_application = None |
|
1003 |
return create_application( |
|
1004 |
definition, |
|
1005 |
applicant, |
|
1006 |
comments, |
|
1007 |
precursor_application, |
|
1008 |
commit |
|
1009 |
) |
Also available in: Unified diff