def clean_slug(self): """ Ensure that the slug is valid and not taken. """ data = self.cleaned_data['slug'] if data != self.project.slug: if exists_project_slug(data): raise forms.ValidationError('The slug is already taken by another project.') return data
def save(self): project = super().save(commit=False) # Set the core project and slug core_project = CoreProject.objects.create() project.core_project = core_project slug = get_random_string(20) while exists_project_slug(slug): slug = get_random_string(20) project.slug = slug project.save() # Create the author object for the user author = Author.objects.create( project=project, user=self.user, display_order=1, corresponding_email=self.user.get_primary_email(), is_submitting=True, is_corresponding=True) author.import_profile_info() # Create file directory os.mkdir(project.file_root()) return project
def save(self): project = super().save(commit=False) # Direct copy over fields for attr in [f.name for f in Metadata._meta.fields]: if attr not in ['slug', 'version', 'creation_datetime']: setattr(project, attr, getattr(self.latest_project, attr)) # Set new fields slug = get_random_string(20) while exists_project_slug(slug): slug = get_random_string(20) project.slug = slug project.creation_datetime = timezone.now() project.version_order = self.latest_project.version_order + 1 project.is_new_version = True project.save() # Copy over the author/affiliation objects for p_author in self.latest_project.authors.all(): author = Author.objects.create( project=project, user=p_author.user, display_order=p_author.display_order, is_submitting=p_author.is_submitting, is_corresponding=p_author.is_corresponding, corresponding_email=self.user.get_primary_email(), ) for p_affiliation in p_author.affiliations.all(): Affiliation.objects.create(name=p_affiliation.name, author=author) # Other related objects for p_reference in self.latest_project.references.all(): reference = Reference.objects.create( description=p_reference.description, project=project) for p_publication in self.latest_project.publications.all(): publication = Publication.objects.create( citation=p_publication.citation, url=p_publication.url, project=project) for parent_project in self.latest_project.parent_projects.all(): project.parent_projects.add(parent_project) for p_topic in self.latest_project.topics.all(): topic = Topic.objects.create(project=project, description=p_topic.description) # Create file directory os.mkdir(project.file_root()) current_file_root = project.file_root() older_file_root = self.latest_project.file_root() for (directory, subdirs, files) in os.walk(older_file_root): rel_dir = os.path.relpath(directory, older_file_root) destination = os.path.join(current_file_root, rel_dir) for d in subdirs: try: os.mkdir(os.path.join(destination, d)) except FileExistsError: pass for f in files: # Skip linking files that are automatically generated # during publication. if (directory == older_file_root and f in ('SHA256SUMS.txt', 'LICENSE.txt')): continue try: os.link(os.path.join(directory, f), os.path.join(destination, f)) except FileExistsError: pass return project
def save(self): project = super().save(commit=False) slug = get_random_string(20) while exists_project_slug(slug): slug = get_random_string(20) project.slug = slug # Direct copy over fields for field in (field.name for field in Metadata._meta.fields): if field not in ['slug', 'version', 'creation_datetime']: setattr(project, field, getattr(self.latest_project, field)) # Set new fields project.creation_datetime = timezone.now() project.version_order = self.latest_project.version_order + 1 project.is_new_version = True # Change internal links (that point to files within the # published project) to point to their new locations in the # active project project.update_internal_links(old_project=self.latest_project) project.save() # Copy over the author/affiliation objects for p_author in self.latest_project.authors.all(): author = Author.objects.create( project=project, user=p_author.user, display_order=p_author.display_order, is_submitting=p_author.is_submitting, is_corresponding=p_author.is_corresponding, corresponding_email=self.user.get_primary_email(), ) for p_affiliation in p_author.affiliations.all(): Affiliation.objects.create(name=p_affiliation.name, author=author) # Other related objects for p_reference in self.latest_project.references.all(): reference = Reference.objects.create( description=p_reference.description, project=project) for p_publication in self.latest_project.publications.all(): publication = Publication.objects.create( citation=p_publication.citation, url=p_publication.url, project=project) for parent_project in self.latest_project.parent_projects.all(): project.parent_projects.add(parent_project) for p_topic in self.latest_project.topics.all(): Topic.objects.create(project=project, description=p_topic.description) documents = [] content_type = ContentType.objects.get_for_model(ActiveProject) for uploaded_document in self.latest_project.uploaded_documents.all(): uploaded_document.id = None uploaded_document.object_id = project.pk uploaded_document.content_type = content_type uploaded_document.document = ContentFile( content=uploaded_document.document.read(), name=uploaded_document.document.name) documents.append(uploaded_document) UploadedDocument.objects.bulk_create(documents) project.required_trainings.set( self.latest_project.required_trainings.all()) current_file_root = project.file_root() older_file_root = self.latest_project.file_root() ignored_files = ('SHA256SUMS.txt', 'LICENSE.txt') if settings.COPY_FILES_TO_NEW_VERSION: ProjectFiles().cp_dir(older_file_root, current_file_root, ignored_files=ignored_files) return project