def formfield(self, **kwargs): defaults = {'widget': TagAutocompleteTagIt(max_tags=self.max_tags)} defaults.update(kwargs) # As an ugly hack, we override the admin widget if defaults['widget'] == AdminTextInputWidget: defaults['widget'] = TagAutocompleteTagIt(max_tags=self.max_tags) return super(TagAutocompleteTagItField, self).formfield(**defaults)
class CmsForm(PermalinkForm): class Meta: model = CommonCms title = forms.CharField(max_length=512, help_text=_('Title in 512 characters')) image = files_widget.forms.FilesFormField(required=False, fields=(forms.CharField(required=False), forms.CharField(required=False), forms.CharField(required=False), ), widget=files_widget.forms.widgets.ImageWidget()) permalink = forms.CharField() summary = forms.CharField(required=False, widget=forms.Textarea(attrs={'rows': 5, 'maxlength': 240})) description = forms.CharField(required=False, widget=CKEditorWidget()) topics = forms.ModelMultipleChoiceField(queryset=Topic.objects.filter(level=0), widget=forms.CheckboxSelectMultiple(attrs={'id': 'id_topics'})) tags = TagField(required=False, widget=TagAutocompleteTagIt(max_tags=False), help_text='') in_the_news = forms.ModelMultipleChoiceField( required=False, queryset=Party.objects.all(), widget=autocomplete_light.MultipleChoiceWidget(CmsHasPartyAutocomplete, attrs={'placeholder': _('Type to search organizations or people by name.'), 'class': 'form-control'} ) ) is_promoted = forms.BooleanField( required=False, widget=forms.CheckboxInput(attrs={'id': 'id_is_promoted'}) )
class SectionAdminModelForm(ModelForm): # It uses the same tags than articles. (TODO: explain better this comment) tags = TagField(widget=TagAutocompleteTagIt({ 'app': 'core', 'model': 'article' }), required=False) class Meta: model = Section fields = "__all__"
class ArticleAdminModelForm(ModelForm): body = CharField(widget=MarkdownWidget()) headline = CharField(label=u'Título', widget=TextInput(attrs={'style': 'width:600px'})) slug = CharField( label='Slug', widget=TextInput(attrs={ 'style': 'width:600px', 'readonly': 'readonly' }), help_text=u'Se genera automáticamente en base al título.', ) tags = TagField(widget=TagAutocompleteTagIt(max_tags=False), required=False) def clean_tags(self): """ This is a hack to bypass the bug that: 1 tag with spaces is considered as many tags by the lib. This doesn't ocurr with 2 tags or more. With double quotes, the tag with spaces is correctly interpreted. """ tags = self.cleaned_data.get('tags') if tags and ',' not in tags: # there is just 1 tag tags = tags.strip('"') tags = '"' + tags + '"' return tags def clean(self): cleaned_data = super(ArticleAdminModelForm, self).clean() date_value = (self.cleaned_data.get('date_published') if self.cleaned_data.get('is_published') else self.cleaned_data.get('date_created')) or date.today() targets = Article.objects.filter( Q(is_published=True) & Q(date_published__year=date_value.year) & Q(date_published__month=date_value.month) | Q(is_published=False) & Q(date_created__year=date_value.year) & Q(date_created__month=date_value.month), slug=slugify( cleanhtml(ldmarkup(smart_quotes( cleaned_data.get('headline'))))), ) if self.instance.id: targets = targets.exclude(id=self.instance.id) if targets: raise ValidationError( u'Ya existe un artículo en ese mes con el mismo título.') class Meta: model = Article fields = "__all__"
class ArticleAdminModelForm(ModelForm): body = CharField(widget=MarkdownWidget()) headline = CharField(label='Título', widget=TextInput(attrs={'style': 'width:600px'})) tags = TagField(widget=TagAutocompleteTagIt(max_tags=False), required=False) def clean_tags(self): """ This is a hack to bypass the bug that: 1 tag with spaces is considered as many tags by the lib. This doesn't ocurr with 2 tags or more. With double quotes, the tag with spaces is correctly interpreted. """ tags = self.cleaned_data.get('tags') if tags and ',' not in tags: # there is just 1 tag tags = tags.strip('"') tags = '"' + tags + '"' return tags class Meta: model = Article
class JobEditForm(CommonForm): def __init__(self, *args, **kwargs): try: user = kwargs.pop('user') except KeyError: user = None super(JobEditForm, self).__init__(*args, **kwargs) if user: self.fields['organization'].queryset = Organization.objects.filter( Q(admins=user) | Q(created_by=user)) title = forms.CharField(max_length=255, widget=forms.TextInput()) contact_information = forms.CharField(widget=CKEditorWidget( config_name='minimal')) description = forms.CharField(required=False, widget=CKEditorWidget(config_name='default')) role = forms.ChoiceField( required=True, widget=forms.Select(attrs={'class': 'form-control'}), choices=Job.ROLE_CHOICES) position = forms.ChoiceField( widget=forms.Select(attrs={'class': 'form-control'}), choices=Job.POSITION_CHOICES) salary_min = forms.IntegerField(required=False, min_value=0, max_value=1000000000) salary_max = forms.IntegerField(required=False, min_value=0, max_value=1000000000) equity_min = forms.DecimalField(required=False, min_value=0, max_value=1000000000, max_digits=10, decimal_places=2) equity_max = forms.DecimalField(required=False, min_value=0, max_value=1000000000, max_digits=10, decimal_places=2) remote = forms.ChoiceField( widget=forms.RadioSelect(attrs={'id': 'id_remote'}), choices=Job.REMOTE_CHOICES) years_of_experience = forms.IntegerField(required=False) country = forms.ModelChoiceField( required=True, queryset=Country.objects.all(), widget=forms.Select(attrs={'class': 'form-control'})) location = forms.CharField(required=False, max_length=255, widget=forms.TextInput()) skills = TagField(required=False, widget=TagAutocompleteTagIt(max_tags=False)) status = forms.ChoiceField( widget=forms.RadioSelect(attrs={'id': 'id_status'}), choices=Job.STATUS_CHOICES, initial=STATUS_PUBLISHED) organization = forms.ModelChoiceField( required=False, queryset=Organization.objects.none(), widget=forms.Select(attrs={'class': 'form-control'}))
class AccountEditForm(PermalinkForm): GENDER_CHOICES = ( ('M', 'Male'), ('F', 'Female'), ) image = files_widget.forms.FilesFormField( required=False, fields=( forms.CharField(required=False), forms.CharField(required=False), forms.CharField(required=False), ), widget=files_widget.forms.widgets.ImageWidget()) username = forms.CharField(max_length=30) email = forms.EmailField(max_length=75) gender = forms.ChoiceField( required=False, widget=forms.RadioSelect(attrs={'id': 'id_gender'}), choices=GENDER_CHOICES) password = forms.CharField(required=False, max_length=128, widget=forms.PasswordInput()) password2 = forms.CharField(required=False, max_length=128, widget=forms.PasswordInput()) first_name = forms.CharField(required=True, max_length=30, widget=forms.TextInput()) last_name = forms.CharField(required=True, max_length=30, widget=forms.TextInput()) skills = TagField(required=False, widget=TagAutocompleteTagIt(max_tags=False), help_text='') occupation = forms.CharField(required=False, max_length=128, widget=forms.TextInput()) summary = forms.CharField( required=False, widget=forms.Textarea(attrs={ 'rows': 2, 'maxlength': SUMMARY_MAX_LENGTH })) description = forms.CharField(required=False, widget=CKEditorWidget(config_name='minimal')) homepage_url = forms.CharField(required=False, max_length=255, widget=forms.TextInput()) # Notification # User relation notification_allow_email_send_organizationhaspeople = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={ 'id': 'notification_allow_email_send_organizationhaspeople' })) notification_allow_email_send_partysupportparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_partysupportparty'})) notification_allow_email_send_partyfollowparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_partyfollowparty'})) notification_allow_email_send_partycontactparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_partycontactparty'})) notification_allow_email_send_partytestifyparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_partytestifyparty'})) notification_allow_email_send_partyinvitetestifyparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={ 'id': 'notification_allow_email_send_partyinvitetestifyparty' })) notification_allow_email_send_partylove = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_partylove'})) # Organization relation notification_allow_email_send_organization_partypartnerparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput(attrs={ 'id': 'notification_allow_email_send_organization_partypartnerparty' })) notification_allow_email_send_organization_userexperienceorganization = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={ 'id': 'notification_allow_email_send_organization_userexperienceorganization' })) notification_allow_email_send_organization_partysupportparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput(attrs={ 'id': 'notification_allow_email_send_organization_partysupportparty' })) notification_allow_email_send_organization_partyfollowparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput(attrs={ 'id': 'notification_allow_email_send_organization_partyfollowparty' })) notification_allow_email_send_organization_partycontactparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput(attrs={ 'id': 'notification_allow_email_send_organization_partycontactparty' })) notification_allow_email_send_organization_partytestifyparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput(attrs={ 'id': 'notification_allow_email_send_organization_partytestifyparty' })) notification_allow_email_send_organization_partyinvitetestifyparty = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={ 'id': 'notification_allow_email_send_organization_partyinvitetestifyparty' })) notification_allow_email_send_organization_partylove = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={ 'id': 'notification_allow_email_send_organization_partylove' })) # Follower notification_allow_email_send_from_follow = forms.NullBooleanField( required=False, widget=forms.CheckboxInput( attrs={'id': 'notification_allow_email_send_from_follow'})) # Taxonomy user_roles = forms.ModelMultipleChoiceField( required=True, queryset=UserRole.objects.all(), widget=forms.CheckboxSelectMultiple(attrs={'id': 'id_user_roles'})) interests = forms.ModelMultipleChoiceField( required=False, queryset=Topic.objects.all().filter(parent=None).order_by('title'), widget=forms.CheckboxSelectMultiple(attrs={'id': 'id_interests'})) country = forms.ModelChoiceField( required=False, queryset=Country.objects.all(), widget=forms.Select(attrs={'class': 'form-control'})) # External facebook_url = forms.URLField(required=False, max_length=255, widget=forms.TextInput()) twitter_url = forms.URLField(required=False, max_length=255, widget=forms.TextInput()) linkedin_url = forms.URLField(required=False, max_length=255, widget=forms.TextInput()) homepage_url = forms.URLField(required=False, max_length=255, widget=forms.TextInput()) # SYSTEM promote = forms.NullBooleanField(widget=forms.CheckboxInput()) organizations = forms.ModelMultipleChoiceField( required=False, queryset=Organization.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( OrganizationAutocomplete, attrs={ 'placeholder': _('Type for search organization by name.'), 'class': 'form-control' })) portfolios = forms.ModelMultipleChoiceField( required=False, queryset=Portfolio.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( PortfolioAutocomplete, attrs={ 'placeholder': _('Type for search portfolios by title'), 'class': 'form-control' })) experiences = forms.ModelMultipleChoiceField( required=False, queryset=UserExperienceOrganization.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( UserExperienceOrganizationAutocomplete)) recipients = forms.ModelMultipleChoiceField( required=False, queryset=Organization.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( OrganizationRecipientAutocomplete, attrs={ 'placeholder': _('Type to search organizations by name.'), 'class': 'form-control' })) gived_fundings = forms.ModelMultipleChoiceField( required=False, queryset=PartyReceivedFundingParty.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( PartyGivedFundingPartyAutocomplete)) invest_recipients = forms.ModelMultipleChoiceField( required=False, queryset=Party.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( OrganizationInvestRecipientAutocomplete, attrs={ 'placeholder': _('Type to search organizations or people by name.'), 'class': 'form-control' })) gived_investings = forms.ModelMultipleChoiceField( required=False, queryset=PartyReceivedInvestingParty.objects.all(), widget=autocomplete_light.MultipleChoiceWidget( PartyGivedInvestingPartyAutocomplete)) PERMALINK_FIELDS = ['username', 'email'] def __init__(self, inst=None, model=None, request_user=None, required_password=False, *args, **kwargs): super(AccountEditForm, self).__init__(inst, model, request_user, *args, **kwargs) self.inst = inst if required_password: self.fields['password'].required = True self.fields['password2'].required = True def clean_password2(self): password = self.cleaned_data.get('password', '') password2 = self.cleaned_data['password2'] if password != password2: raise forms.ValidationError(_('Password mismatch.')) return password2 def clean_organizations(self): organizations = self.cleaned_data.get('organizations') user_roles = self.cleaned_data.get('user_roles') if user_roles and not user_roles.filter( permalink='individual') and not organizations: raise forms.ValidationError(_('This field is required.')) return organizations