def get_form(self): if self.request.method == 'GET': display_data = self.session_data.copy() display_data['date_received'] = timezone.now() return DeclarationLetterDisplay(display_data, validate=True) elif self.request.method in ('POST', 'PUT'): return DeclarationLetterReviewForm(data=self.request.POST)
def get_display_form_for_user(self, user): """ based on user information, get the correct Form class and return it instantiated with the data for self """ # TODO: get rid of this method, and put it elsewhere and make it right if not user.is_staff: DisplayFormClass = user.profile.get_submission_display_form() else: DisplayFormClass = display_form_selector.get_combined_form_class( counties=[ intake.constants.Counties.SAN_FRANCISCO, intake.constants.Counties.CONTRA_COSTA, intake.constants.Counties.ALAMEDA, intake.constants.Counties.MONTEREY, intake.constants.Counties.SOLANO, intake.constants.Counties.SAN_DIEGO, intake.constants.Counties.SAN_JOAQUIN, intake.constants.Counties.SANTA_CLARA, intake.constants.Counties.FRESNO, ]) init_data = dict( date_received=self.get_local_date_received(), counties=list(self.get_counties().values_list('slug', flat=True)), organizations=list( self.organizations.values_list('name', flat=True)) ) init_data.update(self.answers) for key, value in init_data.items(): if isinstance(value, str): init_data[key] = mark_safe(value) display_form = DisplayFormClass(init_data, validate=True) display_form.display_only = True display_form.display_template_name = "formation/intake_display.jinja" display_form.submission = self show_declaration = any(self.organizations.all().values_list( 'requires_declaration_letter', flat=True)) if show_declaration: declaration_letter_form = DeclarationLetterDisplay( init_data, validate=True) return display_form, declaration_letter_form return display_form, None
def instantiate_display_form_with_submission(form_class, submission): """Feed a submission and its associated data into a display form class """ # one query to get all related objects orgs = list(submission.organizations.select_related('county')) county_slugs = [org.county.slug for org in orgs] init_data = dict(date_received=submission.get_local_date_received(), counties=county_slugs, organizations=[org.name for org in orgs]) init_data.update(submission.answers) display_form = form_class(init_data, validate=True) display_form.display_only = True display_form.display_template_name = "formation/intake_display.jinja" display_form.submission = submission show_declaration = any(map(lambda o: o.requires_declaration_letter, orgs)) if show_declaration: declaration_letter_form = DeclarationLetterDisplay(init_data, validate=True) return display_form, declaration_letter_form return display_form, None