datatracker/ietf/group/utils.py
2012-01-24 17:17:24 +00:00

29 lines
903 B
Python

from ietf.group.models import *
def save_group_in_history(group):
def get_model_fields_as_dict(obj):
return dict((field.name, getattr(obj, field.name))
for field in obj._meta.fields
if field is not obj._meta.pk)
# copy fields
fields = get_model_fields_as_dict(group)
del fields["charter"] # Charter is saved canonically on Group
fields["group"] = group
grouphist = GroupHistory(**fields)
grouphist.save()
# save RoleHistory
for role in group.role_set.all():
rh = RoleHistory(name=role.name, group=grouphist, email=role.email, person=role.person)
rh.save()
# copy many to many
for field in group._meta.many_to_many:
if field.rel.through and field.rel.through._meta.auto_created:
setattr(grouphist, field.name, getattr(group, field.name).all())
return grouphist