class CustomDataPullForm(forms.Form): data_pull = forms.ChoiceField( label=ugettext_lazy("Data Pull"), choices=((pull.slug, pull.name) for pull in CUSTOM_DATA_PULLS.values())) month = forms.DateField(required=True, widget=forms.DateInput()) location_id = forms.CharField(label=ugettext_lazy("Location"), widget=Select(choices=[]), required=False) def __init__(self, request, domain, *args, **kwargs): self.domain = domain super(CustomDataPullForm, self).__init__(*args, **kwargs) self.helper = HQFormHelper() self.helper.layout = crispy.Layout( crispy.Field('data_pull'), crispy.Field('month', id="month_select", css_class="date-picker"), crispy.Field('location_id', id='location_search_select'), hqcrispy.FormActions( crispy.ButtonHolder(Submit('submit', ugettext_lazy("Submit"))))) def clean_month(self): month = self.cleaned_data['month'] if month and month.day != 1: self.add_error("month", "Only first of month should be selected") month = month.strftime('%Y-%m-%d') return month def submit(self, domain, email): run_data_pull.delay(self.cleaned_data['data_pull'], domain, self.cleaned_data['month'], self.cleaned_data['location_id'], email)
def __init__(self, slug_or_file, db_alias, month, location_id): """ run data export by either passing slug to a custom data pull or file name/path to a sql file which will be read as a single query """ if month: # convert to string if date object received month = str(month) self.slug_or_file = slug_or_file self.month = month self.location_id = location_id data_pull_class = CUSTOM_DATA_PULLS.get(self.slug_or_file, DirectDataPull) self.data_pull_obj = data_pull_class(db_alias, query_file_path=self.slug_or_file, month=self.month, location_id=self.location_id)
class CustomDataPullForm(forms.Form): data_pull = forms.ChoiceField( label=ugettext_lazy("Data Pull"), choices=((pull.slug, pull.name) for pull in CUSTOM_DATA_PULLS.values())) month = forms.DateField(required=True, widget=forms.DateInput()) location_id = forms.CharField(label=ugettext_lazy("Location"), widget=Select(choices=[]), required=False) def __init__(self, request, domain, *args, **kwargs): self.domain = domain super(CustomDataPullForm, self).__init__(*args, **kwargs) self.helper = HQFormHelper() self.helper.layout = crispy.Layout( crispy.Field('data_pull'), crispy.Field('month', id="month_select", css_class="date-picker"), crispy.Field('location_id', id='location_search_select'), hqcrispy.FormActions( crispy.ButtonHolder(Submit('submit', ugettext_lazy("Submit"))))) def clean_month(self): month = self.cleaned_data['month'] if month and month.day != 1: self.add_error("month", "Only first of month should be selected") return month def clean_location_id(self): location_id_slug = self.cleaned_data['location_id'] if location_id_slug: return self._extract_location_id(location_id_slug) return location_id_slug @staticmethod def _extract_location_id(location_id_slug): from corehq.apps.reports.filters.users import ExpandedMobileWorkerFilter selected_ids = ExpandedMobileWorkerFilter.selected_location_ids( [location_id_slug]) return selected_ids[0] if selected_ids else None def submit(self, domain, email): run_data_pull.delay(self.cleaned_data['data_pull'], domain, self.cleaned_data['month'], self.cleaned_data['location_id'], email)