From a27b21b1a9e803d75825a7a59c99ff72d066c871 Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Thu, 19 Dec 2013 17:32:09 +0100 Subject: [PATCH] clone all docs and media --- .../commands/teleforma-copy-seminars.py | 31 +++++++++++++------ teleforma/models/core.py | 22 ++++++++++++- teleforma/models/pro.py | 5 +-- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/teleforma/management/commands/teleforma-copy-seminars.py b/teleforma/management/commands/teleforma-copy-seminars.py index 03d0f83b..cef1b19b 100644 --- a/teleforma/management/commands/teleforma-copy-seminars.py +++ b/teleforma/management/commands/teleforma-copy-seminars.py @@ -27,17 +27,28 @@ class Command(BaseCommand): for seminar in Seminar.objects.all(): if seminar.expiry_date: if seminar.expiry_date.year == from_year: - questions = seminar.question.all() - seminar.pk = None - seminar.save() - seminar.publish_date = seminar.publish_date.replace(year=to_year) - seminar.expiry_date = seminar.expiry_date.replace(year=to_year) - seminar.save() - print ("updated:", seminar) + print ("cloning:", seminar) + clone = seminar.clone() + clone.publish_date = clone.publish_date.replace(year=to_year) + clone.expiry_date = clone.expiry_date.replace(year=to_year) + clone.save() + print ('dates updated', clone) + + for field in seminar._meta.many_to_many: + if type(field) == Document or type(field) == Media: + source = getattr(seminar, field.attname) + destination = getattr(clone, field.attname) + for item in source.all(): + item_clone = item.clone() + item_clone.readers = [] + item_clone.save() + destination.remove(item) + destination.add(item_clone) + print ("documents and medias cloned and assigned:", clone) + questions = seminar.question.all() for question in questions: - question.pk = None - question.save() - question.seminar = seminar + question_clone = question.clone() + question_clone.seminar = clone question.save() print ("updated:", question) diff --git a/teleforma/models/core.py b/teleforma/models/core.py index 9f9cdee1..3878dbec 100755 --- a/teleforma/models/core.py +++ b/teleforma/models/core.py @@ -42,6 +42,7 @@ import urllib import string import datetime import mimetypes +import copy from django.conf import settings from django.db.models import * @@ -92,6 +93,25 @@ class MetaCore: app_label = app_label +class ClonableMixin(object): + + def clone(self): + """Return an identical copy of the instance with a new ID.""" + if not self.pk: + raise ValueError('Instance must be saved before it can be cloned.') + duplicate = copy.copy(self) + # Setting pk to None tricks Django into thinking this is a new object. + duplicate.pk = None + duplicate.save() + # ... but the trick loses all ManyToMany relations. + for field in self._meta.many_to_many: + source = getattr(self, field.attname) + destination = getattr(duplicate, field.attname) + for item in source.all(): + destination.add(item) + return duplicate + + class Organization(Model): name = CharField(_('name'), max_length=255) @@ -304,7 +324,7 @@ class LiveStream(Model): verbose_name = _('live stream') -class MediaBase(Model): +class MediaBase(ClonableMixin, Model): "Base media resource" title = CharField(_('title'), max_length=1024, blank=True) diff --git a/teleforma/models/pro.py b/teleforma/models/pro.py index bd2de5b0..62a70a77 100755 --- a/teleforma/models/pro.py +++ b/teleforma/models/pro.py @@ -43,6 +43,7 @@ from teleforma.models.core import * from forms_builder.forms.models import Form from mezzanine.core.managers import DisplayableManager + class SeminarType(models.Model): name = models.CharField(_('name'), max_length=255, blank=True) @@ -55,7 +56,7 @@ class SeminarType(models.Model): verbose_name = _('Seminar type') -class Seminar(Displayable): +class Seminar(ClonableMixin, Displayable): # title, description, keywords and dates are given by Displayable @@ -131,7 +132,7 @@ class Seminar(Displayable): ordering = ['rank'] -class Question(models.Model): +class Question(ClonableMixin, models.Model): element_type = 'question' -- 2.39.5