class StudentsGroupForm(forms.ModelForm): subject = None participants = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(StudentsGroupForm, self).__init__(*args, **kwargs) self.subject = kwargs["initial"].get("subject", None) if self.instance.id: self.subject = self.instance.subject self.fields["participants"].queryset = self.subject.students.all() def clean_name(self): name = self.cleaned_data.get("name", "") if self.instance.id: same_name = (self.subject.group_subject.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count()) else: same_name = self.subject.group_subject.filter( name__unaccent__iexact=name).count() if same_name > 0: self._errors["name"] = [ _("This subject already has a group with this name") ] return ValueError return name class Meta: model = StudentsGroup fields = ["name", "description", "participants"] widgets = { "description": forms.Textarea, "participants": forms.SelectMultiple, }
class FileLinkForm(forms.ModelForm): subject = None MAX_UPLOAD_SIZE = 10*1024*1024 students = ParticipantsMultipleChoiceField(queryset = None, required = False) def __init__(self, *args, **kwargs): super(FileLinkForm, self).__init__(*args, **kwargs) self.subject = kwargs['initial'].get('subject', None) if self.instance.id: self.subject = self.instance.topic.subject self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) self.fields['students'].queryset = self.subject.students.all() self.fields['groups'].queryset = self.subject.group_subject.all() tags = forms.CharField(label = _('Tags'), required = False) class Meta: model = FileLink fields = ['name', 'file_content', 'brief_description', 'all_students', 'students', 'groups', 'visible'] labels = { 'name': _('File name'), } widgets = { 'brief_description': forms.Textarea, 'students': forms.SelectMultiple, 'groups': forms.SelectMultiple, 'file_content': ResubmitFileWidget(attrs={'accept':'image/jpeg,image/x-citrix-jpeg,image/png,image/x-citrix-png,image/x-png,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.slideshow,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,text/html,application/msword,application/vnd.oasis.opendocument.presentation,application/vnd.oasis.opendocument.spreadsheet,application/vnd.oasis.opendocument.text,application/pdf,application/vnd.ms-powerpoint'}), } def clean_name(self): name = self.cleaned_data.get('name', '') topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() else: same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() if same_name > 0: self._errors['name'] = [_('This subject already has a file link with this name')] return ValueError return name def clean_file_content(self): file_content = self.cleaned_data.get('file_content', False) if file_content: if hasattr(file_content, '_size'): if file_content._size > self.MAX_UPLOAD_SIZE: self._errors['file_content'] = [_("The file is too large. It should have less than 10MB.")] return ValueError elif not self.instance.pk: self._errors['file_content'] = [_('This field is required.')] return ValueError return file_content def save(self, commit = True): super(FileLinkForm, self).save(commit = True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data['tags'].split(",") #Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name = tag).exists() if exist: new_tag = Tag.objects.get(name = tag) else: new_tag = Tag.objects.create(name = tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class WebpageForm(forms.ModelForm): subject = None students = ParticipantsMultipleChoiceField(queryset = None, required = False) def __init__(self, *args, **kwargs): super(WebpageForm, self).__init__(*args, **kwargs) self.subject = kwargs['initial'].get('subject', None) if self.instance.id: self.subject = self.instance.topic.subject self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) self.fields['students'].queryset = self.subject.students.all() self.fields['groups'].queryset = self.subject.group_subject.all() tags = forms.CharField(label = _('Tags'), required = False) class Meta: model = Webpage fields = ['name', 'content', 'brief_description', 'all_students', 'students', 'groups', 'show_window', 'visible'] labels = { 'name': _('Webpage name'), 'content': _('Webpage content'), } widgets = { 'content': forms.Textarea, 'brief_description': forms.Textarea, 'students': forms.SelectMultiple, 'groups': forms.SelectMultiple, } def clean_name(self): name = self.cleaned_data.get('name', '') topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count() else: same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count() if same_name > 0: self._errors['name'] = [_('This subject already has a webpage with this name')] return ValueError return name def clean_content(self): content = self.cleaned_data.get('content', '') cleaned_content = strip_tags(content) if cleaned_content == '': self._errors['content'] = [_('This field is required.')] return ValueError return content def save(self, commit = True): super(WebpageForm, self).save(commit = True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data['tags'].split(",") #Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name = tag).exists() if exist: new_tag = Tag.objects.get(name = tag) else: new_tag = Tag.objects.create(name = tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class YTVideoForm(forms.ModelForm): subject = None control_subject = forms.CharField(widget=forms.HiddenInput()) students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(YTVideoForm, self).__init__(*args, **kwargs) self.subject = kwargs['initial'].get('subject', None) if self.instance.id: self.subject = self.instance.topic.subject self.initial['tags'] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.initial['control_subject'] = self.subject.id self.fields['students'].queryset = self.subject.students.all() self.fields['groups'].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_('Tags'), required=False) class Meta: model = YTVideo fields = [ 'name', 'url', 'brief_description', 'all_students', 'students', 'groups', 'show_window', 'visible' ] labels = { 'name': _('Video title'), } widgets = { 'brief_description': forms.Textarea, 'students': forms.SelectMultiple, 'groups': forms.SelectMultiple, } def clean_name(self): name = self.cleaned_data.get('name', '') topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count() else: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self._errors['name'] = [ _('This subject already has a resource with this name') ] return ValueError return name def clean_url(self): url = self.cleaned_data.get('url', '') if url[0].lower() != "h": url = "https://" + url if (re.compile("https?://w{3}\.youtube\.com/").match(url) == None and re.compile("https?://youtu\.be/").match(url) == None) or requests.get(url).status_code == 404: self._errors['url'] = [ _('Invalid URL. It should be an YouTube link.') ] return ValueError if re.compile("https?://youtu\.be/").match(url) != None: url = url.replace("youtu.be", "www.youtube.com/embed") return url def save(self, commit=True): super(YTVideoForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data['tags'].split(",") #Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class PDFFileForm(forms.ModelForm): subject = None MAX_UPLOAD_SIZE = 10 * 1024 * 1024 students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(PDFFileForm, self).__init__(*args, **kwargs) self.subject = kwargs.get("initial").get("subject", None) if self.instance.id: self.subject = self.instance.topic.subject self.initial["tags"] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.fields["students"].queryset = self.subject.students.all() self.fields["groups"].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_("Tags"), required=False) class Meta: model = PDFFile fields = [ "name", "file", "brief_description", "show_window", "all_students", "students", "groups", "visible", ] labels = { "name": _("File name"), } widgets = { "brief_description": forms.Textarea, "students": forms.SelectMultiple, "groups": forms.SelectMultiple, "file": ResubmitFileWidget( attrs={ "accept": "application/pdf, application/x-pdf, application/x-bzpdf, application/x-gzpdf" }), } def clean_name(self): name = self.cleaned_data.get("name", "") topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = (topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count()) else: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self._errors["name"] = [ _("This subject already has a pdf file with this name") ] return ValueError return name def clean_file(self): file = self.cleaned_data.get("file", False) if file: if hasattr(file, "_size"): if file._size > self.MAX_UPLOAD_SIZE: self._errors["file"] = [ _("The file is too large. It should have less than 10MB." ) ] return ValueError elif not self.instance.pk: self._errors["file"] = [_("This field is required.")] return ValueError return file def save(self, commit=True): super(PDFFileForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data["tags"].split(",") # Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class QuestionaryForm(forms.ModelForm): subject = None topic = None control_subject = forms.CharField(widget=forms.HiddenInput()) students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(QuestionaryForm, self).__init__(*args, **kwargs) self.subject = kwargs.get('initial').get('subject', None) self.topic = kwargs['initial'].get('topic', None) if self.instance.id: self.subject = self.instance.topic.subject self.topic = self.instance.topic self.initial['tags'] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.initial['control_subject'] = self.subject.id self.fields['students'].queryset = self.subject.students.all() self.fields['groups'].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_('Tags'), required=False) class Meta: model = Questionary fields = [ 'name', 'presentation', 'data_ini', 'data_end', 'brief_description', 'show_window', 'all_students', 'students', 'groups', 'visible' ] labels = { 'name': _('Questionary Name'), } widgets = { 'presentation': forms.Textarea, 'brief_description': forms.Textarea, 'students': forms.SelectMultiple, 'groups': forms.SelectMultiple, } def clean(self): cleaned_data = super(QuestionaryForm, self).clean() data_ini = cleaned_data.get('data_ini', None) data_end = cleaned_data.get('data_end', None) name = cleaned_data.get('name', '') if self.topic: if self.instance.id: same_name = self.topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count() else: same_name = self.topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self.add_error( 'name', _('This subject already has a questionary with this name')) if data_ini: if not data_ini == ValueError: if not self.instance.id and data_ini.date() < datetime.today( ).date(): self.add_error( 'data_ini', _("This input should be filled with a date equal or after today's date." )) if data_ini.date() < self.subject.init_date: self.add_error( 'data_ini', _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date)) if data_ini.date() > self.subject.end_date: self.add_error( 'data_ini', _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date)) else: self.add_error('data_ini', _('This field is required')) if data_end: if not data_end == ValueError: if not self.instance.id and data_end.date() < datetime.today( ).date(): self.add_error( 'data_end', _("This input should be filled with a date equal or after today's date." )) if data_end.date() < self.subject.init_date: self.add_error( 'data_end', _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date)) if data_end.date() > self.subject.end_date: self.add_error( 'data_end', _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date)) if not data_ini == ValueError and data_ini and data_end < data_ini: self.add_error( 'data_end', _("This input should be filled with a date equal or before the date stabilished for the init." )) else: self.add_error('data_end', _('This field is required')) return cleaned_data def save(self, commit=True): super(QuestionaryForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data['tags'].split(",") #Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class LinkForm(forms.ModelForm): subject = None MAX_UPLOAD_SIZE = 10 * 1024 * 1024 students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(LinkForm, self).__init__(*args, **kwargs) self.subject = kwargs['initial'].get('subject', None) if self.instance.id: self.subject = self.instance.topic.subject self.initial['tags'] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.fields['students'].queryset = self.subject.students.all() self.fields['groups'].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_('Tags'), required=False) link_url = forms.CharField(label=_('Website URL'), required=True) class Meta: model = Link fields = [ 'name', 'link_url', 'brief_description', 'all_students', 'students', 'groups', 'visible' ] labels = { 'name': _('Link name'), 'end_view': _('End View'), 'end_view_date': _('End View Date') } widgets = { 'brief_description': forms.Textarea, 'students': forms.SelectMultiple, 'groups': forms.SelectMultiple, } def clean_name(self): name = self.cleaned_data.get('name', '') topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count() else: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self._errors['name'] = [ _('There is already a link with this name on this subject') ] return ValueError return name def clean_link_url(self): link_url = self.cleaned_data.get('link_url', '') if link_url[0].lower() != "h": link_url = "https://" + link_url if validators.url(link_url) != True: self._errors['link_url'] = [ _('Invalid URL. It should be an valid link.') ] return ValueError return link_url def save(self, commit=True): super(LinkForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data['tags'].split(",") #Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class MaterialDeliveryForm(forms.ModelForm): subject = None topic = None students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): super(MaterialDeliveryForm, self).__init__(*args, **kwargs) self.subject = kwargs.get("initial").get("subject", None) self.topic = kwargs["initial"].get("topic", None) if self.instance.id: self.subject = self.instance.topic.subject self.topic = self.instance.topic self.initial["tags"] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.fields["students"].queryset = self.subject.students.all() self.fields["groups"].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_("Tags"), required=False) class Meta: model = MaterialDelivery fields = [ "name", "presentation", "data_ini", "data_end", "brief_description", "show_window", "all_students", "students", "groups", "visible", ] labels = { "name": _("Material Delivery Name"), } widgets = { "presentation": forms.Textarea, "brief_description": forms.Textarea, "students": forms.SelectMultiple, "groups": forms.SelectMultiple, } def clean(self): cleaned_data = super(MaterialDeliveryForm, self).clean() data_ini = cleaned_data.get("data_ini", None) data_end = cleaned_data.get("data_end", None) name = cleaned_data.get("name", "") presentation = self.cleaned_data.get('presentation', '') topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = (topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count()) else: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self.add_error( "name", _("This subject already has a material delivery with this name" )) break cleaned_content = strip_tags(presentation) if cleaned_content == '': self.add_error('presentation', _('This field is required.')) todaysDate = timezone.localtime(timezone.now()).date() if data_ini: if not data_ini == ValueError: if not self.instance.id and data_ini.date() < todaysDate: self.add_error( "data_ini", _("This input should be filled with a date equal or after today's date." ), ) if data_ini.date() < self.subject.init_date: self.add_error( "data_ini", _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date), ) if data_ini.date() > self.subject.end_date: self.add_error( "data_ini", _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date), ) else: self.add_error("data_ini", _("This field is required")) if data_end: if not data_end == ValueError: if not self.instance.id and data_end.date() < todaysDate: self.add_error( "data_end", _("This input should be filled with a date equal or after today's date." ), ) if data_end.date() < self.subject.init_date: self.add_error( "data_end", _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date), ) if data_end.date() > self.subject.end_date: self.add_error( "data_end", _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date), ) if not data_ini == ValueError and data_ini and data_end < data_ini: self.add_error( "data_end", _("This input should be filled with a date equal or after the date stabilished for the init." ), ) else: self.add_error("data_end", _("This field is required")) return cleaned_data def save(self, commit=True): super(MaterialDeliveryForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data["tags"].split(",") # Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance
class H5PForm(forms.ModelForm): MAX_UPLOAD_SIZE = 10 * 1024 * 1024 subject = None control_subject = forms.CharField(widget=forms.HiddenInput()) students = ParticipantsMultipleChoiceField(queryset=None, required=False) def __init__(self, *args, **kwargs): self.request = kwargs.pop("request") super(H5PForm, self).__init__(*args, **kwargs) self.subject = kwargs["initial"].get("subject", None) if self.instance.id: self.subject = self.instance.topic.subject self.initial["tags"] = ", ".join( self.instance.tags.all().values_list("name", flat=True)) self.initial["control_subject"] = self.subject.id self.fields["students"].queryset = self.subject.students.all() self.fields["groups"].queryset = self.subject.group_subject.all() tags = forms.CharField(label=_("Tags"), required=False) class Meta: model = H5P fields = [ "name", "file", "data_ini", "data_end", "brief_description", "all_students", "students", "groups", "show_window", "visible", ] widgets = { "brief_description": forms.Textarea, "students": forms.SelectMultiple, "groups": forms.SelectMultiple, "file": ResubmitFileWidget(), } def clean(self): cleaned_data = super(H5PForm, self).clean() h5pfile = cleaned_data.get('file') if "file" in self.request.FILES: interface = H5PDjango(self.request.user) paths = handleUploadedFile(h5pfile, h5pfile.name) validator = interface.h5pGetInstance('validator', paths['folderPath'], paths['path']) if not validator.isValidPackage(False, False): raise forms.ValidationError( _('The uploaded file was not a valid h5p package.')) _mutable = self.request.POST._mutable self.request.POST._mutable = True self.request.POST['file_uploaded'] = True self.request.POST['disable'] = 0 self.request.POST['author'] = self.request.user.username self.request.POST['h5p_upload'] = paths['path'] self.request.POST['h5p_upload_folder'] = paths['folderPath'] self.request.POST._mutable = _mutable if not h5pInsert(self.request, interface): raise forms.ValidationError('Error during saving the content.') data_ini = cleaned_data.get("data_ini", None) data_end = cleaned_data.get("data_end", None) name = cleaned_data.get("name", "") topics = self.subject.topic_subject.all() for topic in topics: if self.instance.id: same_name = (topic.resource_topic.filter( name__unaccent__iexact=name).exclude( id=self.instance.id).count()) else: same_name = topic.resource_topic.filter( name__unaccent__iexact=name).count() if same_name > 0: self.add_error( "name", _("This subject already has a h5p resource with this name") ) break if data_ini: if not data_ini == ValueError: if not self.instance.id and data_ini.date() < datetime.today( ).date(): self.add_error( "data_ini", _("This input should be filled with a date equal or after today's date." ), ) if data_ini.date() < self.subject.init_date: self.add_error( "data_ini", _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date), ) if data_ini.date() > self.subject.end_date: self.add_error( "data_ini", _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date), ) else: self.add_error("data_ini", _("This field is required")) if data_end: if not data_end == ValueError: if not self.instance.id and data_end.date() < datetime.today( ).date(): self.add_error( "data_end", _("This input should be filled with a date equal or after today's date." ), ) if data_end.date() < self.subject.init_date: self.add_error( "data_end", _('This input should be filled with a date equal or after the subject begin date.("%s")' ) % (self.subject.init_date), ) if data_end.date() > self.subject.end_date: self.add_error( "data_end", _('This input should be filled with a date equal or before the subject end date.("%s")' ) % (self.subject.end_date), ) if not data_ini == ValueError and data_ini and data_end < data_ini: self.add_error( "data_end", _("This input should be filled with a date equal or after the date stabilished for the init." ), ) else: self.add_error("data_end", _("This field is required")) return cleaned_data def save(self, commit=True): super(H5PForm, self).save(commit=True) self.instance.save() previous_tags = self.instance.tags.all() tags = self.cleaned_data["tags"].split(",") # Excluding unwanted tags for prev in previous_tags: if not prev.name in tags: self.instance.tags.remove(prev) for tag in tags: tag = tag.strip() exist = Tag.objects.filter(name=tag).exists() if exist: new_tag = Tag.objects.get(name=tag) else: new_tag = Tag.objects.create(name=tag) if not new_tag in self.instance.tags.all(): self.instance.tags.add(new_tag) return self.instance