Example #1
0
class DummyModel(BaseEntityModel):
    """
    Used to ensure that models that don't register for entity syncing aren't synced.
    """
    dummy_data = models.CharField(max_length=64)

    objects = ManagerUtilsManager()
Example #2
0
class TestPkChar(models.Model):
    """
    A test model with a primary key that is a char field.
    """
    my_key = models.CharField(max_length=128, primary_key=True)
    char_field = models.CharField(max_length=128, null=True)

    objects = ManagerUtilsManager()
Example #3
0
class TestPkForeignKey(models.Model):
    """
    A test model with a primary key thats a foreign key to another model.
    """
    my_key = models.ForeignKey(TestModel, primary_key=True)
    char_field = models.CharField(max_length=128, null=True)

    objects = ManagerUtilsManager()
Example #4
0
class TestForeignKeyModel(models.Model):
    """
    A test model that has a foreign key.
    """
    int_field = models.IntegerField()
    test_model = models.ForeignKey(TestModel)

    objects = ManagerUtilsManager()
class TestAutoDateTimeModel(models.Model):
    """
    A model to test that upserts work with auto_now and auto_now_add
    """
    int_field = models.IntegerField(unique=True)
    auto_now_field = models.DateTimeField(auto_now=True)
    auto_now_add_field = models.DateTimeField(auto_now_add=True)

    objects = ManagerUtilsManager()
Example #6
0
class TestModel(models.Model):
    """
    A model for testing manager utils.
    """
    int_field = models.IntegerField()
    char_field = models.CharField(max_length=128, null=True)
    float_field = models.FloatField(null=True)

    objects = ManagerUtilsManager()
Example #7
0
class TestModel(models.Model):
    """
    A model for testing manager utils.
    """
    int_field = models.IntegerField(null=True)
    char_field = models.CharField(max_length=128, null=True)
    float_field = models.FloatField(null=True)
    time_zone = TimeZoneField(default='UTC')

    objects = ManagerUtilsManager()
Example #8
0
class TourStatus(models.Model):
    """
    This is the model that represents the relationship between a user and a tour. Keeps
    track of whether the tour has been completed by a user.
    """
    tour = models.ForeignKey(Tour)
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    complete = models.BooleanField(default=False)
    create_time = models.DateTimeField(auto_now_add=True)
    complete_time = models.DateTimeField(null=True, blank=True, default=None)

    objects = ManagerUtilsManager()
class TestUniqueTzModel(models.Model):
    """
    A model for testing manager utils with a timezone field as the uniqueness constraint.
    """
    int_field = models.IntegerField(null=True, unique=True)
    char_field = models.CharField(max_length=128, null=True)
    float_field = models.FloatField(null=True)
    time_zone = TimeZoneField(unique=True)

    objects = ManagerUtilsManager()

    class Meta:
        unique_together = ('int_field', 'char_field')
class TestModel(models.Model):
    """
    A model for testing manager utils.
    """
    int_field = models.IntegerField(null=True, unique=True)
    char_field = models.CharField(max_length=128, null=True)
    float_field = models.FloatField(null=True)
    json_field = JSONField(default=dict)
    array_field = ArrayField(models.CharField(max_length=128), default=list)
    time_zone = TimeZoneField(default='UTC')

    objects = ManagerUtilsManager()

    class Meta:
        unique_together = ('int_field', 'char_field')
class RegisteredForDeletionReceipt(models.Model):
    """
    Specifies a receipt of a model object that was registered for deletion by the dynamic
    initial data process.
    """
    # The model object that was registered
    model_obj_type = models.ForeignKey(ContentType)
    model_obj_id = models.PositiveIntegerField()
    model_obj = GenericForeignKey('model_obj_type', 'model_obj_id', for_concrete_model=False)

    # The time at which it was registered for deletion
    register_time = models.DateTimeField()

    # Use manager utils for bulk updating capabilities
    objects = ManagerUtilsManager()

    class Meta:
        unique_together = ('model_obj_type', 'model_obj_id')
Example #12
0
class SmartManagerObject(models.Model):
    """
    Tracks objects associated with model templates. This model is useful when model templates
    are used to manage deletions.
    """
    # The model template that manages this object
    smart_manager = models.ForeignKey(SmartManager, on_delete=models.CASCADE)

    # The generic foreign key to the object
    model_obj_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    model_obj_id = models.PositiveIntegerField()
    model_obj = GenericForeignKey('model_obj_type',
                                  'model_obj_id',
                                  for_concrete_model=False)

    objects = ManagerUtilsManager()

    class Meta:
        unique_together = ('model_obj_type', 'model_obj_id')
Example #13
0
class Step(models.Model):
    """
    Represents one step of the tour that must be completed. The custom logic is implemented
    in the class specified in step_class
    """
    name = models.CharField(max_length=128, unique=True)
    display_name = models.CharField(max_length=128)
    url = models.CharField(max_length=128, null=True, blank=True)
    tour = models.ForeignKey(Tour, related_name='steps')
    parent_step = models.ForeignKey('self', null=True, related_name='steps')
    step_class = models.CharField(max_length=128, unique=True)
    sort_order = models.IntegerField(default=0)

    objects = ManagerUtilsManager()

    def load_step_class(self):
        """
        Imports and returns the step class.
        """
        return import_by_path(self.step_class)(self)

    def __str__(self):
        return '{0}'.format(self.display_name)
Example #14
0
class SmartManager(models.Model):
    """
    Specifies the template, its associated model template class, and whether or not deletions should
    be managed by this model template.
    """
    # A unique identifier to load smart managers by name
    name = models.CharField(max_length=128,
                            unique=True,
                            null=True,
                            default=None)

    # The loadable model template class that inherits BaseSmartManager
    smart_manager_class = models.CharField(max_length=128)

    # True if this model template also deletes its objects when elements of the template
    # (or the template itself) is deleted
    manages_deletions = models.BooleanField(default=True)

    # The primary object that this smart manager manages
    primary_obj_type = models.ForeignKey(ContentType,
                                         null=True,
                                         on_delete=models.CASCADE)
    primary_obj_id = models.PositiveIntegerField(default=0)
    primary_obj = GenericForeignKey('primary_obj_type', 'primary_obj_id')

    # The template of the model(s) being managed
    template = JSONField()

    objects = ManagerUtilsManager()

    def __str__(self):
        return str(self.name)

    def clean(self):
        """
        Verify that the object can be built and the template class can be loaded. If any exception happens, raise
        a validation error.
        """
        try:
            smart_manager = import_string(self.smart_manager_class)(
                self.template)
            smart_manager.build()
        except Exception as e:
            raise ValidationError('{0} - {1}'.format(str(e),
                                                     traceback.format_exc()))

    @transaction.atomic
    def save(self, *args, **kwargs):
        """
        Builds the objects managed by the template before saving the template.
        """
        super(SmartManager, self).save(*args, **kwargs)

        smart_manager = import_string(self.smart_manager_class)(self.template)
        primary_built_obj = smart_manager.build()

        # Do an update of the primary object type and id after it has been built. We use an update since
        # you can't call save in a save method. We may want to put this in post_save as well later.
        if primary_built_obj:
            self.primary_obj_type = ContentType.objects.get_for_model(
                primary_built_obj)
            self.primary_obj_id = primary_built_obj.id
            SmartManager.objects.filter(id=self.id).update(
                primary_obj_type=self.primary_obj_type,
                primary_obj_id=primary_built_obj.id)

        # Sync all of the objects from the built template
        sync(self.smartmanagerobject_set.all(), [
            SmartManagerObject(
                smart_manager=self,
                model_obj_id=built_obj.id,
                model_obj_type=ContentType.objects.get_for_model(
                    built_obj, for_concrete_model=False),
            ) for built_obj in smart_manager.built_objs
        ], ['smart_manager_id', 'model_obj_id', 'model_obj_type_id'])
Example #15
0
class BaseEntityModel(models.Model):
    class Meta:
        abstract = True

    objects = ManagerUtilsManager()