Пример #1
0
def _generate_transition_history(instance, transition, requester, attachments,
                                 history_kwargs, action_names, field):
    """Return history object (without saving it) based on parameters."""
    field_value = getattr(instance, field, None)
    try:
        target = instance._meta.get_field(field).choices.from_id(
            int(transition.target)).name
    except ValueError:
        target = None

    try:
        source = instance._meta.get_field(field).choices.from_id(
            int(field_value)).name
    except ValueError:
        source = None

    transition_history = TransitionsHistory.objects.create(
        transition_name=transition.name,
        content_type=get_content_type_for_model(instance._meta.model),
        object_id=instance.pk,
        logged_user=requester,
        kwargs=history_kwargs,
        actions=action_names,
        source=source,
        target=target)
    transition_history.attachments.add(*attachments)
    return transition_history
Пример #2
0
def migrate_attachment_item(apps, schema_editor):
    AttachmentItem = apps.get_model('attachments', 'AttachmentItem')
    for item in AttachmentItem.objects.all():
        model = apps.get_model(item.content_type.app_label,
                               item.content_type.model)
        item.content_type_id = get_content_type_for_model(model).id
        item.save()
Пример #3
0
def migrate_transitionshistory(apps, schema_editor):
    TransitionsHistory = apps.get_model('transitions', 'TransitionsHistory')
    for item in TransitionsHistory.objects.all():
        model = apps.get_model(item.content_type.app_label,
                               item.content_type.model)
        item.content_type_id = get_content_type_for_model(model).id
        item.save()
Пример #4
0
 def refresh(self, obj, new_objects=None, deleted_objects=None):
     """
     Refresh relation between object (e.g., asset, licence) and
     attachment. It is works fine with a formset.
     """
     content_type = get_content_type_for_model(obj)
     if new_objects:
         self.attach(obj.pk, content_type, new_objects)
     if deleted_objects:
         self.dettach(obj.pk, content_type, deleted_objects)
Пример #5
0
    def get_items_for_object(self, obj):
        """
        Method retruns query set contains all item related to concrete object.

        Example:
        >>> obj = DataCenterAsset.objects.get(id=2)
        >>> AttachmentItem.objects.get_items_for_object(obj)
        [<AttachmentItem: image/png data center asset: 2>]
        """
        content_type = get_content_type_for_model(obj)
        return self.get_queryset().select_related('attachment').filter(
            object_id=obj.pk,
            content_type=content_type,
        )
Пример #6
0
    def get_attachments_for_object(self, obj):
        """
        Returns list of all related attachments to specific object.

        Example of use:
        >>> obj = DataCenterAsset.objects.get(id=2)
        >>> Attachment.objects.get_attachments_for_object(obj)
        [<Attachment: document.pdf (application/pdf) uploaded by root>]
        """
        content_type = get_content_type_for_model(obj)
        return self.get_queryset().filter(
            items__content_type=content_type,
            items__object_id=obj.id,
        )
Пример #7
0
 def setUp(self):
     self.dc_1 = DataCenterAssetFactory()
     self.dc_2 = DataCenterAssetFactory()
     self.bo_1 = BackOfficeAssetFactory()
     self.bo_2 = BackOfficeAssetFactory()
     self.support = SupportFactory()
     for obj in [self.dc_1, self.dc_2, self.bo_1, self.bo_2]:
         BaseObjectsSupport.objects.create(support=self.support,
                                           baseobject=obj)
     user = UserFactory()
     self.attachment = Attachment.objects.create_from_file_path(
         __file__, user)
     self.attachment_item = AttachmentItem.objects.attach(
         self.support.pk, get_content_type_for_model(self.support),
         [self.attachment])
Пример #8
0
def transition_history(obj):
    """
    Display transition history for model.

    Args:
        obj: Django model instance

    Example:
        {% transition_history object %}
    """
    content_type = get_content_type_for_model(obj)
    history = TransitionsHistory.objects.filter(
        content_type=content_type, object_id=obj.pk
    ).select_related('logged_user').order_by('-created')

    return {
        'transitions_history': history,
        'transition_history_in_fieldset': True
    }
Пример #9
0
 def save(self, commit=True):
     """
     Overrided standard save method. Saving object with attributes:
         * uploaded_by - user from request,
         * mime_type - uploaded file's content type.
     """
     obj = super().save(commit=False)
     md5 = Attachment.get_md5_sum(obj.file)
     attachment = Attachment.objects.filter(md5=md5)
     if obj.pk:
         attachment = attachment.exclude(pk=obj.pk)
     attachment = attachment.first()
     # _parent_object is an object to which it is assigned Attachment.
     # _parent_object is set in attachment.views.AttachmentsView
     #  get_formset method.
     if attachment:
         # if file with the same MD5 is already saved, reuse it and
         # attach it parent object
         content_type = get_content_type_for_model(
             self._parent_object._meta.model)
         if not AttachmentItem.objects.filter(
                 content_type=content_type,
                 object_id=self._parent_object.pk,
                 attachment__md5=md5).exists():
             AttachmentItem.objects.attach(self._parent_object.pk,
                                           content_type, [attachment])
         elif not obj.pk:
             # if another file with existing MD5 is uploaded for the same
             # object, show warninig message
             messages.add_message(
                 self._request, messages.WARNING,
                 _(('Another attachment with the same signature ({}) is '
                    'already attached to this object').format(
                        attachment.original_filename)))
         return
     obj.md5 = md5
     obj.uploaded_by = self._request.user
     file = self.cleaned_data.get('file', None)
     if file and hasattr(file, 'content_type'):
         obj.mime_type = file.content_type
     obj.save()
     return obj
Пример #10
0
 def test_get_content_type_for_model(self, expected_model, model):
     self.assertEqual(ContentType.objects.get_for_model(expected_model),
                      get_content_type_for_model(model))