def __init__(self, user, data, *args, **kwargs): super(CreateBenchmarkStep12Form, self).__init__(data=data, *args, **kwargs) regions = [('', 'All')] regions.extend(list(Region.regions.values_list('id', 'name').order_by('name'))) self.fields['geo'].choices = regions proposed_industry = LinkedInIndustry.get_proposal(user.contacts) industry = [('', 'All')] if proposed_industry: industry.extend(list(proposed_industry)) else: industry.extend(list(LinkedInIndustry.objects.values_list('code', 'name'))) self.fields['industry'].choices = industry units = data and data.get('0-units') or kwargs.get('initial', {}).get('units') if units and not units in [v for v, _ in QuestionOptions.UNITS]: choices = list(QuestionOptions.UNITS) choices.insert(0, (units, units)) choices = tuple(choices) self.fields['units'] = forms.ChoiceField(initial=units, choices=choices) question_type = int(data and data.get(kwargs['prefix'] + '-question_type', '1') or '1') if question_type == Question.MULTIPLE or question_type == Question.RANKING: self.fields['units'].required = False elif question_type == Question.NUMERIC or question_type == Question.RANGE: self.fields['answer_options'].required = False self.fields['answer_options'].validators = [] elif question_type == Question.YES_NO: self.fields['answer_options'].required = False self.fields['units'].required = False self.fields['answer_options'].validators = []
def __init__(self, user, step0data, wizard, except_ids=set(), benchmark=None, *args, **kwargs): super(CreateBenchmarkStep3Form, self).__init__(*args, **kwargs) self.user = user self.benchmark = benchmark if not step0data: self.min_number_of_answers = 5 self.fields['geo'].initial = u'' else: self.min_number_of_answers = self.num(step0data.get('0-minimum_number_of_answers')) self.fields['geo'].initial = step0data.get('0-geo') data = kwargs.get('data') or {} self.is_continue = data.get('is_continue', False) and not ( data.get('add_selected') or data.get('save_and_wizard_goto_step')) regions = [('', 'All')] regions.extend(list(Region.regions.values_list('id', 'name').order_by('name'))) self.fields['geo'].choices = regions industries = list(LinkedInIndustry.get_proposal(user.contacts)) self.fields['industry'].choices = industries cleaned_data = { 'geo': data.get('%s-%s' % (self.prefix, 'geo')) if data else self.fields['geo'].initial, 'industry': data.getlist('%s-%s' % (self.prefix, 'industry')) if data else self.fields['industry'].initial, 'name': data.get('%s-%s' % (self.prefix, 'name')), 'role': data.get('%s-%s' % (self.prefix, 'role')), } contact_filter = {} name_filter = None if cleaned_data.get('name'): search_query = cleaned_data.get('name').split() if len(search_query) > 1: name_filter = Q(last_name__istartswith=search_query[0], first_name__istartswith=search_query[1]) | \ Q(last_name__istartswith=search_query[1], first_name__istartswith=search_query[0]) else: name_filter = Q(last_name__istartswith=cleaned_data.get('name')) | \ Q(first_name__istartswith=cleaned_data.get('name')) if cleaned_data.get('role'): contact_filter['headline__icontains'] = cleaned_data.get('role') if cleaned_data.get('industry'): contact_filter['company___industry__code__in'] = cleaned_data.get('industry') if cleaned_data.get('geo'): contact_filter['location__parent__id'] = cleaned_data.get('geo') self.contacts_filtered = user.contacts.filter(name_filter, **contact_filter).exclude(id__in=except_ids) \ .order_by('last_name').select_related('user', 'company', 'company___industry', 'location') if name_filter else \ user.contacts.filter(**contact_filter).exclude(id__in=except_ids).order_by('last_name')\ .select_related('user', 'company', 'company___industry', 'location') for contact in self.contacts_filtered: self.fields['contact-{0}-invite'.format(contact.id)] = \ forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'choose-checkbox'}), required=False) contact.invite_element = 'contact-{0}-invite'.format(contact.id) self.fields['contact-{0}-secondary'.format(contact.id)] = \ forms.BooleanField(widget=forms.CheckboxInput(attrs={'class': 'share-checkbox'}), required=False) contact.secondary_element = 'contact-{0}-secondary'.format(contact.id) if not data: data = self.fields self.add_suggested_contacts(cleaned_data.get('geo'), cleaned_data.get('industry'), user, except_ids) self.selected_contacts = self.add_selected_contacts(data, except_ids) wizard.selected_contacts = self.selected_contacts wizard.end_date = self.end_date
def industry(self, value): self._industry = LinkedInIndustry.get(code=value) if str(value).isdigit() else LinkedInIndustry.get(value)