Function that can copy django objects with many to many realtions and translations

remotes/origin/1203
Nazar Kotjuk 12 years ago
parent 1946a04da4
commit 51111309ae
  1. 57
      article/models.py
  2. 64
      conference/models.py
  3. 13
      exposition/admin.py
  4. 52
      exposition/models.py
  5. 12
      functions/views_help.py
  6. 57
      news/models.py
  7. 62
      place_conference/models.py
  8. 59
      place_exposition/models.py
  9. 64
      seminar/models.py
  10. 44
      templates/admin/exposition/exposition_all.html
  11. 93
      theme/models.py
  12. 64
      webinar/models.py

@ -1,7 +1,16 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from django.template.defaultfilters import slugify
import copy
class ArticleManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
class Article(TranslatableModel):
"""
@ -10,6 +19,9 @@ class Article(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = ArticleManager
url = models.SlugField(unique=True)
theme = models.ManyToManyField('theme.Theme')
tag = models.ManyToManyField('theme.Tag', related_name='tags',blank=True, null=True)
@ -30,3 +42,46 @@ class Article(TranslatableModel):
def __unicode__(self):
return self.lazy_translation_getter('main_title', self.pk)
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if Article.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate

@ -1,11 +1,22 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
import copy
#custom functions
from functions.custom_fields import EnumField
class ConferenceManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
CURRENCY = ('RUB', 'USD', 'EUR')
class Conference(TranslatableModel):
@ -14,6 +25,9 @@ class Conference(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = ConferenceManager()
url = models.SlugField(unique=True)
data_begin = models.DateField(verbose_name='Дата начала')
data_end = models.DateField(verbose_name='Дата окончания')
@ -73,6 +87,54 @@ class Conference(TranslatableModel):
def cancel(self):
self.canceled_by_administrator = True
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if Conference.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
# duplicate should not be published
duplicate.is_published = False
duplicate.cancel_by_administrator = False
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate
class TimeTable(TranslatableModel):
"""

@ -18,6 +18,7 @@ from file.forms import FileModelForm
import random
#custom views
from functions.custom_views import objects_list, delete_object
from functions.views_help import get_referer
def exposition_all(request):
@ -51,18 +52,16 @@ def exposition_switch(request, url, action):
return HttpResponse('error')
@login_required
def exposition_copy(request, url):
exposition = Exposition.objects.safe_get(url=url)
if not exposition:
return HttpResponse('error')
return HttpResponseRedirect(get_referer(request))
else:
exposition.pk = None
exposition.url += '_copy'
exposition.is_published = False
exposition.cancel_by_administrator = False
exposition.save()
return HttpResponse('success')
exposition.clone()
return HttpResponseRedirect(get_referer(request))
@login_required
def exposition_add(request):

@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
import copy
#
from functions.custom_fields import EnumField
@ -110,6 +111,54 @@ class Exposition(TranslatableModel):
self.canceled_by_administrator = True
self.save()
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if Exposition.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
# duplicate should not be published
duplicate.is_published = False
duplicate.cancel_by_administrator = False
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate
class TimeTable(TranslatableModel):
"""
@ -122,5 +171,4 @@ class TimeTable(TranslatableModel):
#translated fields
translations = TranslatedFields(
name = models.CharField(verbose_name='Название', max_length=255)
)
)

@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
import re
def get_referer(request, default=None):
referer = request.META.get('HTTP_REFERER')
if not referer:
return default
# remove the protocol and split the url at the slashes
referer = re.sub('^https?:\/\/', '', referer).split('/')
# add the slash at the relative path's view and finished
referer = u'/' + u'/'.join(referer[1:])
return referer

@ -1,14 +1,25 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
import copy
#functions
from functions.custom_fields import EnumField
class NewsManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
TYPES = ('announcement', 'news', 'overview')
class News(TranslatableModel):
#set manager of this model
objects = NewsManager
content_type = models.ForeignKey(ContentType, null=True)
object_id = models.PositiveIntegerField(blank=True, null=True)
object = generic.GenericForeignKey(content_type, object_id)
@ -37,3 +48,47 @@ class News(TranslatableModel):
def __unicode__(self):
return self.lazy_translation_getter('main_title', self.pk)
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if News.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate

@ -1,9 +1,19 @@
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes import generic
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from functions.custom_fields import EnumField
from functions.custom_fields import LocationField
import copy
class PlaceConferenceManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
CONFERENCE_TYPE = (('Convention centre', 'Конгресс-центр'), ('Exposition centre', 'Конференц зал'),)
@ -15,6 +25,9 @@ class PlaceConference(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = PlaceConferenceManager()
url = models.SlugField(unique=True)
country = models.ForeignKey('country.Country', on_delete=models.PROTECT)
city = models.ForeignKey('city.City', on_delete=models.PROTECT)
@ -60,6 +73,53 @@ class PlaceConference(TranslatableModel):
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if PlaceConference.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
# duplicate should not be published
duplicate.is_published = False
duplicate.cancel_by_administrator = False
duplicate.save()
# but lost all Translations and Halls.
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
# copy halls
halls = Hall.objects.filter(place_conference=getattr(self, 'id'))
for hall in halls:
duplicate_hall = copy.copy(hall)
duplicate_hall.place_conference = duplicate
duplicate_hall.save()
return duplicate
class Hall(models.Model):
"""
Create Hall model which saves information about halls in PlaceConference

@ -1,8 +1,18 @@
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.contenttypes import generic
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from functions.custom_fields import EnumField, LocationField
import copy
class PlaceExpositionManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
EXPOSITION_TYPE = (('Exposition complex', 'Выставочный комплекс'), ('Convention centre', 'Конгессо-выставочный центр'),
@ -15,6 +25,9 @@ class PlaceExposition(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = PlaceExpositionManager()
url = models.SlugField(unique=True)
country = models.ForeignKey('country.Country', on_delete=models.PROTECT)
city = models.ForeignKey('city.City', on_delete=models.PROTECT)
@ -69,6 +82,50 @@ class PlaceExposition(TranslatableModel):
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if PlaceExposition.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
duplicate.save()
# but lost all Translations and Halls.
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
# copy halls
halls = Hall.objects.filter(place_exposition=getattr(self, 'id'))
for hall in halls:
duplicate_hall = copy.copy(hall)
duplicate_hall.exposition = duplicate
duplicate_hall.save()
return duplicate
class Hall(models.Model):
"""
Create Hall model which saves information about halls in PlaceExposition

@ -1,10 +1,19 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
import copy
#
from functions.custom_fields import EnumField
from functions.custom_fields import LocationField
class SeminarManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
CURRENCY = ('RUB', 'USD', 'EUR')
@ -14,6 +23,9 @@ class Seminar(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = SeminarManager()
url = models.SlugField(unique=True)
data_begin = models.DateTimeField(verbose_name='Дата начала')
data_end = models.DateTimeField(verbose_name='Дата окончания')
@ -69,4 +81,52 @@ class Seminar(TranslatableModel):
return self.lazy_translation_getter('name', unicode(self.pk))
def cancel(self):
self.canceled_by_administrator = True
self.canceled_by_administrator = True
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if Seminar.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
# duplicate should not be published
duplicate.is_published = False
duplicate.cancel_by_administrator = False
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate

@ -3,28 +3,28 @@
{% block scripts %}
<script>
$(document).ready(function(){
$('#on').click(function(){
$('.on').click(function(){
var url = $(this).attr('href')
var $this = $(this)
$.get(
url, function(data){
if (data == 'on'){
$this.hide();
$('#off').show();
$this.siblings('.off').show();
}
});//end get
return false;
});
$('#off').click(function(){
$('.off').click(function(){
var url = $(this).attr('href')
var $this = $(this)
$.get(
url, function(data){
if (data == 'off'){
$this.hide();
$('#on').show();
$this.siblings('.on').show();
}
}
)
@ -45,7 +45,6 @@
<table class="table table-hover">
<thead>
<tr>
<th>id</th>
<th>Название</th>
<th>Дата начала</th>
<th>&nbsp;</th>
@ -55,25 +54,32 @@
{% for item in objects %}
<tr>
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.data_begin }}</td>
<td class="center sorting_1">
<a class="btn btn-warning" style="{% if item.is_published %}{% else %}display: none;{% endif %}"
href="/admin/exposition/switch/{{ item.url }}/off" id="off">
<i class="icon-remove-sign icon-white"></i> Отключить
<td>
<a class="btn-small btn-warning off" style="{% if item.is_published %}{% else %}display: none;{% endif %}"
href="/admin/exposition/switch/{{ item.url }}/off">
Отключить
</a>
<a class="btn btn-success" style="{% if item.is_published %}display: none;{% else %}{% endif %}"
href="/admin/exposition/switch/{{ item.url }}/on" id="on">
<i class="icon-ok icon-white"></i> Включить
<a class="btn-small btn-success on" style="{% if item.is_published %}display: none;{% else %}{% endif %}"
href="/admin/exposition/switch/{{ item.url }}/on">
Включить
</a>
<a class="btn btn-info" href="/admin/exposition/change/{{ item.url|lower }}">
<i class="icon-edit icon-white"></i> Изменить
</td>
<td>
<a class="btn-small btn-info" href="/admin/exposition/change/{{ item.url|lower }}">
Изменить
</a>
</td>
<td>
<a class="btn-small btn-inverse" href="/admin/exposition/copy/{{ item.url }}" id="copy">
Копировать
</a>
</td>
<a class="btn btn-danger" href="/admin/exposition/delete/{{ item.url|lower }}">
<i class="icon-trash icon-white"></i> Удалить
<td class="center sorting_1">
<a class="btn-small btn-danger" href="/admin/exposition/delete/{{ item.url|lower }}">
Удалить
</a>
</td>

@ -1,8 +1,19 @@
# -*- coding: utf-8 -*-
from django.db import models
from django.utils.translation import ugettext_lazy as _
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
from bitfield import BitField
import copy
class ThemeManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
class Theme(TranslatableModel):
"""
@ -11,6 +22,9 @@ class Theme(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = ThemeManager()
FLAGS = (
('exposition', 'Выставка'),
('conference', 'Конференция'),
@ -35,6 +49,46 @@ class Theme(TranslatableModel):
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
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. Django thinking this is a new object.
duplicate.pk = None
duplicate.save()
# but lost all Translations.
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate
class TagManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
class Tag(TranslatableModel):
"""
@ -43,6 +97,9 @@ class Tag(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = TagManager()
theme = models.ForeignKey(Theme, on_delete=models.PROTECT, related_name='themes')
#translated fields
translations = TranslatedFields(
@ -60,4 +117,36 @@ class Tag(TranslatableModel):
#modified = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.lazy_translation_getter('name', unicode(self.pk))
return self.lazy_translation_getter('name', unicode(self.pk))
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. Django thinking this is a new object.
duplicate.pk = None
duplicate.save()
# but lost all Translations.
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate

@ -1,10 +1,19 @@
# -*- coding: utf-8 -*-
from django.db import models
from hvad.models import TranslatableModel, TranslatedFields
from hvad.models import TranslatableModel, TranslatedFields, TranslationManager
import copy
#
from functions.custom_fields import EnumField
class WebinarManager(TranslationManager):
def safe_get(self, **kwargs):
model = self.model
try:
return model.objects.get(**kwargs)
except:
return None
CURRENCY = ('RUB', 'USD', 'EUR')
@ -14,6 +23,9 @@ class Webinar(TranslatableModel):
Uses hvad.TranslatableModel which is child of django.db.models class
"""
#set manager of this model
objects = WebinarManager()
url = models.SlugField(unique=True)
data_begin = models.DateTimeField(verbose_name='Дата начала', blank=True)
#relations
@ -66,4 +78,52 @@ class Webinar(TranslatableModel):
return self.lazy_translation_getter('name', unicode(self.pk))
def cancel(self):
self.canceled_by_administrator = True
self.canceled_by_administrator = True
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. Django thinking this is a new object.
duplicate.pk = None
# url must be unique
duplicate.url += '_copy'
if Webinar.objects.safe_get(url=duplicate.url):
#already has copy this instance
return
# duplicate should not be published
duplicate.is_published = False
duplicate.cancel_by_administrator = False
duplicate.save()
# but lost all ManyToMany relations and Translations.
# copy 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)
# copy translations
languages = self.get_available_languages()
ignore_fields = ['id', 'master', 'language_code']
for code in languages:
duplicate.translate(code)
tr = self._meta.translations_model.objects.get(language_code = code,master__id=self.pk)
for field in duplicate._translated_field_names:
if field in ignore_fields:
continue
setattr(duplicate, field, getattr(tr, field))
duplicate.save()
return duplicate
Loading…
Cancel
Save