本文整理汇总了Python中simple_history.register函数的典型用法代码示例。如果您正苦于以下问题:Python register函数的具体用法?Python register怎么用?Python register使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了register函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_reregister
def test_reregister(self):
register(Restaurant, manager_name='again')
register(User, manager_name='again')
self.assertTrue(hasattr(Restaurant, 'updates'))
self.assertFalse(hasattr(Restaurant, 'again'))
self.assertTrue(hasattr(User, 'histories'))
self.assertFalse(hasattr(User, 'again'))
示例2: setup_m2m_history
def setup_m2m_history(self, cls):
m2m_history_fields = self.m2m_fields
if m2m_history_fields is ALL_M2M_FIELDS:
for field in cls._meta.many_to_many:
field = getattr(cls, field.name).field
assert isinstance(field, models.fields.related.ManyToManyField), \
('%s must be a ManyToManyField' % field.name)
if not sum([
isinstance(item, HistoricalRecords) for item in field.remote_field.through.__dict__.values()
]):
through_model = field.remote_field.through
if through_model._meta.auto_created and not_registered(through_model):
through_model.history = HistoricalRecords()
register(through_model)
elif m2m_history_fields:
assert (isinstance(m2m_history_fields, list) or isinstance(m2m_history_fields, tuple)), \
'm2m_history_fields must be a list or tuple'
for field_name in m2m_history_fields:
field = getattr(cls, field_name).field
assert isinstance(field, models.fields.related.ManyToManyField), \
('%s must be a ManyToManyField' % field_name)
if not sum([
isinstance(item, HistoricalRecords) for item in field.remote_field.through.__dict__.values()
]):
through_model = field.remote_field.through
if through_model._meta.auto_created and not_registered(through_model):
through_model.history = HistoricalRecords()
register(through_model)
示例3: contribute_to_class
def contribute_to_class(self, cls, name):
self.manager_name = name
self.module = cls.__module__
models.signals.class_prepared.connect(self.finalize, sender=cls)
setattr(cls, 'simple_history_manager', name)
def save_without_historical_record(self, *args, **kwargs):
"""Caution! Make sure you know what you're doing before you use this method."""
self.skip_history_when_saving = True
ret = self.save(*args, **kwargs)
del self.skip_history_when_saving
return ret
setattr(cls, 'save_without_historical_record', save_without_historical_record)
# Injecting HistoricalRecords into ManyToManyFields' intermediate tables ('through' models)
if hasattr(cls, 'm2m_history_fields'):
m2m_history_fields = getattr(cls, 'm2m_history_fields', None)
assert (isinstance(m2m_history_fields, list)
or isinstance(m2m_history_fields, tuple)), 'm2m_history_fields must be a list or tuple'
for field_name in m2m_history_fields:
field = getattr(cls, field_name).field
assert isinstance(field, models.fields.related.ManyToManyField), ('%s must be a ManyToManyField' % field_name)
if not sum([isinstance(item, HistoricalRecords) for item in field.rel.through.__dict__.values()]):
field.rel.through.history = HistoricalRecords()
simple_history.register(field.rel.through)
示例4: setup_m2m_history
def setup_m2m_history(self, cls):
m2m_history_fields = self.m2m_fields
if m2m_history_fields:
assert (isinstance(m2m_history_fields, list) or isinstance(m2m_history_fields, tuple)), 'm2m_history_fields must be a list or tuple'
for field_name in m2m_history_fields:
field = getattr(cls, field_name).field
assert isinstance(field, models.fields.related.ManyToManyField), ('%s must be a ManyToManyField' % field_name)
if not sum([isinstance(item, HistoricalRecords) for item in field.rel.through.__dict__.values()]):
field.rel.through.history = HistoricalRecords()
register(field.rel.through)
示例5: finalize
def finalize(self, sender, **kwargs):
try:
hint_class = self.cls
except AttributeError: # called via `register`
pass
else:
if hint_class is not sender: # set in concrete
if not (self.inherit and issubclass(sender, hint_class)):
return # set in abstract
if hasattr(sender._meta, 'simple_history_manager_attribute'):
raise exceptions.MultipleRegistrationsError(
'{}.{} registered multiple times for history tracking.'.format(
sender._meta.app_label,
sender._meta.object_name,
)
)
if sender._meta.proxy:
original_class = [base_class for base_class in sender.__bases__ if base_class._meta.abstract is False][0]
# Parent model must be registered before the proxy model is
if not_registered(original_class):
# Ignore the `app` kwarg, since the proxy model may be in a different app than the original model
register_kwargs = {
'manager_name': self.manager_name,
'records_class': self.__class__,
'verbose_name': self.user_set_verbose_name,
'bases': self.bases,
'user_related_name': self.user_related_name,
'm2m_fields': self.m2m_fields,
}
register(original_class, **register_kwargs)
# Proxy models use their parent's history model
history_model = getattr(sender, self.manager_name).model
else:
history_model = self.create_history_model(sender)
module = importlib.import_module(self.module)
setattr(module, history_model.__name__, history_model)
# The HistoricalRecords object will be discarded,
# so the signal handlers can't use weak references.
models.signals.post_save.connect(self.post_save, sender=sender,
weak=False)
models.signals.pre_delete.connect(self.pre_delete, sender=sender,
weak=False)
models.signals.post_delete.connect(self.post_delete, sender=sender,
weak=False)
models.signals.m2m_changed.connect(self.m2m_changed, sender=sender, weak=False)
descriptor = HistoryDescriptor(history_model)
setattr(sender, self.manager_name, descriptor)
sender._meta.simple_history_manager_attribute = self.manager_name
示例6: Poll
from simple_history import register
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
history = HistoricalRecords()
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
register(Choice)
class Place(models.Model):
name = models.CharField(max_length=100)
class Restaurant(Place):
rating = models.IntegerField()
updates = HistoricalRecords()
class Person(models.Model):
name = models.CharField(max_length=100)
示例7: HistoricalRecords
level = models.IntegerField()
date = models.DateTimeField()
history = HistoricalRecords()
@property
def _history_date(self):
return self.date
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
register(Choice)
class Voter(models.Model):
user = models.ForeignKey(User)
choice = models.ForeignKey(Choice, related_name='voters')
class HistoricalRecordsVerbose(HistoricalRecords):
def get_extra_fields(self, model, fields):
def verbose_str(self):
return '%s changed by %s as of %s' % (
self.history_object, self.history_user, self.history_date)
extra_fields = super(
HistoricalRecordsVerbose, self).get_extra_fields(model, fields)
示例8: institutionAdmin
class institutionAdmin (SimpleHistoryAdmin):
list_display = ('user', 'department')
class Meta:
model = Institution
class patientIdAdmin (SimpleHistoryAdmin):
list_display = ('user', 'patient_id')
class Meta:
model = Patient_id
class aggregateAdmin (SimpleHistoryAdmin):
list_display = ('center_id','name_of_center', 'author', 'pub_date')
class Meta:
model = Ext_centers
register(User, inherit=True)
register(Group, inherit=True)
#admin.site.register(User, SimpleHistoryAdmin)
admin.site.register(Institution, institutionAdmin)
admin.site.register(Patient_id, patientIdAdmin)
admin.site.register(Demographic, DemographicsAdmin)
admin.site.register(Diagnosis, DiagnosisAdmin)
admin.site.register(DiagnosisOption,diag_optAdmin)
admin.site.register(A_b_sickle_thal,a_b_sickle_Admin)
admin.site.register(Redcell_enzyme_dis,redcell_enzAdmin)
admin.site.register(Redcell_membrane_dis,redcell_memb_Admin)
admin.site.register(Cong_dyseryth_anaemia,cong_dyserAdmin)
admin.site.register(icd_10, icd10Admin)
admin.site.register(orphaCodes, orphaAdmin)
admin.site.register(Pregnancy,pregnancyAdmin)
示例9: HistoricalRecords
level = models.IntegerField()
date = models.DateTimeField()
history = HistoricalRecords()
@property
def _history_date(self):
return self.date
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
register(Choice)
class Place(models.Model):
name = models.CharField(max_length=100)
class Restaurant(Place):
rating = models.IntegerField()
updates = HistoricalRecords()
class Person(models.Model):
name = models.CharField(max_length=100)
示例10: actual_decorator
def actual_decorator(model):
simple_history.register(model, **kwargs)
return model
示例11: test_registering_with_tracked_abstract_base
def test_registering_with_tracked_abstract_base(self):
class TrackedWithAbstractBaseToRegister(TrackedAbstractBaseA):
pass
with self.assertRaises(exceptions.MultipleRegistrationsError):
register(TrackedWithAbstractBaseToRegister)
示例12: ExternalModel4
from __future__ import unicode_literals
from django.db import models
from simple_history import register
class ExternalModel4(models.Model):
name = models.CharField(max_length=100)
class Meta:
app_label = 'external'
register(ExternalModel4, app='simple_history.tests',
manager_name='histories')
示例13: test_registering_with_tracked_abstract_base
def test_registering_with_tracked_abstract_base(self):
with self.assertRaises(exceptions.MultipleRegistrationsError):
register(InheritTracking4)
示例14: HistoricalRecords
date = models.DateTimeField()
history = HistoricalRecords(cascade_delete_history=True)
@property
def _history_date(self):
return self.date
class Choice(models.Model):
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
register(Choice)
class Voter(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
choice = models.ForeignKey(Choice, on_delete=models.CASCADE, related_name="voters")
def __str__(self):
return "Voter object"
class HistoricalRecordsVerbose(HistoricalRecords):
def get_extra_fields(self, model, fields):
def verbose_str(self):
return "%s changed by %s as of %s" % (
self.history_object,
示例15: register
from django.db import models
from django.utils import timezone
from django.utils.http import urlquote
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.template.defaultfilters import slugify
from .choices import SEX_TYPE
from simple_history import register
register(User)
class CustomUserManager(BaseUserManager):
def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
now = timezone.now()
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, is_staff=is_staff, is_superuser=is_superuser, last_login=now, date_joined=now, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, password=None, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)