| 1 | # coding: utf-8 |
|---|
| 2 | |
|---|
| 3 | from opentrials.repository.trds_forms import ReviewModelForm |
|---|
| 4 | from opentrials.reviewapp.models import Remark |
|---|
| 5 | from opentrials.reviewapp.models import UserProfile |
|---|
| 6 | from opentrials.reviewapp.models import Attachment, Submission |
|---|
| 7 | from django import forms |
|---|
| 8 | from django.core.exceptions import ValidationError |
|---|
| 9 | from django.utils.translation import ugettext as _ |
|---|
| 10 | from django.contrib.auth.models import User |
|---|
| 11 | from django.conf import settings |
|---|
| 12 | |
|---|
| 13 | from polyglot.multilingual_forms import MultilingualBaseForm |
|---|
| 14 | from polyglot.multilingual_forms import MultilingualModelChoiceField, MultilingualModelMultipleChoiceField |
|---|
| 15 | |
|---|
| 16 | from repository.models import Institution, CountryCode |
|---|
| 17 | from repository.widgets import SelectInstitution |
|---|
| 18 | |
|---|
| 19 | ACCESS = [ |
|---|
| 20 | ('public', 'Public'), |
|---|
| 21 | ('private', 'Private'), |
|---|
| 22 | ] |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | class 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 | |
|---|
| 57 | class 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 | |
|---|
| 69 | class 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 | |
|---|
| 77 | class NewAttachmentForm(ReviewModelForm): |
|---|
| 78 | class Meta: |
|---|
| 79 | model = Attachment |
|---|
| 80 | fields = ['file','description','public'] |
|---|
| 81 | |
|---|
| 82 | title = _('New Attachment') |
|---|
| 83 | |
|---|
| 84 | class 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 | |
|---|
| 102 | class UserProfileForm(forms.ModelForm): |
|---|
| 103 | class Meta: |
|---|
| 104 | model = UserProfile |
|---|
| 105 | fields = ['preferred_language'] |
|---|
| 106 | |
|---|
| 107 | title = _('Aditional info for profile') |
|---|
| 108 | |
|---|
| 109 | class UploadTrial(forms.Form): |
|---|
| 110 | submission_xml = forms.CharField(widget=forms.FileInput,required=True) |
|---|
| 111 | |
|---|
| 112 | class OpenRemarkForm(forms.ModelForm): |
|---|
| 113 | class Meta: |
|---|
| 114 | model = Remark |
|---|
| 115 | exclude = ['submission','context','status'] |
|---|
| 116 | |
|---|
| 117 | title = _('Open a new Remark') |
|---|
| 118 | |
|---|
| 119 | class 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 | |
|---|
| 125 | class 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')}) |
|---|