def post(self, *args, **kwargs): post_data = self.request.POST form_class = self.get_form_class() center_form = self.get_form(form_class) pk = session_matches_post_result_form(post_data, self.request) result_form = get_object_or_404(ResultForm, pk=pk) form = safe_form_in_state(result_form, FormState.INTAKE, center_form) if form: return self.form_invalid(form) if center_form.is_valid(): station_number = center_form.cleaned_data.get('station_number') center_number = center_form.cleaned_data.get('center_number') center = Center.objects.get(code=center_number) #Checks if center ballot number and form ballot number are the same general_check = center.sub_constituency and\ result_form.ballot.number != center.sub_constituency.code women_check = center.sub_constituency.ballot_women and\ result_form.ballot.number != center.sub_constituency.\ ballot_women.number if general_check is True and women_check is True: form = add_generic_error(center_form, _(u"Ballot number do not match for" u"center and form")) return self.render_to_response(self.get_context_data( form=form, header_text=_('Intake'), result_form=result_form)) duplicated_forms = result_form.get_duplicated_forms(center, station_number) if duplicated_forms: result_form.station_number = station_number result_form.center = center # a form already exists, send to clearance self.request.session['intake-error'] = INTAKEN_MESSAGE result_form.send_to_clearance() for oneDuplicatedForm in duplicated_forms: if oneDuplicatedForm.form_state != FormState.CLEARANCE: oneDuplicatedForm.send_to_clearance() return redirect('intake-clearance') self.request.session['station_number'] = station_number self.request.session['center_number'] = center_number return redirect(self.success_url) else: return self.render_to_response(self.get_context_data( form=center_form, header_text=_('Intake'), result_form=result_form))
def safe_form_in_state(result_form, states, form): """Check form in state and add an error to form if not in the correct state. :param result_form: The result form to check the state of. :param states: The allowable states for the form. :param form: The form to add an error message to if any exist. :returns: Form with an error message or None. """ try: form_in_state(result_form, states) except SuspiciousOperation as e: return add_generic_error(form, str(e))
def check_group_for_state(result_form, user, form): """Ensure only data entry 1 clerk can access forms in data entry 1 state and similarly for data entry 2. Always allow access for the super administrator. :param result_form: The result form to check access to. :param user: The user to check group of. :param form: The Django form to attach an error to. :returns: A form with an error if access denied, else None. """ if groups.SUPER_ADMINISTRATOR in groups.user_groups(user): return None if ((result_form.form_state == FormState.DATA_ENTRY_1 and not user_is_data_entry_1(user)) or (result_form.form_state == FormState.DATA_ENTRY_2 and not user_is_data_entry_2(user))): message = _(u"Return form to %s" % result_form.form_state_name) return add_generic_error(form, message)
def post(self, *args, **kwargs): tally_id = kwargs.get('tally_id') post_data = self.request.POST form_class = self.get_form_class() center_form = self.get_form(form_class) pk = session_matches_post_result_form(post_data, self.request) result_form = get_object_or_404(ResultForm, pk=pk, tally__id=tally_id) form = safe_form_in_state(result_form, FormState.INTAKE, center_form) if form: return self.form_invalid(form) if center_form.is_valid(): station_number = center_form.cleaned_data.get('station_number') center_number = center_form.cleaned_data.get('center_number') center = Center.objects.get(code=center_number, tally__id=tally_id) # Checks if center ballot number and form ballot number are the # same is_error = False center_sub = center.sub_constituency if center_sub: is_general = result_form.ballot.number == \ center.sub_constituency.code if not is_general: is_women = center_sub.ballot_women is not None if not is_women or (is_women and result_form.ballot.number != center_sub.ballot_women.number): is_error = True if is_error: form = add_generic_error( center_form, _(u"Ballot number do not match for" u"center and form")) return self.render_to_response( self.get_context_data(form=form, header_text=_('Intake'), result_form=result_form, tally_id=tally_id)) duplicated_forms = result_form.get_duplicated_forms( center, station_number) if duplicated_forms: result_form.station_number = station_number result_form.center = center # a form already exists, send to clearance self.request.session[ 'intake-error'] = INTAKE_DUPLICATE_ERROR_MESSAGE if result_form.form_state != FormState.CLEARANCE: result_form.send_to_clearance() for form in duplicated_forms: if form.form_state != FormState.CLEARANCE: form.send_to_clearance() return redirect('intake-clearance', tally_id=tally_id) self.request.session['station_number'] = station_number self.request.session['center_number'] = center_number return redirect(self.success_url, tally_id=tally_id) else: return self.render_to_response( self.get_context_data(form=center_form, header_text=_('Intake'), result_form=result_form))
def post(self, *args, **kwargs): tally_id = kwargs.get('tally_id') post_data = self.request.POST form_class = self.get_form_class() center_form = self.get_form(form_class) pk = session_matches_post_result_form(post_data, self.request) result_form = get_object_or_404(ResultForm, pk=pk, tally__id=tally_id) form = safe_form_in_state(result_form, FormState.INTAKE, center_form) if form: return self.form_invalid(form) if center_form.is_valid(): station_number = center_form.cleaned_data.get('station_number') center_number = center_form.cleaned_data.get('center_number') center = Center.objects.get(code=center_number, tally__id=tally_id) # Checks if center ballot number and form ballot number are the # same is_error = False center_sub = center.sub_constituency if center_sub: is_general = result_form.ballot.number == \ center.sub_constituency.code if not is_general: is_women = center_sub.ballot_women is not None if not is_women or ( is_women and result_form.ballot.number != center_sub.ballot_women.number): is_error = True if is_error: form = add_generic_error(center_form, _(u"Ballot number do not match for" u"center and form")) return self.render_to_response(self.get_context_data( form=form, header_text=_('Intake'), result_form=result_form, tally_id=tally_id)) duplicated_forms = result_form.get_duplicated_forms(center, station_number) if duplicated_forms: result_form.station_number = station_number result_form.center = center # a form already exists, send to clearance self.request.session[ 'intake-error'] = INTAKE_DUPLICATE_ERROR_MESSAGE if result_form.form_state != FormState.CLEARANCE: result_form.send_to_clearance() for form in duplicated_forms: if form.form_state != FormState.CLEARANCE: form.send_to_clearance() return redirect('intake-clearance', tally_id=tally_id) self.request.session['station_number'] = station_number self.request.session['center_number'] = center_number return redirect(self.success_url, tally_id=tally_id) else: return self.render_to_response(self.get_context_data( form=center_form, header_text=_('Intake'), result_form=result_form))