def clean(self): u"""General clean routine of this form. Its main task is to detect missing or wrongly-filled fields according to the selected reference type. The RIS specification contains (often odd) rules about which fields are required, and which fields must not be filled for a given reference type. This is enforced here, among minor other checks. :Return: the cleaned data :rtype: dict mapping str to ``object`` """ cleaned_data = self.cleaned_data if cleaned_data["endpage"] and not cleaned_data["startpage"]: self._errors["endpage"] = ErrorList([ _(u"You must not give an end page if there is no start page.") ]) del cleaned_data["endpage"] if cleaned_data[ "reference_type"] != "GEN" and "date" in cleaned_data and not cleaned_data[ "date"]: chantal_utils.append_error( self, _(u"This field is required for this reference type."), "date") del cleaned_data["date"] self._forbid_field("part_title", utils.reference_types_without_part) self._forbid_field("part_authors", utils.reference_types_without_part) if cleaned_data["reference_type"] in ["ABST", "INPR", "JOUR", "JFULL", "MGZN", "NEWS"] \ and not (cleaned_data["publication_title"] or cleaned_data["publication_title_abbrev"]): self._errors["publication_title"] = \ ErrorList([_(u"The publication title (or its abbreviation) is required for this reference type.")]) del cleaned_data["publication_title"] del cleaned_data["publication_title_abbrev"] self._forbid_field("volume", [ "ART", "CTLG", "COMP", "INPR", "ICOMM", "MAP", "MPCT", "PAMP", "PCOMM", "SLIDE", "SOUND", "UNPB", "VIDEO" ]) self._forbid_field("issue", [ "ART", "ADVS", "BILL", "BOOK", "CONF", "ELEC", "HEAR", "ICOMM", "MPCT", "MUSIC", "PAMP", "PCOMM", "RPRT", "SER", "SLIDE", "SOUND", "STAT", "UNBILL", "UNPB", "VIDEO" ]) self._forbid_field("startpage", [ "ART", "CTLG", "COMP", "HEAR", "INPR", "ICOMM", "MAP", "MPCT", "MUSIC", "PAMP", "PCOMM", "SLIDE", "SOUND", "THES", "UNBILL", "UNPB", "VIDEO" ]) self._forbid_field("endpage", [ "ART", "CTLG", "COMP", "HEAR", "INPR", "ICOMM", "MAP", "MPCT", "MUSIC", "PAMP", "PCOMM", "SLIDE", "SOUND", "UNBILL", "UNPB", "VIDEO" ]) self._forbid_field("publisher", [ "BILL", "ICOMM", "PAT", "PCOMM", "SLIDE", "STAT", "UNBILL", "UNPB" ]) self._forbid_field("city", [ "BILL", "ELEC", "HEAR", "ICOMM", "PCOMM", "SLIDE", "STAT", "UNBILL", "UNPB" ]) return cleaned_data
def is_referentially_valid(export_form, add_to_shelf_form, add_to_list_form, remove_from_list_form, selection_box_forms, global_dummy_form, references_list): u"""Test whether all forms are consistent with each other. In particular, the user must use exactly one of the given forms. He must not try to export references and add them to a shelf at the same time. :Parameters: - `export_form`: bound form for exporting references - `add_to_shelf_form`: bound form for adding references to a shelf - `add_to_list_form`: bound form for adding references to a references list - `remove_from_list_form`: bound form for removing references form a references list; may be ``None`` if the search is not limited to a particular references list - `selection_box_forms`: bound forms with the selected samples - `global_dummy_form`: bound form which contains global error messages which occur here - `references_list`: the references list the bulk view is limited to :type export_form: `ExportForm` :type add_to_shelf_form: `AddToShelfForm` :type add_to_list_form: `AddToListForm` :type remove_from_list_form: `RemoveFromListForm` or ``NoneType`` :type selection_box_forms: list of `SelectionBoxForm` :type global_dummy_form: ``django.forms.Form`` :type references_list: unicode :Return: whether all forms are consistent and obey to the constraints :rtype: bool """ referentially_valid = True action = None actions = [] if export_form.is_valid() and export_form.cleaned_data["format"]: actions.append("export") if add_to_shelf_form.is_valid() and add_to_shelf_form.cleaned_data["new_shelf"]: actions.append("shelf") if add_to_list_form.is_valid() and ( add_to_list_form.cleaned_data["existing_list"] or add_to_list_form.cleaned_data["new_list"]): actions.append("list") if references_list and remove_from_list_form.is_valid() and remove_from_list_form.cleaned_data["remove"]: actions.append("remove") if not actions: referentially_valid = False if export_form.is_valid() and add_to_shelf_form.is_valid() and add_to_list_form.is_valid() and \ (not references_list or remove_from_list_form.is_valid()): chantal_utils.append_error(global_dummy_form, _(u"You must select an action.")) elif len(actions) > 1: chantal_utils.append_error(global_dummy_form, _(u"You can't do more that one thing at the same time.")) referentially_valid = False else: action = actions[0] if not any(selection_box_form.is_valid() and selection_box_form.cleaned_data["selected"] for selection_box_form in selection_box_forms): chantal_utils.append_error(global_dummy_form, _(u"You must select at least one sample.")) referentially_valid = False return referentially_valid, action
def clean(self): u"""Class clean method which assures that at most one of the fields is given. Additionally, it checks that the name for a new list doesn't already exist in the database. """ _ = ugettext cleaned_data = self.cleaned_data if cleaned_data["existing_list"] and cleaned_data["new_list"]: chantal_utils.append_error( self, _(u"You must not give both an existing and a new list."), "new_list") del cleaned_data["new_list"], cleaned_data["existing_list"] elif not self.optional and not cleaned_data[ "existing_list"] and not cleaned_data["new_list"]: chantal_utils.append_error( self, _(u"You must give either an existing or a new list."), "new_list") del cleaned_data["new_list"], cleaned_data["existing_list"] elif cleaned_data["new_list"] and cleaned_data[ "new_list"] in self.short_listnames: chantal_utils.append_error(self, _(u"This listname is already given."), "new_list") del cleaned_data["new_list"] return cleaned_data
def clean(self): u"""General clean routine of this form. Its main task is to detect missing or wrongly-filled fields according to the selected reference type. The RIS specification contains (often odd) rules about which fields are required, and which fields must not be filled for a given reference type. This is enforced here, among minor other checks. :Return: the cleaned data :rtype: dict mapping str to ``object`` """ cleaned_data = self.cleaned_data if cleaned_data["endpage"] and not cleaned_data["startpage"]: self._errors["endpage"] = ErrorList([_(u"You must not give an end page if there is no start page.")]) del cleaned_data["endpage"] if cleaned_data["reference_type"] != "GEN" and "date" in cleaned_data and not cleaned_data["date"]: chantal_utils.append_error(self, _(u"This field is required for this reference type."), "date") del cleaned_data["date"] self._forbid_field("part_title", utils.reference_types_without_part) self._forbid_field("part_authors", utils.reference_types_without_part) if cleaned_data["reference_type"] in ["ABST", "INPR", "JOUR", "JFULL", "MGZN", "NEWS"] \ and not (cleaned_data["publication_title"] or cleaned_data["publication_title_abbrev"]): self._errors["publication_title"] = \ ErrorList([_(u"The publication title (or its abbreviation) is required for this reference type.")]) del cleaned_data["publication_title"] del cleaned_data["publication_title_abbrev"] self._forbid_field("volume", ["ART", "CTLG", "COMP", "INPR", "ICOMM", "MAP", "MPCT", "PAMP", "PCOMM", "SLIDE", "SOUND", "UNPB", "VIDEO"]) self._forbid_field("issue", ["ART", "ADVS", "BILL", "BOOK", "CONF", "ELEC", "HEAR", "ICOMM", "MPCT", "MUSIC", "PAMP", "PCOMM", "RPRT", "SER", "SLIDE", "SOUND", "STAT", "UNBILL", "UNPB", "VIDEO"]) self._forbid_field("startpage", ["ART", "CTLG", "COMP", "HEAR", "INPR", "ICOMM", "MAP", "MPCT", "MUSIC", "PAMP", "PCOMM", "SLIDE", "SOUND", "THES", "UNBILL", "UNPB", "VIDEO"]) self._forbid_field("endpage", ["ART", "CTLG", "COMP", "HEAR", "INPR", "ICOMM", "MAP", "MPCT", "MUSIC", "PAMP", "PCOMM", "SLIDE", "SOUND", "UNBILL", "UNPB", "VIDEO"]) self._forbid_field("publisher", ["BILL", "ICOMM", "PAT", "PCOMM", "SLIDE", "STAT", "UNBILL", "UNPB"]) self._forbid_field("city", ["BILL", "ELEC", "HEAR", "ICOMM", "PCOMM", "SLIDE", "STAT", "UNBILL", "UNPB"]) return cleaned_data
def clean(self): u"""Class clean method which assures that at most one of the fields is given. Additionally, it checks that the name for a new list doesn't already exist in the database. """ _ = ugettext cleaned_data = self.cleaned_data if cleaned_data["existing_list"] and cleaned_data["new_list"]: chantal_utils.append_error(self, _(u"You must not give both an existing and a new list."), "new_list") del cleaned_data["new_list"], cleaned_data["existing_list"] elif not self.optional and not cleaned_data["existing_list"] and not cleaned_data["new_list"]: chantal_utils.append_error(self, _(u"You must give either an existing or a new list."), "new_list") del cleaned_data["new_list"], cleaned_data["existing_list"] elif cleaned_data["new_list"] and cleaned_data["new_list"] in self.short_listnames: chantal_utils.append_error(self, _(u"This listname is already given."), "new_list") del cleaned_data["new_list"] return cleaned_data
def is_referentially_valid(export_form, add_to_shelf_form, add_to_list_form, remove_from_list_form, selection_box_forms, global_dummy_form, references_list): u"""Test whether all forms are consistent with each other. In particular, the user must use exactly one of the given forms. He must not try to export references and add them to a shelf at the same time. :Parameters: - `export_form`: bound form for exporting references - `add_to_shelf_form`: bound form for adding references to a shelf - `add_to_list_form`: bound form for adding references to a references list - `remove_from_list_form`: bound form for removing references form a references list; may be ``None`` if the search is not limited to a particular references list - `selection_box_forms`: bound forms with the selected samples - `global_dummy_form`: bound form which contains global error messages which occur here - `references_list`: the references list the bulk view is limited to :type export_form: `ExportForm` :type add_to_shelf_form: `AddToShelfForm` :type add_to_list_form: `AddToListForm` :type remove_from_list_form: `RemoveFromListForm` or ``NoneType`` :type selection_box_forms: list of `SelectionBoxForm` :type global_dummy_form: ``django.forms.Form`` :type references_list: unicode :Return: whether all forms are consistent and obey to the constraints :rtype: bool """ referentially_valid = True action = None actions = [] if export_form.is_valid() and export_form.cleaned_data["format"]: actions.append("export") if add_to_shelf_form.is_valid( ) and add_to_shelf_form.cleaned_data["new_shelf"]: actions.append("shelf") if add_to_list_form.is_valid() and ( add_to_list_form.cleaned_data["existing_list"] or add_to_list_form.cleaned_data["new_list"]): actions.append("list") if references_list and remove_from_list_form.is_valid( ) and remove_from_list_form.cleaned_data["remove"]: actions.append("remove") if not actions: referentially_valid = False if export_form.is_valid() and add_to_shelf_form.is_valid() and add_to_list_form.is_valid() and \ (not references_list or remove_from_list_form.is_valid()): chantal_utils.append_error(global_dummy_form, _(u"You must select an action.")) elif len(actions) > 1: chantal_utils.append_error( global_dummy_form, _(u"You can't do more that one thing at the same time.")) referentially_valid = False else: action = actions[0] if not any(selection_box_form.is_valid() and selection_box_form.cleaned_data["selected"] for selection_box_form in selection_box_forms): chantal_utils.append_error(global_dummy_form, _(u"You must select at least one sample.")) referentially_valid = False return referentially_valid, action