class EditTopicForm(forms.Form): """Form for the member list of a topic. Note that it is allowed to have no members at all in a topic. However, if the topic is confidential, the currently logged-in user must remain a member of the topic. """ members = MultipleUsersField(label=_("Members"), required=False) confidential = forms.BooleanField(label=_("confidential"), required=False) topic_manager = UserField(label=capfirst(_("topic manager"))) def __init__(self, user, topic, *args, **kwargs): super(EditTopicForm, self).__init__(*args, **kwargs) self.fields["members"].set_users(user, topic.members.all()) self.fields["members"].widget.attrs["size"] = 30 self.fields["confidential"].initial = topic.confidential self.user = user self.topic = topic self.fields["topic_manager"].set_users(user, topic.manager) def clean(self): cleaned_data = super(EditTopicForm, self).clean() if "members" in cleaned_data and "confidential" in cleaned_data: if cleaned_data["confidential"] and \ not any(permissions.has_permission_to_edit_topic(user, self.topic) for user in cleaned_data["members"]): self.add_error("members", _("In confidential topics, at least one member must have permission to edit the topic.")) return cleaned_data
class UserListForm(forms.Form): """Form class for selecting the user to change the permissions for him/her. """ selected_user = UserField(label=_("Change the permissions of")) def __init__(self, user, *args, **kwargs): super(UserListForm, self).__init__(*args, **kwargs) self.fields["selected_user"].set_users(user)
class SampleSeriesForm(forms.ModelForm): """Form for editing and creating sample series. """ short_name = forms.CharField(label=capfirst(_("name")), max_length=50) currently_responsible_person = UserField(label=capfirst(_("currently responsible person"))) topic = TopicField(label=capfirst(_("topic"))) samples = utils.MultipleSamplesField(label=capfirst(_("samples"))) class Meta: model = models.SampleSeries exclude = ("timestamp", "results", "name", "id") def __init__(self, user, data=None, **kwargs): """I have to initialise the form here, especially because the sample set to choose from must be found and the name of an existing series must not be changed. """ super(SampleSeriesForm, self).__init__(data, **kwargs) sample_series = kwargs.get("instance") samples = user.my_samples.all() important_samples = sample_series.samples.all() if sample_series else set() self.fields["samples"].set_samples(user, samples, important_samples) self.fields["samples"].widget.attrs.update({"size": "15", "style": "vertical-align: top"}) self.fields["short_name"].widget.attrs.update({"size": "50"}) if sample_series: self.fields["short_name"].required = False if sample_series: self.fields["currently_responsible_person"].set_users(user, sample_series.currently_responsible_person) else: self.fields["currently_responsible_person"].choices = ((user.pk, six.text_type(user)),) self.fields["topic"].set_topics(user, sample_series.topic if sample_series else None) def clean_short_name(self): """Prevents users from just adding whitespaces. """ short_name = self.cleaned_data["short_name"].strip() if not short_name and self.fields["short_name"].required: raise ValidationError(_("This field is required."), code="required") return short_name def clean_description(self): """Forbid image and headings syntax in Markdown markup. """ description = self.cleaned_data["description"] jb_common.utils.base.check_markdown(description) return description def validate_unique(self): """Overridden to disable Django's intrinsic test for uniqueness. I simply disable this inherited method completely because I do my own uniqueness test in the create view itself. I cannot use Django's built-in test anyway because it leads to an error message in wrong German (difficult to fix, even for the Django guys). """ pass
class NewTopicForm(forms.Form): """Form for adding a new topic. I need only its new name and restriction status. """ new_topic_name = forms.CharField(label=_("Name of new topic"), max_length=80) # Translators: Topic which is not open to senior members confidential = forms.BooleanField(label=_("confidential"), required=False) parent_topic = forms.ChoiceField(label=_("Upper topic"), required=False) topic_manager = UserField(label=capfirst(_("topic manager"))) def __init__(self, user, *args, **kwargs): super(NewTopicForm, self).__init__(*args, **kwargs) self.fields["new_topic_name"].widget.attrs["size"] = 40 self.user = user if user.is_superuser: self.fields["parent_topic"].choices = [(topic.pk, topic) for topic in Topic.objects.iterator()] else: self.fields["parent_topic"].choices = [(topic.pk, topic.get_name_for_user(user)) for topic in Topic.objects.filter(department=user.jb_user_details.department).iterator() if permissions.has_permission_to_edit_topic(user, topic)] self.fields["parent_topic"].choices.insert(0, ("", 9 * "-")) self.fields["topic_manager"].set_users(user, user) self.fields["topic_manager"].initial = user.pk def clean_new_topic_name(self): topic_name = self.cleaned_data["new_topic_name"] topic_name = " ".join(topic_name.split()) return topic_name def clean_parent_topic(self): pk = self.cleaned_data.get("parent_topic") if pk: parent_topic = Topic.objects.get(pk=int(pk)) if not permissions.has_permission_to_edit_topic(self.user, parent_topic): raise ValidationError(_("You are not allowed to edit the topic “{parent_topic}”.").\ format(parent_topic=parent_topic.name)) return parent_topic elif not permissions.has_permission_to_edit_topic(self.user): raise ValidationError(_("You are only allowed to create sub topics. You have to select an upper topic.")) def clean(self): cleaned_data = super(NewTopicForm, self).clean() if "new_topic_name" in cleaned_data: topic_name = cleaned_data["new_topic_name"] parent_topic = self.cleaned_data.get("parent_topic", None) if Topic.objects.filter(name=topic_name, department=self.user.jb_user_details.department, parent_topic=parent_topic).exists(): self.add_error("new_topic_name", _("This topic name is already used.")) if parent_topic and parent_topic.manager != cleaned_data.get("topic_manager"): self.add_error("topic_manager", _("The topic manager must be the topic manager from the upper topic.")) return cleaned_data
class ActionForm(forms.Form): """Form for all the things you can do with the selected samples. """ new_currently_responsible_person = UserField( label=_("New currently responsible person"), required=False) new_topic = TopicField(label=capfirst(_("new topic")), required=False) new_current_location = forms.CharField(label=_("New current location"), required=False, max_length=50) new_tags = forms.CharField( label=_("New sample tags"), required=False, max_length=255, help_text=_("separated with commas, no whitespace")) copy_to_user = MultipleUsersField(label=_("Copy to user"), required=False) clearance = forms.ChoiceField(label=capfirst(_("clearance")), required=False) comment = forms.CharField(label=_("Comment for recipient"), widget=forms.Textarea, required=False) remove_from_my_samples = forms.BooleanField( label=_("Remove from “My Samples”"), required=False) def __init__(self, user, *args, **kwargs): """ :param user: the user whose “My Samples” list should be generated :type user: django.contrib.auth.models.User """ super(ActionForm, self).__init__(*args, **kwargs) self.fields["new_currently_responsible_person"].set_users(user, user) self.fields["copy_to_user"].set_users(user) try: self.fields["copy_to_user"].choices.remove( (user.id, get_really_full_name(user))) except ValueError: pass self.fields["new_topic"].set_topics(user) self.fields["clearance"].choices = [("", "---------"), ("0", _("sample only")), ("1", _("all processes up to now")) ] self.clearance_choices = {"": None, "0": (), "1": "all"} def clean_comment(self): """Forbid image and headings syntax in Markdown markup. """ comment = self.cleaned_data["comment"] check_markdown(comment) return comment def clean_clearance(self): return self.clearance_choices[self.cleaned_data["clearance"]] def clean_new_tags(self): tags = self.cleaned_data.get("new_tags", "") if tags: tags = re.sub(r"\s", "", tags).strip(",") return tags def clean(self): cleaned_data = super(ActionForm, self).clean() if cleaned_data["copy_to_user"]: if not cleaned_data["comment"]: self.add_error( "comment", ValidationError(_( "If you copy samples over to another person, you must enter a short comment." ), code="required")) if cleaned_data["clearance"] is not None and not cleaned_data.get( "copy_to_user"): self.add_error( "copy_to_user", ValidationError(_( "If you set a clearance, you must copy samples to another user." ), code="required")) del cleaned_data["clearance"] if (cleaned_data["new_currently_responsible_person"] or cleaned_data["new_topic"] or cleaned_data["new_current_location"] ) and not cleaned_data.get("comment"): raise ValidationError( _("If you edit samples, you must enter a short comment."), code="invalid") return cleaned_data