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

Revision 763, 4.7 kB (checked in by jose.brandao, 3 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.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            self.fields['primary_sponsor'].queryset = self.fields['primary_sponsor'].queryset.filter(creator=self.user)
46
47    form_title = _('Initial Trial Data')
48    scientific_title = forms.CharField(widget=forms.Textarea, 
49                                       label=_('Scientific Title'), 
50                                       max_length=2000)
51    recruitment_country = MultilingualModelMultipleChoiceField(
52                                                    label=_('Recruitment Country'),
53                                                    model=CountryCode,
54                                                    label_field='description',)
55    language = forms.ChoiceField(label=_('Submission language'), 
56                                 choices=settings.MANAGED_LANGUAGES_CHOICES)
57
58class PrimarySponsorForm(ReviewModelForm):
59    class Meta:
60        model = Institution
61        exclude = ['address']
62    form_title = _('Primary Sponsor')
63   
64    country = MultilingualModelChoiceField(
65            label=_('Country'),
66            queryset=CountryCode.objects.all(),
67            required=True,
68            label_field='description',)
69
70class ExistingAttachmentForm(forms.ModelForm):
71    class Meta:
72        model = Attachment
73        exclude = ['submission']
74
75    title = _('Existing Attachment')
76    file = forms.CharField(required=False,label=_('File'),max_length=255)
77
78class NewAttachmentForm(ReviewModelForm):
79    class Meta:
80        model = Attachment
81        fields = ['file','description','public']
82
83    title = _('New Attachment')
84   
85class UserForm(forms.ModelForm):
86    def clean_email(self):
87        """
88        Validate that the supplied email address is unique for the
89        site.
90       
91        """
92        if self.cleaned_data['email'] != self.instance.email:
93            if User.objects.filter(email__iexact=self.cleaned_data['email']):
94                raise forms.ValidationError(_("This email address is already in use. Please supply a different email address."))
95        return self.cleaned_data['email']
96       
97    class Meta:
98        model = User
99        fields = ['first_name','last_name','email']
100   
101    title = _('User Profile')
102
103class UserProfileForm(forms.ModelForm):
104    class Meta:
105        model = UserProfile
106        fields = ['preferred_language']
107
108    title = _('Aditional info for profile')
109
110class UploadTrial(forms.Form):
111    submission_xml = forms.CharField(widget=forms.FileInput,required=True)
112
113class OpenRemarkForm(forms.ModelForm):
114    class Meta:
115        model = Remark
116        exclude = ['submission','context','status']
117
118    title = _('Open a new Remark')
119   
120class ContactForm(forms.Form):
121    name = forms.CharField(label=_("Name"), max_length=50)
122    from_email = forms.EmailField(label=_("E-mail"))
123    subject = forms.CharField(label=_("Subject"), max_length=50)
124    message = forms.CharField(label=_("Message"), widget=forms.Textarea)
125
126class TermsUseForm(forms.Form):
127    agree = forms.BooleanField(label=_("I read and agree to all terms above"), required=True, 
128                               error_messages={'required': _('You must agree to the terms')})
Note: See TracBrowser for help on using the browser.