root/trunk/opentrials/reviewapp/forms.py @ 764

Revision 764, 4.6 kB (checked in by jose.brandao, 2 years ago)

Ticket #74 - Allow trialist to choose a sponsor form a list

Line 
1# coding: utf-8
2
3from opentrials.repository.trds_forms import ReviewModelForm
4from opentrials.reviewapp.models import Remark
5from opentrials.reviewapp.models import UserProfile
6from opentrials.reviewapp.models import Attachment, Submission
7from django import forms
8from django.core.exceptions import ValidationError
9from django.utils.translation import ugettext as _
10from django.contrib.auth.models import User
11from django.conf import settings
12
13from polyglot.multilingual_forms import MultilingualBaseForm
14from polyglot.multilingual_forms import MultilingualModelChoiceField, MultilingualModelMultipleChoiceField
15
16from repository.models import Institution, CountryCode
17from repository.widgets import SelectInstitution
18
19ACCESS = [
20    ('public', 'Public'),
21    ('private', 'Private'),
22]
23
24
25class InitialTrialForm(ReviewModelForm):
26    class Meta:
27        model = Submission
28        exclude = ['trial', 'status', 'staff_note', 'title']
29       
30    def __init__(self, *args, **kwargs):
31        self.user = kwargs.pop('user', None)
32
33        self.base_fields.keyOrder = ['language', 'scientific_title', 'recruitment_country',
34                'primary_sponsor',]
35
36        super(InitialTrialForm, self).__init__(*args, **kwargs)
37
38        self.fields['primary_sponsor'].queryset = Institution.objects.filter(creator=self.user).order_by('name')
39        self.fields['primary_sponsor'].widget = SelectInstitution(formset_prefix='primary_sponsor')
40
41        if self.user:
42            self.fields['language'] = forms.ChoiceField(label=_('Submission language'), 
43                        choices=settings.MANAGED_LANGUAGES_CHOICES, 
44                        initial=self.user.get_profile().preferred_language)
45
46    form_title = _('Initial Trial Data')
47    scientific_title = forms.CharField(widget=forms.Textarea, 
48                                       label=_('Scientific Title'), 
49                                       max_length=2000)
50    recruitment_country = MultilingualModelMultipleChoiceField(
51                                                    label=_('Recruitment Country'),
52                                                    model=CountryCode,
53                                                    label_field='description',)
54    language = forms.ChoiceField(label=_('Submission language'), 
55                                 choices=settings.MANAGED_LANGUAGES_CHOICES)
56
57class PrimarySponsorForm(ReviewModelForm):
58    class Meta:
59        model = Institution
60        exclude = ['address']
61    form_title = _('Primary Sponsor')
62   
63    country = MultilingualModelChoiceField(
64            label=_('Country'),
65            queryset=CountryCode.objects.all(),
66            required=True,
67            label_field='description',)
68
69class ExistingAttachmentForm(forms.ModelForm):
70    class Meta:
71        model = Attachment
72        exclude = ['submission']
73
74    title = _('Existing Attachment')
75    file = forms.CharField(required=False,label=_('File'),max_length=255)
76
77class NewAttachmentForm(ReviewModelForm):
78    class Meta:
79        model = Attachment
80        fields = ['file','description','public']
81
82    title = _('New Attachment')
83   
84class UserForm(forms.ModelForm):
85    def clean_email(self):
86        """
87        Validate that the supplied email address is unique for the
88        site.
89       
90        """
91        if self.cleaned_data['email'] != self.instance.email:
92            if User.objects.filter(email__iexact=self.cleaned_data['email']):
93                raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
94        return self.cleaned_data['email']
95       
96    class Meta:
97        model = User
98        fields = ['first_name','last_name','email']
99   
100    title = _('User Profile')
101
102class UserProfileForm(forms.ModelForm):
103    class Meta:
104        model = UserProfile
105        fields = ['preferred_language']
106
107    title = _('Aditional info for profile')
108
109class UploadTrial(forms.Form):
110    submission_xml = forms.CharField(widget=forms.FileInput,required=True)
111
112class OpenRemarkForm(forms.ModelForm):
113    class Meta:
114        model = Remark
115        exclude = ['submission','context','status']
116
117    title = _('Open a new Remark')
118   
119class ContactForm(forms.Form):
120    name = forms.CharField(label=_("Name"), max_length=50)
121    from_email = forms.EmailField(label=_("E-mail"))
122    subject = forms.CharField(label=_("Subject"), max_length=50)
123    message = forms.CharField(label=_("Message"), widget=forms.Textarea)
124
125class TermsUseForm(forms.Form):
126    agree = forms.BooleanField(label=_("I read and agree to all terms above"), required=True, 
127                               error_messages={'required': _('You must agree to the terms')})
Note: See TracBrowser for help on using the browser.